You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.0 KiB

import requests
import json
def query_chat_api(query_text):
"""
调用聊天API并返回结果
:param query_text: 要查询的文本
:return: API返回的响应数据
"""
url = "http://www.hzkjai.com:27002/api/chat"
headers = {
"Content-Type": "application/json"
}
data = {
"query": query_text
}
try:
response = requests.post(url, headers=headers, data=json.dumps(data))
response.raise_for_status()
return response.json()
except requests.exceptions.RequestException as e:
print(f"请求发生异常: {str(e)}")
return None
def main():
# 示例查询
query = "讲一下项羽的故事3"
result = query_chat_api(query)
if result:
if result.get("code") == 0:
print("API调用成功:")
print(result["data"]["reply"])
else:
print(f"API返回错误: {result.get('message', '未知错误')}")
if __name__ == "__main__":
main()