Get startedGet started for free

Batching messages

You are developing a fitness application to track running and cycling training, but find out that all your customers' distances have been measured in kilometers, and you'd like to have them also converted to miles.

You decide to use the OpenAI API to send requests for each measurement, but want to avoid using a for loop that would send too many requests. You decide to send the requests in batches, specifying a system message that asks to convert each of the measurements from kilometers to miles and present the results in a table containing both the original and converted measurements.

The measurements list (containing a list of floats) and the get_response() function have already been imported.

This exercise is part of the course

Developing AI Systems with the OpenAI API

View Course

Exercise instructions

  • Provide a system message to request a response with all measurements as a table (make sure you specify that they are in kilometers and should be converted into miles).
  • Append one user message per measurement to the messages list.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

client = OpenAI(api_key="")

messages = []
# Provide a system message and user messages to send the batch
messages.append(____)
# Append measurements to the message
[messages.append(____) for i in measurements]

response = get_response(messages)
print(response)
Edit and Run Code