Get startedGet started for free

Creating the get_response() function

Most of the exercises in this course will call the chat.completions endpoint of the OpenAI API with a user prompt. Here, you will create a get_response() function that receives a prompt as input and returns the response as an output, which in future exercises will be pre-loaded for you.

The OpenAI package, and OpenAI API Python client have been pre-loaded.

This exercise is part of the course

Prompt Engineering with the OpenAI API

View Course

Exercise instructions

  • Create a request to the chat.completions endpoint inside the get_response() function.
  • Try out the function with a prompt that asks the model to write a poem about ChatGPT.

Hands-on interactive exercise

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

def get_response(prompt):
  # Create a request to the chat completions endpoint
  response = client.____.____.____(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}], 
    temperature = 0)
  return response.choices[0].message.content

# Test the function with your prompt
response = get_response("____")
print(response)
Edit and Run Code