1. 配置环境变量
export NEW_API_BASE_URL="https://api.example.com/v1"
export NEW_API_KEY="sk-your-api-key"
$env:NEW_API_BASE_URL = "https://api.example.com/v1"
$env:NEW_API_KEY = "sk-your-api-key"
2. 获取模型列表
curl "$NEW_API_BASE_URL/models" \
-H "Authorization: Bearer $NEW_API_KEY"
3. 发起对话请求
curl "$NEW_API_BASE_URL/chat/completions" \
-H "Authorization: Bearer $NEW_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL_ID",
"messages": [
{"role": "user", "content": "你好,请用一句话介绍你自己"}
]
}'
{
"id": "chatcmpl-example",
"object": "chat.completion",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "你好,我是通过统一 API 接入的 AI 助手。"
},
"finish_reason": "stop"
}
]
}
SDK 接入
Python
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["NEW_API_KEY"],
base_url=os.environ["NEW_API_BASE_URL"],
)
response = client.chat.completions.create(
model="YOUR_MODEL_ID",
messages=[{"role": "user", "content": "你好"}],
)
print(response.choices[0].message.content)
Node.js
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: process.env.NEW_API_KEY,
baseURL: process.env.NEW_API_BASE_URL,
});
const response = await client.chat.completions.create({
model: 'YOUR_MODEL_ID',
messages: [{ role: 'user', content: '你好' }],
});
console.log(response.choices[0].message.content);