نقطة نهاية إكمالات المحادثة
تعرّفوا إلى مصفوفة messages التي تتضمن أدوار system وuser وassistant، وصمّموا أول prompt لكم، وفسّروا كائن الاستجابة الوارد من API.
نقطة نهاية إكمالات المحادثة درس مجاني في AI Engineering Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Engineering Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Engineering Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
The Messages Array Architecture
The Chat Completions endpoint runs on a messages array: a list of turns, each with a role (system, user, or assistant). The model is stateless, so you send the history every time.
from openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{'role': 'system', 'content': 'You are a concise Python tutor.'},
{'role': 'user', 'content': 'What is a list comprehension?'}
]
)
print(response.choices[0].message.content)The System Role: Defining Behavior
The system message is your biggest lever. It sets the model's persona, rules, and format before the user types a word. Invest time here — it shapes everything. See the code.
system_prompt = '''You are a customer support agent for TechShop.
You help customers with: order tracking, returns, and product questions.
You do NOT discuss pricing changes or competitor products.
Always respond in 2-3 sentences maximum.
If you cannot help, say: 'Let me connect you with a human agent.'
'''
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{'role': 'system', 'content': system_prompt},
{'role': 'user', 'content': 'Where is my order #12345?'}
]
)Multi-Turn Conversation Management
To keep a conversation going, you append each turn to the messages array and resend it all. That's how the model seems to remember — you're feeding it the full history.
history = [
{'role': 'system', 'content': 'You are a helpful assistant.'}
]
def chat(user_message):
history.append({'role': 'user', 'content': user_message})
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=history
)
assistant_reply = response.choices[0].message.content
history.append({'role': 'assistant', 'content': assistant_reply})
return assistant_reply
print(chat('My name is Alice.'))
print(chat('What is my name?')) # model remembers 'Alice'Anatomy of the API Response
The response is an object, not just text. choices holds the reply, finish_reason says why it stopped, and usage counts tokens — which is your cost. Log these in production.
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content': 'Say hello in one word.'}]
)
# Accessing response fields
print('Content:', response.choices[0].message.content)
print('Finish reason:', response.choices[0].finish_reason) # 'stop'
print('Model:', response.model) # exact version like gpt-4o-mini-2024-07-18
print('Prompt tokens:', response.usage.prompt_tokens)
print('Completion tokens:', response.usage.completion_tokens)
print('Total tokens:', response.usage.total_tokens)Understanding finish_reason
finish_reason tells you why generation stopped. 'stop' means done; 'length' means it hit max_tokens and got cut off mid-answer. Always check it — truncation is a silent bug.
def safe_completion(messages, max_tokens=500):
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=messages,
max_tokens=max_tokens
)
choice = response.choices[0]
if choice.finish_reason == 'length':
print(f'WARNING: Response was truncated at {max_tokens} tokens!')
elif choice.finish_reason == 'content_filter':
print('WARNING: Response blocked by content filter!')
return None
return choice.message.contentSelecting the Right Model
Pick the right model for the job. gpt-4o is the powerhouse for hard reasoning; gpt-4o-mini is far cheaper and handles most tasks well. Benchmark before assuming bigger wins.
# Model comparison guidance
models = {
'gpt-4o': {
'use_for': 'Complex reasoning, code generation, nuanced analysis',
'input_cost_per_1M': 2.50, # USD
'output_cost_per_1M': 10.00
},
'gpt-4o-mini': {
'use_for': 'Classification, extraction, summarization, Q&A',
'input_cost_per_1M': 0.15,
'output_cost_per_1M': 0.60
}
}
# gpt-4o is ~17x more expensive on input tokensContent Types in Messages
A message's content can be more than text. For vision models like gpt-4o, you pass a list mixing text and images — so you can ask questions about charts or screenshots.
# Sending an image to a vision-capable model
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{
'role': 'user',
'content': [
{
'type': 'text',
'text': 'What is in this image? Describe in one sentence.'
},
{
'type': 'image_url',
'image_url': {'url': 'https://example.com/photo.jpg'}
}
]
}
]
)The n Parameter: Multiple Completions
The n parameter returns several completions for one prompt. Handy for picking the best, or for confidence: if all n agree, the model is sure; if they clash, be wary.
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[{'role': 'user', 'content': 'Name the capital of Germany.'}],
n=3, # generate 3 independent completions
temperature=0.5
)
for i, choice in enumerate(response.choices):
print(f'Completion {i+1}: {choice.message.content}')
# Check if all completions agree (confidence signal)
answers = [c.message.content.strip() for c in response.choices]
print('All agree:', len(set(answers)) == 1)Handling the Response as a String
To grab the reply as text, the path is always response.choices[0].message.content. Wrap it in a helper — and guard for None, which happens on tool calls or filters.
def get_completion(prompt, system='You are a helpful assistant.', model='gpt-4o-mini'):
'''Simple helper that returns the response text as a string.'''
response = client.chat.completions.create(
model=model,
messages=[
{'role': 'system', 'content': system},
{'role': 'user', 'content': prompt}
]
)
content = response.choices[0].message.content
if content is None:
raise ValueError(f'No content in response. Finish reason: {response.choices[0].finish_reason}')
return content
result = get_completion('Explain recursion in one sentence.')
print(result)Inspecting the Raw Request and Response
Debugging odd replies? Inspect the raw request and response. Setting OPENAI_LOG=debug prints the full body to your terminal — the fastest way to see what's on the wire.
import json
import httpx
# Enable debug logging (shows full request/response)
import os
os.environ['OPENAI_LOG'] = 'debug'
# Or use a custom logging client:
class LoggingClient(httpx.Client):
def send(self, request, *args, **kwargs):
print('REQUEST:', request.method, request.url)
print('BODY:', json.loads(request.content))
response = super().send(request, *args, **kwargs)
print('STATUS:', response.status_code)
return responseBuilding a Minimal Chat Loop
Now you can build a minimal chat loop: keep a messages list, append each turn, send it all, repeat. That simple pattern powers every chat app on the API. The code shows it.
import openai
client = openai.OpenAI()
SYSTEM_PROMPT = 'You are a helpful assistant. Be concise.'
def simple_chat_loop():
messages = [{'role': 'system', 'content': SYSTEM_PROMPT}]
print('Chat started. Type "quit" to exit.')
while True:
user_input = input('You: ').strip()
if user_input.lower() == 'quit':
break
if not user_input:
continue
messages.append({'role': 'user', 'content': user_input})
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=messages,
max_tokens=500
)
assistant_reply = response.choices[0].message.content
messages.append({'role': 'assistant', 'content': assistant_reply})
print(f'Assistant: {assistant_reply}\n')
print('Example chat loop defined. Run simple_chat_loop() to start.')Quick Check
Test your understanding of AI Engineering concepts from this lesson.
Lesson Recap
You learned the chat core: the messages array controls the conversation, and the response carries content, finish_reason, and token counts. Next: parameters.
الأسئلة الشائعة
هل درس «نقطة نهاية إكمالات المحادثة» مجاني؟
نعم — نص درس «نقطة نهاية إكمالات المحادثة» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Engineering Academy، انتقل إلى CoddyKit PRO. تتضمن دورة AI Engineering Academy 4 دروس في المجموع.
ماذا ستتعلم في «نقطة نهاية إكمالات المحادثة»؟
تعرّفوا إلى مصفوفة messages التي تتضمن أدوار system وuser وassistant، وصمّموا أول prompt لكم، وفسّروا كائن الاستجابة الوارد من API. تتمرن على AI Engineering Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ AI Engineering Academy؟
لا تُشترط خبرة سابقة. AI Engineering Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «نقطة نهاية إكمالات المحادثة»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس AI Engineering Academy هذا؟
نعم. كل درس في AI Engineering Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- إعداد بيئة Python الخاصة بكم
- نقطة نهاية إكمالات المحادثة
- التحكم في سلوك النموذج باستخدام المعلمات
- معالجة الأخطاء وحدود معدل الطلبات