Skip to main content

A powerful Python toolkit for simplifying LLM integration and management with multi-model scheduling, fault tolerance, and load balancing support

Project description

llmakits

一个功能强大的Python工具包,用于简化大语言模型(LLM)的集成和管理。支持多模型调度、故障转移、负载均衡等功能。

功能特性

  • 🚀 多模型支持: 支持OpenAI、智谱AI、DashScope、ModelScope等多个主流LLM平台
  • 🔄 智能调度: 内置模型故障转移和负载均衡机制
  • 🔑 密钥管理: 灵活的API密钥配置和管理
  • 📊 消息处理: 强大的消息格式化、验证和提取功能
  • 🛡️ 错误处理: 完善的重试机制和异常处理
  • 📝 流式输出: 支持流式响应处理
  • 🎯 电商工具: 内置电商场景专用工具集

安装

pip install llmakits

更新:

pip install --upgrade llmakits

快速开始

1. 配置模型和API密钥

创建配置文件 config/models_config.yaml

Models_config:
  my_models:
    - sdk_name: "openai"
      model_name: "gpt-3.5-turbo"
    - sdk_name: "zhipu"
      model_name: "glm-4-flash"

创建配置文件 config/keys_config.yaml

openai:
  base_url: "https://api.openai.com/v1"
  api_keys:
    - "your-openai-api-key-1"
    - "your-openai-api-key-2"

zhipu:
  base_url: "https://open.bigmodel.cn/api/paas/v4"
  api_keys:
    - "your-zhipu-api-key"

2. 加载模型

from llmakits import load_models

# 加载配置好的模型
models = load_models('config/models_config.yaml', 'config/keys_config.yaml')

# 获取模型组
my_models = models['my_models']

3. 发送消息

使用新的 ModelDispatcher 类(推荐)

from llmakits.dispatcher import ModelDispatcher

# 创建调度器实例
dispatcher = ModelDispatcher()

# 准备消息
message_info = {
    "system": "你是一个 helpful 助手",
    "user": "请介绍一下Python编程语言"
}

# 执行任务
result, tokens = dispatcher.execute_task(message_info, my_models)
print(f"结果: {result}")
print(f"使用token数: {tokens}")

# 按需获取模型切换次数(两种方法)
switch_count = dispatcher.model_switch_count  # 直接访问属性
print(f"模型切换次数: {switch_count}")

# 或者使用方法获取
switch_count = dispatcher.get_model_switch_count()
print(f"模型切换次数: {switch_count}")

# 如果需要执行多个任务,可以重置计数器
dispatcher.reset_model_switch_count()

使用兼容函数(旧版本)

from llmakits.dispatcher import execute_task

# 准备消息
message_info = {
    "system": "你是一个 helpful 助手",
    "user": "请介绍一下Python编程语言"
}

# 执行任务
result, tokens, switch_count = execute_task(message_info, my_models)
print(f"结果: {result}")
print(f"使用token数: {tokens}")
print(f"模型切换次数: {switch_count}")

4. 直接使用模型客户端

from llmakits import BaseOpenai

# 创建模型客户端
model = BaseOpenai(
    platform="openai",
    base_url="https://api.openai.com/v1",
    api_keys=["your-api-key"],
    model_name="gpt-3.5-turbo"
)

# 发送消息
messages = [
    {"role": "system", "content": "你是一个 helpful 助手"},
    {"role": "user", "content": "Hello!"}
]

result, tokens = model.send_message(messages)
print(f"回复: {result}")

高级功能

消息处理

from llmakits.message import prepare_messages, extract_json_from_string, convert_to_json

# 准备消息
messages = prepare_messages(system="你是一个助手", user="请帮忙", assistant="好的")

# 提取并转换为JSON
json_str = '{"name": "test"} some text'
result = convert_to_json(json_str)

模型调度

使用 ModelDispatcher 类(推荐)

from llmakits.dispatcher import ModelDispatcher

# 创建调度器实例
dispatcher = ModelDispatcher()

# 带验证函数的任务执行
def validate_result(result):
    return "error" not in result.lower()

# 执行任务
result, tokens = dispatcher.execute_task(
    message_info={"user": "生成一段JSON"},
    llm_models=my_models,
    format_json=True,  # 格式化为JSON
    validate_func=validate_result  # 验证结果
)

# 获取模型切换次数
switches = dispatcher.model_switch_count  # 直接访问属性
print(f"模型切换次数: {switches}")

使用兼容函数

from llmakits.dispatcher import execute_task

# 带验证函数的任务执行
def validate_result(result):
    return "error" not in result.lower()

result, tokens = execute_task(
    message_info={"user": "生成一段JSON"},
    llm_models=my_models,
    format_json=True,  # 格式化为JSON
    validate_func=validate_result  # 验证结果
)

电商工具

from llmakits.e_commerce import ECommerceTools

# 使用电商专用工具
ec_tools = ECommerceTools()
result = ec_tools.generate_product_description("电子产品", "智能手机")

支持的模型平台

  • OpenAI: GPT-3.5, GPT-4 等系列模型
  • 智谱AI (Zhipu): GLM-4, GLM-3 等系列模型
  • DashScope: 通义千问系列模型
  • ModelScope: 各种开源模型如Qwen、DeepSeek等

配置说明

模型配置 (models_config.yaml)

按功能分组配置模型,支持为不同场景配置不同的模型组合:

Models_config:
  # 标题生成专用模型组
  generate_title:
    - sdk_name: "dashscope"
      model_name: "qwen3-max-preview"

  # 翻译专用模型组
  translate_box:
    - sdk_name: "modelscope"
      model_name: "Qwen/Qwen3-32B"

密钥配置 (keys_config.yaml)

支持多密钥配置,自动切换和负载均衡:

platform_name:
  base_url: "api-endpoint-url"
  api_keys:
    - "api-key-1"
    - "api-key-2"

错误处理

llmakits内置了完善的错误处理机制:

  • 自动重试: 请求失败时自动重试,最多5次
  • 密钥切换: API密钥失效时自动切换到备用密钥
  • 模型切换: 当前模型失败时自动尝试下一个可用模型
  • 异常捕获: 详细的异常信息和处理建议

开发指南

添加新的模型平台

  1. 继承 BaseClient 类实现新的客户端
  2. BaseOpenai 中添加平台特定的处理逻辑
  3. 更新配置文件格式支持

自定义消息处理

通过 llmakits.message 模块提供的工具函数:

  • prepare_messages(): 准备标准格式的消息
  • extract_json_from_string(): 从文本中提取JSON
  • validate_message_format(): 验证消息格式

许可证

Apache 2.0 License

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

llmakits-0.1.1.tar.gz (23.9 kB view details)

Uploaded Source

Built Distribution

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

llmakits-0.1.1-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file llmakits-0.1.1.tar.gz.

File metadata

  • Download URL: llmakits-0.1.1.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for llmakits-0.1.1.tar.gz
Algorithm Hash digest
SHA256 38466c069e1674238111cc5038b1c1787820aa032adfb4051f9ab195905617a4
MD5 2ac19866f45511f08ff8ffbec5d3cac2
BLAKE2b-256 3a855d63c6ee2f06d36a10d3f9cae2629c04371c2606cd0f229b210a4aae7b66

See more details on using hashes here.

File details

Details for the file llmakits-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: llmakits-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 25.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.18

File hashes

Hashes for llmakits-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 25d7c8c1cafb8e123255ab7622ef1ac5737660ed1d455d8b4fd05d5054b6c2a4
MD5 1dc6a65b948bc0880548439b97d88bdf
BLAKE2b-256 b6b5c659c43cf33f7517289ed40fc09b3473ce7a7b24679c25b304b97f424914

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