Tạo phản hồi bằng API ChatCompletions
API OpenAI ChatCompletions thường được sử dụng trên các mô hình và nền tảng AI tổng quát. Mặc dù bạn nên sử dụng API Phản hồi để phát triển dự án mới, nhưng có khả năng bạn sẽ gặp phải các tình huống trong đó API ChatCompletions hữu ích cho việc duy trì mã về khả năng tương thích đa nền tảng.
Gửi lời nhắc
API ChatCompletions sử dụng bộ sưu tập các đối tượng tin nhắn ở định dạng JSON để đóng gói lời nhắc:
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)
Giữ lại ngữ cảnh hội thoại
Không giống như API Phản hồi , API ChatCompletins không cung cấp tính năng theo dõi phản hồi có trạng thái. Để giữ lại ngữ cảnh hội thoại, bạn phải viết mã để theo dõi thủ công các lời nhắc và phản hồi trước đó.
# 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...
Trong một ứng dụng thực tế, cuộc trò chuyện có thể được thực hiện trong một vòng lặp; như thế này:
# 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}
)
Kết quả từ ví dụ này trông tương tự như sau:
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!
Mỗi lời nhắc và hoàn thành của người dùng mới được thêm vào cuộc trò chuyện và toàn bộ lịch sử cuộc trò chuyện được gửi trong mỗi lượt.
Mặc dù không có đầy đủ tính năng như API Phản hồi , nhưng API ChatCompletions đã được thiết lập tốt trong hệ sinh thái mô hình AI tổng quát, vì vậy sẽ rất hữu ích nếu bạn làm quen với nó.