Generieren von Antworten mit der ChatCompletions-API
Die OpenAI ChatCompletions-API wird häufig für generative KI-Modelle und -Plattformen verwendet. Obwohl die Antwort-API für die neue Projektentwicklung empfohlen wird, treten wahrscheinlich Szenarien auf, in denen die ChatCompletions-API für die Codewartung plattformübergreifender Kompatibilität nützlich ist.
Senden eines Prompts
Die ChatCompletions-API verwendet Sammlungen von Nachrichtenobjekten im JSON-Format, um Eingabeaufforderungen zu kapseln:
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)
Beibehalten des Unterhaltungskontexts
Im Gegensatz zur Antwort-API stellt die ChatCompletins-API kein feature für die statusbehaftete Antwortnachverfolgung bereit. Um den Unterhaltungskontext beizubehalten, müssen Sie Code schreiben, um vorherige Eingabeaufforderungen und Antworten manuell nachzuverfolgen.
# 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...
In einer echten Anwendung wird die Unterhaltung wahrscheinlich in einer Schleife implementiert; So:
# 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}
)
Die Ausgabe dieses Beispiels sieht in etwa so aus:
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!
Jeder neue Benutzer Prompt und Abschluss wird der Kommunikation hinzugefügt, und die gesamte Historie der Kommunikation wird in jeder Runde gesendet.
Die ChatCompletions-API ist zwar nicht so umfassend wie die Antwort-API, aber im generativen KI-Modellökosystem gut etabliert, daher ist es hilfreich, mit der API vertraut zu sein.