-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Mwessc
wants to merge
1
commit into
google-gemini:main
Choose a base branch
from
Mwessc:add-github-issues-example
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
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,181 @@ | ||
{ | ||
"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
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. This function can be improved in two ways:
Here's a suggestion that addresses both points.
|
||
] | ||
}, | ||
{ | ||
"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", | ||
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. 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 | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
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