A Python utility library for streamlined Large Language Model interactions with unified API and conversation management.
Project description
hbllmutils
hbllmutils is a Python utility library designed to streamline interactions with Large Language Models (LLMs) by
providing robust configuration management, a unified API for OpenAI-compatible endpoints, and intuitive conversation
history handling.
Features
- Flexible LLM Configuration: Easily manage multiple LLM API endpoints and models through a simple YAML
configuration file (
.llmconfig.yaml), supporting default and fallback settings. - OpenAI-Compatible API: Interact with various LLM providers that adhere to the OpenAI API specification, offering both synchronous and asynchronous request methods.
- Streaming Responses: Efficiently handle streaming responses from LLMs, including optional extraction of reasoning content.
- Conversation History Management: Build and maintain complex conversation histories with support for different roles (system, user, assistant) and multimodal content (text, images).
- Extensible Design: Built with extensibility in mind, allowing for easy integration of new models or custom behaviors.
Installation
You can simply install it with the pip command line from the official PyPI site.
pip install hbllmutils
For more information about installation, you can refer to the Installation Guide.
Configuration: .llmconfig.yaml
The library uses a .llmconfig.yaml file to manage your LLM API credentials and model configurations. This file can be
placed in your project's root directory or specified explicitly. Below is an example configuration demonstrating how to
set up multiple API providers and define models, including default and fallback options.
deepseek: &deepseek
base_url: https://api.deepseek.com/v1
api_token: sk-457***af74
aihubmix: &aihubmix
base_url: https://aihubmix.com/v1
api_token: sk-6B9***F0Ad
aigcbest: &aigcbest
base_url: https://api2.aigcbest.top/v1
api_token: sk-tbK***49kA
openroute: &openroute
base_url: https://openrouter.ai/api/v1
api_token: sk-or-v1-9bf***a3d4
models:
__default__:
<<: *deepseek
model_name: deepseek-chat
deepseek-R1:
<<: *deepseek
model_name: deepseek-reasoner
deepseek-V3:
<<: *deepseek
model_name: deepseek-chat
__fallback__:
<<: *aihubmix
Explanation of the configuration:
- Anchors (
&and*): YAML anchors are used to define reusable blocks. For example,&deepseekdefines a block nameddeepseekwhich can be referenced later using*deepseek. modelssection: This is the core of your model definitions.__default__: Specifies the default model to use if nomodel_nameis explicitly provided toload_llm_model.deepseek-R1,deepseek-V3: Specific model configurations that inherit properties from the defined anchors and can override them (e.g.,model_name).__fallback__: Defines a fallback API endpoint. If a requestedmodel_nameis not found in themodelssection, the__fallback__configuration will be used, with the requestedmodel_nameautomatically assigned.
Quick Start Example
This example demonstrates how to load a model using the configuration file and interact with it using streaming responses and conversation history.
First, ensure you have a .llmconfig.yaml file set up as described above in your project directory.
import sys
from pprint import pprint
from hbllmutils.history import LLMHistory
from hbllmutils.model import load_llm_model
# Load the LLM model named 'deepseek-V3' from your .llmconfig.yaml
# The library will automatically look for .llmconfig.yaml in the current directory
model = load_llm_model(model_name='deepseek-V3')
print(f"Loaded Model: {model}")
pprint(model)
# Initialize conversation history with a system prompt
history = LLMHistory().set_system_prompt(
'tell me the appearance of this guy, use json format, like {\'description\': \'xxxxx\', \'name\': \'original name\'}.'
).append_user(
'donald trump'
)
# Ask the model a question and get a streaming response
# with_reasoning=True will include any internal reasoning from the model in the stream
f = model.ask_stream(
messages=history.to_json(),
with_reasoning=True,
)
print(f"\nStreaming Response (with reasoning):\n")
# Iterate through the stream and print chunks as they arrive
for chunk in f:
print(chunk, end='')
sys.stdout.flush()
print(f"\n\nAccumulated Reasoning: {f.reasoning_content}")
print(f"Accumulated Content: {f.content}")
# Alternatively, for non-streaming responses, use the ask method:
# response_content = model.ask(messages=history.to_json())
# print(f"\nNon-streaming Response: {response_content}")
# If you need reasoning content for non-streaming:
# reasoning, content = model.ask(messages=history.to_json(), with_reasoning=True)
# print(f"\nNon-streaming Reasoning: {reasoning}")
# print(f"Non-streaming Content: {content}")
Contributing
Contributions are welcome! Please feel free to open issues or submit pull requests on the GitHub repository.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hbllmutils-0.0.1.tar.gz.
File metadata
- Download URL: hbllmutils-0.0.1.tar.gz
- Upload date:
- Size: 25.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c487ad827203c9871b00a2a6a842a556bb8f1155ab6ac5965d0d2d177a996c6b
|
|
| MD5 |
6c46cadc2f480f530c73a7dd19c986ad
|
|
| BLAKE2b-256 |
de217ddb3b889f597813feb50a9d0cccdd8405a4d7183914ce38bce8bab8b108
|
File details
Details for the file hbllmutils-0.0.1-py3-none-any.whl.
File metadata
- Download URL: hbllmutils-0.0.1-py3-none-any.whl
- Upload date:
- Size: 26.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9c8a45bcaf48f0823ae736e36ead1fd76795f510ef9f37f01d3bd77f50f67287
|
|
| MD5 |
00f627058f8f5c5276b69a1004ee9140
|
|
| BLAKE2b-256 |
a979e2353be9bef205188d97b434be44575960234646b0f597be6d1c423f91db
|