parent
b4f0639f29
commit
2686186c0a
@ -0,0 +1,39 @@
|
||||
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()
|
Loading…
Reference in new issue