Adding guardrails
You are developing a chatbot that provides advice for tourists visiting Rome. You've been asked to keep the topics limited to only covering questions about food and drink, attractions, history and things to do around the city. For any other topic, the chatbot should apologize and say 'Apologies, but I am not allowed to discuss this topic.'.
Bu egzersiz
Developing AI Systems with the OpenAI API
kursunun bir parçasıdırEgzersiz talimatları
- Write a
usermessage with theuser_requestgiven, and asystemmessage to tell the model to assess the question first: if it is allowed, provide a reply, otherwise provide the message: 'Apologies, but I am not allowed to discuss this topic.'. - Print the response.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
client = OpenAI(api_key="")
user_request = "Can you recommend a good restaurant in Berlin?"
# Write the system and user message
messages = ____
response = client.chat.completions.create(
model="gpt-4o-mini", messages=messages
)
# Print the response
____