
#  Creating an AI chatbot

```
Exercise ID 1583253
```

##  Assignment 

An online learning platform called **Easy as Pi** that specializes in teaching math skills has contracted you to help develop an AI tutor. You immediately see that you can build this feature on top of the OpenAI API, and start to design a simple proof-of-concept (POC) for the major stakeholders at the company. This POC will demonstrate the core functionality required to build the final feature and the power of the OpenAI's GPT models.

Example system and user messages have been provided for you, but feel free to play around with these to change the model's behavior or design a completely different chatbot!

The `openai` package has been pre-loaded for you.

##  Pre exercise code 

```
import openai
```



##  Instructions 

- Assign your API key to `openai.api_key`.
- Create a dictionary to house the user message in a format that can be sent to the API; then append it to `messages`.
- Create a Chat request to send `messages` to the model.
- Extract the assistant's message, convert it to a dictionary, and append it to `messages`.



```
# Set your API key
openai.api_key = "____"

messages = [{"role": "system", "content": "You are a helpful math tutor."}]
user_msgs = ["Explain what pi is.", "Summarize this in two bullet points."]

for q in user_msgs:
    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 = openai.____.____(
        model="gpt-3.5-turbo",
        ____,
        max_tokens=100
    )
    
    # Convert the assistant's message to a dict and append to messages
    assistant_dict = dict(____)
    ____
    print("Assistant: ", response["choices"][0]["message"]["content"], "\n")
```

##  Hints 

- To create `user_dict`, the `"role"` key must be assigned to `"user"`.
- Extract the role and content information from the JSON response so `assistant_dict` looks this:

```
{"role": "assistant", "content": "Example response from the assistant."}

```



##  Solution 

```
# Set your API key
openai.api_key = "<OPENAI_API_TOKEN>"

messages = [{"role": "system", "content": "You are a helpful math tutor."}]
user_msgs = ["Explain what pi is.", "Summarize this in two bullet points."]

for q in user_msgs:
    print("User: ", q)
    
    # Create a dictionary for the user message from q and append to messages
    user_dict = {"role": "user", "content": q}
    messages.append(user_dict)
    
    # Create the API request
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
        max_tokens=100
    )
    
    # Convert the assistant's message to a dict and append to messages
    assistant_dict = dict(response["choices"][0]["message"])
    messages.append(assistant_dict)
    print("Assistant: ", response["choices"][0]["message"]["content"], "\n")
```


