MulaiMulai sekarang secara gratis

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.

Latihan ini adalah bagian dari kursus

Prompt Engineering with the OpenAI API

Lihat Kursus

Petunjuk latihan

  • 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.

Latihan interaktif praktis

Cobalah latihan ini dengan menyelesaikan kode contoh berikut.

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 dan Jalankan Kode