1+ {
2+ "nbformat" : 4 ,
3+ "nbformat_minor" : 0 ,
4+ "metadata" : {
5+ "colab" : {
6+ "provenance" : []
7+ },
8+ "kernelspec" : {
9+ "name" : " python3" ,
10+ "display_name" : " Python 3"
11+ },
12+ "language_info" : {
13+ "name" : " python"
14+ }
15+ },
16+ "cells" : [
17+ {
18+ "cell_type" : " code" ,
19+ "source" : [
20+ " import sys\n " ,
21+ " import IPython\n " ,
22+ " \n " ,
23+ " %pip install --upgrade --user --quiet google-genai\n " ,
24+ " app = IPython.Application.instance()\n " ,
25+ " app.kernel.do_shutdown(True)\n " ,
26+ " \n " ,
27+ " if \" google.colab\" in sys.modules:\n " ,
28+ " from google.colab import auth\n " ,
29+ " \n " ,
30+ " auth.authenticate_user()"
31+ ],
32+ "metadata" : {
33+ "id" : " 2sHCLvoCw6TA"
34+ },
35+ "execution_count" : null ,
36+ "outputs" : []
37+ },
38+ {
39+ "cell_type" : " code" ,
40+ "source" : [
41+ " import os\n " ,
42+ " \n " ,
43+ " PROJECT_ID = \" [your-project-id]\" # @param {type: \" string\" , placeholder: \" [your-project-id]\" , isTemplate: true}\n " ,
44+ " if not PROJECT_ID or PROJECT_ID == \" [your-project-id]\" :\n " ,
45+ " PROJECT_ID = str(os.environ.get(\" GOOGLE_CLOUD_PROJECT\" ))\n " ,
46+ " \n " ,
47+ " LOCATION = os.environ.get(\" GOOGLE_CLOUD_REGION\" , \" us-central1\" )\n " ,
48+ " \n " ,
49+ " from google import genai\n " ,
50+ " \n " ,
51+ " client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)"
52+ ],
53+ "metadata" : {
54+ "id" : " JG5BoLBdw5je"
55+ },
56+ "execution_count" : null ,
57+ "outputs" : []
58+ },
59+ {
60+ "cell_type" : " code" ,
61+ "source" : [
62+ " from google.genai.types import GenerateContentConfig, Part, SafetySetting\n " ,
63+ " \n " ,
64+ " client = genai.Client(vertexai=True, project=PROJECT_ID, location=LOCATION)\n " ,
65+ " MODEL_ID = \" meta/llama-4-maverick-17b-128e-instruct-maas\"\n "
66+ ],
67+ "metadata" : {
68+ "id" : " 6snbS6-Rxh5C"
69+ },
70+ "execution_count" : null ,
71+ "outputs" : []
72+ },
73+ {
74+ "cell_type" : " markdown" ,
75+ "source" : [
76+ " Defining the format we want to return..."
77+ ],
78+ "metadata" : {
79+ "id" : " DsRB381L0bwb"
80+ }
81+ },
82+ {
83+ "cell_type" : " code" ,
84+ "source" : [
85+ " response_schema = {\n " ,
86+ " \" type\" : \" ARRAY\" ,\n " ,
87+ " \" items\" : {\n " ,
88+ " \" type\" : \" ARRAY\" ,\n " ,
89+ " \" items\" : {\n " ,
90+ " \" type\" : \" OBJECT\" ,\n " ,
91+ " \" properties\" : {\n " ,
92+ " \" rating\" : {\" type\" : \" INTEGER\" },\n " ,
93+ " \" flavor\" : {\" type\" : \" STRING\" },\n " ,
94+ " \" sentiment\" : {\n " ,
95+ " \" type\" : \" STRING\" ,\n " ,
96+ " \" enum\" : [\" POSITIVE\" , \" NEGATIVE\" , \" NEUTRAL\" ],\n " ,
97+ " },\n " ,
98+ " \" explanation\" : {\" type\" : \" STRING\" },\n " ,
99+ " },\n " ,
100+ " \" required\" : [\" rating\" , \" flavor\" , \" sentiment\" , \" explanation\" ],\n " ,
101+ " },\n " ,
102+ " },\n " ,
103+ " }\n "
104+ ],
105+ "metadata" : {
106+ "id" : " 7CyBOejOz8iR"
107+ },
108+ "execution_count" : null ,
109+ "outputs" : []
110+ },
111+ {
112+ "cell_type" : " markdown" ,
113+ "source" : [
114+ " defining the product reviews in the prompt and asking the model to generate based on the response_schema passed in."
115+ ],
116+ "metadata" : {
117+ "id" : " ckdF273K0g7E"
118+ }
119+ },
120+ {
121+ "cell_type" : " code" ,
122+ "source" : [
123+ " prompt = \"\"\"\n " ,
124+ " Analyze the following product reviews, output the sentiment classification, and give an explanation.\n " ,
125+ " \n " ,
126+ " - \" Absolutely loved it! Best ice cream I've ever had.\" Rating: 4, Flavor: Strawberry Cheesecake\n " ,
127+ " - \" Quite good, but a bit too sweet for my taste.\" Rating: 1, Flavor: Mango Tango\n " ,
128+ " \"\"\"\n " ,
129+ " \n " ,
130+ " response = client.models.generate_content(\n " ,
131+ " model=MODEL_ID,\n " ,
132+ " contents=prompt,\n " ,
133+ " config=GenerateContentConfig(\n " ,
134+ " response_mime_type=\" application/json\" ,\n " ,
135+ " response_schema=response_schema,\n " ,
136+ " ),\n " ,
137+ " )\n " ,
138+ " product_reviews: dict = response.parsed\n " ,
139+ " print(product_reviews)"
140+ ],
141+ "metadata" : {
142+ "colab" : {
143+ "base_uri" : " https://localhost:8080/"
144+ },
145+ "id" : " Z_ka6SP90Am-" ,
146+ "outputId" : " 69d91ee1-96d4-44f9-d22d-c4f4a9887e82"
147+ },
148+ "execution_count" : null ,
149+ "outputs" : [
150+ {
151+ "output_type" : " stream" ,
152+ "name" : " stdout" ,
153+ "text" : [
154+ " [[{'explanation': \" The reviewer used the phrase 'Absolutely loved it' and stated it was the 'Best ice cream I've ever had', indicating a very positive sentiment despite the rating being less than 5.\" , 'flavor': 'Strawberry Cheesecake', 'rating': 4, 'sentiment': 'POSITIVE'}, {'explanation': \" The reviewer described the product as 'Quite good', but expressed a negative aspect by stating it was 'a bit too sweet', aligning with the low rating given. The negative aspect outweighs the positive, leading to an overall negative sentiment.\" , 'flavor': 'Mango Tango', 'rating': 1, 'sentiment': 'NEGATIVE'}]]\n "
155+ ]
156+ }
157+ ]
158+ }
159+ ]
160+ }
0 commit comments