Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
261 changes: 261 additions & 0 deletions quickstarts-js/Models.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,261 @@
/*
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* Markdown (render)
# Gemini API: List models

This notebook demonstrates how to list the models that are available for you to use in the Gemini API, and how to find details about a model.

## Setup
### Install SDK and set-up the client

### API Key Configuration

To ensure security, avoid hardcoding the API key in frontend code. Instead, set it as an environment variable on the server or local machine.

When using the Gemini API client libraries, the key will be automatically detected if set as either `GEMINI_API_KEY` or `GOOGLE_API_KEY`. If both are set, `GOOGLE_API_KEY` takes precedence.

For instructions on setting environment variables across different operating systems, refer to the official documentation: [Set API Key as Environment Variable](https://ai.google.dev/gemini-api/docs/api-key#set-api-env-var)

In code, the key can then be accessed as:

```js
ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
```
*/

// [CODE STARTS]
module = await import("https://esm.sh/@google/[email protected]");
GoogleGenAI = module.GoogleGenAI;
ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

MODEL_ID = "gemini-2.5-flash" // ["gemini-2.5-flash-lite", "gemini-2.5-flash", "gemini-2.5-pro"]
Comment on lines +41 to +45
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

It's a good practice in JavaScript to declare variables with const or let to avoid polluting the global scope. I've updated the declarations to use const. Also, the MODEL_ID variable was declared but never used, so I've removed it to improve code clarity.

const module = await import("https://esm.sh/@google/[email protected]");
const GoogleGenAI = module.GoogleGenAI;
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

// [CODE ENDS]

/* Markdown (render)
## List models

Use `listModels()` to see what models are available. These models support `generateContent`, the main method used for prompting.
*/

// [CODE STARTS]
models = await ai.models.list();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The ai.models.list() method is called multiple times in this notebook (here, and on lines 192 and 223). To improve performance and avoid redundant API calls, you should call this method once, store the result in a variable, and then reuse that variable throughout the notebook.

for await (model of models) {
console.log(model.name);
}
// [CODE ENDS]

/* Output Sample

models/embedding-gecko-001

models/gemini-2.5-pro-preview-03-25

models/gemini-2.5-flash-preview-05-20

models/gemini-2.5-flash

models/gemini-2.5-flash-lite-preview-06-17

models/gemini-2.5-pro-preview-05-06

models/gemini-2.5-pro-preview-06-05

models/gemini-2.5-pro

models/gemini-2.0-flash-exp

models/gemini-2.0-flash

models/gemini-2.0-flash-001

models/gemini-2.0-flash-exp-image-generation

models/gemini-2.0-flash-lite-001

models/gemini-2.0-flash-lite

models/gemini-2.0-flash-preview-image-generation

models/gemini-2.0-flash-lite-preview-02-05

models/gemini-2.0-flash-lite-preview

models/gemini-2.0-pro-exp

models/gemini-2.0-pro-exp-02-05

models/gemini-exp-1206

models/gemini-2.0-flash-thinking-exp-01-21

models/gemini-2.0-flash-thinking-exp

models/gemini-2.0-flash-thinking-exp-1219

models/gemini-2.5-flash-preview-tts

models/gemini-2.5-pro-preview-tts

models/learnlm-2.0-flash-experimental

models/gemma-3-1b-it

models/gemma-3-4b-it

models/gemma-3-12b-it

models/gemma-3-27b-it

models/gemma-3n-e4b-it

models/gemma-3n-e2b-it

models/gemini-flash-latest

models/gemini-flash-lite-latest

models/gemini-pro-latest

models/gemini-2.5-flash-lite

models/gemini-2.5-flash-image-preview

models/gemini-2.5-flash-image

models/gemini-2.5-flash-preview-09-2025

models/gemini-2.5-flash-lite-preview-09-2025

models/gemini-robotics-er-1.5-preview

models/gemini-2.5-computer-use-preview-10-2025

models/embedding-001

models/text-embedding-004

models/gemini-embedding-exp-03-07

models/gemini-embedding-exp

models/gemini-embedding-001

models/aqa

models/imagen-3.0-generate-002

models/imagen-4.0-generate-preview-06-06

models/imagen-4.0-ultra-generate-preview-06-06

models/imagen-4.0-generate-001

models/imagen-4.0-ultra-generate-001

models/imagen-4.0-fast-generate-001

models/veo-2.0-generate-001

models/gemini-2.0-flash-live-001

models/gemini-live-2.5-flash-preview

models/gemini-2.5-flash-live-preview

models/gemini-2.5-flash-native-audio-latest

models/gemini-2.5-flash-native-audio-preview-09-2025

models/lyria-realtime-exp

*/

/* Markdown (render)
These models support `embedContent`, used for embeddings:
*/

// [CODE STARTS]
models = await ai.models.list();

for await (model of models) {
if (model.supportedActions?.includes("embedContent")) {
console.log(model.name);
}
}

// [CODE ENDS]

/* Output Sample

models/embedding-001

models/text-embedding-004

models/gemini-embedding-exp-03-07

models/gemini-embedding-exp

models/gemini-embedding-001

*/

/* Markdown (render)
## Find details about a model

You can see more details about a model, including the `inputTokenLimit` and `outputTokenLimit` as follows.
*/

// [CODE STARTS]
models = await ai.models.list();

for await (model of models) {
if (model.name === "models/gemini-2.5-flash") {
console.log(model);
}
}
Comment on lines +223 to +229
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Instead of fetching all models and iterating through them to find a specific one, it's more efficient to use the ai.models.get() method. This allows you to retrieve a model's details directly by its name.

const modelInfo = await ai.models.get("models/gemini-2.5-flash");
console.log(modelInfo);


// [CODE ENDS]

/* Output Sample

{
"name": "models/gemini-2.5-flash",
"displayName": "Gemini 2.5 Flash",
"description": "Stable version of Gemini 2.5 Flash, our mid-size multimodal model that supports up to 1 million tokens, released in June of 2025.",
"version": "001",
"tunedModelInfo": {},
"inputTokenLimit": 1048576,
"outputTokenLimit": 65536,
"supportedActions": [
"generateContent",
"countTokens",
"createCachedContent",
"batchGenerateContent"
]
}

*/

/* Markdown (render)
## Learning more

* To learn how use a model for prompting, see the [Prompting](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Prompting.ipynb) quickstart.

* To learn how use a model for embedding, see the [Embedding](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Embeddings.ipynb) quickstart.

* For more information on models, visit the [Gemini models](https://ai.google.dev/models/gemini) documentation.
*/
1 change: 1 addition & 0 deletions quickstarts-js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ Stay tuned, more JavaScript notebooks are on the way!
| File API | Learn how to upload, use, retrieve, and delete files (text, image, audio, code) with the Gemini File API for multimodal prompts. | File upload, multimodal prompts, text/code/media files | [![Open in AI Studio](https://storage.googleapis.com/generativeai-downloads/images/Open_in_AIStudio.svg)](https://aistudio.google.com/apps/bundled/file_api?showPreview=true) | <img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/javascript/javascript-original.svg" alt="JS" width="20"/> [File_API.js](./File_API.js) |
| Audio | Demonstrates how to use audio files with Gemini: upload, prompt, summarize, transcribe, and analyze audio and YouTube content. | Audio file upload, inline audio, transcription, YouTube analysis | [![Open in AI Studio](https://storage.googleapis.com/generativeai-downloads/images/Open_in_AIStudio.svg)](https://aistudio.google.com/apps/bundled/audio?showPreview=true) | <img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/javascript/javascript-original.svg" alt="JS" width="20"/> [Audio.js](./Audio.js) |
| Get Started LearnLM | Explore LearnLM, an experimental model for AI tutoring, with examples of system instructions for test prep, concept teaching, learning activities, and homework help. | AI tutoring, system instructions, adaptive learning, education | [![Open in AI Studio](https://storage.googleapis.com/generativeai-downloads/images/Open_in_AIStudio.svg)](https://aistudio.google.com/apps/bundled/get_started_learnlm?showPreview=true) | <img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/javascript/javascript-original.svg" alt="JS" width="20"/> [Get_started_LearnLM.js](./Get_started_LearnLM.js) |
| Models | Learn how to list available Gemini models and view model details such as token limits, capabilities, and supported actions. | List models, model details, token limits, supported actions | [![Open in AI Studio](https://storage.googleapis.com/generativeai-downloads/images/Open_in_AIStudio.svg)](#) | <img src="https://cdn.jsdelivr.net/gh/devicons/devicon/icons/javascript/javascript-original.svg" alt="JS" width="20"/> [Models.js](./Models.js) |