diff --git a/examples/GitHub_Issues_Viewer.ipynb b/examples/GitHub_Issues_Viewer.ipynb new file mode 100644 index 000000000..708c39f9c --- /dev/null +++ b/examples/GitHub_Issues_Viewer.ipynb @@ -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": [ + "" + ] + }, + { + "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" + ] + }, + { + "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", + "\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 +}