我根根API文档,获取了应用的ID和会话ID,在进行对话时,返回的状态码为200,但是内容为空,谁能帮忙看一下:
代码:
def get_application_info():
url = ‘http://XXXX/api/application/profile’
headers = {
‘accept’: ‘application/json’,
‘AUTHORIZATION’: ‘application-XXXX’
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
data = response.json()
profile_id = data[‘data’][‘id’]
print(“应用ID为:”,profile_id)
return profile_id
def get_chat_id():
profile_id = get_application_info()
if not profile_id:
print(“无法获取profile_id,操作终止”)
return None
url = f’http://XXXXXXX/api/application/{profile_id}/chat/open’
headers = {
‘accept’: ‘application/json’,
‘AUTHORIZATION’: ‘application-5435721a33XXXXXXX83a1’
}
try:
response = requests.get(url, headers=headers)
response.raise_for_status() # 如果状态码不是200,抛出异常
data = response.json()
session_id = data[‘data’]
# print("聊天会话打开成功")
# print(response.json())
print("会话ID为:",session_id)
return session_id
except requests.exceptions.RequestException as e:
print(f"打开聊天会话失败: {e}")
return None
except ValueError as e:
print(f"响应内容不是有效的JSON格式: {e}")
return None
def chat_with_mk():
chat_id = get_chat_id()
# url = f’http://XXXXXX/api/application/{profile_id}/chat/open’
url = f’http://XXXXX/api/application/chat_message/{chat_id}’
headers = {
“Content-Type”: “application/json”,
“Authorization”: “application-543572XXXXXXa1”
}
payload = {
“message”: “请介绍一下你自己”,
“re_chat”: “false”,
“stream”: “false”,
“document_list”: [],
“image_list”: [],
“audio_list”: [],
“video_list”: [],
“form_data”: {}
}
response = requests.post(url, json=payload, headers=headers)
print(response.json())
return response.json()
应用ID为: f0094f9a-8874-11f0-a7bXXXXX120003
会话ID为: c1a059cc-b4a8-11f0-9XXXXXXX20003
{‘code’: 200, ‘message’: ‘成功’, ‘data’: {‘chat_id’: ‘c1a059cc-b4a8-11f0-9XXXXX03’, ‘id’: ‘c1abf9b2-b4a8-11f0-97XXXXXXX003’, ‘operate’: True, ‘content’: ‘’, ‘is_end’: True, ‘answer_list’: [], ‘completion_tokens’: 0, ‘prompt_tokens’: 0}}