Sentiment Analysis
Copyright 2025 Google LLC.
Gemini API: Sentiment Analysis
You will use the Gemini to extract sentiment scores of reviews.
Configure your API key
To run the following cell, your API key must be stored it in a Colab Secret named GOOGLE_API_KEY. If you don't already have an API key, or you're not sure how to create a Colab Secret, see Authentication for an example.
Example
Start by defining how you want your JSON to be returned and which categories you would like to classify an item by. After that, go ahead and define some examples. In this case, you are trying to classify reviews as positive, neutral, or negative.
Take a look at each of the probabilities returned to see how each of these reviews would be classified by the Gemini model.
Helper function to generate content from sentiment llm:
{'negative_sentiment_score': <Magnitude.STRONG: 'strong'>,
'neutral_sentiment_score': <Magnitude.WEAK: 'weak'>,
'positive_sentiment_score': <Magnitude.WEAK: 'weak'>}
{'negative_sentiment_score': <Magnitude.WEAK: 'weak'>,
'neutral_sentiment_score': <Magnitude.WEAK: 'weak'>,
'positive_sentiment_score': <Magnitude.STRONG: 'strong'>}
{'negative_sentiment_score': <Magnitude.WEAK: 'weak'>,
'neutral_sentiment_score': <Magnitude.STRONG: 'strong'>,
'positive_sentiment_score': <Magnitude.WEAK: 'weak'>}
Summary
You have now used the Gemini API to analyze the sentiment of restaurant reviews using structured data. Try out other types of texts, such as comments under a video or emails.
Please see the other notebooks in this directory to learn more about how you can use the Gemini API for other JSON related tasks.