Creating a dual-prompt get_response() function
The following exercises will be based on calling the chat.completions
endpoint of the OpenAI API with two prompts (a system prompt and a user prompt). To prepare for this, in this exercise you will create a dual-prompt get_response()
function that receives two prompts as input (system_prompt
and user_prompt
) and returns the response as an output. You will then apply this function to any example of your choice.
The OpenAI
package has been pre-loaded for you.
This exercise is part of the course
ChatGPT Prompt Engineering for Developers
Exercise instructions
- Assign the role and content of each message in the
messages
list. - Try out the function by passing a
system_prompt
and auser_prompt
of your choice.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
client = OpenAI(api_key="")
def get_response(system_prompt, user_prompt):
# Assign the role and content for each message
messages = [{"role": ____, "content": ____},
{"role": ____, "content": ____}]
response = client.chat.completions.create(
model="gpt-4o-mini", messages= messages, temperature=0)
return response.choices[0].message.content
# Try the function with a system and user prompts of your choice
response = get_response("____", "____")
print(response)