Open In App

Prompt Engineering for Inference

Last Updated : 27 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

You must have faced such questions in your exam when you are supposed to answer some questions based on the text or passage provided. This is also known as the process of inferring relevant information from a large piece of text. ChatGPT model is also efficient in performing such tasks we just need to provide clear instructions to the model.

Import the Openai package and assign the Openai API key

Python3




import openai
import os
 
openai.api_key = "<OpenAI API Key>"


Let’s check How it works

Python3




# Review
review = '''
GeeksforGeeks courses are very good for getting a job.
It offers a wide range of courses covering various topics such as programming languages,
data structures and algorithms, web development, machine learning, and more.
'''
 
# Prompt
PROMPT =  f"""What is the sentiment of the following product review,
which is delimited with triple backticks?
 
Review text: '''{review}'''
"""
 
# Massage
MASSAGE = [{"role": "user", "content": PROMPT}]
 
# Generate responses
response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=MASSAGE,
        temperature=1
    )
 
# Print response
response


Output:

<OpenAIObject chat.completion id=chatcmpl-7T6rWVI2wYx5x0y3qnbd1kUXCjhFY at 0x7f03a10cbd10> JSON: {
"id": "chatcmpl-7T6rWVI2wYx5x0y3qnbd1kUXCjhFY",
"object": "chat.completion",
"created": 1687172246,
"model": "gpt-3.5-turbo-0301",
"usage": {
"prompt_tokens": 76,
"completion_tokens": 8,
"total_tokens": 84
},
"choices": [
{
"message": {
"role": "assistant",
"content": "The sentiment of the review is positive."
},
"finish_reason": "stop",
"index": 0
}
]
}

Prompt Engineering for Inference

Prompt

PROMPT = f”””What is the sentiment of the following product review,

which is delimited with triple backticks?

Review text: ”'{review}”’

“””

Create a function based on the above prompt and the result

Python3




#Create a function based on above prompt and result
def get_completion(prompt, model="gpt-3.5-turbo",
                   temperature=0):
    messages = [{"role": "user", "content": prompt}]
    response = openai.ChatCompletion.create(
        model=model,
        messages=messages,
        temperature=temperature
    )
 
    return response.choices[0].message["content"]


Let’s say we have the product review that we used earlier. And the developers are not interested in the details they just want to know how many reviews are positive and how many of them are negative.

Review

Python3




lamp_review = """
I love this game very much. I love the colourful candies.\
Recently, the developer of the application changed the\
music and added new candies. Although, the new music\
is also good but the previous one was actually suitable \
for the game and it was perfect for this game. We wish \
if we get the options to switch the music to the previous\
one. We miss that music alot and hope one day we get it\
back again. What I don't like about this game is the\
presence of horse and cartoons with dark complexion.
"""


Sentiment

Python3




prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?
 
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)


Output:

The sentiment of the review is generally positive, with the reviewer expressing love for the game and its
colorful candies. However, there is also a negative aspect mentioned regarding the presence of horse and
cartoons with dark complexion. Additionally, the reviewer expresses a desire to have the previous music
option back, indicating some dissatisfaction with the recent changes made by the developer.

Here we can observe that the model has detected both the positive and the negative sentiments of the user in the product and that is the actual case but which one is more dominant here?

Python3




# Review
movie_review = """
Adipurush,” whose Sanskrit title can be read as “First Man,”
and whose considerable budget (Rs 500 crore, or about $67 million)
allegedly surpasses all prior Indian mega-productions? Honestly,
the movie’s rough computer graphics might eclipse all other considerations
since there’s so much bad green-screen image-compositing in “Adipurush,”
and it all looks cheap and uninspired
"""
 
# Prompt
prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?
 
Give your answer as a single word, either "Positive" \
or "Negative".
 
Review text: '''{movie_review}'''
"""
# Generate response
response = get_completion(prompt)
# Response
print(response)


Output:

Negative

So, from the above output, the developers will realize that the user is not satisfied with the new features that have been rolled out.

Python3




prompt = f"""
Identify a list of emotions that the user of the \
following review is expressing in the review. Include no more than \
five items in the list. Format your answer as a list of \
lower-case words separated by commas.
 
Review text: '''{movie_review}'''
"""
response = get_completion(prompt)
print(response)


Output:

disappointment, frustration, criticism, skepticism, dissatisfaction

This is also one of the best features of the ChatGPT model is that it can even format the answer or the generated response as per the format required by the user. As we can observe in the above example we have asked for a comma-separated list of emotions and that is exactly what we have received.

Python3




story = '''
The Mahabharata is an ancient Indian epic that tells
the story of the conflict between the Pandavas and the Kauravas,
two sets of cousins. The Battle of Kurukshetra is a crucial event in the epic,
fought on the sacred plains of Kurukshetra. During this battle, Lord Krishna imparts
the teachings of the Bhagavad Gita, known as the Geeta Udesh, to Arjuna.
The Geeta Udesh addresses deep philosophical concepts and guides Arjuna
in his moral dilemma about fighting in the war. It emphasizes the importance of fulfilling
one's duty, detaching from desires,
and seeking spiritual enlightenment amidst the challenges of life.
'''


Now let’s assume we have a passage here and we are supposed to answer some questions based on this. Let’s 

Python3




prompt = f"""
Identify the following items from the story text:
- Sentiment (positive or negative)
- Which place has been discussed with main focus?
- Name of the mythological character discussed.
- When it happens?
 
The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Sentiment", "Place", "Summary", "Name" and "Time" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
 
Review text: '''{story}'''
"""
response = get_completion(prompt)
print(response)


Output:

{
"Sentiment": "positive",
"Place": "Kurukshetra",
"Summary": "Review of the Mahabharata epic and the importance of the Battle of Kurukshetra and the teachings of the Bhagavad Gita",
"Name": "Lord Krishna",
"Time": "unknown"
}

More the detail we will provide about our requirements the better will be the results provided by the model and this has been proved by the above prompt that we have used.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads