เอกสาร API Ollama: คู่มือเริ่มต้นอย่างรวดเร็ว
คู่มือนี้ให้รายละเอียดที่เป็นรูปธรรมสำหรับ API ที่เข้ากันได้กับ OpenAI ของเรา ซึ่งช่วยให้คุณสลับมาใช้โมเดลไม่มีเซ็นเซอร์ของเราได้โดยไม่จำเป็นต้องแก้ไขโค้ดไคลเอนต์ที่มีอยู่
URL ฐานและการตรวจสอบสิทธิ์
URL ฐานคือ HTTPS://api.ollamaapidocs.com/v1 ส่งคีย์ของคุณใน header 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"}]
}'
จุดจบ API Chat Completions
ส่งคำขอ POST ไปยัง /v1/chat/completions โดยตั้งค่า model เป็น 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 จะส่งมอบโทเค็นผ่าน Server-Sent Events และรองรับการเรียกใช้ฟังก์ชัน
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 ที่เข้มงวด คุณยังสามารถปรับอุณหภูมิ top_p และลำดับหยุดได้
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"}]
}'
ข้อผิดพลาดและ Context Window
จัดการสถานะ 401 สำหรับคีย์ที่ไม่ถูกต้อง, 402 สำหรับไม่มีเครดิต และ 429 สำหรับข้อจำกัดอัตราการใช้งาน โมเดลรองรับหน้าต่างบริบทขนาด 100,000 tokens
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)คำถามที่พบบ่อย
เอกสาร API ของ ollama สำหรับบริการนี้คืออะไร
คู่มือนี้ครอบคลุม URL พื้นฐาน การตรวจสอบสิทธิ์ และเอนด์พอยต์สำหรับ API ที่โฮสต์ของเรา มันช่วยให้คุณใช้ SDK มาตรฐานของ OpenAI กับโมเดล uncensored ของเรา
ฉันจัดการกับการตอบกลับแบบสตรีมมิ่งได้อย่างไร?
ตั้งค่าพารามิเตอร์ stream เป็น true ในการร้องขอของคุณ API จะส่งโทเค็นผ่าน Server-Sent Events ซึ่งอนุญาตให้ประมวลผลแบบเรียลไทม์
เกิดอะไรขึ้นหากฉันเกินขีดจำกัดอัตราการใช้งาน?
คุณจะได้รับสถานะโค้ด 429 โดยจำกัดที่ 300 คำขอต่อนาทีต่อคีย์ พร้อมกันสูงสุด 8 คำขอ
สร้างคีย์ API ของคุณ
สมัครสมาชิก คัดลอกคีย์ของคุณ แล้วอัปเดต URL พื้นฐาน นั่นคือสิ่งที่คุณต้องการเพื่อเริ่มต้น