-
Notifications
You must be signed in to change notification settings - Fork 54
Update README.md for improved structure and clarity #97
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
wenakanew
wants to merge
2
commits into
google-gemini:main
Choose a base branch
from
wenakanew:readme-update
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.
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 |
|---|---|---|
| @@ -1,31 +1,259 @@ | ||
| # Gemini API examples | ||
| # Gemini API Examples | ||
|
|
||
| This repository contains example code for key features of the Gemini API. The | ||
| repo is organized by programming language. | ||
| This repository contains example code for key features of the Gemini API. | ||
| The repo is organized by programming language. | ||
|
|
||
| The examples are embedded in the | ||
| [Gemini API reference](https://ai.google.dev/api) and other places in the | ||
| developer documentation. | ||
| [Gemini API reference](https://ai.google.dev/api) and other places in the developer documentation. | ||
| Every example is: | ||
|
|
||
| Each file is structured as a runnable test case, ensuring that examples are | ||
| executable and functional. Each test demonstrates a single concept and contains | ||
| special comments called _region tags_ that demarcate the test scaffolding from | ||
| the example code snippet. Here's a Python example: | ||
| * **Runnable** — all examples execute as real tests or standalone scripts | ||
| * **Minimal & pedagogical** — each file teaches one concept | ||
| * **Multi-language** — Go, Java, JavaScript/TypeScript, Python, and REST | ||
| * **Documentation-aware** — region tags allow automatic snippet extraction | ||
| * **Kept up-to-date** with evolving Gemini features | ||
|
|
||
| ```python | ||
| def test_text_gen_text_only_prompt(self): | ||
| # [START text_gen_text_only_prompt] | ||
| from google import genai | ||
|
|
||
| client = genai.Client() | ||
| response = client.models.generate_content( | ||
| model="gemini-2.0-flash", contents="Write a story about a magic backpack." | ||
| ) | ||
| print(response.text) | ||
| # [END text_gen_text_only_prompt] | ||
| # **Repository Structure** | ||
|
|
||
| ``` | ||
| api-examples/ | ||
| ├── go/ # Go examples + tests | ||
| ├── java/ # Java examples (Maven project) | ||
| ├── javascript/ # JS/TS examples + tests | ||
| ├── python/ # Python runnable examples | ||
| ├── rest/ # cURL-style shell examples | ||
| ├── third_party/ # Multimedia files for multimodal examples | ||
| ├── CONTRIBUTING.md | ||
| ├── LICENSE | ||
| └── README.md # (this file) | ||
| ``` | ||
|
|
||
| Each language folder includes: | ||
|
|
||
| * Example files (e.g., `chat.go`, `embed.py`, `text_generation.js`) | ||
| * Matching tests (e.g., `chat_test.go`, `chat.test.js`) | ||
| * Language-specific README instructions | ||
|
|
||
|
|
||
| # **What’s Included** | ||
|
|
||
| The repository covers a wide set of Gemini capabilities: | ||
|
|
||
| ## **Core Text & Chat** | ||
|
|
||
| * Text generation | ||
| * Thinking mode | ||
| * Controlled generation | ||
| * Chat sessions | ||
| * System instructions | ||
| * Safety settings | ||
|
|
||
| ## **Structured & Programmatic Generation** | ||
|
|
||
| * Function calling | ||
| * Code execution | ||
|
|
||
| ## **Data & Tokens** | ||
|
|
||
| * Embeddings | ||
| * Token counting | ||
| * File uploads & retrieval | ||
|
|
||
| ## **Advanced Features** | ||
|
|
||
| * Grounding | ||
| * Model parameter configuration | ||
| * Caching | ||
|
|
||
| ## **Multimodal** | ||
|
|
||
| * Examples using images, videos, and audio (in `third_party/`) | ||
|
|
||
|
|
||
| # **Quickstart by Language** | ||
|
|
||
| Below are short, ready-to-run instructions for each language folder. | ||
|
|
||
|
|
||
| ## **Go** | ||
|
|
||
| ### Install & Run | ||
|
|
||
| ```bash | ||
| cd go | ||
| go mod tidy | ||
| export GOOGLE_API_KEY="YOUR_KEY" | ||
| go test ./... | ||
| ``` | ||
|
|
||
| Each example is a Go test file such as `chat_test.go`, `code_execution_test.go`, etc. | ||
|
|
||
|
|
||
| ## **Java** | ||
|
|
||
| ### Set Up | ||
|
|
||
| 1. Ensure you have a Java JDK (version 11 or higher) and Maven installed | ||
| 2. Open the `java/` folder in your preferred IDE or command line | ||
| 3. Run `mvn clean install` to download dependencies | ||
|
|
||
| ### Set API Key | ||
|
|
||
| Export the API key to your environment: | ||
|
|
||
| ```bash | ||
| export GOOGLE_API_KEY="YOUR_KEY" | ||
| ``` | ||
|
|
||
| ### Run a Test | ||
|
|
||
| ```bash | ||
| mvn -Dtest=<TestClassName> test | ||
| ``` | ||
|
|
||
| Example: | ||
|
|
||
| ```bash | ||
| mvn -Dtest=CodeExecutionTest test | ||
| ``` | ||
|
|
||
|
|
||
| ## **JavaScript / TypeScript** | ||
|
|
||
| ### Install | ||
|
|
||
| ```bash | ||
| cd javascript | ||
| npm install | ||
| export GOOGLE_API_KEY="YOUR_KEY" | ||
| ``` | ||
|
|
||
| ### Run Tests | ||
|
|
||
| ```bash | ||
| npm test | ||
| ``` | ||
|
|
||
| The API reference can be configured to show the code between the region tags. | ||
| To run a specific test file: | ||
|
|
||
| ```bash | ||
| node <feature>.test.js | ||
| ``` | ||
|
|
||
| Example: | ||
|
|
||
| ```bash | ||
| node text_generation.test.js | ||
| ``` | ||
|
|
||
| ### Format Code | ||
|
|
||
| ```bash | ||
| npm run format | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## **Python** | ||
|
|
||
| ### Install | ||
|
|
||
| ```bash | ||
| cd python | ||
| pip install absl-py google-genai Pillow pyink | ||
wenakanew marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| export GOOGLE_API_KEY="YOUR_KEY" | ||
| ``` | ||
|
|
||
| ### Run an Example | ||
|
|
||
| ```bash | ||
| python <filename>.py | ||
| ``` | ||
|
|
||
| Example: | ||
|
|
||
| ```bash | ||
| python text_generation.py | ||
| ``` | ||
|
|
||
| ### Format Code | ||
|
|
||
| ```bash | ||
| pyink . | ||
| ``` | ||
|
|
||
|
|
||
| ## **REST (Shell)** | ||
|
|
||
| ```bash | ||
| cd rest | ||
| export GOOGLE_API_KEY="YOUR_KEY" | ||
| ./embed.sh | ||
| ``` | ||
|
|
||
|
|
||
| # **Region Tags** | ||
|
|
||
| All examples use **region tags** that enable automated extraction into Gemini documentation. | ||
|
|
||
| ```python | ||
| # [START gemini_text_gen] | ||
| response = client.generate_text("Hello!") | ||
| # [END gemini_text_gen] | ||
| ``` | ||
|
|
||
| When contributing examples: | ||
|
|
||
| * Region tags **must** remain correctly paired | ||
| * Only example code should be inside the tags | ||
| * Test scaffolding should remain outside | ||
|
|
||
| This keeps documentation perfectly aligned with working code. | ||
|
|
||
|
|
||
| # **Testing Philosophy** | ||
|
|
||
| Every example in this repo is **automatically verified**. | ||
|
|
||
| * **Go** → `go test` | ||
| * **Java** → `mvn test` | ||
| * **JavaScript/TypeScript** → Node test files | ||
| * **Python** → runnable scripts | ||
| * **REST** → executable shell scripts | ||
|
|
||
| Tests ensure examples remain: | ||
|
|
||
| * Correct | ||
| * Up-to-date | ||
| * Documentation-ready | ||
| * Idiomatic for each language | ||
|
|
||
|
|
||
| # **Contributing** | ||
|
|
||
| If you're contributing, please ensure that code inside region tags follows best practices: | ||
|
|
||
| * **Clear** — easy for beginners to understand | ||
| * **Complete** — runnable with minimal setup | ||
| * **Concise** — no unnecessary lines | ||
|
|
||
| ### You can contribute by: | ||
|
|
||
| * Adding missing examples | ||
| * Enhancing clarity of existing examples | ||
| * Expanding test coverage | ||
| * Adding new model features | ||
| * Improving region tags | ||
| * Submitting bug fixes | ||
| * Improving language-specific READMEs | ||
|
|
||
| ### PR Checklist | ||
|
|
||
| Before submitting: | ||
|
|
||
| If you're contributing, please make sure that the code within region tags | ||
| follows best practices for example code: clear, complete, and concise. | ||
| * Code runs successfully | ||
| * Region tags are correct | ||
| * Tests are added or updated | ||
| * Code is minimal & idiomatic | ||
| * Commit message is descriptive | ||
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.
Running tests by directly invoking the test file with
nodeis unconventional for a JavaScript project. Typically, a test runner like Jest, Mocha, or the built-in Node.js test runner is used, invoked via a script inpackage.json(e.g.,npm test). This provides better test reporting and aligns with standard JS development practices. Consider updating the project to use a standard test runner and changing this instruction tonpm test.