
#  The Chat Completion endpoint

```
Exercise ID 1578137
```

##  Assignment 

The models available via the Chat Completions endpoint can not only perform similar single-turn tasks as models from the Completions endpoint, but can also be used to have multi-turn conversations.

To enable multi-turn conversations, the endpoint supports three different roles:

- **System**: controls assistant's **behavior**
- **User**: **instruct** the assistant
- **Assistant**: response to user instruction

In this exercise, you'll make your first request to the Chat Completions endpoint to answer the following question:

**What is the difference between a for loop and a while loop?**

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 request to the `ChatCompletion` endpoint using both **system** and **user** messages to answer the question, **What is the difference between a for loop and a while loop?**
- Extract and print the assistant's text response.



```
# Set your API key
openai.api_key = "____"

# Create a request to the ChatCompletion endpoint
response = openai.____.____(
  model="gpt-3.5-turbo",
  messages=[
    {"role": ____,
     "content": "You are a helpful data science tutor."},
    ____
  ]
)

# Extract and print the assistant's text response
print(____)
```

##  Hints 

- To create a request to ChatCompletion endpoint, use the `ChatCompletion.create()` method.
- The first role in the list passed to the `messages` argument should be the `"system"` role.
- Like the `"system"` messages, the user message should be comprised of a dictionary with `"role"` and `"content"` keys.



##  Solution 

```
# Set your API key
openai.api_key = "<OPENAI_API_TOKEN>"

# Create a request to the ChatCompletion endpoint
response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system",
     "content": "You are a helpful data science tutor."},
    {"role": "user",
     "content": "What is the difference between a for loop and a while loop?"}
  ]
)

# Extract the assistant's text response
print(response['choices'][0]['message']['content'])
```


