快速开始
1.获取鉴权信息


2.复制完整代码并执行
2.1 ChatCompletion Pro
import requests
import readline
group_id = "请填写您的group_id"
api_key = "请填写您的api_key"
url = f"https://api.minimax.chat/v1/text/chatcompletion_pro?GroupId={group_id}"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
# tokens_to_generate/bot_setting/reply_constraints可自行修改
request_body = payload = {
"model": "abab5.5-chat",
"tokens_to_generate": 1024,
"reply_constraints": {"sender_type": "BOT", "sender_name": "MM智能助理"},
"messages": [],
"bot_setting": [
{
"bot_name": "MM智能助理",
"content": "MM智能助理是一款由MiniMax自研的,没有调用其他产品的接口的大型语言模型。MiniMax是一家中国科技公司,一直致力于进行大模型相关的研究。",
}
],
}
# 添加循环完成多轮交互
while True:
# 下面的输入获取是基于python终端环境,请根据您的场景替换成对应的用户输入获取代码
line = input("发言:")
# 将当次输入内容作为用户的一轮对话添加到messages
request_body["messages"].append(
{"sender_type": "USER", "sender_name": "小明", "text": line}
)
response = requests.post(url, headers=headers, json=request_body)
reply = response.json()["reply"]
print(f"reply: {reply}")
# 将当次的ai回复内容加入messages
request_body["messages"].extend(response.json()["choices"][0]["messages"])
2.2 T2A
import requests
group_id = "请填写您的group_id"
api_key = "请填写您的api_key"
url = f"https://api.minimax.chat/v1/text_to_speech?GroupId={group_id}"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
data = {
"voice_id": "male-qn-qingse",// 如同时传入voice_id和timber_weights时,则会自动忽略voice_id,以timber_weights传递的参数为准
"text": "你好",
"model": "speech-01",
"speed": 1.0,
"vol": 1.0,
"pitch": 0,
"timber_weights": [
{
"voice_id": "male-qn-qingse",
"weight": 1
},
{
"voice_id": "female-shaonv",
"weight": 1
},
{
"voice_id": "female-yujie",
"weight": 1
},
{
"voice_id": "audiobook_male_2",
"weight": 1
}
]
}
response = requests.post(url, headers=headers, json=data)
print("trace_id", response.headers.get("Trace-Id"))
if response.status_code != 200 or "json" in response.headers["Content-Type"]:
print("调用失败", response.status_code, response.text)
exit()
with open("output.mp3", "wb") as f:
f.write(response.content)
3.构建请求头
3.1 ChatCompletion pro
url = f"https://api.minimax.chat/v1/text/chatcompletion_pro?GroupId={group_id}"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
3.2 T2A
url = f"https://api.minimax.chat/v1/text_to_speech?GroupId={group_id}"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
4.构建请求内容
4.1 ChatCompletion pro
tokens_to_generate/bot_setting/reply_constraints可自行修改
request_body = payload = {
"model": "abab5.5-chat",
"tokens_to_generate": 1024,
"reply_constraints": {"sender_type": "BOT", "sender_name": "MM智能助理"},
"messages": [],
"bot_setting": [
{
"bot_name": "MM智能助理",
"content": "MM智能助理是一款由MiniMax自研的,没有调用其他产品的接口的大型语言模型。MiniMax是一家中国科技公司,一直致力于进行大模型相关的研究。",
}
],
}
line = input("发言:")
将当次输入内容作为用户的一轮对话添加到messages
request_body["messages"].append(
{"sender_type": "USER", "sender_name": "小明", "text": line}
)
4.2 T2A
data = {
"voice_id": "male-qn-qingse",// 如同时传入voice_id和timber_weights时,则会自动忽略voice_id,以timber_weights传递的参数为准
"text": "你好",
"model": "speech-01",
"speed": 1.0,
"vol": 1.0,
"pitch": 0,
"timber_weights": [
{
"voice_id": "male-qn-qingse",
"weight": 1
},
{
"voice_id": "female-shaonv",
"weight": 1
},
{
"voice_id": "female-yujie",
"weight": 1
},
{
"voice_id": "audiobook_male_2",
"weight": 1
}
]
}
5.完成交互
5.1 ChatCompletion pro
response = requests.post(url, headers=headers, json=request_body)
将当次的ai回复内容加入messages
request_body["messages"].extend(response.json()["choices"][0]["messages"])
5.2 T2A
response = requests.post(url, headers=headers, json=data)
print("trace_id", response.headers.get("Trace-Id"))
if response.status_code != 200 or "json" in response.headers["Content-Type"]:
print("调用失败", response.status_code, response.text)
exit()
with open("output.mp3", "wb") as f:
f.write(response.content)