Using conditional prompts
Building upon the previous task, your next challenge is to enhance the responses you received. When processing a given text, you need to determine its language, count the number of sentences, and generate a suitable title if the text contains more than one sentence. However, here's the new twist: if the text consists of only one sentence, no title should be generated, and instead, the model should display "N/A". This modification ensures that the title is generated only for texts with multiple sentences, providing a more refined and practical output for your platform's users.
The OpenAI
package, the get_response()
function, and the sample text
have been pre-loaded for you.
This exercise is part of the course
ChatGPT Prompt Engineering for Developers
Exercise instructions
- Create the
instructions
, with the directions to infer the language and the number of sentences of the given delimitedtext
; then if the text contains more than one sentence, generate a suitable title for it, otherwise, write 'N/A' for the title. - Create the
output_format
, with directions to include the text, language, number of sentences, and title, each on a separate line,and ensure to use 'Text:', 'Language:', and 'Title:' as prefixes for each line.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
client = OpenAI(api_key="")
# Create the instructions
instructions = ____
# Create the output format
output_format = ____
prompt = instructions + output_format + f"```{text}```"
response = get_response(prompt)
print(response)