OpenAI SDK
LowPrice 使用 OpenAI 兼容协议。使用官方 OpenAI SDK 时,只需要修改 API Key 和 Base URL。
| 字段 | 值 |
|---|---|
| API Key | 你在 LowPrice 控制台创建的 Key |
| Base URL | https://api.lowprice.one/v1 |
| Model | 控制台中显示的模型调用名 |
Python
Section titled “Python”pip install openaiimport os
from openai import OpenAI
client = OpenAI( api_key=os.environ["LOWPRICE_API_KEY"], base_url="https://api.lowprice.one/v1",)
response = client.chat.completions.create( model=os.environ["LOWPRICE_MODEL"], messages=[ {"role": "system", "content": "你是一个简洁、准确的中文助手。"}, {"role": "user", "content": "解释一下什么是 API 网关"}, ],)
print(response.choices[0].message.content)Node.js
Section titled “Node.js”npm install openaiimport OpenAI from 'openai';
const client = new OpenAI({ apiKey: process.env.LOWPRICE_API_KEY, baseURL: 'https://api.lowprice.one/v1',});
const response = await client.chat.completions.create({ model: process.env.LOWPRICE_MODEL, messages: [ { role: 'system', content: '你是一个简洁、准确的中文助手。' }, { role: 'user', content: '解释一下什么是 API 网关' }, ],});
console.log(response.choices[0]?.message?.content);启用流式响应后,客户端应该边接收边渲染,而不是等待完整响应。
Python
Section titled “Python”stream = client.chat.completions.create( model=os.environ["LOWPRICE_MODEL"], messages=[{"role": "user", "content": "写一段简短的产品介绍"}], stream=True,)
for chunk in stream: text = chunk.choices[0].delta.content if text: print(text, end="", flush=True)Node.js
Section titled “Node.js”const stream = await client.chat.completions.create({ model: process.env.LOWPRICE_MODEL, messages: [{ role: 'user', content: '写一段简短的产品介绍' }], stream: true,});
for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content ?? '');}- 前端不要暴露 API Key
- 通过服务端代理请求
api.lowprice.one - 为上游请求设置合理超时
- 对 429 和 5xx 使用有上限的指数退避
- 记录 Request ID,方便定位问题