API codex OpenAI: เริ่มต้นอย่างรวดเร็วด้วย Python
คู่มือนี้แสดงวิธีใช้ LLM แบบไม่มีเซ็นเซอร์ร่วมกับ SDK มาตรฐานของ OpenAI ผ่าน API ที่เข้ากันได้กับ OpenAI คุณสามารถเริ่มส่งคำขอได้ทันทีโดยการอัปเดต URL พื้นฐานและคีย์ API
ติดตั้ง Official SDK
ใช้ SDK ทางการเพื่อให้เข้ากันได้กับ API proxy OpenAI ของเรา วิธีนี้ใช้งานได้กับไลบรารีมาตรฐานใดๆ ที่รองรับโปรโตคอล OpenAI
- ติดตั้งแพ็กเกจผ่านตัวจัดการแพ็กเกจที่คุณต้องการ
- ตรวจสอบการติดตั้งโดยการนำเข้าไลบรารี
from openai import OpenAI
client = OpenAI(base_url="https://api.openaicodexapi.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)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openaicodexapi.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);
ตั้งค่า URL ฐานและคีย์ของคุณ
ชี้ไคลเอนต์ของคุณไปยัง endpoint gateway openai ของเราเพื่อเข้าถึงโมเดล AI แบบ uncensored เปลี่ยน URL base เริ่มต้นด้วย endpoint เฉพาะของเรา
- ตั้งค่าคีย์ API ในตัวแปรสภาพแวดล้อมของคุณ
- กำหนดค่า URL พื้นฐานเป็น
https://api.openaicodexapi.com/v1.
การตั้งค่านี้ทำให้คำขอของคุณถูกส่งไปยังเซิร์ฟเวอร์ของเราอย่างถูกต้องโดยไม่จำเป็นต้องปรับโครงสร้างโค้ดที่มีอยู่
curl https://api.openaicodexapi.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Explain how prepaid billing works for this API."}]
}'
ส่งคำขอครั้งแรกของคุณ
ส่ง text completion แบบง่ายเพื่อทดสอบการเชื่อมต่อ ใช้รหัสโมเดล uncensored สำหรับคำขอ
- กำหนดเนื้อหาข้อความ
- เรียกขอตอบ
from openai import OpenAI
client = OpenAI(base_url="https://api.openaicodexapi.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)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openaicodexapi.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);การตอบกลับส่งข้อความที่สร้างขึ้นโดยตรง
เปิดใช้งานการสตรีมและโหมด JSON
สตรีมการตอบกลับเพื่อลดเวลาแฝงหรือบังคับใช้โหมด JSON สำหรับข้อมูลที่มีโครงสร้าง ตั้งค่า stream=true สำหรับการส่งมอบโทเค็นแบบเรียลไทม์
- ใช้
response_formatสำหรับผลลัพธ์แบบ JSON - จัดการชิ้นส่วนข้อมูลในโค้ดของคุณ
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)curl https://api.openaicodexapi.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Explain how prepaid billing works for this API."}]
}'การสตรีมช่วยให้คุณแสดงผลลัพธ์บางส่วนได้รวดเร็ว
จัดการ Tools และ Function Calling
กำหนดเครื่องมือในคำขอเพื่อเปิดใช้งานการเรียกใช้ฟังก์ชัน ระบุคำจำกัดความของเครื่องมือและปล่อยให้โมเดลตัดสินใจเมื่อจะใช้
- กำหนดสคีมาฟังก์ชัน
- แยก tool calls ในการตอบกลับ
from openai import OpenAI
client = OpenAI(base_url="https://api.openaicodexapi.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)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openaicodexapi.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);คุณสมบัตินี้ช่วยให้สามารถผสานรวมกับระบบภายนอกได้
ข้อจำกัด, ข้อผิดพลาด และหน้าต่างบริบท
ติดตามการใช้งานของคุณเพื่อหลีกเลี่ยงขีดจำกัดอัตราหรือข้อผิดพลาด หน้าต่างบริบทรองรับโทเค็นได้รวม 100,000 โทเค็น
- จัดการข้อผิดพลาด 401 สำหรับคีย์ที่ไม่ถูกต้อง
- จัดการข้อผิดพลาด 402 สำหรับเครดิตไม่เพียงพอ
- จัดการข้อผิดพลาด 429 สำหรับข้อจำกัดอัตราการใช้งาน
curl https://api.openaicodexapi.com/v1/chat/completions \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "uncensored",
"messages": [{"role": "user", "content": "Explain how prepaid billing works for this API."}]
}'from openai import OpenAI
client = OpenAI(base_url="https://api.openaicodexapi.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)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://api.openaicodexapi.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);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)FAQ
API ของ OpenAI Codex คืออะไร?
เป็น API LLM แบบไม่เซ็นเซอร์ที่เข้ากันได้กับโปรโตคอล OpenAI คุณสามารถใช้งานร่วมกับ SDK มาตรฐานได้โดยการอัปเดต URL พื้นฐาน
ฉันจะจัดการข้อผิดพลาดได้อย่างไร?
ข้อผิดพลาดทั่วไปรวมถึง 401 สำหรับคีย์ไม่ถูกต้อง, 402 เมื่อไม่มีเครดิต และ 429 สำหรับขีดจำกัดอัตรา ตรวจสอบยอดคงเหลือบัญชีและความถูกต้องของคีย์ของคุณ
ขนาด context window คืออะไร?
หน้าต่างบริบทคือ 100,000 token สำหรับทั้ง prompt และ completion รวมกัน เอาต์พุตสูงสุดคือ 16,000 token ต่อคำขอ
รับคีย์ API ของคุณ
ลงทะเบียน คัดลอกคีย์ และอัปเดต base URL แค่นั้นเอง