使用 OpenAI SDK 从 Python 和 JavaScript 调用 Unquant
Unquant 支持 OpenAI chat completions 格式,因此官方 SDK 可直接使用。只需修改 base URL 和密钥,其余代码保持不变。
Python
pip install openaiimport 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 openaiimport 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 请求头指定的时间后重试。
完整错误列表参见错误参考。
继续阅读
- 如何在无审查无限模型上使用 Claude Code通过 Unquant 的 Anthropic 兼容端点在 GLM-5.3-Flash、Qwen 3.8 Flash Next 或 DeepSeek V4.1 Flash 1017 上运行 Claude Code:只需设置两个环境变量,无需代理,token 无限,上下文窗口 262K。
- 如何将 Open WebUI 连接到 Unquant将 Unquant 以 OpenAI API 连接方式添加至 Open WebUI:URL、密钥和模型列表。在无审查 GLM-5.3-Flash、Qwen 3.8 Flash Next 和 DeepSeek V4.1 Flash 1017 上搭建私有的类 ChatGPT 界面。
- 面向编程与智能体的无审查 AI开发者为何在编程和智能体工作流中使用无审查模型:安全研究、逆向工程或涉及敏感内容的处理管道不会遭到拒绝,加之 262K 上下文和无限 token,长时间智能体循环畅通无阻。