OpenAI Codex API: Pythonでのクイックスタート
このガイドでは、OpenAI互換APIを介して標準のOpenAI SDKを使用して無制限LLMを使用する方法を示します。ベースURLとAPIキーを更新するだけで、すぐにリクエストの送信を開始できます。
公式SDKをインストール
OpenAI互換プロトコルをサポートする任意の標準ライブラリで動作するため、公式SDKを使用してOpenAIプロキシ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);
ベースURLとキーを設定
クライアントをopenaiゲートウェイエンドポイントに接続して、uncensored 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 リアルタイムトークン配信用。
- 使用
response_formatfor JSON output. - コード内でチャンクを処理します。
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トークンをサポートします。
- 無効なキーによる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
OpenAI Codex APIとは何か?
フィルターなしのLLM APIで、OpenAIプロトコルと互換性があります。ベースURLを更新するだけで、標準的なSDKで使用できます。
エラーを処理するにはどうすればよいですか?
よくあるエラーには、無効なキーによる401、クレジットなしによる402、レート制限による429が含まれます。アカウント残高とキーの有効性を確認してください。
コンテキストウィンドウのサイズはいくらですか?
コンテキストウィンドウはプロンプトと補合を合わせて100,000トークンです。最大出力はリクエストあたり16,000トークンです。
APIキーを取得
登録、キーのコピー、ベースURLの更新。これだけです。