OpenAI 兼容 API
通过一个 OpenAI 风格的接口使用 Claude 和 GPT 模型。
BASE URL
https://api.apivai.com/v1APIVAI 使用 OpenAI 的 HTTP 请求格式,凡是支持自定义 base URL 的工具和 SDK 都能直接接入,无需额外开发。只需替换 base URL 和 Key,其余代码保持不变。
接口列表
| 方法与路径 | 用途 | 说明 |
|---|---|---|
| GET /v1/models | 获取模型列表 | 写死模型名之前先调用它确认。 |
| POST /v1/chat/completions | 对话补全 | 接收 OpenAI 风格的 messages 数组。 |
| POST /v1/chat/completions | 流式对话 | 加上 "stream": true 即以 SSE 方式逐段返回。 |
| POST /v1/responses | Responses 接口 | Codex CLI 使用该接口。 |
请求头
Authorization: Bearer YOUR_APIVAI_API_KEY
Content-Type: application/json
用 cURL 快速验证
# 1) List the models available to your key
curl https://api.apivai.com/v1/models \
-H "Authorization: Bearer $APIVAI_API_KEY"
# 2) Send a minimal chat request
curl https://api.apivai.com/v1/chat/completions \
-H "Authorization: Bearer $APIVAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "YOUR_MODEL_NAME",
"messages": [{"role": "user", "content": "Return one short sentence."}]
}'
OpenAI SDK
Python
from openai import OpenAI
client = OpenAI(
api_key="YOUR_APIVAI_API_KEY",
base_url="https://api.apivai.com/v1",
)
resp = client.chat.completions.create(
model="YOUR_MODEL_NAME",
messages=[
{"role": "system", "content": "You are a concise technical assistant."},
{"role": "user", "content": "Explain API gateways in 2 sentences."},
],
temperature=0.2,
)
print(resp.choices[0].message.content)
Node.js
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.APIVAI_API_KEY,
baseURL: "https://api.apivai.com/v1",
});
const resp = await client.chat.completions.create({
model: "YOUR_MODEL_NAME",
messages: [{ role: "user", content: "Explain API gateways in 2 sentences." }],
});
console.log(resp.choices[0]?.message?.content);
流式输出
将 stream 设为 true,并逐段读取响应体。每段数据以 "data:" 开头,收到 [DONE] 或连接关闭即表示结束。
stream = client.chat.completions.create(
model="YOUR_MODEL_NAME",
messages=[{"role": "user", "content": "Write a short deployment checklist."}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
函数调用(工具调用)
支持标准的两步流程:模型返回工具调用,你执行工具,再把结果发回去获取最终回答。
import json
TOOLS = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "Get the current weather for a city.",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"],
},
},
}]
messages = [{"role": "user", "content": "What's the weather in Tokyo?"}]
first = client.chat.completions.create(model=MODEL, messages=messages, tools=TOOLS)
choice = first.choices[0].message
messages.append(choice)
for call in choice.tool_calls or []:
args = json.loads(call.function.arguments)
messages.append({
"role": "tool",
"tool_call_id": call.id,
"content": json.dumps({"city": args["city"], "temp_c": 21}),
})
final = client.chat.completions.create(model=MODEL, messages=messages, tools=TOOLS)
print(final.choices[0].message.content)
常用框架
LangChain
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://api.apivai.com/v1",
api_key="YOUR_APIVAI_API_KEY",
model="YOUR_MODEL_NAME",
)
print(llm.invoke("Explain API gateways in 2 sentences.").content)
LlamaIndex
from llama_index.llms.openai_like import OpenAILike
llm = OpenAILike(
model="YOUR_MODEL_NAME",
api_base="https://api.apivai.com/v1",
api_key="YOUR_APIVAI_API_KEY",
is_chat_model=True,
)
print(llm.complete("Explain API gateways in 2 sentences."))
如何选择模型
- 用你的 API Key 调用 GET /v1/models。
- 从返回结果中选一个模型 ID。
- 用它发一条简短的对话请求。
- 确认可用后再写进应用配置。
「兼容」的含义
请求格式与 OpenAI 保持一致,但并非所有 OpenAI 接口的全部参数在任何场景下都受支持。迁移现有项目时,建议先用最小请求体跑通,确认模型可用,再逐个添加可选参数。