OpenAI SDK
UberLLM 实现了 OpenAI Chat Completions API。任何使用官方 openai SDK(或兼容客户端)的代码,只需设置两项内容即可运行:
base_url/baseURL→https://api.uberllm.dev/v1api_key/apiKey→ 您的ull_key
其他所有内容 — messages、tools、response_format、stream、max_tokens、temperature — 保持不变。
Python
import os
from openai import OpenAI
client = OpenAI(
base_url="https://api.uberllm.dev/v1",
api_key=os.environ["UBERLLM_API_KEY"],
)
# Non-streaming
resp = client.chat.completions.create(
model="deepseek/deepseek-v4-flash",
messages=[{"role": "user", "content": "Explain rope embeddings briefly."}],
)
print(resp.choices[0].message.content)
print(resp.usage) # prompt_tokens / completion_tokens are returned per request
# Streaming
stream = client.chat.completions.create(
model="deepseek/deepseek-v4-flash",
messages=[{"role": "user", "content": "Count to five."}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
TypeScript / JavaScript
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://api.uberllm.dev/v1",
apiKey: process.env.UBERLLM_API_KEY,
});
const resp = await client.chat.completions.create({
model: "deepseek/deepseek-v4-flash",
messages: [{ role: "user", content: "Explain rope embeddings briefly." }],
});
console.log(resp.choices[0].message.content);
工具调用
工具/函数调用原样传递。使用那些宣称支持工具调用的模型(参见 模型与路由;目录中标记为 supports_tools 的模型)。
resp = client.chat.completions.create(
model="deepseek/deepseek-v4-flash",
messages=[{"role": "user", "content": "What is the weather in Paris?"}],
tools=[{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}],
)
print(resp.choices[0].message.tool_calls)
调用工具的回复总是以 finish_reason: "tool_calls" 结尾(在 /v1/messages 上是 stop_reason: "tool_use"),无论是否流式传输,无论由哪个提供商提供。将每个工具调用原样返回:有些会带有一个 extra_content 字段(Gemini 的思维签名),模型在下一轮对话中需要它。
回复格式
无论哪个提供商提供服务,每个回复都具有相同的字段:OpenAI 聊天补全字段(id, object, created, model, choices, usage)以及我们的 usage.cost_usd。提供商自行添加的字段(如 x_groq, service_tier, system_fingerprint, usage.queue_time 等)将被移除。
推理模型会在 message(或 streaming 时为 delta)中,以相同的文本,同时在 reasoning 和 reasoning_content 中发送其推理过程,这样,读取其中任何一个字段的客户端都能获取到。当提供商发送 OpenRouter 风格的 reasoning_details 时,它们将保持不变地通过。没有推理过程的回复将不包含这两个字段。
提供商拒绝但仅用于调整答案的请求字段(如 reasoning, thinking, verbosity, top_k 等)将针对该提供商被丢弃,并且在必要时,reasoning: {"effort": "low"} 将作为 reasoning_effort 发送。以文本部分列表形式发送的消息内容,对于只接受字符串的提供商,将作为单个字符串发送。发送给只读取文本的模型图片、音频或文件将被拒绝,并附带一个明确说明的 400:请首先检查 input_modalities 中的 GET /v1/models。
列出模型
curl https://api.uberllm.dev/v1/models \
-H "Authorization: Bearer $UBERLLM_API_KEY"
返回 OpenAI 列表格式 ({"object":"list","data":[{"id": "...", ...}]}),
其中包含您现在可以调用的聊天模型;?type=all 添加图像、视频、音乐和
语音模型。每个聊天模型包含 context_length、max_output_tokens、
input_modalities、supports_tools、supports_reasoning、supports_vision 和
supported_parameters (参见 模型与路由)。
公共目录 (无需认证) 也可在 GET /api/v1/public/models 找到,并附有
每个端点的定价。
流式传输用量
对于流式传输请求,UberLLM 会自动请求 token 用量 (stream_options.include_usage),因此最终的 chunk 会携带一个 usage 对象,其中包含 cost_usd,这是该调用以美元计费的金额。非流式传输的回复也会有 usage.cost_usd,以及 X-UberLLM-Cost-USD header(除了已经以 keepalive 空格开始的慢速回复:这种情况下只有 body 字段)。您的账单是市场为该调用报告的费用。回复不会等待该报告:当报告尚未到达时,cost_usd 是我们的估算值,您的余额和 Dashboard → Activity 会在稍后显示确切的费用。如果用量缺失,UberLLM 会回退到 tokenizer 估算值 — 此类行会在 Dashboard → Activity 中标记。
Responses API
client.responses.create 和 client.responses.stream 也适用于所有聊天模型:文本、工具调用 (function_call → function_call_output)、结构化输出和 streaming。UberLLM 不存储任何内容,因此请在每次调用时发送整个对话 input,而不是 previous_response_id。详情:
Responses API。
r = client.responses.create(
model="deepseek/deepseek-v4.1-flash",
instructions="Answer in one short sentence.",
input="What is the capital of France?",
)
print(r.output_text)