Skip to main content

轻量级LLM API客户端库,支持缓存、重试和详细日志记录功能

Project description

LLMClient

轻量级LLM API客户端库,基于Python开发,支持缓存、重试和详细日志记录功能。

作者: Leo marticle.ios@gmail.com

English Version | 中文版

特性

  • 轻量便捷:简洁的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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dsapi_llmclient-0.1.5.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dsapi_llmclient-0.1.5-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file dsapi_llmclient-0.1.5.tar.gz.

File metadata

  • Download URL: dsapi_llmclient-0.1.5.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.12

File hashes

Hashes for dsapi_llmclient-0.1.5.tar.gz
Algorithm Hash digest
SHA256 093b5688e096e8135f038856fcafbd95eaabe4a770bb8aebd7f47444dd017fd7
MD5 dfb50c63edc42c72fce428c7769ad230
BLAKE2b-256 f156acd94f99c92597d1c8dacda2293654c34d0f98a22fff579628780df606ee

See more details on using hashes here.

File details

Details for the file dsapi_llmclient-0.1.5-py3-none-any.whl.

File metadata

File hashes

Hashes for dsapi_llmclient-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 288f008dc34af77e85d0c8079e13a8d3c7b1d7de10503211a6cb311a5503147f
MD5 4528603b9f10e1c4c0749127d05103d8
BLAKE2b-256 cfa5cd010ea550e33ba78213d588d08c9485fe4e0cb41154e912712e6cfa3369

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page