文本与代码调用
以下示例均使用 Flex247 OpenAI Compatible 端点。
基础聊天
bash
curl https://api.noflex.work/v1/chat/completions \
-H "Authorization: Bearer sk-your-flex247-key" \
-H "Content-Type: application/json" \
-d '{
"model": "your-model-id",
"messages": [{"role":"user","content":"写一句产品标语"}]
}'带系统指令
json
{
"model": "your-model-id",
"messages": [
{"role": "system", "content": "你是一名资深代码审查员,先指出风险,再给出最小修改。"},
{"role": "user", "content": "请检查下面的函数。"}
]
}多轮对话
每次请求都带上需要保留的历史消息:
js
const messages = [
{ role: 'user', content: '给项目取三个名字' },
{ role: 'assistant', content: 'Nova、Orbit、Canvas' },
{ role: 'user', content: '选择第二个,并写一句标语' }
]
const result = await client.chat.completions.create({
model: 'your-model-id',
messages
})JSON 输出
如果模型支持结构化输出,可要求返回 JSON。生产环境仍应验证字段与类型:
json
{
"model": "your-model-id",
"messages": [
{"role":"user","content":"返回 JSON:包含 title、summary、tags 三个字段"}
],
"response_format": {"type":"json_object"}
}超时与重试
- 只对超时、连接中断和部分
5xx错误做有限次数重试。 - 对
401先检查密钥,不要持续重试。 - 对
429使用指数退避,并降低并发。 - 对长任务使用流式输出,避免客户端误以为请求卡住。