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.
Deze oefening maakt deel uit van de cursus
Prompt Engineering with the OpenAI API
Oefeninstructies
- Create a request to the
chat.completionsendpoint inside theget_response()function. - Try out the function with a prompt that asks the model to write a poem about ChatGPT.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
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)