跳到主要内容

Chat Completions

适用范围:HangToken 当前线上服务 最后核验:2026-08-26

最小请求​

curl https://hangtoken.com/v1/chat/completions \
-H "Authorization: Bearer sk-[你的密钥]" \
-H "Content-Type: application/json" \
-d '{"model":"[模型ID]","messages":[{"role":"system","content":"回答要简洁"},{"role":"user","content":"你好"}],"temperature":0.7,"stream":false}'

常用字段​

字段类型/必填说明

modelstring / 是从 /v1/models 获取

messagesarray / 是按顺序传 system、user、assistant 消息

temperaturenumber / 否控制随机性;并非所有模型支持

max_tokensnumber / 否限制最大输出;字段兼容性取决于模型

streamboolean / 否true 时返回 SSE

toolsarray / 否声明函数工具;需模型支持

Python SDK​

from openai import OpenAI
import os

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"], base_url="https://hangtoken.com/v1", timeout=60)
result = client.chat.completions.create(model="[模型ID]", messages=[{"role":"user","content":"你好"}])
print(result.choices[0].message.content)

Node.js SDK​

import OpenAI from "openai";

const client = new OpenAI({apiKey: process.env.OPENAI_API_KEY, baseURL: "https://hangtoken.com/v1", timeout: 60000});
const result = await client.chat.completions.create({model: "[模型ID]", messages: [{role: "user", content: "你好"}]});
console.log(result.choices[0].message.content);

流式请求​

curl -N https://hangtoken.com/v1/chat/completions \
-H "Authorization: Bearer sk-[你的密钥]" -H "Content-Type: application/json" \
-d '{"model":"[模型ID]","messages":[{"role":"user","content":"写三点建议"}],"stream":true}'

错误与重试​

不要重试 400、401、402、403、404。429、网络超时或临时 5xx 可使用 1、2、4 秒退避并加入随机抖动,建议最多重试 3 次。非幂等工具调用需自行避免重复执行。