基于 FastAPI 构建的 K2Think AI 模型代理服务,提供 OpenAI 兼容的 API 接口。
pip install -r requirements.txt
cp config.example .env
# 编辑 .env 文件,配置你的API密钥和其他选项
# 复制token示例文件并编辑
cp tokens.example.txt tokens.txt
# 编辑tokens.txt文件,添加你的实际K2Think tokens
python k2think_proxy.py
服务将在 http://localhost:8001 启动。
# 构建镜像
docker build -t k2think-api .
# 先创建 .env 文件和tokens.txt,然后编辑配置
cp config.example .env
cp tokens.example.txt tokens.txt
# 编辑tokens.txt添加实际的token
# 运行容器
docker run -d \
--name k2think-api \
-p 8001:8001 \
-v $(pwd)/tokens.txt:/app/tokens.txt:ro \
-v $(pwd)/.env:/app/.env:ro \
k2think-api
# 先创建 .env 文件和tokens.txt
cp config.example .env
cp tokens.example.txt tokens.txt
# 编辑 .env 文件配置API密钥等
# 编辑 tokens.txt 添加实际的K2Think tokens
# 启动服务
docker-compose up -d
# 检查服务状态
docker-compose logs -f k2think-api
POST /v1/chat/completions
curl -X POST http://localhost:8001/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-k2think" \
-d '{
"model": "MBZUAI-IFM/K2-Think",
"messages": [
{"role": "user", "content": "你擅长什么?"}
],
"stream": false
}'
GET /v1/models
curl http://localhost:8001/v1/models \
-H "Authorization: Bearer sk-k2think"
查看token池状态:
curl http://localhost:8001/admin/tokens/stats
重置指定token:
curl -X POST http://localhost:8001/admin/tokens/reset/0
重置所有token:
curl -X POST http://localhost:8001/admin/tokens/reset-all
重新加载token文件:
curl -X POST http://localhost:8001/admin/tokens/reload
| 变量名 | 默认值 | 说明 |
|---|---|---|
VALID_API_KEY | 无默认值 | API 访问密钥(必需) |
K2THINK_API_URL | https://www.k2think.ai/api/chat/completions | K2Think API端点 |
| 变量名 | 默认值 | 说明 |
|---|---|---|
TOKENS_FILE | tokens.txt | Token文件路径 |
MAX_TOKEN_FAILURES | 3 | Token最大失败次数 |
| 变量名 | 默认值 | 说明 |
|---|---|---|
HOST | 0.0.0.0 | 服务监听地址 |
PORT | 8001 | 服务端口 |
TOOL_SUPPORT | true | 是否启用工具调用功能 |
详细配置说明请参考 config.example 文件。
import openai
# 配置客户端
client = openai.OpenAI(
base_url="http://localhost:8001/v1",
api_key="sk-k2think"
)
# 发送聊天请求
response = client.chat.completions.create(
model="MBZUAI-IFM/K2-Think",
messages=[
{"role": "user", "content": "解释一下量子计算的基本原理"}
],
stream=False
)
print(response.choices[0].message.content)
# 流式聊天
stream = client.chat.completions.create(
model="MBZUAI-IFM/K2-Think",
messages=[
{"role": "user", "content": "写一首关于人工智能的诗"}
],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content is not None:
print(chunk.choices[0].delta.content, end="")
K2-Think 模型具有以下特点:
<think></think> 和 <answer></answer> 标签结构化输出Token 相关问题
/admin/tokens/stats 查看token状态,使用 /admin/tokens/reset-all 重置所有tokentokens.txt 文件添加新token,然后访问 /admin/tokens/reload 重新加载/health 端点查看简要统计,或 /admin/tokens/stats 查看详细信息端口冲突
PORT 环境变量# Docker 容器日志
docker logs k2think-api
# docker-compose日志
docker-compose logs -f k2think-api
# 本地运行日志
# 日志会直接输出到控制台
Token文件映射
tokens.txt 通过volume映射到容器内,支持动态更新:ro健康检查
docker ps 查看健康状态安全考虑
K2Think API 代理现在支持 OpenAI Function Calling 规范的工具调用功能。
tools 和 tool_choice 参数使用配置检查脚本验证你的环境变量设置:
# 检查当前配置
python check_config_simple.py
# 查看配置示例
python check_config_simple.py --example
| 变量名 | 默认值 | 说明 |
|---|---|---|
TOOL_SUPPORT | true | 是否启用工具调用功能 |
import openai
client = openai.OpenAI(
base_url="http://localhost:8001/v1",
api_key="sk-k2think"
)
# 定义工具
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称,例如:北京、上海"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "温度单位"
}
},
"required": ["city"]
}
}
}
]
# 发送工具调用请求
response = client.chat.completions.create(
model="MBZUAI-IFM/K2-Think",
messages=[
{"role": "user", "content": "北京今天天气怎么样?"}
],
tools=tools,
tool_choice="auto" # auto, none, required 或指定特定工具
)
# 处理响应
if response.choices[0].message.tool_calls:
for tool_call in response.choices[0].message.tool_calls:
function_name = tool_call.function.name
function_args = tool_call.function.arguments
print(f"调用工具: {function_name}")
print(f"参数: {function_args}")
# 在这里执行实际的工具调用
# tool_result = execute_tool(function_name, function_args)
# 继续对话,将工具结果返回给模型
# ...
项目包含完整的测试套件,位于 test/ 文件夹:
# 运行所有测试
cd test
python run_all_tests.py
# 运行特定测试
python run_all_tests.py debug_test # 基础功能测试
python run_all_tests.py test_tools # 完整工具调用测试
python run_all_tests.py test_contentpart # ContentPart序列化测试
python run_all_tests.py test_message_accumulation # 消息累积测试
# 查看工具调用示例
python tool_example.py
测试套件包含:
详细说明请参考 test/README.md。
"auto": 让模型自动决定是否使用工具(推荐)"none": 禁用工具调用"required": 强制模型使用工具{"type": "function", "function": {"name": "tool_name"}}: 强制使用特定工具MIT License
欢迎提交 Issue 和 Pull Request!
Content type
Image
Digest
sha256:267c37e35…
Size
62.6 MB
Last updated
about 1 year ago
docker pull orangeqiu/k2think2api