cd /workspace/projects
python src/main.py默认服务地址:http://localhost:8000
端点: POST /run
请求体:
{
"messages": [
{
"type": "human",
"content": "你的问题"
}
]
}响应:
{
"messages": [
{
"type": "human",
"content": "你的问题"
},
{
"type": "ai",
"content": "Agent 的回答"
}
],
"run_id": "xxx-xxx-xxx"
}端点: POST /stream
请求体: 同上
响应: Server-Sent Events (SSE) 格式的流式输出
端点: POST /v1/chat/completions
请求体:
{
"model": "topiclab-agent",
"messages": [
{
"role": "user",
"content": "你的问题"
}
],
"stream": false
}import requests
import json
# API 地址
API_URL = "http://localhost:8000"
def ask_question(question: str) -> str:
"""调用 Agent 获取回答"""
# 构造请求
payload = {
"messages": [
{
"type": "human",
"content": question
}
]
}
# 发送请求
response = requests.post(
f"{API_URL}/run",
json=payload,
headers={"Content-Type": "application/json"}
)
# 解析响应
result = response.json()
# 提取 AI 回答
if "messages" in result:
for msg in reversed(result["messages"]):
if msg.get("type") == "ai":
return msg["content"]
return "未获取到回答"
# 使用示例
if __name__ == "__main__":
question = "如何初始化 topiclab-cli?"
answer = ask_question(question)
print(f"问题: {question}")
print(f"回答:\n{answer}")# 同步调用
curl -X POST http://localhost:8000/run \
-H "Content-Type: application/json" \
-d '{
"messages": [
{
"type": "human",
"content": "如何初始化 topiclab-cli?"
}
]
}'
# OpenAI 兼容接口
curl -X POST http://localhost:8000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "topiclab-agent",
"messages": [
{
"role": "user",
"content": "查看通知"
}
],
"stream": false
}'const API_URL = 'http://localhost:8000';
async function askQuestion(question) {
const response = await fetch(`${API_URL}/run`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
messages: [
{
type: 'human',
content: question
}
]
})
});
const result = await response.json();
// 提取 AI 回答
if (result.messages) {
for (let i = result.messages.length - 1; i >= 0; i--) {
if (result.messages[i].type === 'ai') {
return result.messages[i].content;
}
}
}
return '未获取到回答';
}
// 使用示例
(async () => {
const question = '如何初始化 topiclab-cli?';
const answer = await askQuestion(question);
console.log('问题:', question);
console.log('回答:\n', answer);
})();服务启动前需要设置以下环境变量(已自动配置):
COZE_WORKLOAD_IDENTITY_API_KEY: API 密钥COZE_INTEGRATION_MODEL_BASE_URL: 模型服务地址COZE_WORKSPACE_PATH: 工作目录路径
400: 请求格式错误500: 服务内部错误504: 请求超时(默认 15 分钟)
{
"detail": "Invalid JSON format"
}- 首次调用: 可能需要 3-5 秒(初始化模型和工具)
- 后续调用: 1-3 秒(取决于问题复杂度)
- 文档更新: 每 3 小时自动更新一次 Skill 文档
- 单轮回复: Agent 会一次性给出完整答案,不会追问
- 指令优先: 回答以 topiclab-cli 命令为主
- 直接高效: 无冗余解释,适合程序调用
- 自动更新: Skill 文档每 3 小时自动更新
# 检查服务是否运行
curl http://localhost:8000/health
# 查看服务状态
curl http://localhost:8000/