SDK 示例
用官方 SDK 调用 OminiApi:Python、Node.js、Go 与环境变量
OminiApi 兼容官方接口,直接用各家官方 SDK,只需要改地址和 Key。
地址速查
| SDK | 地址 |
|---|---|
| OpenAI(Python / Node / Go) | https://omini.ominiapi.top/v1 |
| Anthropic(Python / Node) | https://omini.ominiapi.top |
| Google Gen AI(Python / Node) | https://omini.ominiapi.top |
用环境变量配置
大多数官方 SDK 会自动读取环境变量,这样代码里不用写死地址和 Key:
export OPENAI_BASE_URL="https://omini.ominiapi.top/v1"
export OPENAI_API_KEY="你的 API Key"Python
pip install openaifrom openai import OpenAI
client = OpenAI(base_url="https://omini.ominiapi.top/v1", api_key="你的 API Key")
resp = client.chat.completions.create(
model="模型名",
messages=[{"role": "user", "content": "你好"}],
)
print(resp.choices[0].message.content)Node.js
npm i openaiimport OpenAI from 'openai';
const client = new OpenAI({
baseURL: 'https://omini.ominiapi.top/v1',
apiKey: process.env.OMINI_API_KEY,
});
const resp = await client.chat.completions.create({
model: '模型名',
messages: [{ role: 'user', content: '你好' }],
});
console.log(resp.choices[0].message.content);Go
go get github.com/openai/openai-gopackage main
import (
"context"
"fmt"
"github.com/openai/openai-go"
"github.com/openai/openai-go/option"
)
func main() {
client := openai.NewClient(
option.WithBaseURL("https://omini.ominiapi.top/v1"),
option.WithAPIKey("你的 API Key"),
)
resp, err := client.Chat.Completions.New(context.Background(), openai.ChatCompletionNewParams{
Model: "模型名",
Messages: []openai.ChatCompletionMessageParamUnion{openai.UserMessage("你好")},
})
if err != nil {
panic(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}