SDK 接入指南
allmodel.top 兼容 OpenAI SDK,支持 Python、Node.js、Go 等主流语言。
核心配置
只需修改两处配置,即可将现有 OpenAI 应用切换到 allmodel.top:
1. API Key:填入您的 allmodel.top API Key(格式:am-xxxxx)
2. Base URL:设为 https://allmodel.top/v1
Python
Python
pip install openai
from openai import OpenAI
client = OpenAI(
api_key="am-your-api-key-here",
base_url="https://allmodel.top/v1"
)
# 对话补全
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}]
)
# 嵌入向量
embedding = client.embeddings.create(
model="text-embedding-3-small",
input="Hello world"
)
# 图像生成
image = client.images.generate(
model="dall-e-3",
prompt="A cute cat"
)
LangChain
LangChain Python
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
model="gpt-4o",
api_key="am-your-api-key-here",
base_url="https://allmodel.top/v1"
)
response = llm.invoke("用一句话解释量子计算")
print(response.content)
Node.js / TypeScript
Node.js
npm install openai
import OpenAI from 'openai';
const client = new OpenAI({
apiKey: 'am-your-api-key-here',
baseURL: 'https://allmodel.top/v1'
});
const chat = await client.chat.completions.create({
model: 'gpt-4o',
messages: [{ role: 'user', content: 'Hello!' }]
});
console.log(chat.choices[0].message.content);
Go
Go
go get github.com/sashabaranov/go-openai
package main
import (
"context"
openai "github.com/sashabaranov/go-openai"
)
func main() {
client := openai.NewClient("am-your-api-key-here")
client.BaseURL = "https://allmodel.top/v1"
resp, _ := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "gpt-4o",
Messages: []openai.ChatCompletionMessage{
{Role: "user", Content: "Hello!"},
},
},
)
println(resp.Choices[0].Message.Content)
}
直接 HTTP 调用
不依赖任何 SDK,直接用 HTTP 请求调用:
HTTP 请求
curl https://allmodel.top/v1/chat/completions \
-H "Authorization: Bearer am-your-api-key-here" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4o",
"messages": [{"role": "user", "content": "Hello!"}]
}'
allmodel.top