快速开始¶
三步完成第一次模型调用:准备账号、创建 API Key、用熟悉的 OpenAI SDK 发出请求。
准备账号¶
打开控制台,按页面提供的方式注册并完成验证。如果显示等待审批,需先等待账号获批。详见账号与 API Key。
创建 API Key¶
登录控制台,在 Dashboard 页面的 API Keys 区域点击 Create API Key。按页面提示完成账号验证。
复制生成的密钥,它以
hyi-开头。之后也可以在同一位置再次查看。把密钥放进环境变量
TENCENT_INFERENCE_API_KEY,本文档的所有示例都从这里读取:macOS / Linux:
export TENCENT_INFERENCE_API_KEY="你的 API Key"
Windows PowerShell:
$env:TENCENT_INFERENCE_API_KEY = "你的 API Key"
每个账号同时只有一个有效的 API Key。点击 Regenerate 会生成新密钥,旧密钥立即失效。
警告
API Key 等同于账号凭据:通过它发起的调用都计入你的账号。不要把它提交到代码仓库、写进前端或移动端代码,也不要分享给他人。密钥泄露后请立即在控制台点击 Regenerate,让旧密钥失效。
发起第一次调用¶
平台兼容 OpenAI 的 Chat Completions 接口,下面三种方式任选其一。
使用示例前,请先在控制台确认 deepseek-v4.1-flash 对你的账号可用;若不可用,替换为账号模型列表中的 ID。
Python¶
先安装 SDK:pip install openai。
import os
from openai import OpenAI
client = OpenAI(
base_url="https://freeinference.qq.com/v1",
api_key=os.environ["TENCENT_INFERENCE_API_KEY"],
)
response = client.chat.completions.create(
model="deepseek-v4.1-flash",
messages=[{"role": "user", "content": "用 Python 写一个快速排序函数"}],
)
print(response.choices[0].message.content)
Node.js¶
先安装 SDK:npm install openai,并把示例保存为 .mjs 文件运行。
import OpenAI from "openai";
const client = new OpenAI({
baseURL: "https://freeinference.qq.com/v1",
apiKey: process.env.TENCENT_INFERENCE_API_KEY,
});
const response = await client.chat.completions.create({
model: "deepseek-v4.1-flash",
messages: [{ role: "user", content: "用 Python 写一个快速排序函数" }],
});
console.log(response.choices[0].message.content);
cURL¶
curl --fail-with-body 'https://freeinference.qq.com/v1/chat/completions' \
-H "Authorization: Bearer $TENCENT_INFERENCE_API_KEY" \
-H "Content-Type: application/json" \
--data-binary @- <<'JSON'
{
"model": "deepseek-v4.1-flash",
"messages": [{"role": "user", "content": "用 Python 写一个快速排序函数"}]
}
JSON
收到模型的回复,就说明接入成功了。
流式输出¶
设置 stream=True,回复会边生成边返回,适合聊天界面:
stream = client.chat.completions.create(
model="deepseek-v4.1-flash",
messages=[{"role": "user", "content": "介绍一下你自己"}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
查看可用模型¶
curl -s 'https://freeinference.qq.com/v1/models' \
-H "Authorization: Bearer $TENCENT_INFERENCE_API_KEY"
携带 API Key 时,返回的是你的账号可以调用的模型,字段含义见模型列表。
下一步¶
开发工具接入:在 Cline、Roo Code、Cursor、Cherry Studio 等工具中使用。
Claude Code:用 Claude Code 连接平台的模型。
API 参考:接口、请求头、错误码与调用限制。