
#  Find and replace

```
Exercise ID 1578140
```

##  Assignment 

Text completion models can be used for much more than answering questions. In this exercise, you'll explore the model's ability to transform a text prompt.

Find-and-replace tools have been around for decades, but they are often limited to identifying and replacing exact words or phrases. You've been provided with a block of text discussing cars, and you'll use a completion model to update the text to discuss planes instead, updating the text appropriately. The `openai` package has been pre-loaded for you.

**Warning**: if you send many requests or use lots of tokens in a short period, you may hit your rate limit and see an `openai.error.RateLimitError`. If you see this error, please wait a minute for your quota to reset and you should be able to begin sending more requests. Please see [OpenAI's rate limit error support article](https://help.openai.com/en/articles/6897202-ratelimiterror) for more information.

##  Pre exercise code 

```
import openai
```



##  Instructions 

- Assign your API key to `openai.api_key`.
- Create a request to the `Completion` endpoint, sending the `prompt` provided to the `gpt-3.5-turbo-instruct` model and using `max_tokens=100`.
- Extract and print the text response from the API.



```
# Set your API key
openai.api_key = "____"

prompt="""Replace car with plane and adjust phrase:
A car is a vehicle that is typically powered by an internal combustion engine or an electric motor. It has four wheels, and is designed to carry passengers and/or cargo on roads or highways. Cars have become a ubiquitous part of modern society, and are used for a wide variety of purposes, such as commuting, travel, and transportation of goods. Cars are often associated with freedom, independence, and mobility."""

# Create a request to the Completion endpoint
response = openai.____.____(
  ____
)

# Extract and print the response text
print(____)
```

##  Hints 

- Create a request to the `Completion` endpoint using the `Completion.create()` method.
- To extract the response text from `response`, you'll need to access the `"choices"` key, then subset the first element of the resulting list, and finally, access the `"text"` key.



##  Solution 

```
# Set your API key
openai.api_key = "<OPENAI_API_TOKEN>"

prompt="""Replace car with plane and adjust phrase:
A car is a vehicle that is typically powered by an internal combustion engine or an electric motor. It has four wheels, and is designed to carry passengers and/or cargo on roads or highways. Cars have become a ubiquitous part of modern society, and are used for a wide variety of purposes, such as commuting, travel, and transportation of goods. Cars are often associated with freedom, independence, and mobility."""

# Create a request to the Completion endpoint
response = openai.Completion.create(
  model="gpt-3.5-turbo-instruct",
  prompt=prompt,
  max_tokens=100
)

# Extract and print the response text
print(response["choices"][0]["text"])
```


