استدعاء LLM من Python
أرسل المطالبات وحلّل الاستجابات
استدعاء LLM من Python درس مجاني في NLP Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في NLP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة NLP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Talk to a Model in Code
You do not run giant models on your laptop. Instead you send text to a hosted model over an API and get a reply back. 🌐
Install a Client
Most providers ship a Python client library so you can call the model with a few clean lines instead of raw HTTP.
pip install openaiKeep Your Key Secret
Calls are authenticated with an API key. Store it in an environment variable, never hard-coded in your source files.
import os
key = os.environ["OPENAI_API_KEY"]Create the Client
You start by building a client object. It reads your key and handles the network details for every request you make.
from openai import OpenAI
client = OpenAI()Messages, Not Just Text
Chat models take a list of messages, each tagged with a role like system, user, or assistant.
The System Role
A system message sets the model's behavior up front, like telling it to answer briefly or act as a helpful tutor.
Send Your Prompt
You put your question in a user message and send the whole list to the model in a single call.
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Hi!"}])Parse the Response
The reply is a structured object. The text you want sits inside the first choice, ready to read or store.
text = resp.choices[0].message.content
print(text)Control With Temperature
The temperature setting controls randomness. Low values give steady answers; high values give more creative, varied ones.
Cap the Output
Setting max tokens limits how long the reply can be, which keeps responses tidy and your costs predictable.
Handle Failures
Networks fail and limits get hit, so wrap calls in try/except and retry gracefully when an error comes back.
Quick Check
Where do you find the model's text in a chat completion response?
Recap
Install a client, load your key from the environment, send role-tagged messages, then read the text from the first choice. ✅
الأسئلة الشائعة
هل درس «استدعاء LLM من Python» مجاني؟
نعم — نص درس «استدعاء LLM من Python» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة NLP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة NLP Academy 4 دروس في المجموع.
ماذا ستتعلم في «استدعاء LLM من Python»؟
أرسل المطالبات وحلّل الاستجابات تتمرن على NLP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ NLP Academy؟
لا تُشترط خبرة سابقة. NLP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «استدعاء LLM من Python»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس NLP Academy هذا؟
نعم. كل درس في NLP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- ما الذي يجعل النموذج كبيرًا
- استدعاء LLM من Python
- التوجيه بدون أمثلة وبأمثلة قليلة
- المخرجات المنظّمة وحواجز الحماية