cd test/custom_react_agent
python api.py
服务将在 http://localhost:8000 启动
curl http://localhost:8000/health
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"question": "请问哪个高速服务区的档口数量最多?", "user_id": "doudou"}'
# 查看用户的对话列表
curl "http://localhost:8000/api/v0/react/users/doudou/conversations?limit=5"
# 查看特定对话的详细内容
curl "http://localhost:8000/api/v0/react/users/doudou/conversations/doudou:20250115103000001"
# 普通对话
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"question": "你好", "user_id": "alice"}'
# SQL查询
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"question": "查询收入最高的服务区", "user_id": "alice"}'
# 继续对话 (使用相同thread_id)
curl -X POST http://localhost:8000/api/chat \
-H "Content-Type: application/json" \
-d '{"question": "详细说明一下", "user_id": "alice", "thread_id": "alice:20250115103000001"}'
# 获取用户对话列表
curl "http://localhost:8000/api/v0/react/users/alice/conversations"
# 限制返回数量
curl "http://localhost:8000/api/v0/react/users/alice/conversations?limit=10"
# 获取特定对话详情
curl "http://localhost:8000/api/v0/react/users/alice/conversations/alice:20250115103000001"
import requests
def chat_with_agent(question, user_id, thread_id=None):
url = "http://localhost:8000/api/chat"
payload = {
"question": question,
"user_id": user_id
}
if thread_id:
payload["thread_id"] = thread_id
response = requests.post(url, json=payload)
return response.json()
# 使用示例
result = chat_with_agent("请问服务区数据查询", "alice")
print(f"回答: {result['data']['response']}")
import requests
def get_user_conversations(user_id, limit=10):
"""获取用户对话列表"""
url = f"http://localhost:8000/api/v0/react/users/{user_id}/conversations"
params = {"limit": limit}
response = requests.get(url, params=params)
return response.json()
def get_conversation_detail(user_id, thread_id):
"""获取对话详情"""
url = f"http://localhost:8000/api/v0/react/users/{user_id}/conversations/{thread_id}"
response = requests.get(url)
return response.json()
# 使用示例
conversations = get_user_conversations("alice", limit=5)
print(f"找到 {len(conversations['data']['conversations'])} 个对话")
if conversations['data']['conversations']:
thread_id = conversations['data']['conversations'][0]['thread_id']
detail = get_conversation_detail("alice", thread_id)
print(f"对话包含 {detail['data']['message_count']} 条消息")
async function chatWithAgent(question, userId, threadId = null) {
const response = await fetch('http://localhost:8000/api/chat', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
question: question,
user_id: userId,
...(threadId && { thread_id: threadId })
})
});
return await response.json();
}
// 使用示例
const result = await chatWithAgent("查询服务区信息", "alice");
console.log("回答:", result.data.response);
async function getUserConversations(userId, limit = 10) {
const response = await fetch(
`http://localhost:8000/api/v0/react/users/${userId}/conversations?limit=${limit}`
);
return await response.json();
}
async function getConversationDetail(userId, threadId) {
const response = await fetch(
`http://localhost:8000/api/v0/react/users/${userId}/conversations/${threadId}`
);
return await response.json();
}
// 使用示例
const conversations = await getUserConversations("alice", 5);
console.log(`找到 ${conversations.data.conversations.length} 个对话`);
if (conversations.data.conversations.length > 0) {
const firstConv = conversations.data.conversations[0];
const detail = await getConversationDetail("alice", firstConv.thread_id);
console.log(`对话详情:`, detail.data);
}
cd test/custom_react_agent
python test_api.py
cd test/custom_react_agent
python test_conversation_api.py
python test_api.py "查询服务区收入排名"
# 获取用户的历史对话,显示对话列表
conversations = get_user_conversations("user123", limit=20)
for conv in conversations['data']['conversations']:
print(f"[{conv['formatted_time']}] {conv['conversation_preview']}")
# 客服查看用户的完整对话历史
user_id = "customer_456"
conversations = get_user_conversations(user_id)
for conv in conversations['data']['conversations']:
thread_id = conv['thread_id']
detail = get_conversation_detail(user_id, thread_id)
print(f"对话时间: {conv['formatted_time']}")
print(f"消息数量: {detail['data']['message_count']}")
# 显示详细消息...
# 分析用户的对话模式
conversations = get_user_conversations("analyst_user")
total_messages = sum(conv['message_count'] for conv in conversations['data']['conversations'])
avg_messages = total_messages / len(conversations['data']['conversations'])
print(f"平均每个对话 {avg_messages:.1f} 条消息")
{user_id}:{timestamp}
doudou:20250115103000001
20250115103000001
│ │ │ │ │ │ │
│ │ │ │ │ │ └── 毫秒 (001)
│ │ │ │ │ └──── 秒 (30)
│ │ │ │ └────── 分钟 (30)
│ │ │ └──────── 小时 (10)
│ │ └────────── 日 (15)
│ └───────────── 月 (01)
└─────────────── 年 (2025)
# 检查服务状态
curl http://localhost:8000/health
# 查看详细日志
python api.py # 查看启动日志
# 测试基础功能
python test_api.py "你好"
# 测试新功能
python test_conversation_api.py
🎉 现在你已经掌握了Custom React Agent API的基本用法和新的对话历史管理功能!
📚 更多详细信息请参考: 完整API文档