
#  In-context learning

```
Exercise ID 1578138
```

##  Assignment 

For more complex use cases, the models lack the understanding or context of the problem to provide a suitable response from a prompt. In these cases, you need to provide examples to the model for it to learn from, so-called **in-context learning**.

In this exercise, you'll improve on a Python programming tutor built on the OpenAI API by providing an example that the model can learn from.

Here is an example of a user and assistant message you can use, but feel free to try out your own:

- User &rarr; **Explain what the min() function does.**
- Assistant &rarr; **The min() function returns the smallest item from an iterable.**

The `openai` package has been pre-loaded for you.

##  Pre exercise code 

```
import openai
```



##  Instructions 

- Assign your API key to `openai.api_key`.
- Add a similar coding example in the form of user and assistant messages to `messages` so the model can learn more about the desired response.



```
# Set your API key
openai.api_key = "____"

response = openai.ChatCompletion.create(
   model="gpt-3.5-turbo",
   # Add a user and assistant message for in-context learning
   messages=[
     {"role": "system", "content": "You are a helpful Python programming tutor."},
     ____,
     ____,
     {"role": "user", "content": "Explain what the type() function does."}
   ]
)

print(response['choices'][0]['message']['content'])
```

##  Hints 

- Messages must be provided as a dictionary with `"role"` and `"content"` keys.
- To provide an example, both a user message and an ideal response need to be provided as a pair.



##  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",
   # Add a user and assistant message for in-context learning
   messages=[
     {"role": "system", "content": "You are a helpful Python programming tutor."},
     {"role": "user", "content": "Explain what the min() function does."},
     {"role": "assistant", "content": "The min() function returns the smallest item from an iterable."},
     {"role": "user", "content": "Explain what the type() function does."}
   ]
)

print(response['choices'][0]['message']['content'])
```


