区域与接入
SDK 示例
curl / Python / Node.js 的端到端 /v1 示例 —— Answers、Agent(Responses)、数据、文件。
针对托管 API 的最小端到端示例。
BASE=https://api.mirobody.ai/v1KEY="mb_live_..."
# 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":"permanent","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)
Section titled “Python(OpenAI SDK)”from openai import OpenAI
client = OpenAI(api_key="mb_live_...", base_url="https://api.mirobody.ai/v1")
# 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 requests
up = requests.post( "https://api.mirobody.ai/v1/files", headers={"Authorization": "Bearer mb_live_..."}, 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)
Section titled “openai-agents SDK(你的工具 + Mirobody 的 agent)”openai-agents SDK 只需改 base URL 即可对接 Agent API:
from agents import Agent, ModelSettings, Runner, function_tool, set_default_openai_client, set_tracing_disabledfrom openai import AsyncOpenAI
set_default_openai_client(AsyncOpenAI( base_url="https://api.mirobody.ai/v1", api_key="mb_live_..."))set_tracing_disabled(True) # tracing 会请求 api.openai.com
@function_tooldef 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)底层交接协议见 Function calling。
Node.js(OpenAI SDK)
Section titled “Node.js(OpenAI SDK)”import OpenAI from "openai";
const client = new OpenAI({ apiKey: process.env.MIROBODY_API_KEY, baseURL: "https://api.mirobody.ai/v1",});
// 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);更多:Answers API · Agent API · 数据 · 文件 · 标准化。