← 返回首页

API 接入指南

本服务提供 OpenAI 兼容格式的 API,可直接使用 OpenAI SDK 或 HTTP 请求调用。

Base URL

https://ollama.casatwy.com/api/v1

认证方式

在请求头中添加:

Authorization: Bearer sk-your-key

可用端点

方法路径说明
GET/api/v1/models获取可用模型列表
POST/api/v1/chat/completions聊天补全(支持流式)

代码示例

cURL

curl https://ollama.casatwy.com/api/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer sk-your-key" \
  -d '{
    "model": "qwen2.5:latest",
    "messages": [{"role": "user", "content": "你好"}]
  }'

Python(openai SDK)

from openai import OpenAI

client = OpenAI(
    base_url="https://ollama.casatwy.com/api/v1",
    api_key="sk-your-key",
)

response = client.chat.completions.create(
    model="qwen2.5:latest",
    messages=[{"role": "user", "content": "你好"}],
)
print(response.choices[0].message.content)

Node.js(openai SDK)

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://ollama.casatwy.com/api/v1",
  apiKey: "sk-your-key",
});

const response = await client.chat.completions.create({
  model: "qwen2.5:latest",
  messages: [{ role: "user", content: "你好" }],
});
console.log(response.choices[0].message.content);

接入第三方工具

支持所有兼容 OpenAI API 格式的工具,例如 ChatBox、Open WebUI 等。配置方法:

  1. API Base URL 填写:https://ollama.casatwy.com/api/v1
  2. API Key 填写你获取到的 Key(sk-xxx
  3. 模型名称选择或手动输入即可

在线测试

输入你的 API Key,点击测试按钮验证是否可用。