You can use the Gemini models to answer many queries without any additional context. Zero-shot prompting is useful for situations when your queries are not complicated and do not require a specific schema.
You can create your API key using Google AI Studio with a single click.
Remember to treat your API key like a password. Don’t accidentally save it in a notebook or source file you later commit to GitHub. In this notebook we will be storing the API key in a .env file. You can also set it as an environment variable or use a secret manager.
Another option is to set the API key as an environment variable. You can do this in your terminal with the following command:
$ export GEMINI_API_KEY="<YOUR_API_KEY>"
Load the API key
To load the API key from the .env file, we will use the dotenv package. This package loads environment variables from a .env file into process.env.
$ npm install dotenv
Then, we can load the API key in our code:
const dotenv =require("dotenv") astypeofimport("dotenv");dotenv.config({ path:"../../.env",});const GEMINI_API_KEY =process.env.GEMINI_API_KEY??"";if (!GEMINI_API_KEY) {thrownewError("GEMINI_API_KEY is not set in the environment variables");}console.log("GEMINI_API_KEY is set in the environment variables");
GEMINI_API_KEY is set in the environment variables
Note
In our particular case the .env is is two directories up from the notebook, hence we need to use ../../ to go up two directories. If the .env file is in the same directory as the notebook, you can omit it altogether.
With the new SDK, now you only need to initialize a client with you API key (or OAuth if using Vertex AI). The model is now set in each call.
const google =require("@google/genai") astypeofimport("@google/genai");const ai =new google.GoogleGenAI({ apiKey: GEMINI_API_KEY });
Select a model
Now select the model you want to use in this guide, either by selecting one in the list or writing it down. Keep in mind that some models, like the 2.5 ones are thinking models and thus take slightly more time to respond (cf. thinking notebook for more details and in particular learn how to switch the thiking off).
Here are a few examples with zero-shot prompting. Note that in each of these examples, you can simply provide the task, with zero examples.
const response_1 =await ai.models.generateContent({ model: MODEL_ID, contents:` Sort following animals from biggest to smallest: fish, elephant, dog `,});tslab.display.markdown(response_1.text??"");
Here’s the order from biggest to smallest:
Elephant
Dog
Fish
const response_2 =await ai.models.generateContent({ model: MODEL_ID, contents:` Classify sentiment of review as positive, negative or neutral: I go to this restaurant every week, I love it so much. `,});tslab.display.markdown(response_2.text??"");
Positive
const response_3 =await ai.models.generateContent({ model: MODEL_ID, contents:` Extract capital cities from the text: During the summer I visited many countries in Europe. First I visited Italy, specifically Sicily and Rome. Then I visited Cologne in Germany and the trip ended in Berlin. `,});tslab.display.markdown(response_3.text??"");
Rome, Berlin
const response_4 =await ai.models.generateContent({ model: MODEL_ID, contents:` Find and fix the error in this Python code: def add_numbers(a, b): return a + b print(add_numbers(5, "10")) `,});tslab.display.markdown(response_4.text??"");
The error in this Python code is a TypeError.
Error:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
Reason:
You are trying to add an integer (5) and a string ("10") using the + operator. In Python, the + operator behaves differently based on the data types:
For numbers (integers, floats), it performs arithmetic addition.
For strings, it performs string concatenation (joining them).
Python is a strongly typed language and will not automatically convert a string to an integer (or vice versa) when you’re trying to perform arithmetic operations that are not defined for those specific mixed types.
Fix:
To fix this, you need to ensure both arguments passed to the add_numbers function are numbers. You can convert the string "10" to an integer using int().
def add_numbers(a, b):return a + b# Fix: Convert "10" to an integer using int()print(add_numbers(5, int("10")))
Output of the fixed code:
15
const response_5 =await ai.models.generateContent({ model: MODEL_ID, contents:` Solve this math problem: A train travels 120 km in 2 hours. What is its average speed? `,});tslab.display.markdown(response_5.text??"");
To find the average speed, you use the formula:
Average Speed = Total Distance / Total Time
Given: * Total Distance = 120 km * Total Time = 2 hours
Now, plug the values into the formula:
Average Speed = 120 km / 2 hours Average Speed = 60 km/h
The average speed of the train is 60 km/h.
const response_6 =await ai.models.generateContent({ model: MODEL_ID, contents:` Identify the names of people, places, and countries in this text: Emmanuel Macron, the president of France, announced a AI partnership in collaboration with the United Arab Emirates. `,});tslab.display.markdown(response_6.text??"");
Here are the names identified from the text:
People:
Emmanuel Macron
Places:
France
United Arab Emirates
Countries:
France
United Arab Emirates
const response_7 =await ai.models.generateContent({ model: MODEL_ID, contents:` Correct the grammar in this sentence: She don't like playing football but she enjoy to watch it. `,});tslab.display.markdown(response_7.text??"");
Here’s the corrected sentence:
She doesn’t like playing football, but she enjoys watching it.
Here’s why:
“She don’t” should be “She doesn’t” (subject-verb agreement for third person singular).
“she enjoy” should be “she enjoys” (subject-verb agreement for third person singular).
“enjoy to watch” should be “enjoys watching” (the verb “enjoy” is typically followed by a gerund, not an infinitive).
A comma is often used before “but” when connecting two independent clauses.
Next steps
Be sure to explore other examples of prompting in the repository. Try writing prompts about classifying your own data, or try some of the other prompting techniques such as few-shot prompting.