إنتقل إلى المحتوى الرئيسي

الفصل 1: التعمق في واجهة برمجة تطبيقات OpenAI

الاستخدام الأساسي​

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
model="gpt-4",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
temperature=0.7,
max_tokens=150
)
print(response.choices[0].message.content)

المعلمات الرئيسية​

المعلمةالغرضالنطاق
temperatureالعشوائية0-2
max_tokensحد الإخراجيعتمد على النموذج
top_pأخذ العينات النواة0-1
frequency_penaltyالتحكم في التكرار-2 إلى 2

استدعاء الوظائف​

response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "What's the weather in Paris?"}],
tools=[{
"type": "function",
"function": {
"name": "get_weather",
"parameters": {
"type": "object",
"properties": {"location": {"type": "string"}}
}
}
}]
)

البث​

stream = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": "Tell me a story"}],
stream=True
)
for chunk in stream:
print(chunk.choices[0].delta.content, end="")