
#  Code explanation

```
Exercise ID 1578141
```

##  Assignment 

One of the most popular use cases for using OpenAI models is for explaining complex content, such as technical jargon and code. This is a task that data practitioners, software engineers, and many others must tackle in their day-to-day as they review and utilize code written by others.

In this exercise, you'll use the OpenAI API to explain a block of Python code to understand what it is doing.

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 to send `instruction` to the `gpt-3.5-turbo` model.



```
# Set your API key
openai.api_key = "____"

instruction = """Explain what this Python code does in one sentence:
import numpy as np

heights_dict = {"Mark": 1.76, "Steve": 1.88, "Adnan": 1.73}
heights = heights_dict.values()
print(np.mean(heights))
"""

# Create a request to the ChatCompletion endpoint
response = openai.____.____(
  model="gpt-3.5-turbo",
  ____,
  max_tokens=100
)

print(response['choices'][0]['message']['content'])
```

##  Hints 

- The `.create()` method of the `ChatCompletion` class has an argument, `messages`, for the messages to send to each role.
- The `messages` argument takes a list of dictionaries, where each dictionary must have a `"role"` and `"content"` key.



##  Solution 

```
# Set your API key
openai.api_key = "<OPENAI_API_TOKEN>"

instruction = """Explain what this Python code does in one sentence:
import numpy as np

heights_dict = {"Mark": 1.76, "Steve": 1.88, "Adnan": 1.73}
heights = heights_dict.values()
print(np.mean(heights))
"""

# Create a request to the ChatCompletion endpoint
response = openai.ChatCompletion.create(
  model="gpt-3.5-turbo",
  messages=[
    {"role": "system", "content": "You are a helpful Python programming assistant."},
    {"role": "user", "content": instruction}
  ],
  max_tokens=100
)

print(response['choices'][0]['message']['content'])
```


