-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Added Models.ipynb notebook for JavaScript #1013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Aarchi-07
wants to merge
2
commits into
google-gemini:main
Choose a base branch
from
Aarchi-07:Models.js
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+262
−0
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] | ||
| // [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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // [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. | ||
| */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a good practice in JavaScript to declare variables with
constorletto avoid polluting the global scope. I've updated the declarations to useconst. Also, theMODEL_IDvariable was declared but never used, so I've removed it to improve code clarity.