Appearance
💬 聊天 Chat Completions
接口地址
POST https://portx.asia/v1/chat/completions请求头
| 参数 | 值 |
|---|---|
Content-Type | application/json |
Authorization | Bearer 你的Key |
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✅ | 可用模型 |
messages | array | ✅ | 消息列表 |
temperature | number | ❌ | 随机性,0-2 |
max_tokens | number | ❌ | 最大输出 Token 数 |
stream | boolean | ❌ | 是否流式返回 |
示例代码
Python
python
import openai
client = openai.OpenAI(
api_key="sk-你的Key",
base_url="https://portx.asia/v1"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "你好,请介绍一下自己"}
]
)
print(response.choices[0].message.content)cURL
bash
curl https://portx.asia/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-你的Key" \
-d '{
"model": "gpt-4o",
"messages": [
{"role": "user", "content": "Hello!"}
]
}'Node.js
javascript
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "sk-你的Key",
baseURL: "https://portx.asia/v1"
});
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "你好!" }]
});
console.log(response.choices[0].message.content);流式响应(Streaming)
设置 stream: true 即可开启流式返回:
python
stream = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "写一首诗"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")