Unquant

Opening your workspace…

Unquant

以 OpenAI SDK 透過 Python 和 JavaScript 呼叫 Unquant

Unquant 使用 OpenAI 聊天補全格式,因此官方 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 標頭的時間後再重試。

完整清單請見錯誤參考。

延伸閱讀