Skip to content

flosL/LLM_api_Client

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

LLM API Client - 多提供商大语言模型API客户端

本来只想写个脚本看看大模型api返回的对象是啥,结果搞出来个简单的对话客户端。

一个简洁易用的多提供商LLM API客户端,支持OpenAI、Anthropic、硅基流动等多种大语言模型API调用,提供直观的图形界面操作。会显示原始返回和解析结果。

📑 目录导航

🚀 功能特性

  • 多提供商支持:内置支持OpenAI、Anthropic、硅基流动等主流LLM API提供商
  • 自定义API:灵活配置自定义API端点,支持各种兼容的大语言模型服务
  • 图形界面:基于Tkinter的简洁直观用户界面,便于快速操作
  • 现代Python项目结构:使用pyproject.toml管理依赖,支持标准Python包安装方式

📋 系统要求

  • Python 3.8 或更高版本
  • 安装所需依赖:requests

🔧 使用说明

配置API密钥

直接在客户端输入(会自动保存到“C:\Users\用户名.llm_api_client\config.json”) 或者打开config.json文件配置

预定义的API提供商

要修改预定义的API提供商支持,只需在api_extract.py文件中的API_PROVIDERS字典中添加相应配置:

"new_provider": {
    "name": "提供商名称",
    "url": "API端点URL",
    "header_format": "Bearer {api_key}",
    "default_model": "默认模型名称"
}

👍 在你的项目里调用api_extract.py

api_extract.py模块提供了多种方式集成到你的Python项目中。以下是详细说明:

1. 导入模块和常量

# 导入预定义的API提供商配置常量
from api_extract import API_PROVIDERS

# 导入LLMClient类(推荐)
from api_extract import LLMClient

# 导入兼容函数
from api_extract import call_api, call_siliconflow_api

2. LLMClient类(推荐使用)

这是主要的客户端类,提供了完整的功能支持:

# 创建客户端实例
client = LLMClient()

# 设置提供商和API密钥
client.provider = "openai"  # 或 "siliconflow", "anthropic", "custom"
client.api_key = "your_api_key_here"
client.model = "gpt-3.5-turbo"  # 可选,使用提供商默认模型可以不设置

# 调用API
response = client.call_api("你好,请介绍一下你自己")

# 获取响应内容
if response:
    content = response["content"]
    full_response = response["full_response"]  # 获取完整的API响应
    print(f"AI回答: {content}")

# 保存API密钥到配置文件
client.save_api_key_to_config("your_api_key_here", "openai")

# 从配置文件加载API密钥
api_key = client.load_api_key_from_config("openai")

3. 兼容函数(更简单的使用方式)

如果你只需要简单调用API:

# 通用API调用函数
response = call_api(
    provider="openai",
    api_key="your_api_key_here",
    prompt="你好,请介绍一下你自己",
    model="gpt-3.5-turbo"  # 可选
)

# 特定调用硅基流动API
response = call_siliconflow_api(
    api_key="your_api_key_here",
    prompt="你好,请介绍一下你自己",
    model="tencent/Hunyuan-MT-7B"  # 可选,默认为腾讯混元模型
)

4. API_PROVIDERS常量

这个常量包含了预定义的API提供商配置:

# 查看所有支持的API提供商
print("支持的API提供商:")
for key, config in API_PROVIDERS.items():
    print(f"- {key}: {config['name']} (默认模型: {config['default_model']})")

# 获取特定提供商的信息
siliconflow_config = API_PROVIDERS["siliconflow"]
print(f"硅基流动API URL: {siliconflow_config['url']}")
print(f"硅基流动默认模型: {siliconflow_config['default_model']}")

5. 自定义API配置

对于不在预定义列表中的API提供商:

client = LLMClient()
client.provider = "custom"
client.custom_url = "https://api.example.com/chat/completions"
client.custom_headers = {
    "Authorization": "Bearer your_api_key",
    "Content-Type": "application/json"
}
# 可选:自定义请求体格式
client.custom_payload = {
    "model": "your-custom-model",
    "messages": [{"role": "user", "content": "{prompt}"}],
    "temperature": 0.7
}

# 调用自定义API
response = client.call_api("你好,请介绍一下你自己")

📄 许可证

MIT License

About

一个简洁易用的自定义多提供商LLM API客户端,显示原始返回和解析结果

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages