中文 ▾

Ollama API 文档:快速入门指南

本指南提供了我们 OpenAI 兼容 API 的具体细节,允许您在不更改现有客户端代码的情况下替换为我们的无审查模型。

基础 URL 和认证

基础 URL 为 HTTPS://api.ollamaapidocs.com/v1。请在标准的 Authorization 请求头中传入您的密钥。

curl https://api.ollamaapidocs.com/v1/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "uncensored",
    "messages": [{"role": "user", "content": "List installed models in the local directory"}]
  }'

聊天完成端点

发送 POST 请求至 /v1/chat/completions,将模型设置为 uncensored。

from openai import OpenAI

client = OpenAI(base_url="https://api.ollamaapidocs.com/v1", api_key="YOUR_KEY")

resp = client.chat.completions.create(
    model="uncensored",
    messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)

流式传输与工具支持

通过在请求中将 stream 设置为 true 来启用流式传输。API 通过服务器发送的事件返回令牌,并支持函数调用。

stream = client.chat.completions.create(
    model="uncensored",
    messages=[{"role": "user", "content": "Tell the story in second person."}],
    stream=True,
)
for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

JSON 模式与参数

使用 response_format json_object 获取严格的 JSON 输出。您还可以调整 temperature、top_p 和 stop sequences。

import OpenAI from "openai";

const client = new OpenAI({ baseURL: "https://api.ollamaapidocs.com/v1", apiKey: process.env.API_KEY });

const resp = await client.chat.completions.create({
  model: "uncensored",
  messages: [{ role: "user", content: "Draft a villain monologue for my game." }],
});
console.log(resp.choices[0].message.content);

速率限制与约束

每个密钥每分钟的限制为 300 次请求。每个请求支持 100,000 个令文的上下文窗口。

curl https://api.ollamaapidocs.com/v1/chat/completions \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "uncensored",
    "messages": [{"role": "user", "content": "List installed models in the local directory"}]
  }'

错误与上下文窗口

处理 401(密钥无效)、402(无余额)和 429(速率限制)错误。该模型支持 100,000 token 的上下文窗口。

from openai import OpenAI

client = OpenAI(base_url="https://api.ollamaapidocs.com/v1", api_key="YOUR_KEY")

resp = client.chat.completions.create(
    model="uncensored",
    messages=[{"role": "user", "content": "Summarise this thread without softening it."}],
)
print(resp.choices[0].message.content)

常见问题

此服务的 ollama API 文档是什么?

本指南介绍了我们托管 API 的基础 URL、身份验证和端点。它允许您使用标准 OpenAI SDK 配合我们的 uncensored 模型。

如何处理流式传输响应?

在请求中将 stream 参数设置为 true。API 通过服务器发送事件返回 token,允许实时处理。

如果我超出速率限制会怎样?

您将收到 429 状态码。限制为每个密钥每分钟 300 次请求,最多 8 个并发请求。

生成您的 API 密钥

注册,复制你的密钥,并更新基础 URL。这就是你开始所需的全部。

创建键