LLMClient
轻量级LLM API客户端库,基于Python开发,支持缓存、重试和详细日志记录功能。
作者: Leo marticle.ios@gmail.com
特性
- 轻量便捷:简洁的API设计,易于集成和使用
- 环境变量控制:通过环境变量灵活配置所有功能
- SQLite缓存:内置高效的本地缓存机制,减少重复API调用
- 自动重试:内置智能重试机制,应对网络波动和临时错误
- 详细日志:可配置的详细日志记录,支持调试模式
安装
# 克隆仓库
git clone https://github.com/yourusername/llmclient.git
cd llmclient
# 安装依赖
pip install -r requirements.txt
# 安装库(开发模式)
pip install -e .
环境变量配置
创建.env文件并配置以下环境变量:
# API配置
LLM_API_BASE=https://api.deepseek.com/v1
LLM_API_KEY=your_api_key_here
LLM_MODEL=deepseek-chat
LLM_MAX_TOKENS=8000
LLM_TEMPERATURE=1.0
# 缓存配置
LLM_CACHE_ENABLED=true
LLM_CACHE_PATH=~/.llmclient/cache.db
LLM_CACHE_EXPIRY_DAYS=30
# 调试配置
LLM_DEBUG=false
LLM_DEBUG_DIR=~/.llmclient/logs
# 重试配置
LLM_RETRY_ATTEMPTS=3
LLM_RETRY_MIN_WAIT=2
LLM_RETRY_MAX_WAIT=10
快速开始
基本用法
from llmclient import LLMClient
# 创建客户端(使用环境变量配置)
client = LLMClient()
# 发送简单请求
response = client.completion("请用一句话介绍人工智能。")
print(response)
自定义配置
from llmclient import LLMClient, LLMConfig
# 创建自定义配置
config = LLMConfig(
api_base="https://api.deepseek.com/v1",
api_key="your_api_key_here",
model="deepseek-chat",
max_tokens=2000,
temperature=0.7,
cache_enabled=True,
debug_enabled=True
)
# 使用自定义配置创建客户端
client = LLMClient(config)
# 发送请求
response = client.completion("请列举三种常见的机器学习算法。")
print(response)
多轮对话
from llmclient import LLMClient
client = LLMClient()
# 构建对话历史
messages = [
{"role": "user", "content": "你好,我想了解一下深度学习。"},
{"role": "assistant", "content": "你好!深度学习是机器学习的一个分支,它使用多层神经网络来模拟人脑的学习过程。有什么具体方面你想了解的吗?"},
{"role": "user", "content": "请介绍一下CNN和RNN的区别。"}
]
response = client.chat_completion(messages)
print(response)
使用工具函数
from llmclient import LLMClient
from llmclient.utils import format_prompt, extract_json, chunk_text
client = LLMClient()
# 使用模板
template = """请为一家{industry}公司起一个名字,要求:
1. 名字要简洁易记
2. 能反映公司的行业特点
3. 有创意和现代感"""
prompt = format_prompt(template, industry="人工智能")
response = client.completion(prompt)
print(response)
# 分割长文本
long_text = "这是一段非常长的文本..." * 100
chunks = chunk_text(long_text, chunk_size=4000, overlap=200)
for chunk in chunks:
response = client.completion(chunk)
print(response)
高级功能
缓存管理
from llmclient import LLMClient
client = LLMClient()
# 清理过期缓存
deleted_count = client.clear_cache()
print(f"已清理 {deleted_count} 条过期缓存")
# 清理指定天数前的缓存
deleted_count = client.clear_cache(days=7)
print(f"已清理 {deleted_count} 条7天前的缓存")
调试模式
from llmclient import LLMClient
client = LLMClient()
# 启用调试模式
client.set_debug(True)
# 发送请求(将记录详细日志)
response = client.completion("这是一个测试请求")
# 禁用调试模式
client.set_debug(False)
示例脚本
查看 example.py 获取更多使用示例。
python example.py
依赖
- requests: HTTP请求
- tenacity: 重试机制
- python-dotenv: 环境变量加载
- sqlite3: 内置缓存数据库
许可证
MIT
Metadata
Release files for dsapi-llmclient 0.1.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| dsapi_llmclient-0.1.5.tar.gz | 12.7 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| dsapi_llmclient-0.1.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 27.8 kB
Release files / dsapi_llmclient-0.1.5.tar.gz
| Download URL | dsapi_llmclient-0.1.5.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
093b5688e096e8135f038856fcafbd95eaabe4a770bb8aebd7f47444dd017fd7
|
|
BLAKE2b-256 checksum How to use checksums |
f156acd94f99c92597d1c8dacda2293654c34d0f98a22fff579628780df606ee
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.12
|
Release files / dsapi_llmclient-0.1.5-py3-none-any.whl
| Download URL | dsapi_llmclient-0.1.5-py3-none-any.whl |
|---|---|
| Size | 15.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
288f008dc34af77e85d0c8079e13a8d3c7b1d7de10503211a6cb311a5503147f
|
|
BLAKE2b-256 checksum How to use checksums |
cfa5cd010ea550e33ba78213d588d08c9485fe4e0cb41154e912712e6cfa3369
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.9.12
|
Security for dsapi-llmclient 0.1.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.