Python调用MaxKB API进行对话,内容为空

我根根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}}

可以参考一下

import json
import logging
import requests

#配置日志
logging.basicConfig(level=logging.INFO, format=‘%(asctime)s - %(levelname)s - %(message)s’)

def initialize():
config = {
# 应用 API密钥
‘authorization_apikey’: ‘应用API KEY’,
# 获取会话ID
‘get_chat_url’: ‘https://IP地址:端口/chat/api/open’,
# 对话(使用参数:会话ID)
‘chat_url’: ‘https://IP地址:端口/chat/api/chat_message/{chat_id}’,
}
return config

def get_chat_id_by_app_id(config):
headers = {
“accept”: “application/json”,
‘Authorization’: f’Bearer {config[“authorization_apikey”]}’
}

try:
    response = requests.get(config['get_chat_url'], headers=headers)
    response.raise_for_status()
    return response.json().get('data')  # 直接返回JSON对象中的data字段
except requests.exceptions.RequestException as e:
    logging.error(f"Error getting chat ID by app ID: {e}")
    return None

def send_chat_message(config, chat_id, message):
headers = {
“Content-Type”: “application/json”,
“Authorization”: f’Bearer {config[“authorization_apikey”]}’
}
payload = {
“message”: message,
“stream”: False,
“re_chat”: False
}
try:
# 格式化URL,插入chat_id
formatted_url = config[‘chat_url’].format(chat_id=chat_id)
response = requests.post(formatted_url, headers=headers, json=payload)
response.raise_for_status()
return response.json().get(‘data’)
except requests.exceptions.RequestException as e:
logging.error(f"Error sending chat message: {e}")
return None

def main(problem):
config = initialize()
chat_id = get_chat_id_by_app_id(config)
if chat_id:
response = send_chat_message(config, chat_id, problem)
if response:
# return response
return response.get(‘content’)
else:
return “AI回答没有结果输出”
else:
return “没有生成chat_id”

问题的原因我自己找到了。我调用的API均是正常的,返回的状态码是200,内容为空的原因在于:我自己设计的应用,对于问题做了判断,对于不相关的问题,返回的内容可能就是为空的。后来我重新设计了一个应用,调用API能正常返回内容

好的,感谢