OpenAI codex API:使用 Python 快速入門
本指南展示如何透過我們的 openai compatible API 使用標準 OpenAI SDK 來使用無審查 LLM。您可以透過更新基礎 URL 和 API 金鑰立即開始發送請求。
安裝官方 SDK
使用官方 SDK 以確保與我們 openai 代理 API 的兼容性。此方法適用於任何支持 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);
設置您的 Base URL 和密鑰
將您的客戶端指向我們的 OpenAI 閘道器端點以存取無審查的 AI 模型。將預設的基礎 URL 替換為我們的特定端點。
- 在您的環境變數中設定 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."}]
}'
發送您的第一個請求
發送簡單的文本完成請求以測試連線。使用模型 ID 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 用於即時 token 交付。
- 使用
response_formatfor 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."}]
}'串流有助於您快速顯示部分結果。
處理工具與函式呼叫
在請求中定義工具以啟用函式呼叫。指定工具定義並讓模型決定何時使用它們。
- 定義函式架構。
- 從回應中解析工具呼叫。
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 個 token。
- 處理無效金鑰的 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)常見問題
OpenAI Codex API 是什麼?
這是一款無審查的 LLM API,與 OpenAI 協議相容。您可以透過更新基礎網址,使用標準 SDK 進行連接。
如何處理錯誤?
常見錯誤包括 401(無效金鑰)、402(無餘額)和 429(速率限制)。請檢查您的帳戶餘額和金鑰有效性。
上下文視窗的大小是多少?
上下文窗口總計 100,000 token,包含提示詞與補全內容。最大輸出為每個請求 16,000 token。
取得您的 API 金鑰
註冊、複製金鑰並更新基礎 URL。就是這些。