BaşlayınÜcretsiz Başlayın

Creating an AI chatbot

To complete your POC, you'll integrate your message history with a for loop, so you can send repeated prompts to the model, storing each response in the message history in series.

Bu egzersiz

Working with the OpenAI API

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Loop over the user messages (user_msgs).
  • Create a dictionary for the user message in each iteration, and append it to messages.
  • Send messages to the model in a chat request.
  • Append the assistant message dictionary to messages.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

client = OpenAI(api_key="")

messages = [{"role": "system", "content": "You are a helpful math tutor that speaks concisely."}]
user_msgs = ["Explain what pi is.", "Summarize this in two bullet points."]

# Loop over the user questions
for q in ____:
    print("User: ", q)
    
    # Create a dictionary for the user message from q and append to messages
    user_dict = {"role": ____, "content": ____}
    messages.append(____)
    
    # Create the API request
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        ____,
        max_completion_tokens=100
    )
    
    # Append the assistant's message to messages
    assistant_dict = {"role": "assistant", "content": response.choices[0].message.content}
    messages.append(____)
    print("Assistant: ", response.choices[0].message.content, "\n")
Kodu Düzenle ve Çalıştır