File size: 2,033 Bytes
962759f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# -*- coding: utf-8 -*-
import json
import requests
# API 配置
API_BASE = "http://localhost:8080"
API_KEY = "sk-your-api-key"
def test_weather_query():
"""测试天气查询"""
print("=" * 50)
print("上海天气查询测试")
print("=" * 50)
# 工具定义
tool = {
"type": "function",
"function": {
"name": "get_weather",
"description": "查询指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string", "description": "城市名称"},
"date": {"type": "string", "description": "查询日期(可选)"}
},
"required": ["city"]
}
}
}
# 发送请求
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
data = {
"model": "GLM-4.5",
"messages": [
{"role": "user", "content": "查询上海2025年9月3日的天气"}
],
"tools": [tool]
}
print("\n发送请求...")
response = requests.post(f"{API_BASE}/v1/chat/completions",
headers=headers,
json=data)
if response.status_code == 200:
result = response.json()
message = result["choices"][0]["message"]
print("\n模型响应:")
if message.get("tool_calls"):
print("检测到工具调用:")
for tc in message["tool_calls"]:
print(f" - 工具: {tc['function']['name']}")
print(f" - 参数: {tc['function']['arguments']}")
else:
print("未检测到工具调用")
print(f"内容: {message.get('content', '无内容')[:100]}...")
else:
print(f"请求失败: {response.status_code}")
print(f"错误信息: {response.text}")
if __name__ == "__main__":
test_weather_query() |