安装官方 SDK
首先在你的环境中安装官方的 OpenAI Python SDK。该库为你处理 HTTP 请求和 JSON 序列化。
- 运行
pip install openai在你的终端中。 - 请确保使用最新版本的库。
此单一依赖项使您能够访问行业通用的标准客户端接口。
配置基础 URL 和密钥
将您的客户端指向我们的基础 URL,设置您的 API 密钥以进行身份验证。这确保所有请求路由到我们的无审查模型服务器。
使用 https://api.qwenmaxapi.com/v1 作为您的基础 URL。在 获取 API 密钥 页面,通过 Google 或电子邮件登录。密钥在注册后立即显示。
发送您的第一次聊天补全
发送简单的文本请求至 /v1/chat/completions 端点。模型根据提示返回无限制文本。
使用模型 ID uncensored. API 接受标准参数,如 temperature 和 top_p。您可以调整这些参数以控制创造性。
启用流式传输并处理 Token
启用流式传输以在生成时接收令牌。这降低了长响应的感知延迟。
设置 stream=True 在你的请求中。服务器发送服务器发送事件 (SSE)。Token 使用详情出现在最后一个数据块中。
在代码中处理流以渐进显示输出。
测试 JSON 模式和函数调用
使用 JSON 模式进行结构化数据输出。设置 response_format 到 {"type": "json_object"}. 这确保模型返回有效的 JSON。
通过提供 tools 定义。模型可以根据您的提示上下文调用工具。使用 tool_choice 以强制或允许工具使用。
限制、错误和上下文窗口
在扩展集成之前了解您的限制。上下文窗口总共为 100,000 Token。
每次请求的最大输出为 16,000 个 token。每个密钥每分钟允许 300 次请求。每个密钥允许 8 个并发请求。
处理密钥无效(401)或余额不足(402)等错误。错误请求不消耗余额,因此是免费的。
cURL
curl https://api.qwenmaxapi.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "List the top 5 arguments against cloud computing"}]
}'Python
from openai import OpenAI
client = OpenAI(base_url="https://api.qwenmaxapi.com/v1", api_key="YOUR_KEY")
resp = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)Node.js
import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.qwenmaxapi.com/v1", apiKey: process.env.API_KEY });
const resp = await client.chat.completions.create({
model: "uncensored",
messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);流式传输
stream = client.chat.completions.create(
model="uncensored",
messages=[{"role": "user", "content": "Tell the story in second person."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)