本来只想写个脚本看看大模型api返回的对象是啥,结果搞出来个简单的对话客户端。
一个简洁易用的多提供商LLM API客户端,支持OpenAI、Anthropic、硅基流动等多种大语言模型API调用,提供直观的图形界面操作。会显示原始返回和解析结果。
- 多提供商支持:内置支持OpenAI、Anthropic、硅基流动等主流LLM API提供商
- 自定义API:灵活配置自定义API端点,支持各种兼容的大语言模型服务
- 图形界面:基于Tkinter的简洁直观用户界面,便于快速操作
- 现代Python项目结构:使用pyproject.toml管理依赖,支持标准Python包安装方式
- Python 3.8 或更高版本
- 安装所需依赖:
requests
直接在客户端输入(会自动保存到“C:\Users\用户名.llm_api_client\config.json”)
或者打开config.json文件配置
要修改预定义的API提供商支持,只需在api_extract.py文件中的API_PROVIDERS字典中添加相应配置:
"new_provider": {
"name": "提供商名称",
"url": "API端点URL",
"header_format": "Bearer {api_key}",
"default_model": "默认模型名称"
}api_extract.py模块提供了多种方式集成到你的Python项目中。以下是详细说明:
# 导入预定义的API提供商配置常量
from api_extract import API_PROVIDERS
# 导入LLMClient类(推荐)
from api_extract import LLMClient
# 导入兼容函数
from api_extract import call_api, call_siliconflow_api这是主要的客户端类,提供了完整的功能支持:
# 创建客户端实例
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")如果你只需要简单调用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" # 可选,默认为腾讯混元模型
)这个常量包含了预定义的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']}")对于不在预定义列表中的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