Skip to content

I've added a new example notebook that demonstrates how you can use t… #887

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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
181 changes: 181 additions & 0 deletions examples/GitHub_Issues_Viewer.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
{
Copy link
Collaborator

@Giom-V Giom-V Aug 7, 2025

Choose a reason for hiding this comment

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

Can you use a model selector like MODEL_ID="gemini-2.5-flash" # @param ["gemini-2.5-flash-lite","gemini-2.5-flash","gemini-2.5-pro"] {"allow-input":true, isTemplate: true}. Also prefer the stable models to the preview ones, gemini-2.5-flash-preview-05-20 in particular is not available anymore, it redirects to 2.5 flash.


Reply via ReviewNB

"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Gemini API: GitHub Issues Viewer\n",
"\n",
"This notebook demonstrates how to use the Gemini API with function calling to interact with the GitHub API and display information about issues in a repository."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/google-gemini/cookbook/blob/main/examples/GitHub_Issues_Viewer.ipynb\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" height=30/></a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Install dependencies\n",
"\n",
"First, install the necessary Python libraries. `google-genai` is for the Gemini API, and `PyGithub` is for interacting with the GitHub API."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install -qU 'google-genai' 'PyGithub'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Set up your API key\n",
"\n",
"To run the following cell, your API key must be stored it in a Colab Secret named `GOOGLE_API_KEY`. If you don't already have an API key, or you're not sure how to create a Colab Secret, see the [Authentication](../quickstarts/Authentication.ipynb) quickstart for an example.\n",
"\n",
"You will also need a GitHub Personal Access Token stored as a Colab Secret named `GITHUB_API_KEY`. See the [GitHub documentation](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token) for instructions on how to create a token."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from google import genai\n",
"import os\n",
"\n",
"try:\n",
" from google.colab import userdata\n",
" GOOGLE_API_KEY = userdata.get(\"GOOGLE_API_KEY\")\n",
" GITHUB_API_KEY = userdata.get(\"GITHUB_API_KEY\")\n",
"except (ModuleNotFoundError, ImportError):\n",
" GOOGLE_API_KEY = os.environ.get(\"GOOGLE_API_KEY\")\n",
" GITHUB_API_KEY = os.environ.get(\"GITHUB_API_KEY\")\n",
"\n",
"if not GOOGLE_API_KEY or not GITHUB_API_KEY:\n",
" raise ValueError(\"Please set the GOOGLE_API_KEY and GITHUB_API_KEY as environment variables or Colab secrets.\")\n",
"\n",
"client = genai.Client(api_key=GOOGLE_API_KEY)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Choose a model\n",
"\n",
"Next, choose a Gemini model that supports function calling. In this example, we'll use `gemini-2.5-flash-preview-05-20`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"MODEL_ID=\"gemini-2.5-flash-preview-05-20\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Define GitHub API functions as tools\n",
"\n",
"Now, define the Python functions that will be used as tools by the Gemini model. These functions will interact with the GitHub API using the `PyGithub` library.\n",
"\n",
"The `get_github_issues` function takes a repository name as input and returns a list of open issues. The docstring of the function is important, as it provides the model with the necessary information to understand what the function does and what arguments it expects."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from github import Github\n",
"\n",
"g = Github(GITHUB_API_KEY)\n",
"\n",
"def get_github_issues(repository_name: str):\n",
" \"\"\"Gets the open issues for a given GitHub repository.\n",
"\n",
" Args:\n",
" repository_name: The name of the repository in the format 'owner/repo'.\n",
" \"\"\"\n",
" repo = g.get_repo(repository_name)\n",
" issues = repo.get_issues(state='open')\n",
" issue_list = []\n",
" for issue in issues:\n",
" issue_list.append({\n",
" 'title': issue.title,\n",
" 'number': issue.number,\n",
" 'url': issue.html_url\n",
" })\n",
" return issue_list"
Comment on lines +123 to +132

Choose a reason for hiding this comment

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

high

This function can be improved in two ways:

  1. Error Handling: The current implementation doesn't handle potential errors from the GitHub API (e.g., repository not found). An unhandled exception will crash the tool. Wrapping the API call in a try...except block makes it more robust.
  2. Result Limiting: For repositories with many open issues, fetching all of them can be slow and may hit API rate limits. It's good practice to limit the number of issues returned to keep the tool responsive.

Here's a suggestion that addresses both points.

    try:
        repo = g.get_repo(repository_name)
        # Limit to the first 30 issues to be efficient and avoid API rate limits.
        issues = repo.get_issues(state='open')[:30]
        return [
            {
                'title': issue.title,
                'number': issue.number,
                'url': issue.html_url
            }
            for issue in issues
        ]
    except Exception as e:
        return f"Could not retrieve issues for '{repository_name}'. Error: {e}"

]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Interact with the model\n",
"\n",
"Now it's time to interact with the Gemini model. Create a `ChatSession` and pass the `get_github_issues` function to the `tools` parameter. Then, send a prompt to the model asking it to retrieve the open issues from a specific repository.\n",
"\n",
"The model will automatically call the `get_github_issues` function with the correct repository name, and then use the returned information to generate a user-friendly response."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"chat = client.chats.create(\n",
" model=MODEL_ID,\n",
" config={\n",
" \"tools\": [get_github_issues]\n",
" }\n",
")\n",
"\n",
"response = chat.send_message(\"Show me the open issues in the google/gemini-api-cookbook repository.\")\n",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wrong repo name, it should be google-gemini/cookbook

"\n",
"print(response.text)"
]
}
],
"metadata": {
"colab": {
"name": "GitHub_Issues_Viewer.ipynb",
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
},
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading