From e4eb18f12217dab0d59d83989a779f88ead41ea1 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Mon, 7 Oct 2024 16:21:29 -0700 Subject: [PATCH 1/2] search grounding quickstart --- quickstarts/README.md | 3 +- quickstarts/Search_Grounding.ipynb | 997 +++++++++++++++++++++++++++++ 2 files changed, 999 insertions(+), 1 deletion(-) create mode 100644 quickstarts/Search_Grounding.ipynb diff --git a/quickstarts/README.md b/quickstarts/README.md index 28c8b4ac..bebaf6d8 100644 --- a/quickstarts/README.md +++ b/quickstarts/README.md @@ -10,8 +10,9 @@ Learn about the capabilities of the Gemini API by checking out these quickstarts * [Audio](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Audio.ipynb): Learn how to use the Gemini API with audio files. * [JSON mode](https://github.com/google-gemini/cookbook/blob/main/quickstarts/JSON_mode.ipynb): Discover how to use JSON mode. * [Function Calling](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Function_calling.ipynb): The Gemini API works great with code. Use this quickstart to learn how to write prompts to understand and call functions. Then check out the [function calling config](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Function_calling_config.ipynb) tutorial to learn more. -* [PDF files](https://github.com/google-gemini/cookbook/blob/main/quickstarts/PDF_Files.ipynb). Learn how to work with PDF files, to and upload text and images. +* [PDF files](https://github.com/google-gemini/cookbook/blob/main/quickstarts/PDF_Files.ipynb): Learn how to work with PDF files, to and upload text and images. * [System Instructions](https://github.com/google-gemini/cookbook/blob/main/quickstarts/System_instructions.ipynb): Give models additional context on how to respond by setting system instructions. +* [Search Grounding](https://colab.research.google.com/github/google-gemini/cookbook/blob/main/quickstarts/Search_Grounding.ipynb): Introduction on how to use the search grounding feature available in Gemini to provide resources to summarized answers from the Gemini model. * [Safety](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Safety.ipynb): Configure safety settings in the Gemini API. * [Streaming](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Streaming.ipynb): Learn how to use streaming for single interactions, and for chat. * [Embeddings](https://github.com/google-gemini/cookbook/blob/main/quickstarts/Embeddings.ipynb): Create high quality and task-specific embeddings. diff --git a/quickstarts/Search_Grounding.ipynb b/quickstarts/Search_Grounding.ipynb new file mode 100644 index 00000000..ee1f9b82 --- /dev/null +++ b/quickstarts/Search_Grounding.ipynb @@ -0,0 +1,997 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "markdown", + "source": [ + "##### Copyright 2024 Google LLC." + ], + "metadata": { + "id": "0fgOxpmGrOvn" + } + }, + { + "cell_type": "code", + "source": [ + "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "# you may not use this file except in compliance with the License.\n", + "# You may obtain a copy of the License at\n", + "#\n", + "# https://www.apache.org/licenses/LICENSE-2.0\n", + "#\n", + "# Unless required by applicable law or agreed to in writing, software\n", + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "# See the License for the specific language governing permissions and\n", + "# limitations under the License." + ], + "metadata": { + "id": "zxdx4xJxrTfP" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "# Gemini API: Search Grounding\n", + "\n", + "\n", + " \n", + "
\n", + " Run in Google Colab\n", + "
" + ], + "metadata": { + "id": "Qw6ttkOtrQ_D" + } + }, + { + "cell_type": "markdown", + "source": [ + "This notebook contains a walkthrough of how to use the search grounding feature in the Gemini API." + ], + "metadata": { + "id": "hqsJDYHlR4fo" + } + }, + { + "cell_type": "code", + "source": [ + "!pip uninstall -y -q google-ai-generativelanguage google-generativeai\n", + "!pip install -q git+https://github.com/google/generative-ai-python" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "mo9jj2QPwPdp", + "outputId": "df1baa5a-ae93-4d30-af0d-70e27e46f908" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", + " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", + " Preparing metadata (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n", + "\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m760.0/760.0 kB\u001b[0m \u001b[31m11.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", + "\u001b[?25h Building wheel for google-generativeai (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [ + "import google.generativeai as genai\n", + "from IPython.display import Markdown, HTML" + ], + "metadata": { + "id": "qK3p_60ly_2u" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "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](https://github.com/google-gemini/gemini-api-cookbook/blob/main/quickstarts/Authentication.ipynb) quickstart for an example." + ], + "metadata": { + "id": "qGycVxWJzHKH" + } + }, + { + "cell_type": "code", + "source": [ + "from google.colab import userdata\n", + "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", + "genai.configure(api_key=GOOGLE_API_KEY)" + ], + "metadata": { + "id": "ova_UQ0Ky9nv" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "genai.configure(api_key=GOOGLE_API_KEY, client_options={'api_endpoint': 'autopush-generativelanguage.sandbox.googleapis.com'})" + ], + "metadata": { + "id": "W8cZ_hOTy6xu" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "## Search Grounding" + ], + "metadata": { + "id": "rPfJ9uJfFWBu" + } + }, + { + "cell_type": "markdown", + "source": [ + "Take a look at the available models from the Gemini API. To perform search grounding, use `'models/gemini-1.5-pro-002'` when calling the API." + ], + "metadata": { + "id": "giHhTw6fzUB2" + } + }, + { + "cell_type": "code", + "source": [ + "for model in genai.list_models():\n", + " if '002' in model.name:\n", + " print(model.name)" + ], + "metadata": { + "id": "vmv9wc7TzCdx", + "colab": { + "base_uri": "https://localhost:8080/" + }, + "outputId": "52b078ff-31d9-409f-abb3-747dadf10748" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "models/gemini-1.5-pro-002\n", + "models/gemini-1.5-flash-002\n" + ] + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "`GoogleSearchRetrieval` is a tool to retrieve public web data for grounding, powered by Google. Part of `GoogleSearchRetrieval` is dynamic search retrieval, which uses real time retrieved data to build answers to queries.\n", + "\n", + "Two possible ways to use include `GoogleSearchRetrieval` are to pass in a string or dictionary to the field. First, you will pass in `google_search_retrieval` to the `tools` parameter in `generate_content`." + ], + "metadata": { + "id": "Odvk0g6J-YtT" + } + }, + { + "cell_type": "code", + "source": [ + "model = genai.GenerativeModel('models/gemini-1.5-pro-002')\n", + "response = model.generate_content(contents=\"What is the land area of Spain?\",\n", + " tools='google_search_retrieval')" + ], + "metadata": { + "id": "m5-7jx4szEUf" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "source": [ + "Take a look at the first response produced by the model." + ], + "metadata": { + "id": "LTbJ7-8B6zMK" + } + }, + { + "cell_type": "code", + "source": [ + "Markdown(response.candidates[0].content.parts[0].text)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 197 + }, + "id": "gimhCk5M6Dg0", + "outputId": "23aca278-7baa-4edc-eb3d-53c72613d6de" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ], + "text/markdown": "Spain's land area is approximately 500,000 square kilometers. More precisely, slightly different figures are reported by different sources, ranging from 499,733 sq. km to 506,030 sq. km, likely due to variations in measurement methodologies and inclusion/exclusion of certain territories. This makes it the largest country in Southern Europe, the second largest in Western Europe (after France), and the fourth largest on the European continent (after Russia, Ukraine, and France).\n\nBeyond the mainland, Spain's territory includes the Balearic Islands in the Mediterranean, the Canary Islands in the Atlantic, and the autonomous cities of Ceuta and Melilla in North Africa. These islands and cities add to the overall land area, with the Balearic Islands covering almost 5,000 sq. km and the Canary Islands covering over 7,400 sq km. The cities of Ceuta and Melilla are much smaller, covering around 32 sq km combined. The inclusion or exclusion of smaller Spanish territories may account for the different land area figures.\n\nIt's worth noting that Spain's land use is predominantly dedicated to agriculture, livestock farming, and forestry, which account for about 80% of the country. The remaining 20% includes urban areas, infrastructure, and other land uses.\n" + }, + "metadata": {}, + "execution_count": 30 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "Now, take a look at the rendered HTML content gathered by the Gemini API." + ], + "metadata": { + "id": "ktLx_aeE62Fl" + } + }, + { + "cell_type": "code", + "source": [ + "HTML(response.candidates[0].grounding_metadata.search_entry_point.rendered_content)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 65 + }, + "id": "nuRKeAov6JMq", + "outputId": "6eb19ac0-796d-4487-ed9b-fdc6ff93c1f1" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
\n", + "
\n", + " \n", + "
\n" + ] + }, + "metadata": {}, + "execution_count": 16 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "You will also receive links that direct you to sources used for your query." + ], + "metadata": { + "id": "FkTz4ebaFhkm" + } + }, + { + "cell_type": "code", + "source": [ + "response.candidates[0].grounding_metadata.grounding_chunks" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "ooeQfMFieXCM", + "outputId": "b9f42cec-0526-48af-cb2c-1044a2d9db2a" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[web {\n", + " uri: \"https://vertexaisearch.cloud.sandbox.google.com/grounding-api-redirect/AaZmLhMNkRxr0IUdX6RKVu3PgfN7_E75HECNoXAHn8kp7Gl9LVK7SPs-JdIZyEwwhB_i48XmBEJ4a9XXVKIuD5tZ1FVV2PosgK9jrjzmn3V2TcmXvI7mE3DjvMWcGxpJ_aODO-OWI0qMLs8IsKOB9A==\"\n", + " title: \"nationsonline.org\"\n", + "}\n", + ", web {\n", + " uri: \"https://vertexaisearch.cloud.sandbox.google.com/grounding-api-redirect/AaZmLhOsiqOWYA6HphfLPpYCImiPVvuCasg1WIcOnbcdpGiRt9GS0pslqMBuw9H4WsDGoPmnnXIfQo9_SLSPnNmzj2kIlL24kbuDeI8n5CM6HzKihLIRIF6PgpECsl7P-lEtdmAUJ6H3_3AD1Qj3AVpQlS9ZkhUQE0xMNL8jJ2X7d8gmwTUH8Z-GHoaZBX7D_00DtV89IhLrAg==\"\n", + " title: \"lamoncloa.gob.es\"\n", + "}\n", + "]" + ] + }, + "metadata": {}, + "execution_count": 18 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "And supporting segments, showing which parts of the output are quored from which source." + ], + "metadata": { + "id": "7m4ms4Gii9Vb" + } + }, + { + "cell_type": "code", + "source": [ + "response.candidates[0].grounding_metadata.grounding_supports" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "EhNz1_jEit1g", + "outputId": "576408b2-7060-4c43-fb72-fdff11334ab2" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "[segment {\n", + " end_index: 150\n", + " text: \"Spain\\'s land area is approximately 505,000 square kilometers, making it the fourth-largest country in Europe and the second-largest in Western Europe.\"\n", + "}\n", + "grounding_chunk_indices: 0\n", + "confidence_scores: 0.6591796875\n", + ", segment {\n", + " start_index: 152\n", + " end_index: 249\n", + " text: \"This figure includes the mainland and island territories such as the Balearic and Canary Islands.\"\n", + "}\n", + "grounding_chunk_indices: 1\n", + "confidence_scores: 0.8894008994102478\n", + "]" + ] + }, + "metadata": {}, + "execution_count": 20 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "### Customize search grounding parameters\n", + "\n", + "For a dictionary implementation, you don't actually have to pass in `mode` or `dynamic_threshold` key-value pairs. If you don't pass in a value, a default value is created. See the next code cell for this. For the `mode` value, you can pass in one of two specifications:\n", + "\n", + "1. `unspecified`: Always trigger retrieval.\n", + "2. `dynamic`: Run retrieval only when system decides it is necessary.\n", + "\n", + "The `dynamic_threshold` is a float value that acts as a threshold on how relevant answers returned should be. You can tweak this parameter depending on how much variation you want in your answer." + ], + "metadata": { + "id": "xE2Xgidy-ft7" + } + }, + { + "cell_type": "code", + "source": [ + "model = genai.GenerativeModel('models/gemini-1.5-pro-002')\n", + "response = model.generate_content(contents=\"What is the land area of Spain?\",\n", + " tools={\"google_search_retrieval\": {\"dynamic_retrieval_config\": {\"mode\": \"MODE_DYNAMIC\",\n", + " \"dynamic_threshold\": 0.5}}})" + ], + "metadata": { + "id": "peaRvsMw0Mee" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "Markdown(response.candidates[0].content.parts[0].text)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 98 + }, + "id": "yLJtDF78Fcjn", + "outputId": "66645674-579f-4ade-950a-1edbcb3b1433" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ], + "text/markdown": "Spain's land area is approximately 505,000 square kilometers, making it the fourth-largest country in Europe and the second-largest in Western Europe. This figure includes the mainland and island territories such as the Balearic and Canary Islands. The mainland itself occupies around 493,500 square kilometers. It is worth noting that slight variations exist in the reported figures depending on the source and measurement methods. As of today, September 25th, 2024, this information is considered up-to-date, but it's important to remember that these figures might be revised in the future.\n" + }, + "metadata": {}, + "execution_count": 17 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "You can also pass in an empty dictionary. Default values for `mode` and `dynamic_threshold` will be used." + ], + "metadata": { + "id": "zZK7tGTYIWmR" + } + }, + { + "cell_type": "code", + "source": [ + "model = genai.GenerativeModel('models/gemini-1.5-pro-002')\n", + "response = model.generate_content(contents=\"What is the land area of Spain?\",\n", + " tools={\"google_search_retrieval\": {}})" + ], + "metadata": { + "id": "jfH3O8wT-BH_" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "Markdown(response.candidates[0].content.parts[0].text)" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 197 + }, + "id": "MuYaEuGGIhrv", + "outputId": "455a78af-99d8-45e5-fbae-cd93f7eecedd" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "" + ], + "text/markdown": "Spain's land area is approximately 505,000 square kilometers. This makes it the largest country in Southern Europe, the second largest in Western Europe (after France), and the fourth largest on the European continent (after Russia, Ukraine, and France).\n\nIt's worth noting that this figure includes the mainland and the island territories. The mainland itself covers about 493,500 square kilometers, while the Balearic and Canary Islands add another 12,500 square kilometers. Two small Spanish cities, Ceuta and Melilla, located on the North African coast, contribute a small additional area. Spain also possesses several small, uninhabited enclaves.\n\nSpain is known for its diverse geography, ranging from mountains and plateaus to coastal plains. The central plateau, the Meseta Central, is a prominent feature, divided by mountain ranges and surrounded by other significant mountainous regions like the Pyrenees and the Sierra Nevada. This varied terrain contributes to a wide range of climates and ecosystems across the country. As of today, September 25, 2024, please remember that these figures might be subject to revision as more precise measurements become available.\n" + }, + "metadata": {}, + "execution_count": 13 + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "## With chat" + ], + "metadata": { + "id": "X8WW6M_YBwVL" + } + }, + { + "cell_type": "markdown", + "source": [ + "If you attach the `google_search_retrieval` tool to the model, it can be used in every turn of the chat:" + ], + "metadata": { + "id": "hb6Kmd3NCFa3" + } + }, + { + "cell_type": "code", + "source": [ + "model = genai.GenerativeModel('models/gemini-1.5-pro-002', tools=\"google_search_retrieval\")\n", + "chat = model.start_chat()\n", + "result = chat.send_message('what is the area of spain?')" + ], + "metadata": { + "id": "m7drY07ld7pG" + }, + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "display(Markdown(result.text))\n", + "\n", + "display(HTML(result.candidates[0].grounding_metadata.search_entry_point.rendered_content))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 251 + }, + "id": "o7ai4CTZ0uIQ", + "outputId": "2f08ab5a-c2ca-4146-bc9c-94b54ed415e9" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/markdown": "Spain's total area is approximately 505,000 square kilometers, or about 195,000 square miles. This includes the mainland and island territories. The mainland itself covers roughly 504,782 square kilometers (194,897 square miles). It's worth noting that different sources may vary slightly in their figures.\n\nHere's a breakdown of Spain's territory:\n\n* **Mainland:** Located on the Iberian Peninsula, shared with Portugal and Andorra. This is where the majority of Spain's land area and population is located.\n* **Island Territories:** These include the Balearic Islands in the Mediterranean Sea and the Canary Islands in the Atlantic Ocean.\n* **Exclaves:** Spain also has two exclaves (Ceuta and Melilla) located on the northern coast of Africa. There are also smaller territories or Places of Sovereignty (Plazas de soberanía) located on and off the coast of Morocco. One such place is the small exclave of Llívia within France.\n\nDue to this diverse geography, Spain enjoys a varied climate and landscape. From the Pyrenees mountains to the Mediterranean coast, the country offers a wide array of natural environments. It's the largest country in Southern Europe and the fourth largest in continental Europe (after Russia, Ukraine, and France).\n" + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
\n", + "
\n", + "
\n", + " what is the area of spain\n", + "
\n", + "
\n" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "code", + "source": [ + "result = chat.send_message('what is the google stock price?')\n", + "\n", + "\n", + "display(Markdown(result.text))\n", + "\n", + "display(HTML(result.candidates[0].grounding_metadata.search_entry_point.rendered_content))" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 204 + }, + "id": "XZRVpd9xBDRm", + "outputId": "37eb2163-a988-45bb-ca67-f37ed1858784" + }, + "execution_count": null, + "outputs": [ + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/markdown": "As of September 26, 2024, the price of Alphabet stock (GOOGL) is around $163. More specifically, it opened at $163.32. GOOG (Class C shares) is priced at $163.62. These values fluctuate throughout the trading day, so this is a snapshot in time.\n\nIt's important to distinguish between the two classes of Alphabet stock:\n\n* **GOOGL (Class A):** These shares have voting rights.\n* **GOOG (Class C):** These shares do not have voting rights.\n\nGenerally, their prices move very similarly. For the most up-to-the-minute price, you should consult a live stock ticker.\n" + }, + "metadata": {} + }, + { + "output_type": "display_data", + "data": { + "text/plain": [ + "" + ], + "text/html": [ + "\n", + "
\n", + "
\n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + " \n", + "
\n", + "
\n", + " \n", + "
\n" + ] + }, + "metadata": {} + } + ] + }, + { + "cell_type": "markdown", + "source": [ + "But you can also pass the tool through the `send_message` call." + ], + "metadata": { + "id": "M5DKNvBYCZjB" + } + }, + { + "cell_type": "code", + "source": [ + "model = genai.GenerativeModel('models/gemini-1.5-pro-002')\n", + "chat = model.start_chat()\n", + "result = chat.send_message('what is the area of spain?', tools=\"google_search_retrieval\")" + ], + "metadata": { + "id": "37xr_XfvBR04" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file From b4cd0a8a375c07075a837cbf79ffa50b99d49967 Mon Sep 17 00:00:00 2001 From: Shilpa Kancharla Date: Mon, 7 Oct 2024 16:23:30 -0700 Subject: [PATCH 2/2] Reformat notebook --- quickstarts/Search_Grounding.ipynb | 513 +++++++++++++---------------- 1 file changed, 233 insertions(+), 280 deletions(-) diff --git a/quickstarts/Search_Grounding.ipynb b/quickstarts/Search_Grounding.ipynb index ee1f9b82..f7504952 100644 --- a/quickstarts/Search_Grounding.ipynb +++ b/quickstarts/Search_Grounding.ipynb @@ -1,30 +1,22 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "provenance": [] - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - }, - "language_info": { - "name": "python" - } - }, "cells": [ { "cell_type": "markdown", - "source": [ - "##### Copyright 2024 Google LLC." - ], "metadata": { "id": "0fgOxpmGrOvn" - } + }, + "source": [ + "##### Copyright 2024 Google LLC." + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "cellView": "form", + "id": "zxdx4xJxrTfP" + }, + "outputs": [], "source": [ "# @title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", @@ -37,15 +29,13 @@ "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." - ], - "metadata": { - "id": "zxdx4xJxrTfP" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", + "metadata": { + "id": "Qw6ttkOtrQ_D" + }, "source": [ "# Gemini API: Search Grounding\n", "\n", @@ -54,38 +44,27 @@ " Run in Google Colab\n", " \n", "" - ], - "metadata": { - "id": "Qw6ttkOtrQ_D" - } + ] }, { "cell_type": "markdown", - "source": [ - "This notebook contains a walkthrough of how to use the search grounding feature in the Gemini API." - ], "metadata": { "id": "hqsJDYHlR4fo" - } + }, + "source": [ + "This notebook contains a walkthrough of how to use the search grounding feature in the Gemini API." + ] }, { "cell_type": "code", - "source": [ - "!pip uninstall -y -q google-ai-generativelanguage google-generativeai\n", - "!pip install -q git+https://github.com/google/generative-ai-python" - ], + "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/" - }, - "id": "mo9jj2QPwPdp", - "outputId": "df1baa5a-ae93-4d30-af0d-70e27e46f908" + "id": "mo9jj2QPwPdp" }, - "execution_count": null, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", @@ -94,190 +73,173 @@ "\u001b[?25h Building wheel for google-generativeai (pyproject.toml) ... \u001b[?25l\u001b[?25hdone\n" ] } + ], + "source": [ + "!pip uninstall -y -q google-ai-generativelanguage google-generativeai\n", + "!pip install -q git+https://github.com/google/generative-ai-python" ] }, { "cell_type": "code", - "source": [ - "import google.generativeai as genai\n", - "from IPython.display import Markdown, HTML" - ], + "execution_count": null, "metadata": { "id": "qK3p_60ly_2u" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "import google.generativeai as genai\n", + "from IPython.display import Markdown, HTML" + ] }, { "cell_type": "markdown", + "metadata": { + "id": "qGycVxWJzHKH" + }, "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](https://github.com/google-gemini/gemini-api-cookbook/blob/main/quickstarts/Authentication.ipynb) quickstart for an example." - ], - "metadata": { - "id": "qGycVxWJzHKH" - } + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "ova_UQ0Ky9nv" + }, + "outputs": [], "source": [ "from google.colab import userdata\n", "GOOGLE_API_KEY=userdata.get('GOOGLE_API_KEY')\n", "genai.configure(api_key=GOOGLE_API_KEY)" - ], - "metadata": { - "id": "ova_UQ0Ky9nv" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "code", - "source": [ - "genai.configure(api_key=GOOGLE_API_KEY, client_options={'api_endpoint': 'autopush-generativelanguage.sandbox.googleapis.com'})" - ], + "execution_count": null, "metadata": { "id": "W8cZ_hOTy6xu" }, - "execution_count": null, - "outputs": [] + "outputs": [], + "source": [ + "genai.configure(api_key=GOOGLE_API_KEY, client_options={'api_endpoint': 'autopush-generativelanguage.sandbox.googleapis.com'})" + ] }, { "cell_type": "markdown", - "source": [ - "## Search Grounding" - ], "metadata": { "id": "rPfJ9uJfFWBu" - } + }, + "source": [ + "## Search Grounding" + ] }, { "cell_type": "markdown", - "source": [ - "Take a look at the available models from the Gemini API. To perform search grounding, use `'models/gemini-1.5-pro-002'` when calling the API." - ], "metadata": { "id": "giHhTw6fzUB2" - } + }, + "source": [ + "Take a look at the available models from the Gemini API. To perform search grounding, use `'models/gemini-1.5-pro-002'` when calling the API." + ] }, { "cell_type": "code", - "source": [ - "for model in genai.list_models():\n", - " if '002' in model.name:\n", - " print(model.name)" - ], + "execution_count": null, "metadata": { - "id": "vmv9wc7TzCdx", - "colab": { - "base_uri": "https://localhost:8080/" - }, - "outputId": "52b078ff-31d9-409f-abb3-747dadf10748" + "id": "vmv9wc7TzCdx" }, - "execution_count": null, "outputs": [ { - "output_type": "stream", "name": "stdout", + "output_type": "stream", "text": [ "models/gemini-1.5-pro-002\n", "models/gemini-1.5-flash-002\n" ] } + ], + "source": [ + "for model in genai.list_models():\n", + " if '002' in model.name:\n", + " print(model.name)" ] }, { "cell_type": "markdown", + "metadata": { + "id": "Odvk0g6J-YtT" + }, "source": [ "`GoogleSearchRetrieval` is a tool to retrieve public web data for grounding, powered by Google. Part of `GoogleSearchRetrieval` is dynamic search retrieval, which uses real time retrieved data to build answers to queries.\n", "\n", "Two possible ways to use include `GoogleSearchRetrieval` are to pass in a string or dictionary to the field. First, you will pass in `google_search_retrieval` to the `tools` parameter in `generate_content`." - ], - "metadata": { - "id": "Odvk0g6J-YtT" - } + ] }, { "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "m5-7jx4szEUf" + }, + "outputs": [], "source": [ "model = genai.GenerativeModel('models/gemini-1.5-pro-002')\n", "response = model.generate_content(contents=\"What is the land area of Spain?\",\n", " tools='google_search_retrieval')" - ], - "metadata": { - "id": "m5-7jx4szEUf" - }, - "execution_count": null, - "outputs": [] + ] }, { "cell_type": "markdown", - "source": [ - "Take a look at the first response produced by the model." - ], "metadata": { "id": "LTbJ7-8B6zMK" - } + }, + "source": [ + "Take a look at the first response produced by the model." + ] }, { "cell_type": "code", - "source": [ - "Markdown(response.candidates[0].content.parts[0].text)" - ], + "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 197 - }, - "id": "gimhCk5M6Dg0", - "outputId": "23aca278-7baa-4edc-eb3d-53c72613d6de" + "id": "gimhCk5M6Dg0" }, - "execution_count": null, "outputs": [ { - "output_type": "execute_result", "data": { + "text/markdown": "Spain's land area is approximately 500,000 square kilometers. More precisely, slightly different figures are reported by different sources, ranging from 499,733 sq. km to 506,030 sq. km, likely due to variations in measurement methodologies and inclusion/exclusion of certain territories. This makes it the largest country in Southern Europe, the second largest in Western Europe (after France), and the fourth largest on the European continent (after Russia, Ukraine, and France).\n\nBeyond the mainland, Spain's territory includes the Balearic Islands in the Mediterranean, the Canary Islands in the Atlantic, and the autonomous cities of Ceuta and Melilla in North Africa. These islands and cities add to the overall land area, with the Balearic Islands covering almost 5,000 sq. km and the Canary Islands covering over 7,400 sq km. The cities of Ceuta and Melilla are much smaller, covering around 32 sq km combined. The inclusion or exclusion of smaller Spanish territories may account for the different land area figures.\n\nIt's worth noting that Spain's land use is predominantly dedicated to agriculture, livestock farming, and forestry, which account for about 80% of the country. The remaining 20% includes urban areas, infrastructure, and other land uses.\n", "text/plain": [ "" - ], - "text/markdown": "Spain's land area is approximately 500,000 square kilometers. More precisely, slightly different figures are reported by different sources, ranging from 499,733 sq. km to 506,030 sq. km, likely due to variations in measurement methodologies and inclusion/exclusion of certain territories. This makes it the largest country in Southern Europe, the second largest in Western Europe (after France), and the fourth largest on the European continent (after Russia, Ukraine, and France).\n\nBeyond the mainland, Spain's territory includes the Balearic Islands in the Mediterranean, the Canary Islands in the Atlantic, and the autonomous cities of Ceuta and Melilla in North Africa. These islands and cities add to the overall land area, with the Balearic Islands covering almost 5,000 sq. km and the Canary Islands covering over 7,400 sq km. The cities of Ceuta and Melilla are much smaller, covering around 32 sq km combined. The inclusion or exclusion of smaller Spanish territories may account for the different land area figures.\n\nIt's worth noting that Spain's land use is predominantly dedicated to agriculture, livestock farming, and forestry, which account for about 80% of the country. The remaining 20% includes urban areas, infrastructure, and other land uses.\n" + ] }, + "execution_count": 30, "metadata": {}, - "execution_count": 30 + "output_type": "execute_result" } + ], + "source": [ + "Markdown(response.candidates[0].content.parts[0].text)" ] }, { "cell_type": "markdown", - "source": [ - "Now, take a look at the rendered HTML content gathered by the Gemini API." - ], "metadata": { "id": "ktLx_aeE62Fl" - } + }, + "source": [ + "Now, take a look at the rendered HTML content gathered by the Gemini API." + ] }, { "cell_type": "code", - "source": [ - "HTML(response.candidates[0].grounding_metadata.search_entry_point.rendered_content)" - ], + "execution_count": null, "metadata": { - "colab": { - "base_uri": "https://localhost:8080/", - "height": 65 - }, - "id": "nuRKeAov6JMq", - "outputId": "6eb19ac0-796d-4487-ed9b-fdc6ff93c1f1" + "id": "nuRKeAov6JMq" }, - "execution_count": null, "outputs": [ { - "output_type": "execute_result", "data": { - "text/plain": [ - "" - ], "text/html": [ "