ChatCompletions API'siyle yanıt oluşturma
Tip
Daha fazla ayrıntı için Metin ve resimler sekmesine bakın!
OpenAI ChatCompletions API'leri, üretken yapay zeka modellerinde ve platformlarında yaygın olarak kullanılır. Yanıtlar API'sinin yeni proje geliştirmesi için kullanılması önerilse de, ChatCompletions API'sinin kod bakımı veya platformlar arası uyumluluk için yararlı olduğu senaryolarla karşılaşabilirsiniz.
İstem gönderme
ChatCompletions API'sinde, istemleri kapsüllemek için JSON biçimindeki ileti nesneleri koleksiyonları kullanılır:
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)
Konuşma bağlamı koruma
Yanıtlar API'sinin aksine, ChatCompletions API'si durum bilgisi olan bir yanıt izleme özelliği sağlamaz. Konuşma bağlamı korumak için, önceki istemleri ve yanıtları el ile izlemek için kod yazmanız gerekir.
# 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...
Gerçek bir uygulamada, konuşma büyük olasılıkla bir döngüde uygulanır; Böyle:
# 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}
)
Bu örnekteki çıkış şuna benzer:
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!
Her yeni kullanıcı istemi ve tamamlama konuşmaya eklenir ve konuşma geçmişinin tamamı her turda iletilir.
Yanıtlar API'si kadar kapsamlı özelliklere sahip olmasa da, ChatCompletions API'si üretken yapay zeka modeli ekosisteminde iyi konumlanmıştır, bu nedenle bu API'ye aşina olmak faydalıdır.