Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
요청 동시성을 제한하고, 지터와 함께 429 응답을 재시도하며, model 및 route별 사용량을 모니터링하여 CometAPI 속도 제한을 처리하세요.
429
import asyncio import os from openai import AsyncOpenAI client = AsyncOpenAI( api_key=os.environ["COMETAPI_KEY"], base_url="https://api.cometapi.com/v1", ) semaphore = asyncio.Semaphore(5) async def ask(prompt): async with semaphore: completion = await client.chat.completions.create( model="your-model-id", messages=[{"role": "user", "content": prompt}], ) return completion.choices[0].message.content async def main(): prompts = ["Say hello.", "Write a title.", "Return one JSON key."] results = await asyncio.gather(*(ask(prompt) for prompt in prompts)) print(results) asyncio.run(main())
[ "Hello.", "A concise title", "{\"key\":\"value\"}" ]
import OpenAI from "openai"; const client = new OpenAI({ apiKey: process.env.COMETAPI_KEY, baseURL: "https://api.cometapi.com/v1", }); async function sleep(milliseconds) { return new Promise((resolve) => setTimeout(resolve, milliseconds)); } async function createCompletion() { for (let attempt = 0; attempt < 5; attempt += 1) { try { return await client.chat.completions.create({ model: "your-model-id", messages: [{ role: "user", content: "Say hello." }], }); } catch (error) { if (error.status !== 429 || attempt === 4) { throw error; } const delay = Math.min(30000, 1000 * 2 ** attempt); await sleep(delay + Math.random() * 1000); } } } const completion = await createCompletion(); console.log(completion.choices[0].message.content);
{ "choices": [ { "message": { "content": "Hello." } } ] }