为了满足开发者对 OpenAI API 生态的使用需求,我们的 API 新增了对 OpenAI API 格式的支持。通过简单的配置,即可将 MiniMax 的能力接入到 OpenAI API 生态中。
快速开始
1. 安装 OpenAI SDK
2. 配置环境变量
export OPENAI_BASE_URL=https://api.minimaxi.com/v1
export OPENAI_API_KEY=${YOUR_API_KEY}
3. 调用 API
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="MiniMax-M2",
messages=[
{
"role": "system",
"content": "You are a helpful assistant."
},
{
"role": "user",
"content": "Hi, how are you?"
}
]
)
print(response.choices[0].message.content)
支持的模型
使用 OpenAI SDK 时,支持以下 MiniMax 模型:
| 模型名称 | 说明 |
|---|
| MiniMax-M2 | MiniMax-M2 即将于 10.27 上线,敬请期待 |
更多模型信息请参考标准的 MiniMax API 接口文档。
示例代码
基础对话
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="MiniMax-M2",
messages=[
{
"role": "user",
"content": "请用简单的语言解释什么是机器学习"
}
]
)
print(response.choices[0].message.content)
流式响应
from openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create(
model="MiniMax-M2",
messages=[
{
"role": "user",
"content": "写一首关于春天的诗"
}
],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
工具调用(Function Calling)
更多详情参考 MiniMax-M2 函数调用指南
from openai import OpenAI
client = OpenAI()
tools = [
{
"type": "function",
"function": {
"name": "get_weather",
"description": "获取指定城市的天气信息",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称"
}
},
"required": ["city"]
}
}
}
]
response = client.chat.completions.create(
model="MiniMax-M2",
messages=[
{
"role": "user",
"content": "北京今天天气怎么样?"
}
],
tools=tools
)
print(response.choices[0].message)
注意事项
-
使用时需要将
OPENAI_BASE_URL 设置为 https://api.minimaxi.com/v1
-
OPENAI_API_KEY 应设置为您的 MiniMax API Key
-
temperature 参数取值范围为(0.0, 1.0],推荐使用 1.0,超出范围会返回错误
-
部分 OpenAI 参数(如
presence_penalty、frequency_penalty、logit_bias 等)会被忽略
-
当前不支持图像和音频类型的输入
-
n 参数仅支持值为 1
-
旧版的
function_call 已废弃,请使用 tools 参数
相关链接
推荐阅读