繁中 ▾

Ollama API 文件:快速入門指南

本指南提供我們 OpenAI-compatible API 的具體細節,讓您能夠在不更改現有客戶端程式碼的情況下,替換為我們的無審查模型。

基礎 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 會透過伺服器發送事件傳回 token,並支援函式呼叫。

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 token 的上下文視窗。

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。這就是開始所需的一切。

建立金鑰