الانتقال إلى المحتوى الرئيسي
POST
أرسل المحادثات عبر Chat Completions API المتوافق مع OpenAI. اضبط SDK باستخدام base_url="https://api.cometapi.com/v1" ومفتاح CometAPI API الخاص بك. تستخدم أمثلة الإخراج الأساسي والصور وStreaming والمخرجات المنظمة gpt-6-astra. ويستخدم مثال استدعاء الدوال gpt-6.1-sol، بينما يستخدم مثال احتمالات السجل gpt-4.1.
استخدم مرجع OpenAI Chat Completions للاطلاع على تعريفات المعلمات. لاستدعاء أدوات Astra في GPT-6، استخدم Responses، باتباع إرشادات نماذج OpenAI.

أدوار الرسائل

ضع تعليمات التطبيق في رسالة developer قبل رسائل المستخدم عند استخدام أمثلة Astra في GPT-6.

إرسال إدخال Multimodal

استخدم مصفوفة من أجزاء المحتوى لإرسال صورة مع نص:
استخدم image_url.detail لتحديد مستوى تفاصيل معالجة الصورة. يستخدم هذا المثال high.

بث الاستجابات

لتلقي المخرجات تدريجيًا، اضبط stream على true. يتم تسليم الاستجابة على هيئة الأحداث المرسلة من الخادم (SSE). يصل النص في كائنات chat.completion.chunk؛ ويتضمن هذا المثال المختصر مقطع الاستخدام النهائي:
لتضمين إحصاءات استخدام Token في استجابات Streaming، اضبط stream_options.include_usage على true. تظهر بيانات الاستخدام في مقطع نهائي قبل [DONE]. قد تحتوي المقاطع على مصفوفة choices فارغة؛ لذا تحقق منها قبل الوصول إلى choices[0].

طلب مخرجات منظَّمة

استخدم response_format لطلب مخرجات منظَّمة:
يحدد وضع JSON Schema البنية المطلوبة. يطلب وضع JSON Object بيانات JSON صالحة دون فرض مخططك. تحقق من وجود رفض أو استجابة محدودة الطول قبل استخدام النتيجة.

استدعاء الأدوات والدوال

استخدم gpt-6.1-sol مع تعريف دالة:
يُرجع استدعاء الدالة finish_reason: "tool_calls" ومصفوفة message.tool_calls. حلّل arguments الخاص بالدالة والمشفّر بتنسيق JSON، ونفّذ دالتك، ثم أضف رسالة المساعد ورسالة نتيجة tool مع tool_call_id المطابق لها.

اختيار معلمات الطلب

استخدم reasoning_effort وmax_completion_tokens مع Astra GPT-6. تعرض أمثلة Functions وLogprobs خيارات الطلب لـ gpt-6.1-sol وgpt-4.1، على التوالي.للاطلاع على تنسيقات الطلبات الخاصة بـ Claude أو Gemini، راجع Anthropic Messages و Gemini Generate content.
استخدم max_completion_tokens لتقييد عدد Tokens التي يتم إنشاؤها لأمثلة Astra GPT-6. يشمل ذلك الاستدلال والمخرجات المرئية، لذا اترك مساحة لكليهما. max_tokens معلمة قديمة.
استخدم developer لتعليمات التطبيق في أمثلة Astra GPT-6. احتفظ بمحتوى المستخدم النهائي في رسائل user، واحتفظ بردود المساعد السابقة عند متابعة محادثة.

الأسئلة الشائعة

كيفية التعامل مع حدود المعدل؟

عند مواجهة 429 Too Many Requests، طبّق التراجع الأُسّي:

كيفية الحفاظ على سياق المحادثة؟

ضمّن السجل الكامل للمحادثة في مصفوفة messages:

ماذا يعني finish_reason ؟

كيفية التحكم في التكاليف؟

  1. استخدم max_completion_tokens لتحديد الحد الأقصى لطول المخرجات.
  2. قارن أسعار النماذج واختر نموذجًا يلبّي متطلبات عبء العمل لديك.
  3. حافظ على إيجاز Prompt — تجنّب السياق المتكرر.
  4. راقب استخدام Token في حقل الاستجابة usage.

التفويضات

Authorization
string
header
مطلوب

Bearer token authentication. Use your CometAPI key.

الجسم

application/json
model
string
افتراضي:gpt-6-astra
مطلوب

Model ID to use for this request. See the Models page for current options.

مثال:

"gpt-6-astra"

messages
object[]
مطلوب

Conversation messages, including instructions, user input, assistant replies, and tool results.

stream
boolean

If true, partial response tokens are delivered incrementally via server-sent events (SSE). The stream ends with a data: [DONE] message.

temperature
number

Sampling temperature. Omit this field for GPT-6 Astra.

النطاق المطلوب: 0 <= x <= 2
top_p
number

Nucleus sampling threshold. Omit this field for GPT-6 Astra. For sampling overrides, adjust either top_p or temperature.

النطاق المطلوب: 0 <= x <= 1
n
integer

Number of completion choices to generate for each input message. Defaults to 1.

stop

Stop string or list of up to four strings, for models that support stop sequences.

max_tokens
integer
مهمل

Legacy output-token limit. Use max_completion_tokens for the GPT-6 Astra examples.

presence_penalty
number

Number between -2.0 and 2.0. Positive values penalize tokens based on whether they have already appeared, encouraging the model to explore new topics.

النطاق المطلوب: -2 <= x <= 2
frequency_penalty
number

Number between -2.0 and 2.0. Positive values penalize tokens proportionally to how often they have appeared, reducing verbatim repetition.

النطاق المطلوب: -2 <= x <= 2
logit_bias
object

A JSON object mapping token IDs to bias values from -100 to 100. The bias is added to the model's logits before sampling. Values between -1 and 1 subtly adjust likelihood; -100 or 100 effectively ban or force selection of a token.

user
string

A unique identifier for your end-user. Helps with abuse detection and monitoring.

max_completion_tokens
integer

Maximum generated tokens, including visible output and reasoning. Leave enough room for both, as in the image-input example.

response_format
object

Specifies the output format. Use {"type": "json_object"} for JSON mode, or {"type": "json_schema", "json_schema": {...}} for strict structured output.

tools
object[]

Function definitions for a model that supports tool calling on Chat Completions. The function example uses GPT-5.6 Sol; use Responses for GPT-6 Astra tool calls.

tool_choice

Controls how the model selects tools. auto (default): model decides. none: no tools. required: must call a tool.

logprobs
boolean

Return token log probabilities. Use gpt-4.1 as shown in the Logprobs example.

top_logprobs
integer

Number of most likely tokens to return at each position (0-20). Requires logprobs to be true.

النطاق المطلوب: 0 <= x <= 20
reasoning_effort
string

Reasoning effort supported by the selected model. The GPT-6 Astra examples use low; the GPT-5.6 Sol function example uses none. See the model reference for other supported levels.

stream_options
object

Options for streaming. Only valid when stream is true.

service_tier
enum<string>

Specifies the processing tier.

الخيارات المتاحة:
auto,
default,
flex,
priority

الاستجابة

Successful chat completion response.

id
string

Unique completion identifier.

مثال:

"chatcmpl_example"

object
enum<string>

Object type. Non-streaming responses use chat.completion.

الخيارات المتاحة:
chat.completion
مثال:

"chat.completion"

created
integer

Unix timestamp of creation.

مثال:

1788763703

model
string

The model used (may include version suffix).

مثال:

"gpt-6-astra"

choices
object[]

Array of completion choices.

usage
object

Token accounting for this request. Billing uses these counts.

service_tier
string

Service tier that processed the request, when returned.

system_fingerprint
string | null

Model configuration fingerprint, when returned.

آخر تعديل في ٨ أكتوبر ٢٠٢٦