Get startedGet started for free

Sentiment analysis with few-shot prompting

You're working on market research and your goal is to use few-shot prompting to perform sentiment analysis on customer reviews. You are assigning a number for a given customers conversation: -1 if the sentiment is negative, 1 if positive. You provide the following examples as previous conversations for the model to learn from.

  • The product quality exceeded my expectations -> 1
  • I had a terrible experience with this product's customer service -> -1

The OpenAI package has been pre-loaded for you.

This exercise is part of the course

Prompt Engineering with the OpenAI API

View Course

Exercise instructions

  • Provide the examples as previous conversations assigning the text as context for the user role and the number as context for the assistant role.
  • Provide the following text for the model to classify and use the appropriate role: The price of the product is really fair given its features.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

client = OpenAI(api_key="")

response = client.chat.completions.create(
  model = "gpt-4o-mini",
  # Provide the examples as previous conversations
  messages = [{"role": "____", "content": "____"},
              {"role": "____", "content": "____"},
              {"role": "____", "content": "____"},
              {"role": "____", "content": "____"},
              # Provide the text for the model to classify
              {"role": "____", "content": "____"}
             ],
  temperature = 0
)
print(response.choices[0].message.content)
Edit and Run Code