CometAPI API anahtarını ve base URL’yi ayarlayarak OpenAI Python ve Node.js SDK’lerini CometAPI için yapılandırın.
İstemcideki iki ayarı değiştirerek OpenAI SDK’yi CometAPI ile kullanın: API anahtarı ve base URL. Mevcut OpenAI uyumlu istek kodunuzu koruyun, ardından model ID’yi kullanılabilir bir CometAPI model ID ile değiştirin.
SDK’yi yükleme
Aşağıdaki komut OpenAI Python SDK’yi yükler:
pip install openai
Aşağıdaki komut OpenAI Node.js SDK’yi yükler:
npm install openai
Python istemcisi
Aşağıdaki Python örneği bir CometAPI istemcisi oluşturur ve bir sohbet isteği gönderir:
import osfrom openai import OpenAIclient = OpenAI( api_key=os.environ["COMETAPI_KEY"], base_url="https://api.cometapi.com/v1",)completion = client.chat.completions.create( model="your-model-id", messages=[ { "role": "user", "content": "Answer in one short sentence: What is CometAPI?", } ],)print(completion.choices[0].message.content)
Aşağıdaki Node.js örneği OpenAI SDK ile baseURL kullanır:
import OpenAI from "openai";const client = new OpenAI({ apiKey: process.env.COMETAPI_KEY, baseURL: "https://api.cometapi.com/v1",});const completion = await client.chat.completions.create({ model: "your-model-id", messages: [ { role: "user", content: "Answer in one short sentence: What is CometAPI?", }, ],});console.log(completion.choices[0].message.content);
Yanıt yapısı, OpenAI uyumlu aynı chat completion yapısıdır:
{ "id": "chatcmpl_example", "object": "chat.completion", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "CometAPI provides API access to models from multiple providers." } } ]}
Yaygın hatalar
Hata
Düzeltme
401
COMETAPI_KEY değişkeninin ayarlandığını ve isteğin Bearer auth kullandığını doğrulayın.
SDK hâlâ OpenAI’yi çağırıyor
Python’da base_url veya Node.js’te baseURL ayarlayın.