While LLMs are trained extensively on various documents and data, the LLM don’t know everything. New information or information that is not easily accessible cannot be known by the LLM, unless it was specifically added to its corpus of knowledge somehow. For this reason, it is sometimes necessary to provide the LLM, with information and context necessary to answer our queries by providing additional context.
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).
Let’s say you provide some statistics from a recent Olympics competition, and this data wasn’t used to train the LLM. Insert it into the prompt, and input the prompt to the model.
const response =await ai.models.generateContent({ model: MODEL_ID, contents:` QUERY: provide a list of atheletes that competed in olympics exactly 9 times. CONTEXT: Table title: Olympic athletes and number of times they've competed Ian Millar, 10 Hubert Raudaschl, 9 Afanasijs Kuzmins, 9 Nino Salukvadze, 9 Piero d'Inzeo, 8 Raimondo d'Inzeo, 8 Claudia Pechstein, 8 Jaqueline Mourão, 8 Ivan Osiier, 7 François Lafortune, Jr, 7 `,});tslab.display.markdown(response.text??"");
Athletes that competed in the Olympics exactly 9 times:
Hubert Raudaschl
Afanasijs Kuzmins
Nino Salukvadze
Next steps
While some information may be easily searchable online without the use of an LLM, consider data that is not found on the internet, such as private documentation, quickbooks, and forums. Use this code as a template to help you input that information into the Gemini model.
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.