curl 示例前,先选择 Cloud 区域。需要认证的调用必须使用与密钥同一区域的地址。下方示例都使用 MIROBODY_API_BASE:
export MIROBODY_API_BASE="https://api.mirobody.cn/v1" # 中国区账户
# export MIROBODY_API_BASE="https://api.mirobody.ai/v1" # 全球区账户:改用这一行
MIROBODY_API_KEY 设为同一区域控制台创建的密钥。密钥留在 shell 环境变量里;下面的 Python 和 Node 示例会从环境变量读取。
curl
BASE="$MIROBODY_API_BASE"
KEY="$MIROBODY_API_KEY"
# 1) 有据可循的回答(Answers API)
curl $BASE/chat/completions \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"model":"mirobody-flash","messages":[{"role":"user","content":"How is my fasting glucose trending?"}],"user":"alice"}'
# 2) 写入一条记录(retention 必填),再问一次
curl $BASE/data \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"user":"alice","retention":"1h","records":[{"indicator":"fasting_glucose","value":97,"unit":"mg/dL","time":"2026-06-16T07:30:00Z"}]}'
# 3) 上传报告。原件立即保存,抽取的文本稍后即可用。
curl $BASE/files -H "Authorization: Bearer $KEY" -F "user=alice" -F "file=@report.pdf"
# 4) Agent API(Responses)
curl $BASE/responses \
-H "Authorization: Bearer $KEY" -H "Content-Type: application/json" \
-d '{"model":"mirobody-flash","input":"How is my fasting glucose trending?","user":"alice"}'
Python(OpenAI SDK)
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["MIROBODY_API_KEY"], base_url=os.environ["MIROBODY_API_BASE"])
# Answers API —— 非流式
resp = client.chat.completions.create(
model="mirobody-flash",
messages=[{"role": "user", "content": "How is my fasting glucose trending?"}],
user="alice",
)
print(resp.choices[0].message.content)
# Answers API —— 流式
stream = client.chat.completions.create(
model="mirobody-flash",
messages=[{"role": "user", "content": "Summarize my last checkup."}],
user="alice",
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta
if getattr(delta, "content", None):
print(delta.content, end="", flush=True)
# Agent API(Responses)—— 默认即存储、可链式续聊
r1 = client.responses.create(model="mirobody-flash",
input="How is my fasting glucose trending?", user="alice")
print(r1.output_text)
r2 = client.responses.create(model="mirobody-flash",
input="And compared with last quarter?",
previous_response_id=r1.id, user="alice")
print(r2.output_text)
# 文件上传是 multipart,直接调 REST 端点。
# status="processed" 表示原件已接收,不代表文本抽取已经完成。
import os
import requests
up = requests.post(
f'{os.environ["MIROBODY_API_BASE"]}/files',
headers={"Authorization": f'Bearer {os.environ["MIROBODY_API_KEY"]}'},
data={"user": "alice"},
files={"file": open("report.pdf", "rb")},
)
print(up.json()) # {"object": "file", "id": "...", "filename": "report.pdf",
# "bytes": 20544, "status": "processed", "created_at": 1782924296,
# "subject": "alice"}
openai-agents SDK(你的工具 + Mirobody 的 agent)
openai-agents SDK 只需改 base URL 即可对接 Agent API:import os
from agents import Agent, ModelSettings, Runner, function_tool, set_default_openai_client, set_tracing_disabled
from openai import AsyncOpenAI
set_default_openai_client(AsyncOpenAI(
base_url=os.environ["MIROBODY_API_BASE"], api_key=os.environ["MIROBODY_API_KEY"]))
set_tracing_disabled(True) # tracing 会请求 api.openai.com
@function_tool
def book_appointment(date: str) -> str:
"""Book a clinic appointment for the end user (ISO date)."""
return f"Booked: {date} 09:30, Dr. Chen"
agent = Agent(name="Health assistant", model="mirobody-flash", tools=[book_appointment],
model_settings=ModelSettings(extra_body={"user": "alice"}))
result = Runner.run_sync(agent, "Check my recent glucose and book a follow-up if it's trending up.")
print(result.final_output)
Node.js(OpenAI SDK)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.MIROBODY_API_KEY,
baseURL: process.env.MIROBODY_API_BASE,
});
// Answers API —— 非流式
const resp = await client.chat.completions.create({
model: "mirobody-flash",
messages: [{ role: "user", content: "How is my fasting glucose trending?" }],
user: "alice",
});
console.log(resp.choices[0].message.content);
// Answers API —— 流式
const stream = await client.chat.completions.create({
model: "mirobody-flash",
messages: [{ role: "user", content: "Summarize my last checkup." }],
user: "alice",
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}
// Agent API(Responses)
const r = await client.responses.create({
model: "mirobody-flash",
input: "How is my fasting glucose trending?",
user: "alice",
});
console.log(r.output_text);
另见
- 快速开始:同样这些调用,加上前后的准备步骤。
- Agent API(Responses):请求与响应对象的完整说明。
- 把 Answers API 用作工具:从另一个 agent 内部调用 Mirobody。