Unquant

Opening your workspace…

Unquant

使用 OpenAI SDK 从 Python 和 JavaScript 调用 Unquant

Unquant 支持 OpenAI chat completions 格式,因此官方 SDK 可直接使用。只需修改 base URL 和密钥,其余代码保持不变。

配置指南阅读时间 2 分钟更新于 2026 年 10 月 1 日

Python

pip install openai
import os
from openai import OpenAI

client = OpenAI(base_url="https://api.unquant.io/v1", api_key=os.environ["UNQUANT_API_KEY"])

reply = client.chat.completions.create(
    model="glm-5.3-flash",
    messages=[{"role": "user", "content": "Write a noir opening line."}],
)
print(reply.choices[0].message.content)

JavaScript 和 TypeScript

npm install openai
import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.unquant.io/v1", apiKey: process.env.UNQUANT_API_KEY });

const reply = await client.chat.completions.create({
  model: "qwen-3.8-flash-next",
  messages: [{ role: "user", content: "Summarise this chapter in three lines." }],
});
console.log(reply.choices[0].message.content);

流式传输

stream = client.chat.completions.create(
    model="glm-5.3-flash",
    messages=[{"role": "user", "content": "Tell me a long story."}],
    stream=True,
    stream_options={"include_usage": True},
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

include_usage 会在最后一个数据块中附加 token 计数。用量数据仅供你自行监控,不产生任何费用。

图片

请以 base64 数据 URL(PNG、JPEG 或 WebP)格式内联发送图片。不支持通过 URL 获取远程图片。

import base64
data = base64.b64encode(open("photo.jpg", "rb").read()).decode()
client.chat.completions.create(
    model="glm-5.3-flash",
    messages=[{"role": "user", "content": [
        {"type": "text", "text": "What is in this picture?"},
        {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{data}"}},
    ]}],
)

工具调用

tools = [{"type": "function", "function": {
    "name": "get_weather",
    "description": "Current weather for a city",
    "parameters": {"type": "object", "properties": {"city": {"type": "string"}}, "required": ["city"]},
}}]
reply = client.chat.completions.create(model="glm-5.3-flash", tools=tools,
    messages=[{"role": "user", "content": "Weather in Lisbon?"}])
print(reply.choices[0].message.tool_calls)

推理控制

每个模型在回答前都会进行推理,并将思考过程以 reasoning_content 字段单独返回,与回答内容分开。通过 reasoning_effort 传入 none、low 或 high(GLM-5.3-Flash 和 DeepSeek V4.1 Flash 1017 还支持 max):

client.chat.completions.create(model="glm-5.3-flash", reasoning_effort="none",
    messages=[{"role": "user", "content": "Quick answer: capital of Peru?"}])

错误处理

429
并发流限额已达上限

你套餐的所有并发流均在使用中。该请求未启动;请在某个请求完成后重试。

503
暂时不可用

正在进行短暂重启。请等待 Retry-After 请求头指定的时间后重试。

完整错误列表参见错误参考。

继续阅读