diff --git a/gemini/getting-started/intro_gemini_chat.ipynb b/gemini/getting-started/intro_gemini_chat.ipynb
index 742f9fd1a6..b890e0d187 100644
--- a/gemini/getting-started/intro_gemini_chat.ipynb
+++ b/gemini/getting-started/intro_gemini_chat.ipynb
@@ -1,972 +1,935 @@
{
- "cells": [
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "ijGzTHJJUCPY",
- "tags": []
- },
- "outputs": [],
- "source": [
- "# Copyright 2024 Google LLC\n",
- "#\n",
- "# 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."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "VEqbX8OhE8y9"
- },
- "source": [
- "# Getting Started with Chat with the Gemini Pro model\n",
- "\n",
- "
\n",
- " \n",
- " \n",
- " Run in Colab\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- " Run in Colab Enterprise\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- " View on GitHub\n",
- " \n",
- " | \n",
- " \n",
- " \n",
- " Open in Vertex AI Workbench\n",
- " \n",
- " | \n",
- "
\n"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {},
- "source": [
- "| | |\n",
- "|-|-|\n",
- "|Author(s) | [Eric Dong](https://github.com/gericdong) |"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "CkHPv2myT2cx"
- },
- "source": [
- "## Overview\n",
- "\n",
- "This notebook demonstrates how to send chat prompts to the Gemini 1.0 Pro model (`gemini-1.0-pro`) by using the Vertex AI SDK for Python and LangChain. Gemini 1.0 Pro supports prompts with text-only input, including natural language tasks, multi-turn text and code chat, and code generation. It can output text and code.\n",
- "\n",
- "Learn more about [Sending chat prompt requests (Gemini)](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/send-chat-prompts-gemini)."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "DrkcqHrrwMAo"
- },
- "source": [
- "### Objectives\n",
- "\n",
- "In this tutorial, you learn how to send chat prompts to the Gemini 1.0 Pro model (`gemini-1.0-pro`) using the Vertex AI SDK for Python and LangChain.\n",
- "\n",
- "You will complete the following tasks:\n",
- "\n",
- "- Sending chat prompts using Vertex AI SDK for Python\n",
- "- Sending chat prompts using LangChain"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "C9nEPojogw-g"
- },
- "source": [
- "### Costs\n",
- "This tutorial uses billable components of Google Cloud:\n",
- "\n",
- "- Vertex AI\n",
- "\n",
- "Learn about [Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing) and use the [Pricing Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "r11Gu7qNgx1p"
- },
- "source": [
- "## Getting Started"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "No17Cw5hgx12"
- },
- "source": [
- "### Install libraries\n"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "tFy3H3aPgx12",
- "tags": []
- },
- "outputs": [],
- "source": [
- "! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n",
- " langchain-google-vertexai \\\n",
- " langchain"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "j7UyNVSiyQ96"
- },
- "source": [
- "### Restart current runtime\n",
- "\n",
- "To use the newly installed packages in this Jupyter runtime, you must restart the runtime. You can do this by running the cell below, which restarts the current kernel.\n",
- "\n",
- "The restart might take a minute or longer. After its restarted, continue to the next step."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": null,
- "metadata": {
- "id": "YmY9HVVGSBW5",
- "tags": []
- },
- "outputs": [],
- "source": [
- "import IPython\n",
- "\n",
- "app = IPython.Application.instance()\n",
- "app.kernel.do_shutdown(True)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "EXQZrM5hQeKb"
- },
- "source": [
- "\n",
- "⚠️ Wait for the kernel to finish restarting before you continue. ⚠️\n",
- "
"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "dmWOrTJ3gx13"
- },
- "source": [
- "### Authenticate your notebook environment (Colab only)\n",
- "\n",
- "If you are running this notebook on Google Colab, run the cell below to authenticate your environment.\n",
- "\n",
- "This step is not required if you are using [Vertex AI Workbench](https://cloud.google.com/vertex-ai-workbench)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 1,
- "metadata": {
- "id": "NyKGtVQjgx13",
- "tags": []
- },
- "outputs": [],
- "source": [
- "import sys\n",
- "\n",
- "# Additional authentication is required for Google Colab\n",
- "if \"google.colab\" in sys.modules:\n",
- " # Authenticate user to Google Cloud\n",
- " from google.colab import auth\n",
- "\n",
- " auth.authenticate_user()"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "DF4l8DTdWgPY"
- },
- "source": [
- "### Define Google Cloud project information and initialize Vertex AI\n",
- "\n",
- "To get started using Vertex AI, you must have an existing Google Cloud project and [enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
- "\n",
- "Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 2,
- "metadata": {
- "id": "Nqwi-5ufWp_B",
- "tags": []
- },
- "outputs": [],
- "source": [
- "# Define project information\n",
- "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
- "LOCATION = \"us-central1\" # @param {type:\"string\"}\n",
- "\n",
- "# Initialize Vertex AI\n",
- "import vertexai\n",
- "\n",
- "vertexai.init(project=PROJECT_ID, location=LOCATION)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "jXHfaVS66_01"
- },
- "source": [
- "### Import libraries"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 3,
- "metadata": {
- "id": "lslYAvw37JGQ",
- "tags": []
- },
- "outputs": [],
- "source": [
- "from IPython.display import Markdown\n",
- "from langchain.chains import ConversationChain\n",
- "from langchain.memory import ConversationBufferMemory\n",
- "from langchain.prompts import (\n",
- " ChatPromptTemplate,\n",
- " HumanMessagePromptTemplate,\n",
- " MessagesPlaceholder,\n",
- " SystemMessagePromptTemplate,\n",
- ")\n",
- "from langchain_core.messages import HumanMessage, SystemMessage\n",
- "from langchain_google_vertexai import ChatVertexAI, HarmBlockThreshold, HarmCategory\n",
- "from vertexai.generative_models import Content, GenerativeModel, Part"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "4437b7608c8e"
- },
- "source": [
- "## Sending chat prompts using Vertex AI SDK for Python\n",
- "\n",
- "### Load the Gemini 1.0 Pro model\n",
- "\n",
- "Gemini 1.0 Pro supports text and code generation from a text prompt."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 4,
- "metadata": {
- "id": "2998506fe6d1",
- "tags": []
- },
- "outputs": [],
- "source": [
- "model = GenerativeModel(\"gemini-1.0-pro\")"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "wl2AZceWjXoy"
- },
- "source": [
- "### Start a chat session\n",
- "\n",
- "You start a stateful chat session and then send chat prompts with configuration parameters including generation configurations and safety settings."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 5,
- "metadata": {
- "id": "_vLprtHAjNOO",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "Mars has two moons: Phobos and Deimos.\n",
- "\n",
- "**Phobos**\n",
- "\n",
- "* **Closest moon to Mars:** It orbits just 6,000 kilometers (3,700 miles) above the surface.\n",
- "* **Unusual shape:** Phobos is not spherical but rather irregularly shaped, resembling a potato.\n",
- "* **Astronomical sync:** Phobos orbits Mars three times a day, completing its orbit faster than any other known moon in the solar system.\n",
- "* **Unique groove:** Phobos has a prominent groove or canyon called Stickney, which is thought to be the result of an impact.\n",
- "* **Tidal acceleration:** Phobos is gradually spiraling inward towards Mars due to tidal forces. It is estimated that in about 100 million years, Phobos will either crash into Mars or break up into a ring.\n",
- "\n",
- "**Deimos**\n",
- "\n",
- "* **Outer moon of Mars:** Deimos orbits about 23,500 kilometers (14,600 miles) from the planet's surface.\n",
- "* **Small and irregularly shaped:** Deimos is even smaller and more irregularly shaped than Phobos.\n",
- "* **Slow orbit:** Deimos takes about 30 hours to complete one orbit around Mars.\n",
- "* **Captured asteroid:** Deimos is believed to be a captured asteroid rather than a moon that formed alongside Mars.\n",
- "* **Speculation of being artificial:** In 1958, Soviet astronomer Iosif Shklovsky suggested that Deimos might be an artificial satellite placed in orbit around Mars by an advanced extraterrestrial civilization. However, this theory has not gained scientific acceptance.\n"
- ]
- }
- ],
- "source": [
- "chat = model.start_chat()\n",
- "\n",
- "response = chat.send_message(\n",
- " \"\"\"You are an astronomer, knowledgeable about the solar system.\n",
- "How many moons does Mars have? Tell me some fun facts about them.\n",
- "\"\"\"\n",
- ")\n",
- "\n",
- "print(response.text)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "e45ReUIxvTxX"
- },
- "source": [
- "You can use `Markdown` to display the generated text."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 6,
- "metadata": {
- "id": "8QU6brtOuyAx",
- "tags": []
- },
- "outputs": [
- {
- "data": {
- "text/markdown": [
- "Mars has two moons: Phobos and Deimos.\n",
- "\n",
- "**Phobos**\n",
- "\n",
- "* **Closest moon to Mars:** It orbits just 6,000 kilometers (3,700 miles) above the surface.\n",
- "* **Unusual shape:** Phobos is not spherical but rather irregularly shaped, resembling a potato.\n",
- "* **Astronomical sync:** Phobos orbits Mars three times a day, completing its orbit faster than any other known moon in the solar system.\n",
- "* **Unique groove:** Phobos has a prominent groove or canyon called Stickney, which is thought to be the result of an impact.\n",
- "* **Tidal acceleration:** Phobos is gradually spiraling inward towards Mars due to tidal forces. It is estimated that in about 100 million years, Phobos will either crash into Mars or break up into a ring.\n",
- "\n",
- "**Deimos**\n",
- "\n",
- "* **Outer moon of Mars:** Deimos orbits about 23,500 kilometers (14,600 miles) from the planet's surface.\n",
- "* **Small and irregularly shaped:** Deimos is even smaller and more irregularly shaped than Phobos.\n",
- "* **Slow orbit:** Deimos takes about 30 hours to complete one orbit around Mars.\n",
- "* **Captured asteroid:** Deimos is believed to be a captured asteroid rather than a moon that formed alongside Mars.\n",
- "* **Speculation of being artificial:** In 1958, Soviet astronomer Iosif Shklovsky suggested that Deimos might be an artificial satellite placed in orbit around Mars by an advanced extraterrestrial civilization. However, this theory has not gained scientific acceptance."
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "ijGzTHJJUCPY"
+ },
+ "outputs": [],
+ "source": [
+ "# Copyright 2024 Google LLC\n",
+ "#\n",
+ "# 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."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "VEqbX8OhE8y9"
+ },
+ "source": [
+ "# Getting Started with Chat with the Gemini Pro model\n",
+ "\n",
+ "\n",
+ " \n",
+ " \n",
+ " Run in Colab\n",
+ " \n",
+ " | \n",
+ " \n",
+ " \n",
+ " Run in Colab Enterprise\n",
+ " \n",
+ " | \n",
+ " \n",
+ " \n",
+ " View on GitHub\n",
+ " \n",
+ " | \n",
+ " \n",
+ " \n",
+ " Open in Vertex AI Workbench\n",
+ " \n",
+ " | \n",
+ "
\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "f95c904716cd"
+ },
+ "source": [
+ "| | |\n",
+ "|-|-|\n",
+ "|Author(s) | [Eric Dong](https://github.com/gericdong) |"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "CkHPv2myT2cx"
+ },
+ "source": [
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates how to send chat prompts to the Gemini 1.5 Pro model (`gemini-1.5-pro-001`) by using the Vertex AI SDK for Python and LangChain. Gemini 1.5 Pro supports prompts with text-only input, including natural language tasks, multi-turn text and code chat, and code generation. It can output text and code.\n",
+ "\n",
+ "Learn more about [Sending chat prompt requests (Gemini)](https://cloud.google.com/vertex-ai/docs/generative-ai/multimodal/send-chat-prompts-gemini)."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "DrkcqHrrwMAo"
+ },
+ "source": [
+ "### Objectives\n",
+ "\n",
+ "In this tutorial, you learn how to send chat prompts to the Gemini 1.5 Pro model (`gemini-1.5-pro-001`) using the Vertex AI SDK for Python and LangChain.\n",
+ "\n",
+ "You will complete the following tasks:\n",
+ "\n",
+ "- Sending chat prompts using Vertex AI SDK for Python\n",
+ "- Sending chat prompts using LangChain"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "C9nEPojogw-g"
+ },
+ "source": [
+ "### Costs\n",
+ "This tutorial uses billable components of Google Cloud:\n",
+ "\n",
+ "- Vertex AI\n",
+ "\n",
+ "Learn about [Vertex AI pricing](https://cloud.google.com/vertex-ai/pricing) and use the [Pricing Calculator](https://cloud.google.com/products/calculator/) to generate a cost estimate based on your projected usage."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "r11Gu7qNgx1p"
+ },
+ "source": [
+ "## Getting Started"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "No17Cw5hgx12"
+ },
+ "source": [
+ "### Install libraries\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "tFy3H3aPgx12"
+ },
+ "outputs": [],
+ "source": [
+ "! pip3 install --upgrade --quiet google-cloud-aiplatform \\\n",
+ " langchain-google-vertexai \\\n",
+ " langchain"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "j7UyNVSiyQ96"
+ },
+ "source": [
+ "### Restart current runtime\n",
+ "\n",
+ "To use the newly installed packages in this Jupyter runtime, you must restart the runtime. You can do this by running the cell below, which restarts the current kernel.\n",
+ "\n",
+ "The restart might take a minute or longer. After its restarted, continue to the next step."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "id": "YmY9HVVGSBW5"
+ },
+ "outputs": [],
+ "source": [
+ "import IPython\n",
+ "\n",
+ "app = IPython.Application.instance()\n",
+ "app.kernel.do_shutdown(True)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "EXQZrM5hQeKb"
+ },
+ "source": [
+ "\n",
+ "⚠️ Wait for the kernel to finish restarting before you continue. ⚠️\n",
+ "
"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "dmWOrTJ3gx13"
+ },
+ "source": [
+ "### Authenticate your notebook environment (Colab only)\n",
+ "\n",
+ "If you are running this notebook on Google Colab, run the cell below to authenticate your environment.\n",
+ "\n",
+ "This step is not required if you are using [Vertex AI Workbench](https://cloud.google.com/vertex-ai-workbench)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 1,
+ "metadata": {
+ "id": "NyKGtVQjgx13"
+ },
+ "outputs": [],
+ "source": [
+ "import sys\n",
+ "\n",
+ "# Additional authentication is required for Google Colab\n",
+ "if \"google.colab\" in sys.modules:\n",
+ " # Authenticate user to Google Cloud\n",
+ " from google.colab import auth\n",
+ "\n",
+ " auth.authenticate_user()"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "DF4l8DTdWgPY"
+ },
+ "source": [
+ "### Define Google Cloud project information and initialize Vertex AI\n",
+ "\n",
+ "To get started using Vertex AI, you must have an existing Google Cloud project and [enable the Vertex AI API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n",
+ "\n",
+ "Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "metadata": {
+ "id": "Nqwi-5ufWp_B"
+ },
+ "outputs": [],
+ "source": [
+ "# Define project information\n",
+ "PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
+ "LOCATION = \"us-central1\" # @param {type:\"string\"}\n",
+ "\n",
+ "# Initialize Vertex AI\n",
+ "import vertexai\n",
+ "\n",
+ "vertexai.init(project=PROJECT_ID, location=LOCATION)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "jXHfaVS66_01"
+ },
+ "source": [
+ "### Import libraries"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "metadata": {
+ "id": "lslYAvw37JGQ"
+ },
+ "outputs": [],
+ "source": [
+ "from IPython.display import Markdown\n",
+ "from langchain.chains import ConversationChain\n",
+ "from langchain.memory import ConversationBufferMemory\n",
+ "from langchain.prompts import (\n",
+ " ChatPromptTemplate,\n",
+ " HumanMessagePromptTemplate,\n",
+ " MessagesPlaceholder,\n",
+ " SystemMessagePromptTemplate,\n",
+ ")\n",
+ "from langchain_core.messages import HumanMessage, SystemMessage\n",
+ "from langchain_google_vertexai import ChatVertexAI, HarmBlockThreshold, HarmCategory\n",
+ "from vertexai.generative_models import Content, GenerativeModel, Part"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "4437b7608c8e"
+ },
+ "source": [
+ "## Sending chat prompts using Vertex AI SDK for Python\n",
+ "\n",
+ "### Load the Gemini 1.5 Pro model\n",
+ "\n",
+ "Gemini 1.5 Pro supports text and code generation from a text prompt."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "metadata": {
+ "id": "2998506fe6d1"
+ },
+ "outputs": [],
+ "source": [
+ "model = GenerativeModel(\"gemini-1.5-pro-001\")"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "wl2AZceWjXoy"
+ },
+ "source": [
+ "### Start a chat session\n",
+ "\n",
+ "You start a stateful chat session and then send chat prompts with configuration parameters including generation configurations and safety settings."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {
+ "id": "_vLprtHAjNOO"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "Mars has two moons: Phobos and Deimos.\n",
+ "\n",
+ "**Phobos**\n",
+ "\n",
+ "* **Closest moon to Mars:** It orbits just 6,000 kilometers (3,700 miles) above the surface.\n",
+ "* **Unusual shape:** Phobos is not spherical but rather irregularly shaped, resembling a potato.\n",
+ "* **Astronomical sync:** Phobos orbits Mars three times a day, completing its orbit faster than any other known moon in the solar system.\n",
+ "* **Unique groove:** Phobos has a prominent groove or canyon called Stickney, which is thought to be the result of an impact.\n",
+ "* **Tidal acceleration:** Phobos is gradually spiraling inward towards Mars due to tidal forces. It is estimated that in about 100 million years, Phobos will either crash into Mars or break up into a ring.\n",
+ "\n",
+ "**Deimos**\n",
+ "\n",
+ "* **Outer moon of Mars:** Deimos orbits about 23,500 kilometers (14,600 miles) from the planet's surface.\n",
+ "* **Small and irregularly shaped:** Deimos is even smaller and more irregularly shaped than Phobos.\n",
+ "* **Slow orbit:** Deimos takes about 30 hours to complete one orbit around Mars.\n",
+ "* **Captured asteroid:** Deimos is believed to be a captured asteroid rather than a moon that formed alongside Mars.\n",
+ "* **Speculation of being artificial:** In 1958, Soviet astronomer Iosif Shklovsky suggested that Deimos might be an artificial satellite placed in orbit around Mars by an advanced extraterrestrial civilization. However, this theory has not gained scientific acceptance.\n"
+ ]
+ }
],
- "text/plain": [
- ""
+ "source": [
+ "chat = model.start_chat()\n",
+ "\n",
+ "response = chat.send_message(\n",
+ " \"\"\"You are an astronomer, knowledgeable about the solar system.\n",
+ "How many moons does Mars have? Tell me some fun facts about them.\n",
+ "\"\"\"\n",
+ ")\n",
+ "\n",
+ "print(response.text)"
]
- },
- "execution_count": 6,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "Markdown(response.text)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "V2axT2nKuVzP"
- },
- "source": [
- "You can check out the metadata of the response including the `safety_ratings` and `usage_metadata`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 7,
- "metadata": {
- "id": "ih0v9B1vspiF",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "candidates {\n",
- " content {\n",
- " role: \"model\"\n",
- " parts {\n",
- " text: \"Mars has two moons: Phobos and Deimos.\\n\\n**Phobos**\\n\\n* **Closest moon to Mars:** It orbits just 6,000 kilometers (3,700 miles) above the surface.\\n* **Unusual shape:** Phobos is not spherical but rather irregularly shaped, resembling a potato.\\n* **Astronomical sync:** Phobos orbits Mars three times a day, completing its orbit faster than any other known moon in the solar system.\\n* **Unique groove:** Phobos has a prominent groove or canyon called Stickney, which is thought to be the result of an impact.\\n* **Tidal acceleration:** Phobos is gradually spiraling inward towards Mars due to tidal forces. It is estimated that in about 100 million years, Phobos will either crash into Mars or break up into a ring.\\n\\n**Deimos**\\n\\n* **Outer moon of Mars:** Deimos orbits about 23,500 kilometers (14,600 miles) from the planet\\'s surface.\\n* **Small and irregularly shaped:** Deimos is even smaller and more irregularly shaped than Phobos.\\n* **Slow orbit:** Deimos takes about 30 hours to complete one orbit around Mars.\\n* **Captured asteroid:** Deimos is believed to be a captured asteroid rather than a moon that formed alongside Mars.\\n* **Speculation of being artificial:** In 1958, Soviet astronomer Iosif Shklovsky suggested that Deimos might be an artificial satellite placed in orbit around Mars by an advanced extraterrestrial civilization. However, this theory has not gained scientific acceptance.\"\n",
- " }\n",
- " }\n",
- " finish_reason: STOP\n",
- " safety_ratings {\n",
- " category: HARM_CATEGORY_HATE_SPEECH\n",
- " probability: NEGLIGIBLE\n",
- " }\n",
- " safety_ratings {\n",
- " category: HARM_CATEGORY_DANGEROUS_CONTENT\n",
- " probability: NEGLIGIBLE\n",
- " }\n",
- " safety_ratings {\n",
- " category: HARM_CATEGORY_HARASSMENT\n",
- " probability: NEGLIGIBLE\n",
- " }\n",
- " safety_ratings {\n",
- " category: HARM_CATEGORY_SEXUALLY_EXPLICIT\n",
- " probability: NEGLIGIBLE\n",
- " }\n",
- "}\n",
- "usage_metadata {\n",
- " prompt_token_count: 28\n",
- " candidates_token_count: 334\n",
- " total_token_count: 362\n",
- "}\n",
- "\n"
- ]
- }
- ],
- "source": [
- "print(response)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "ZuHvZevgwdj5"
- },
- "source": [
- "You can retrieve the history of the chat session."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 8,
- "metadata": {
- "id": "FYuqKyyktFq7",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "[role: \"user\"\n",
- "parts {\n",
- " text: \"You are an astronomer, knowledgeable about the solar system.\\nHow many moons does Mars have? Tell me some fun facts about them.\\n\"\n",
- "}\n",
- ", role: \"model\"\n",
- "parts {\n",
- " text: \"Mars has two moons: Phobos and Deimos.\\n\\n**Phobos**\\n\\n* **Closest moon to Mars:** It orbits just 6,000 kilometers (3,700 miles) above the surface.\\n* **Unusual shape:** Phobos is not spherical but rather irregularly shaped, resembling a potato.\\n* **Astronomical sync:** Phobos orbits Mars three times a day, completing its orbit faster than any other known moon in the solar system.\\n* **Unique groove:** Phobos has a prominent groove or canyon called Stickney, which is thought to be the result of an impact.\\n* **Tidal acceleration:** Phobos is gradually spiraling inward towards Mars due to tidal forces. It is estimated that in about 100 million years, Phobos will either crash into Mars or break up into a ring.\\n\\n**Deimos**\\n\\n* **Outer moon of Mars:** Deimos orbits about 23,500 kilometers (14,600 miles) from the planet\\'s surface.\\n* **Small and irregularly shaped:** Deimos is even smaller and more irregularly shaped than Phobos.\\n* **Slow orbit:** Deimos takes about 30 hours to complete one orbit around Mars.\\n* **Captured asteroid:** Deimos is believed to be a captured asteroid rather than a moon that formed alongside Mars.\\n* **Speculation of being artificial:** In 1958, Soviet astronomer Iosif Shklovsky suggested that Deimos might be an artificial satellite placed in orbit around Mars by an advanced extraterrestrial civilization. However, this theory has not gained scientific acceptance.\"\n",
- "}\n",
- "]\n"
- ]
- }
- ],
- "source": [
- "print(chat.history)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "kiS2CywXxU2y"
- },
- "source": [
- "### Code chat\n",
- "\n",
- "Gemini 1.0 Pro also supports code generation from a text prompt."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 9,
- "metadata": {
- "id": "1lWiPGQ-cDqC",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "```python\n",
- "def is_leap_year(year):\n",
- " \"\"\"\n",
- " Checks if a year is a leap year.\n",
- "\n",
- " Args:\n",
- " year: The year to check.\n",
- "\n",
- " Returns:\n",
- " True if the year is a leap year, False otherwise.\n",
- " \"\"\"\n",
- "\n",
- " if year % 4 != 0:\n",
- " return False\n",
- "\n",
- " if year % 100 == 0 and year % 400 != 0:\n",
- " return False\n",
- "\n",
- " return True\n",
- "```\n"
- ]
- }
- ],
- "source": [
- "code_chat = model.start_chat()\n",
- "\n",
- "response = code_chat.send_message(\n",
- " \"Write a function that checks if a year is a leap year\"\n",
- ")\n",
- "\n",
- "print(response.text)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "kI781aqpy-lH"
- },
- "source": [
- "You can generate unit tests to test the function in this multi-turn chat."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 10,
- "metadata": {
- "id": "TGHToON4xyOV",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "```python\n",
- "import unittest\n",
- "\n",
- "class TestIsLeapYear(unittest.TestCase):\n",
- "\n",
- " def test_is_leap_year(self):\n",
- " self.assertTrue(is_leap_year(2000))\n",
- " self.assertTrue(is_leap_year(2004))\n",
- " self.assertTrue(is_leap_year(2012))\n",
- "\n",
- " def test_is_not_leap_year(self):\n",
- " self.assertFalse(is_leap_year(1900))\n",
- " self.assertFalse(is_leap_year(1901))\n",
- " self.assertFalse(is_leap_year(2018))\n",
- "```\n"
- ]
- }
- ],
- "source": [
- "response = code_chat.send_message(\"Write a unit test of the generated function\")\n",
- "\n",
- "print(response.text)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "jeZNScL7Ci2A"
- },
- "source": [
- "### Add chat history\n",
- "\n",
- "You can add chat history to a chat by adding messages from role `user` and `model` alternately. System messages can be set in the first part for the first message."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 11,
- "metadata": {
- "id": "JzqUThJv77G9",
- "tags": []
- },
- "outputs": [
- {
- "data": {
- "text/markdown": [
- "Yes, Lord of the Rings and Hobbit are based on the book series of the same names by J.R.R. Tolkien."
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "e45ReUIxvTxX"
+ },
+ "source": [
+ "You can use `Markdown` to display the generated text."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {
+ "id": "8QU6brtOuyAx"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/markdown": [
+ "Mars has two moons: Phobos and Deimos.\n",
+ "\n",
+ "**Phobos**\n",
+ "\n",
+ "* **Closest moon to Mars:** It orbits just 6,000 kilometers (3,700 miles) above the surface.\n",
+ "* **Unusual shape:** Phobos is not spherical but rather irregularly shaped, resembling a potato.\n",
+ "* **Astronomical sync:** Phobos orbits Mars three times a day, completing its orbit faster than any other known moon in the solar system.\n",
+ "* **Unique groove:** Phobos has a prominent groove or canyon called Stickney, which is thought to be the result of an impact.\n",
+ "* **Tidal acceleration:** Phobos is gradually spiraling inward towards Mars due to tidal forces. It is estimated that in about 100 million years, Phobos will either crash into Mars or break up into a ring.\n",
+ "\n",
+ "**Deimos**\n",
+ "\n",
+ "* **Outer moon of Mars:** Deimos orbits about 23,500 kilometers (14,600 miles) from the planet's surface.\n",
+ "* **Small and irregularly shaped:** Deimos is even smaller and more irregularly shaped than Phobos.\n",
+ "* **Slow orbit:** Deimos takes about 30 hours to complete one orbit around Mars.\n",
+ "* **Captured asteroid:** Deimos is believed to be a captured asteroid rather than a moon that formed alongside Mars.\n",
+ "* **Speculation of being artificial:** In 1958, Soviet astronomer Iosif Shklovsky suggested that Deimos might be an artificial satellite placed in orbit around Mars by an advanced extraterrestrial civilization. However, this theory has not gained scientific acceptance."
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
],
- "text/plain": [
- ""
+ "source": [
+ "Markdown(response.text)"
]
- },
- "execution_count": 11,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "chat2 = model.start_chat(\n",
- " history=[\n",
- " Content(\n",
- " role=\"user\",\n",
- " parts=[\n",
- " Part.from_text(\n",
- " \"\"\"\n",
- " My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.\n",
- " Who do you work for?\n",
- " \"\"\"\n",
- " )\n",
- " ],\n",
- " ),\n",
- " Content(role=\"model\", parts=[Part.from_text(\"I work for Ned.\")]),\n",
- " Content(role=\"user\", parts=[Part.from_text(\"What do I like?\")]),\n",
- " Content(role=\"model\", parts=[Part.from_text(\"Ned likes watching movies.\")]),\n",
- " ]\n",
- ")\n",
- "\n",
- "response = chat2.send_message(\"Are my favorite movies based on a book series?\")\n",
- "Markdown(response.text)"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 12,
- "metadata": {
- "id": "G64BrDoxC-K3",
- "tags": []
- },
- "outputs": [
- {
- "data": {
- "text/markdown": [
- "The Hobbit was published in 1937 and The Lord of the Rings was published in 1954."
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "V2axT2nKuVzP"
+ },
+ "source": [
+ "You can check out the metadata of the response including the `safety_ratings` and `usage_metadata`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {
+ "id": "ih0v9B1vspiF"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "candidates {\n",
+ " content {\n",
+ " role: \"model\"\n",
+ " parts {\n",
+ " text: \"Mars has two moons: Phobos and Deimos.\\n\\n**Phobos**\\n\\n* **Closest moon to Mars:** It orbits just 6,000 kilometers (3,700 miles) above the surface.\\n* **Unusual shape:** Phobos is not spherical but rather irregularly shaped, resembling a potato.\\n* **Astronomical sync:** Phobos orbits Mars three times a day, completing its orbit faster than any other known moon in the solar system.\\n* **Unique groove:** Phobos has a prominent groove or canyon called Stickney, which is thought to be the result of an impact.\\n* **Tidal acceleration:** Phobos is gradually spiraling inward towards Mars due to tidal forces. It is estimated that in about 100 million years, Phobos will either crash into Mars or break up into a ring.\\n\\n**Deimos**\\n\\n* **Outer moon of Mars:** Deimos orbits about 23,500 kilometers (14,600 miles) from the planet\\'s surface.\\n* **Small and irregularly shaped:** Deimos is even smaller and more irregularly shaped than Phobos.\\n* **Slow orbit:** Deimos takes about 30 hours to complete one orbit around Mars.\\n* **Captured asteroid:** Deimos is believed to be a captured asteroid rather than a moon that formed alongside Mars.\\n* **Speculation of being artificial:** In 1958, Soviet astronomer Iosif Shklovsky suggested that Deimos might be an artificial satellite placed in orbit around Mars by an advanced extraterrestrial civilization. However, this theory has not gained scientific acceptance.\"\n",
+ " }\n",
+ " }\n",
+ " finish_reason: STOP\n",
+ " safety_ratings {\n",
+ " category: HARM_CATEGORY_HATE_SPEECH\n",
+ " probability: NEGLIGIBLE\n",
+ " }\n",
+ " safety_ratings {\n",
+ " category: HARM_CATEGORY_DANGEROUS_CONTENT\n",
+ " probability: NEGLIGIBLE\n",
+ " }\n",
+ " safety_ratings {\n",
+ " category: HARM_CATEGORY_HARASSMENT\n",
+ " probability: NEGLIGIBLE\n",
+ " }\n",
+ " safety_ratings {\n",
+ " category: HARM_CATEGORY_SEXUALLY_EXPLICIT\n",
+ " probability: NEGLIGIBLE\n",
+ " }\n",
+ "}\n",
+ "usage_metadata {\n",
+ " prompt_token_count: 28\n",
+ " candidates_token_count: 334\n",
+ " total_token_count: 362\n",
+ "}\n",
+ "\n"
+ ]
+ }
],
- "text/plain": [
- ""
+ "source": [
+ "print(response)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "ZuHvZevgwdj5"
+ },
+ "source": [
+ "You can retrieve the history of the chat session."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "metadata": {
+ "id": "FYuqKyyktFq7"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "[role: \"user\"\n",
+ "parts {\n",
+ " text: \"You are an astronomer, knowledgeable about the solar system.\\nHow many moons does Mars have? Tell me some fun facts about them.\\n\"\n",
+ "}\n",
+ ", role: \"model\"\n",
+ "parts {\n",
+ " text: \"Mars has two moons: Phobos and Deimos.\\n\\n**Phobos**\\n\\n* **Closest moon to Mars:** It orbits just 6,000 kilometers (3,700 miles) above the surface.\\n* **Unusual shape:** Phobos is not spherical but rather irregularly shaped, resembling a potato.\\n* **Astronomical sync:** Phobos orbits Mars three times a day, completing its orbit faster than any other known moon in the solar system.\\n* **Unique groove:** Phobos has a prominent groove or canyon called Stickney, which is thought to be the result of an impact.\\n* **Tidal acceleration:** Phobos is gradually spiraling inward towards Mars due to tidal forces. It is estimated that in about 100 million years, Phobos will either crash into Mars or break up into a ring.\\n\\n**Deimos**\\n\\n* **Outer moon of Mars:** Deimos orbits about 23,500 kilometers (14,600 miles) from the planet\\'s surface.\\n* **Small and irregularly shaped:** Deimos is even smaller and more irregularly shaped than Phobos.\\n* **Slow orbit:** Deimos takes about 30 hours to complete one orbit around Mars.\\n* **Captured asteroid:** Deimos is believed to be a captured asteroid rather than a moon that formed alongside Mars.\\n* **Speculation of being artificial:** In 1958, Soviet astronomer Iosif Shklovsky suggested that Deimos might be an artificial satellite placed in orbit around Mars by an advanced extraterrestrial civilization. However, this theory has not gained scientific acceptance.\"\n",
+ "}\n",
+ "]\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(chat.history)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "kiS2CywXxU2y"
+ },
+ "source": [
+ "### Code chat\n",
+ "\n",
+ "Gemini 1.5 Pro also supports code generation from a text prompt."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "id": "1lWiPGQ-cDqC"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "```python\n",
+ "def is_leap_year(year):\n",
+ " \"\"\"\n",
+ " Checks if a year is a leap year.\n",
+ "\n",
+ " Args:\n",
+ " year: The year to check.\n",
+ "\n",
+ " Returns:\n",
+ " True if the year is a leap year, False otherwise.\n",
+ " \"\"\"\n",
+ "\n",
+ " if year % 4 != 0:\n",
+ " return False\n",
+ "\n",
+ " if year % 100 == 0 and year % 400 != 0:\n",
+ " return False\n",
+ "\n",
+ " return True\n",
+ "```\n"
+ ]
+ }
+ ],
+ "source": [
+ "code_chat = model.start_chat()\n",
+ "\n",
+ "response = code_chat.send_message(\n",
+ " \"Write a function that checks if a year is a leap year\"\n",
+ ")\n",
+ "\n",
+ "print(response.text)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "kI781aqpy-lH"
+ },
+ "source": [
+ "You can generate unit tests to test the function in this multi-turn chat."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 10,
+ "metadata": {
+ "id": "TGHToON4xyOV"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "```python\n",
+ "import unittest\n",
+ "\n",
+ "class TestIsLeapYear(unittest.TestCase):\n",
+ "\n",
+ " def test_is_leap_year(self):\n",
+ " self.assertTrue(is_leap_year(2000))\n",
+ " self.assertTrue(is_leap_year(2004))\n",
+ " self.assertTrue(is_leap_year(2012))\n",
+ "\n",
+ " def test_is_not_leap_year(self):\n",
+ " self.assertFalse(is_leap_year(1900))\n",
+ " self.assertFalse(is_leap_year(1901))\n",
+ " self.assertFalse(is_leap_year(2018))\n",
+ "```\n"
+ ]
+ }
+ ],
+ "source": [
+ "response = code_chat.send_message(\"Write a unit test of the generated function\")\n",
+ "\n",
+ "print(response.text)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "jeZNScL7Ci2A"
+ },
+ "source": [
+ "### Add chat history\n",
+ "\n",
+ "You can add chat history to a chat by adding messages from role `user` and `model` alternately. System messages can be set in the first part for the first message."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {
+ "id": "JzqUThJv77G9"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/markdown": [
+ "Yes, Lord of the Rings and Hobbit are based on the book series of the same names by J.R.R. Tolkien."
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "execution_count": 11,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "chat2 = model.start_chat(\n",
+ " history=[\n",
+ " Content(\n",
+ " role=\"user\",\n",
+ " parts=[\n",
+ " Part.from_text(\n",
+ " \"\"\"\n",
+ " My name is Ned. You are my personal assistant. My favorite movies are Lord of the Rings and Hobbit.\n",
+ " Who do you work for?\n",
+ " \"\"\"\n",
+ " )\n",
+ " ],\n",
+ " ),\n",
+ " Content(role=\"model\", parts=[Part.from_text(\"I work for Ned.\")]),\n",
+ " Content(role=\"user\", parts=[Part.from_text(\"What do I like?\")]),\n",
+ " Content(role=\"model\", parts=[Part.from_text(\"Ned likes watching movies.\")]),\n",
+ " ]\n",
+ ")\n",
+ "\n",
+ "response = chat2.send_message(\"Are my favorite movies based on a book series?\")\n",
+ "Markdown(response.text)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "id": "G64BrDoxC-K3"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/markdown": [
+ "The Hobbit was published in 1937 and The Lord of the Rings was published in 1954."
+ ],
+ "text/plain": [
+ ""
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "response = chat2.send_message(\"When were these books published?\")\n",
+ "Markdown(response.text)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "WFlnwb0u54iN"
+ },
+ "source": [
+ "## Sending chat prompts using LangChain\n",
+ "\n",
+ "The Vertex AI Gemini API is integrated with the LangChain Python SDK, making it convenient to build applications on top of Gemini models."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "ga2YnFYPXZgh"
+ },
+ "source": [
+ "### Start a chat session\n",
+ "\n",
+ "You can start a chat by sending chat prompts to the Gemini 1.5 Pro model directly. Gemini 1.5 Pro doesn't support `SystemMessage` at the moment, but `SystemMessage` can be added to the first human message by setting the `convert_system_message_to_human` to `True`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {
+ "id": "s41n0UEwGVZV"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "J'aime la programmation.\n"
+ ]
+ }
+ ],
+ "source": [
+ "system_message = \"You are a helpful assistant who translates English to French.\"\n",
+ "human_message = \"Translate this sentence from English to French. I love programming.\"\n",
+ "\n",
+ "messages = [SystemMessage(content=system_message), HumanMessage(content=human_message)]\n",
+ "\n",
+ "chat = ChatVertexAI(\n",
+ " model_name=\"gemini-1.5-pro-001\",\n",
+ " convert_system_message_to_human=True,\n",
+ " safety_settings={\n",
+ " HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE\n",
+ " },\n",
+ ")\n",
+ "\n",
+ "result = chat.generate([messages])\n",
+ "print(result.generations[0][0].text)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "4VprmGzu4FPO"
+ },
+ "source": [
+ "You can check out the metadata of the generated content."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {
+ "id": "hBLFXgzsIJhw"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "{'is_blocked': False, 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}], 'citation_metadata': None}\n"
+ ]
+ }
+ ],
+ "source": [
+ "print(result.generations[0][0].generation_info)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "RgYHzIUP4PMo"
+ },
+ "source": [
+ "### Use a chat chain with chat prompt template"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 15,
+ "metadata": {
+ "id": "9vZr5vyrV-eI"
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "AIMessage(content=\"J'adore la programmation.\")"
+ ]
+ },
+ "execution_count": 15,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "system_message = \"You are a helpful assistant who translates English to French.\"\n",
+ "human_message = \"Translate this sentence from English to French. I love programming.\"\n",
+ "\n",
+ "messages = [SystemMessage(content=system_message), HumanMessage(content=human_message)]\n",
+ "prompt = ChatPromptTemplate.from_messages(messages)\n",
+ "\n",
+ "chat = ChatVertexAI(\n",
+ " model_name=\"gemini-1.5-pro-001\",\n",
+ " convert_system_message_to_human=True,\n",
+ " safety_settings={\n",
+ " HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE\n",
+ " },\n",
+ ")\n",
+ "\n",
+ "chain = prompt | chat\n",
+ "chain.invoke({})"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {
+ "id": "lB50i-JG9RQp"
+ },
+ "source": [
+ "### Use a conversation chain\n",
+ "\n",
+ "You also can wrap up a chat in `ConversationChain`, which has built-in memory for remembering past user inputs and model outputs."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 16,
+ "metadata": {
+ "id": "UFxQ6AN_5vDQ"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "\n",
+ "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
+ "Prompt after formatting:\n",
+ "\u001b[32;1m\u001b[1;3mSystem: You are a helpful assistant who is good at language translation.\n",
+ "Human: Translate this sentence from English to French. I love programming.\u001b[0m\n",
+ "\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "{'input': 'Translate this sentence from English to French. I love programming.',\n",
+ " 'history': [HumanMessage(content='Translate this sentence from English to French. I love programming.'),\n",
+ " AIMessage(content=\" J'adore la programmation.\")],\n",
+ " 'response': \" J'adore la programmation.\"}"
+ ]
+ },
+ "execution_count": 16,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "model = ChatVertexAI(\n",
+ " model_name=\"gemini-1.5-pro-001\",\n",
+ " convert_system_message_to_human=True,\n",
+ " safety_settings={\n",
+ " HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE\n",
+ " },\n",
+ ")\n",
+ "\n",
+ "prompt = ChatPromptTemplate(\n",
+ " messages=[\n",
+ " SystemMessagePromptTemplate.from_template(\n",
+ " \"You are a helpful assistant who is good at language translation.\"\n",
+ " ),\n",
+ " MessagesPlaceholder(variable_name=\"history\"),\n",
+ " HumanMessagePromptTemplate.from_template(\"{input}\"),\n",
+ " ]\n",
+ ")\n",
+ "\n",
+ "memory = ConversationBufferMemory(memory_key=\"history\", return_messages=True)\n",
+ "conversation = ConversationChain(llm=model, prompt=prompt, verbose=True, memory=memory)\n",
+ "\n",
+ "conversation.invoke(\n",
+ " input=\"Translate this sentence from English to French. I love programming.\"\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {
+ "id": "AJMP2JrMDmS-"
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "\n",
+ "\n",
+ "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
+ "Prompt after formatting:\n",
+ "\u001b[32;1m\u001b[1;3mSystem: You are a helpful assistant who is good at language translation.\n",
+ "Human: Translate this sentence from English to French. I love programming.\n",
+ "AI: J'adore la programmation.\n",
+ "Human: Translate it to Spanish\u001b[0m\n",
+ "\n",
+ "\u001b[1m> Finished chain.\u001b[0m\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "{'input': 'Translate it to Spanish',\n",
+ " 'history': [HumanMessage(content='Translate this sentence from English to French. I love programming.'),\n",
+ " AIMessage(content=\" J'adore la programmation.\"),\n",
+ " HumanMessage(content='Translate it to Spanish'),\n",
+ " AIMessage(content='Me encanta programar.')],\n",
+ " 'response': 'Me encanta programar.'}"
+ ]
+ },
+ "execution_count": 17,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "conversation.invoke(\"Translate it to Spanish\")"
]
- },
- "execution_count": 12,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "response = chat2.send_message(\"When were these books published?\")\n",
- "Markdown(response.text)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "WFlnwb0u54iN"
- },
- "source": [
- "## Sending chat prompts using LangChain\n",
- "\n",
- "The Vertex AI Gemini API is integrated with the LangChain Python SDK, making it convenient to build applications on top of Gemini models."
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "ga2YnFYPXZgh"
- },
- "source": [
- "### Start a chat session\n",
- "\n",
- "You can start a chat by sending chat prompts to the Gemini 1.0 Pro model directly. Gemini 1.0 Pro doesn't support `SystemMessage` at the moment, but `SystemMessage` can be added to the first human message by setting the `convert_system_message_to_human` to `True`."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 13,
- "metadata": {
- "id": "s41n0UEwGVZV",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "J'aime la programmation.\n"
- ]
- }
- ],
- "source": [
- "system_message = \"You are a helpful assistant who translates English to French.\"\n",
- "human_message = \"Translate this sentence from English to French. I love programming.\"\n",
- "\n",
- "messages = [SystemMessage(content=system_message), HumanMessage(content=human_message)]\n",
- "\n",
- "chat = ChatVertexAI(\n",
- " model_name=\"gemini-1.0-pro\",\n",
- " convert_system_message_to_human=True,\n",
- " safety_settings={\n",
- " HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE\n",
- " },\n",
- ")\n",
- "\n",
- "result = chat.generate([messages])\n",
- "print(result.generations[0][0].text)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "4VprmGzu4FPO"
- },
- "source": [
- "You can check out the metadata of the generated content."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 14,
- "metadata": {
- "id": "hBLFXgzsIJhw",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "{'is_blocked': False, 'safety_ratings': [{'category': 'HARM_CATEGORY_HATE_SPEECH', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_DANGEROUS_CONTENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_HARASSMENT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}, {'category': 'HARM_CATEGORY_SEXUALLY_EXPLICIT', 'probability_label': 'NEGLIGIBLE', 'blocked': False}], 'citation_metadata': None}\n"
- ]
- }
- ],
- "source": [
- "print(result.generations[0][0].generation_info)"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "RgYHzIUP4PMo"
- },
- "source": [
- "### Use a chat chain with chat prompt template"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 15,
- "metadata": {
- "id": "9vZr5vyrV-eI",
- "tags": []
- },
- "outputs": [
- {
- "data": {
- "text/plain": [
- "AIMessage(content=\"J'adore la programmation.\")"
- ]
- },
- "execution_count": 15,
- "metadata": {},
- "output_type": "execute_result"
- }
- ],
- "source": [
- "system_message = \"You are a helpful assistant who translates English to French.\"\n",
- "human_message = \"Translate this sentence from English to French. I love programming.\"\n",
- "\n",
- "messages = [SystemMessage(content=system_message), HumanMessage(content=human_message)]\n",
- "prompt = ChatPromptTemplate.from_messages(messages)\n",
- "\n",
- "chat = ChatVertexAI(\n",
- " model_name=\"gemini-1.0-pro\",\n",
- " convert_system_message_to_human=True,\n",
- " safety_settings={\n",
- " HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE\n",
- " },\n",
- ")\n",
- "\n",
- "chain = prompt | chat\n",
- "chain.invoke({})"
- ]
- },
- {
- "cell_type": "markdown",
- "metadata": {
- "id": "lB50i-JG9RQp"
- },
- "source": [
- "### Use a conversation chain\n",
- "\n",
- "You also can wrap up a chat in `ConversationChain`, which has built-in memory for remembering past user inputs and model outputs."
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 16,
- "metadata": {
- "id": "UFxQ6AN_5vDQ",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\n",
- "\n",
- "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
- "Prompt after formatting:\n",
- "\u001b[32;1m\u001b[1;3mSystem: You are a helpful assistant who is good at language translation.\n",
- "Human: Translate this sentence from English to French. I love programming.\u001b[0m\n",
- "\n",
- "\u001b[1m> Finished chain.\u001b[0m\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "{'input': 'Translate this sentence from English to French. I love programming.',\n",
- " 'history': [HumanMessage(content='Translate this sentence from English to French. I love programming.'),\n",
- " AIMessage(content=\" J'adore la programmation.\")],\n",
- " 'response': \" J'adore la programmation.\"}"
- ]
- },
- "execution_count": 16,
- "metadata": {},
- "output_type": "execute_result"
}
- ],
- "source": [
- "model = ChatVertexAI(\n",
- " model_name=\"gemini-1.0-pro\",\n",
- " convert_system_message_to_human=True,\n",
- " safety_settings={\n",
- " HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_LOW_AND_ABOVE\n",
- " },\n",
- ")\n",
- "\n",
- "prompt = ChatPromptTemplate(\n",
- " messages=[\n",
- " SystemMessagePromptTemplate.from_template(\n",
- " \"You are a helpful assistant who is good at language translation.\"\n",
- " ),\n",
- " MessagesPlaceholder(variable_name=\"history\"),\n",
- " HumanMessagePromptTemplate.from_template(\"{input}\"),\n",
- " ]\n",
- ")\n",
- "\n",
- "memory = ConversationBufferMemory(memory_key=\"history\", return_messages=True)\n",
- "conversation = ConversationChain(llm=model, prompt=prompt, verbose=True, memory=memory)\n",
- "\n",
- "conversation.invoke(\n",
- " input=\"Translate this sentence from English to French. I love programming.\"\n",
- ")"
- ]
- },
- {
- "cell_type": "code",
- "execution_count": 17,
- "metadata": {
- "id": "AJMP2JrMDmS-",
- "tags": []
- },
- "outputs": [
- {
- "name": "stdout",
- "output_type": "stream",
- "text": [
- "\n",
- "\n",
- "\u001b[1m> Entering new ConversationChain chain...\u001b[0m\n",
- "Prompt after formatting:\n",
- "\u001b[32;1m\u001b[1;3mSystem: You are a helpful assistant who is good at language translation.\n",
- "Human: Translate this sentence from English to French. I love programming.\n",
- "AI: J'adore la programmation.\n",
- "Human: Translate it to Spanish\u001b[0m\n",
- "\n",
- "\u001b[1m> Finished chain.\u001b[0m\n"
- ]
- },
- {
- "data": {
- "text/plain": [
- "{'input': 'Translate it to Spanish',\n",
- " 'history': [HumanMessage(content='Translate this sentence from English to French. I love programming.'),\n",
- " AIMessage(content=\" J'adore la programmation.\"),\n",
- " HumanMessage(content='Translate it to Spanish'),\n",
- " AIMessage(content='Me encanta programar.')],\n",
- " 'response': 'Me encanta programar.'}"
- ]
- },
- "execution_count": 17,
- "metadata": {},
- "output_type": "execute_result"
+ ],
+ "metadata": {
+ "colab": {
+ "name": "intro_gemini_chat.ipynb",
+ "toc_visible": true
+ },
+ "kernelspec": {
+ "display_name": "Python 3",
+ "name": "python3"
}
- ],
- "source": [
- "conversation.invoke(\"Translate it to Spanish\")"
- ]
- }
- ],
- "metadata": {
- "colab": {
- "name": "intro_gemini_chat.ipynb",
- "toc_visible": true
- },
- "environment": {
- "kernel": "conda-root-py",
- "name": "workbench-notebooks.m115",
- "type": "gcloud",
- "uri": "gcr.io/deeplearning-platform-release/workbench-notebooks:m115"
- },
- "kernelspec": {
- "display_name": "Python 3 (ipykernel) (Local)",
- "language": "python",
- "name": "conda-root-py"
},
- "language_info": {
- "codemirror_mode": {
- "name": "ipython",
- "version": 3
- },
- "file_extension": ".py",
- "mimetype": "text/x-python",
- "name": "python",
- "nbconvert_exporter": "python",
- "pygments_lexer": "ipython3",
- "version": "3.10.13"
- }
- },
- "nbformat": 4,
- "nbformat_minor": 4
+ "nbformat": 4,
+ "nbformat_minor": 0
}