יצר תגובות באמצעות ממשק ה-API של ChatPletions
ממשק OpenAI ChatPletions API נפוץ במודלים ופלטפורמות של בינה מלאכותית גנרטיבית. למרות ש-API Responses מומלץ לפיתוח פרויקטים חדשים, סביר להניח שתיתקל בתרחישים שבהם ה-API של ChatPletions שימושי לתחזוקת קוד של תאימות בין פלטפורמות.
הגשת הנחיה
ממשק ה-API של ChatCompletions משתמש באוספים של אובייקטי הודעות בפורמט JSON כדי לארוז פקודות:
completion = openai_client.chat.completions.create(
model="gpt-4o", # Your model deployment name
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "When was Microsoft founded?"}
]
)
print(completion.choices[0].message.content)
שמירה על הקשר שיחתי
בניגוד ל-API של תגובות , ממשק ה-API של ChatCompletins אינו מספק תכונת מעקב תגובות במצב מלא. כדי לשמור על הקשר שיחתי, עליך לכתוב קוד כדי לעקוב ידנית אחרי ההנחיות והתשובות הקודמות.
# Initial messages
conversation_messages=[
{
"role": "system",
"content": "You are a helpful AI assistant that answers questions and provides information."
}
]
# Add the first user message
conversation_messages.append(
{"role": "user",
"content": "When was Microsoft founded?"}
)
# Get a completion
completion = openai_client.chat.completions.create(
model="gpt-4o",
messages=conversation_messages
)
assistant_message = completion.choices[0].message.content
print("Assistant:", assistant_text)
# Append the response to the conversation
conversation_messages.append(
{"role": "assistant", "content": assistant_text}
)
# Add the next user message
conversation_messages.append(
{"role": "user",
"content": "Who founded it?"}
)
# Get a completion
completion = openai_client.chat.completions.create(
model="gpt-4o",
messages=conversation_messages
)
assistant_message = completion.choices[0].message.content
print("Assistant:", assistant_text)
# and so on...
ביישום אמיתי, השיחה צפויה להתבצע בלולאה; ככה:
# Initial messages
conversation_messages=[
{
"role": "system",
"content": "You are a helpful AI assistant that answers questions and provides information."
}
]
# Loop until the user wants to quit
print("Assistant: Enter a prompt (or type 'quit' to exit)")
while True:
input_text = input('\nYou: ')
if input_text.lower() == "quit":
print("Assistant: Goodbye!")
break
# Add the user message
conversation_messages.append(
{"role": "user",
"content": input_text}
)
# Get a completion
completion = openai_client.chat.completions.create(
model="gpt-4o",
messages=conversation_messages
)
assistant_message = completion.choices[0].message.content
print("\nAssistant:", assistant_message)
# Append the response to the conversation
conversation_messages.append(
{"role": "assistant", "content": assistant_message}
)
הפלט מהדוגמה הזו נראה דומה לזה:
Assistant: Enter a prompt (or type 'quit' to exit)
You: When was Microsoft founded?
Assistant: Microsoft was founded on April 4, 1975 in Albuquerque, New Mexico, USA.
You: Who founded it?
Assistant: Microsoft was founded by Bill Gates and Paul Allen.
You: quit
Assistant: Goodbye!
כל הנחיית משתמש חדשה והשלמה מתווספת לשיחה, וכל היסטוריית השיחה נשלחת בכל תור.
למרות שאינו מלא כמו ה-API של Responses , ממשק ה-API של ChatCompleteions מבוסס היטב באקוסיסטם של מודלים גנרטיביים של בינה מלאכותית, ולכן חשוב להכיר אותו.