{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 1,
      "metadata": {
        "id": "9A9NkTRTfo2I"
      },
      "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": "8fK_rdvvx1iZ"
      },
      "source": [
        "## Overview\n",
        "\n",
        "## Objective\n",
        "\n",
        "This notebook shows how to conecct BigQuery dataset to  Claude models on Vertex AI using  BigQuery DataFrames.\n",
        "\n",
        "### Claude on Vertex AI\n",
        "\n",
        "Anthropic Claude models on Vertex AI offer fully managed and serverless models. To use a Claude model on Vertex AI, send a request directly to the Vertex AI API endpoint.\n",
        "\n",
        "For more information, see the [Use Claude](https://cloud.devsite.corp.google.com/vertex-ai/generative-ai/docs/third-party-models/use-claude) documentation.\n",
        "\n",
        "### BigQuery DataFrames\n",
        "BigQuery DataFrames provides a Pythonic DataFrame and machine learning (ML) API powered by the BigQuery engine. BigQuery DataFrames is an open-source package.\n",
        "\n",
        "For more information, see this documentation\n",
        "https://cloud.google.com/bigquery/docs/reference/bigquery-dataframes\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "nwYvaaW25jYS"
      },
      "source": [
        "### Getting Started\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hVi8v2mxBkeG"
      },
      "source": [
        "#### Authenticate your notebook environment (Colab only)\n",
        "If you are running this notebook on Google Colab, uncomment and run the following cell to authenticate your environment. This step is not required if you are using [Vertex AI Workbench](https://cloud.google.com/vertex-ai-workbench)."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "OHfMDNI76_Pz"
      },
      "outputs": [],
      "source": [
        "# from google.colab import auth\n",
        "# auth.authenticate_user()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gI3KlxQQ_F_T"
      },
      "source": [
        "## Using Anthropic's Vertex SDK + BQ for *Python*"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "E0x3GO6M_O3_"
      },
      "source": [
        "### Getting Started\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_CJrqUvqAfR7"
      },
      "source": [
        "#### Install the latest bigframes package if bigframes version < 1.15.0\n",
        "\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "collapsed": true,
        "executionInfo": {
          "elapsed": 11539,
          "status": "ok",
          "timestamp": 1724257409246,
          "user": {
            "displayName": "Annie Xu",
            "userId": "11935526703047498014"
          },
          "user_tz": 420
        },
        "id": "fi_HLdat_Pce",
        "outputId": "020149f0-9fe8-45de-f160-abe488c0bed2"
      },
      "outputs": [],
      "source": [
        "# !pip install bigframes --upgrade"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "hUiAYUFbBCpR"
      },
      "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 will restart the current kernel."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "id": "jcqgcj_DBFgt"
      },
      "outputs": [],
      "source": [
        "# # Restart kernel after installs so that your environment can access the new packages\n",
        "# import sys\n",
        "\n",
        "# if \"google.colab\" in sys.modules:\n",
        "#     import IPython\n",
        "\n",
        "#     app = IPython.Application.instance()\n",
        "#     app.kernel.do_shutdown(True)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "shZgRl6qbZYP"
      },
      "source": [
        "#### Define Google Cloud project and region information"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 5,
      "metadata": {
        "id": "JZLqMJ6va9fc"
      },
      "outputs": [],
      "source": [
        "# Input your project id\n",
        "PROJECT_ID = \"bigframes-dev\"  # @param {type:\"string\"}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "czcmJpKPBMVC"
      },
      "source": [
        "#### Select Claude Model and Region Availability:\n",
        "https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude#anthropic_claude_quotas_and_supported_context_length"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 6,
      "metadata": {
        "id": "wIBkGcFkK0Ci"
      },
      "outputs": [],
      "source": [
        "REGION = \"us-east5\"  # @param {type:\"string\"}"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "F3UmCLerH0t0"
      },
      "source": [
        "### Load raw sample data to a bigquery dataset\n",
        "\n",
        "Create a BigQuery Dataset and table. You can use the sample museum data in CSV from [here](https://github.com/googleapis/python-bigquery-dataframes/tree/main/notebooks/generative_ai/museum_art.csv).\n",
        "\n",
        "The dataset should be in the **same region** as your chosen claude model. Let's say you selected us-east5 for claude 'haiku', then load the sample data to a dataset in us-east5."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "gijJ2vr5B5nV"
      },
      "source": [
        "### Text generation for BQ Tables using Python BigFrames\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 1000
        },
        "collapsed": true,
        "executionInfo": {
          "elapsed": 756,
          "status": "ok",
          "timestamp": 1724260427446,
          "user": {
            "displayName": "Annie Xu",
            "userId": "11935526703047498014"
          },
          "user_tz": 420
        },
        "id": "cU3Gq7TqHFdi",
        "outputId": "aa5ec159-a91b-4349-e56a-400e90935edc"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "object_number                             string[pyarrow]\n",
              "is_highlight                                      boolean\n",
              "is_public_domain                                  boolean\n",
              "object_id                                           Int64\n",
              "department                                string[pyarrow]\n",
              "object_name                               string[pyarrow]\n",
              "title                                     string[pyarrow]\n",
              "culture                                   string[pyarrow]\n",
              "period                                    string[pyarrow]\n",
              "dynasty                                   string[pyarrow]\n",
              "reign                                     string[pyarrow]\n",
              "portfolio                                 string[pyarrow]\n",
              "artist_role                               string[pyarrow]\n",
              "artist_prefix                             string[pyarrow]\n",
              "artist_display_name                       string[pyarrow]\n",
              "artist_display_bio                        string[pyarrow]\n",
              "artist_suffix                             string[pyarrow]\n",
              "artist_alpha_sort                         string[pyarrow]\n",
              "artist_nationality                        string[pyarrow]\n",
              "artist_begin_date                         string[pyarrow]\n",
              "artist_end_date                           string[pyarrow]\n",
              "object_date                               string[pyarrow]\n",
              "object_begin_date                                   Int64\n",
              "object_end_date                                     Int64\n",
              "medium                                    string[pyarrow]\n",
              "dimensions                                string[pyarrow]\n",
              "credit_line                               string[pyarrow]\n",
              "geography_type                            string[pyarrow]\n",
              "city                                      string[pyarrow]\n",
              "state                                     string[pyarrow]\n",
              "county                                    string[pyarrow]\n",
              "country                                   string[pyarrow]\n",
              "region                                    string[pyarrow]\n",
              "subregion                                 string[pyarrow]\n",
              "locale                                    string[pyarrow]\n",
              "locus                                     string[pyarrow]\n",
              "excavation                                string[pyarrow]\n",
              "river                                     string[pyarrow]\n",
              "classification                            string[pyarrow]\n",
              "rights_and_reproduction                   string[pyarrow]\n",
              "link_resource                             string[pyarrow]\n",
              "metadata_date              timestamp[us, tz=UTC][pyarrow]\n",
              "repository                                string[pyarrow]\n",
              "dtype: object"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "import bigframes\n",
        "import bigframes.pandas as bpd\n",
        "bigframes.options._bigquery_options.project = PROJECT_ID # replace to user project\n",
        "bigframes.options._bigquery_options.location = REGION  #choice a region which the claude model you choice allows\n",
        "df = bpd.read_gbq(\"bigframes-dev.garrettwu_us_east5.museum_art\") # replace with your table\n",
        "df.dtypes"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 461
        },
        "executionInfo": {
          "elapsed": 4568,
          "status": "ok",
          "timestamp": 1724271168583,
          "user": {
            "displayName": "Annie Xu",
            "userId": "11935526703047498014"
          },
          "user_tz": 420
        },
        "id": "exWNXEzLHHaU",
        "outputId": "1b33b64c-c8bd-42e6-ecc3-0ea0b5e492be"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "Query job 1998408a-4e29-4381-9229-cf8585a47dbe is DONE. 7.7 MB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:1998408a-4e29-4381-9229-cf8585a47dbe&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "Query job 817a5321-9852-45da-8b14-004affc20c38 is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:817a5321-9852-45da-8b14-004affc20c38&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "Query job 36aa1b30-acb5-4188-8377-b9f544443db8 is DONE. 955 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:36aa1b30-acb5-4188-8377-b9f544443db8&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>object_id</th>\n",
              "      <th>title</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>285844</td>\n",
              "      <td>Addie Card, 12 years. Spinner in North Pownal ...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>437141</td>\n",
              "      <td>Portrait of a Man</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>670650</td>\n",
              "      <td>[Snow Crystal]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>268450</td>\n",
              "      <td>Newhaven Fisherman</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>646996</td>\n",
              "      <td>전(傳) 오원 장승업 (1843–1897) 청동기와 화초가 있는 정물화 조선|傳 吾...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>287958</td>\n",
              "      <td>Bridge of Augustus at Nani</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>435869</td>\n",
              "      <td>Antoine Dominique Sauveur Aubert (born 1817), ...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>55834</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>45087</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>56883</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>10 rows × 2 columns</p>\n",
              "</div>[10 rows x 2 columns in total]"
            ],
            "text/plain": [
              "   object_id                                              title\n",
              "0     285844  Addie Card, 12 years. Spinner in North Pownal ...\n",
              "1     437141                                  Portrait of a Man\n",
              "2     670650                                     [Snow Crystal]\n",
              "3     268450                                 Newhaven Fisherman\n",
              "4     646996  전(傳) 오원 장승업 (1843–1897) 청동기와 화초가 있는 정물화 조선|傳 吾...\n",
              "5     287958                         Bridge of Augustus at Nani\n",
              "6     435869  Antoine Dominique Sauveur Aubert (born 1817), ...\n",
              "7      55834                                               <NA>\n",
              "8      45087                                               <NA>\n",
              "9      56883                                               <NA>\n",
              "\n",
              "[10 rows x 2 columns]"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# @title query: select top 10 records from table and put into dataframe\n",
        "\n",
        "df = df[[\"object_id\", \"title\"]].head(10)\n",
        "df"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_UZNsP_WDlyr"
      },
      "source": [
        "### Enable Claude model on Vertex AI and Create a BQ External Model Connection\n",
        "\n",
        "\n",
        "*   Step 1: Visit the Vertex AI Model Garden console and select the model tile for Claude model of your choice. Following this doc [link](https://cloud.google.com/vertex-ai/generative-ai/docs/partner-models/use-claude). Click on the **“Enable”** button and follow the instructions.\n",
        "\n",
        "*   Step 2: Create a BQ External Connection\n",
        "Follow the same process like this one: [link](https://cloud.google.com/bigquery/docs/generate-text#create_a_connection). Pay attention to the **supported region** of Claude models and make your conenction follow the same region for example us-east5 for Claude 3.5.\n",
        "\n",
        "\n",
        "\n",
        "\n",
        "\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "8Q4aff5m9QOn"
      },
      "source": [
        "### Use BigQuery DataFrames ML package with Claude LLM  \n",
        "\n",
        "In this example, we are using the Claude3TextGenerator class from BigQuery DataFrames to translate title of art piece to english.\n",
        "\n",
        "Documentation for the Claude3TextGenerator Class: https://cloud.google.com/python/docs/reference/bigframes/latest/bigframes.ml.llm.Claude3TextGenerator"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 10,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 513
        },
        "executionInfo": {
          "elapsed": 25662,
          "status": "ok",
          "timestamp": 1724271197922,
          "user": {
            "displayName": "Annie Xu",
            "userId": "11935526703047498014"
          },
          "user_tz": 420
        },
        "id": "1pdyI5KBTyTD",
        "outputId": "8f1e976b-1fd0-49ba-e068-f480eafb1765"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "Query job 514f5afe-15e0-4474-9e09-fbf94f0fe8ca is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:514f5afe-15e0-4474-9e09-fbf94f0fe8ca&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "Query job 5d4df544-e8a4-42f3-8a94-5f7e79b23562 is DONE. 635 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:5d4df544-e8a4-42f3-8a94-5f7e79b23562&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "Query job 25288d94-b10c-4b39-a272-3969ccb19af3 is DONE. 14 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:25288d94-b10c-4b39-a272-3969ccb19af3&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "Query job d5693878-1037-4798-8aa0-f568ec0be9e3 is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:d5693878-1037-4798-8aa0-f568ec0be9e3&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "Query job 54080328-ba8b-4715-bf2b-3e5b7affa90b is DONE. 4.0 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:54080328-ba8b-4715-bf2b-3e5b7affa90b&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>ml_generate_text_llm_result</th>\n",
              "      <th>ml_generate_text_status</th>\n",
              "      <th>prompt</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>This text is already in English. It appears to...</td>\n",
              "      <td></td>\n",
              "      <td>translate this into English: Addie Card, 12 ye...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>The phrase \"Portrait of a Man\" is already in E...</td>\n",
              "      <td></td>\n",
              "      <td>translate this into English: Portrait of a Man</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>The phrase \"[Snow Crystal]\" is already in Engl...</td>\n",
              "      <td></td>\n",
              "      <td>translate this into English: [Snow Crystal]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>The phrase \"Newhaven Fisherman\" is already in ...</td>\n",
              "      <td></td>\n",
              "      <td>translate this into English: Newhaven Fisherman</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>Here's the English translation:\n",
              "\n",
              "\"Attributed t...</td>\n",
              "      <td></td>\n",
              "      <td>translate this into English: 전(傳) 오원 장승업 (1843...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>I apologize, but I'm not sure which language \"...</td>\n",
              "      <td></td>\n",
              "      <td>translate this into English: Bridge of Augustu...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>This title is already in English. It describes...</td>\n",
              "      <td></td>\n",
              "      <td>translate this into English: Antoine Dominique...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>10 rows × 3 columns</p>\n",
              "</div>[10 rows x 3 columns in total]"
            ],
            "text/plain": [
              "                         ml_generate_text_llm_result ml_generate_text_status  \\\n",
              "0  This text is already in English. It appears to...                           \n",
              "1  The phrase \"Portrait of a Man\" is already in E...                           \n",
              "2  The phrase \"[Snow Crystal]\" is already in Engl...                           \n",
              "3  The phrase \"Newhaven Fisherman\" is already in ...                           \n",
              "4  Here's the English translation:\n",
              "\n",
              "\"Attributed t...                           \n",
              "5  I apologize, but I'm not sure which language \"...                           \n",
              "6  This title is already in English. It describes...                           \n",
              "7                                               <NA>                    <NA>   \n",
              "8                                               <NA>                    <NA>   \n",
              "9                                               <NA>                    <NA>   \n",
              "\n",
              "                                              prompt  \n",
              "0  translate this into English: Addie Card, 12 ye...  \n",
              "1     translate this into English: Portrait of a Man  \n",
              "2        translate this into English: [Snow Crystal]  \n",
              "3    translate this into English: Newhaven Fisherman  \n",
              "4  translate this into English: 전(傳) 오원 장승업 (1843...  \n",
              "5  translate this into English: Bridge of Augustu...  \n",
              "6  translate this into English: Antoine Dominique...  \n",
              "7                                               <NA>  \n",
              "8                                               <NA>  \n",
              "9                                               <NA>  \n",
              "\n",
              "[10 rows x 3 columns]"
            ]
          },
          "execution_count": 10,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "from bigframes.ml import llm\n",
        "model = llm.Claude3TextGenerator(model_name=\"claude-3-5-sonnet\",\n",
        "                                 connection_name=\"bigframes-dev.us-east5.bigframes-rf-conn\" ) # replace with your connection\n",
        "df[\"input_prompt\"] = \"translate this into English: \" + df[\"title\"]\n",
        "result = model.predict(df[\"input_prompt\"])\n",
        "result"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 11,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 461
        },
        "executionInfo": {
          "elapsed": 5249,
          "status": "ok",
          "timestamp": 1724274172557,
          "user": {
            "displayName": "Annie Xu",
            "userId": "11935526703047498014"
          },
          "user_tz": 420
        },
        "id": "Ux1VI5qujHOB",
        "outputId": "7b859943-5e7c-4cc0-d9c2-bb3d44682010"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "Query job 6b6eceaa-e713-493e-beac-481a3d777a5c is DONE. 4.9 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:6b6eceaa-e713-493e-beac-481a3d777a5c&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "Query job 5c660da9-318c-424e-9412-43f09e44a8b3 is DONE. 0 Bytes processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:5c660da9-318c-424e-9412-43f09e44a8b3&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "Query job 82b61007-8370-4514-addb-258d7c48d66c is DONE. 4.9 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:82b61007-8370-4514-addb-258d7c48d66c&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>object_id</th>\n",
              "      <th>title</th>\n",
              "      <th>ml_generate_text_llm_result</th>\n",
              "      <th>prompt</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>285844</td>\n",
              "      <td>Addie Card, 12 years. Spinner in North Pownal ...</td>\n",
              "      <td>This text is already in English. It appears to...</td>\n",
              "      <td>translate this into English: Addie Card, 12 ye...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>437141</td>\n",
              "      <td>Portrait of a Man</td>\n",
              "      <td>The phrase \"Portrait of a Man\" is already in E...</td>\n",
              "      <td>translate this into English: Portrait of a Man</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>2</th>\n",
              "      <td>670650</td>\n",
              "      <td>[Snow Crystal]</td>\n",
              "      <td>The phrase \"[Snow Crystal]\" is already in Engl...</td>\n",
              "      <td>translate this into English: [Snow Crystal]</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>3</th>\n",
              "      <td>268450</td>\n",
              "      <td>Newhaven Fisherman</td>\n",
              "      <td>The phrase \"Newhaven Fisherman\" is already in ...</td>\n",
              "      <td>translate this into English: Newhaven Fisherman</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>4</th>\n",
              "      <td>646996</td>\n",
              "      <td>전(傳) 오원 장승업 (1843–1897) 청동기와 화초가 있는 정물화 조선|傳 吾...</td>\n",
              "      <td>Here's the English translation:\n",
              "\n",
              "\"Attributed t...</td>\n",
              "      <td>translate this into English: 전(傳) 오원 장승업 (1843...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>5</th>\n",
              "      <td>287958</td>\n",
              "      <td>Bridge of Augustus at Nani</td>\n",
              "      <td>I apologize, but I'm not sure which language \"...</td>\n",
              "      <td>translate this into English: Bridge of Augustu...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>6</th>\n",
              "      <td>435869</td>\n",
              "      <td>Antoine Dominique Sauveur Aubert (born 1817), ...</td>\n",
              "      <td>This title is already in English. It describes...</td>\n",
              "      <td>translate this into English: Antoine Dominique...</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>7</th>\n",
              "      <td>55834</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>8</th>\n",
              "      <td>45087</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>9</th>\n",
              "      <td>56883</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "      <td>&lt;NA&gt;</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "<p>10 rows × 4 columns</p>\n",
              "</div>[10 rows x 4 columns in total]"
            ],
            "text/plain": [
              "   object_id                                              title  \\\n",
              "0     285844  Addie Card, 12 years. Spinner in North Pownal ...   \n",
              "1     437141                                  Portrait of a Man   \n",
              "2     670650                                     [Snow Crystal]   \n",
              "3     268450                                 Newhaven Fisherman   \n",
              "4     646996  전(傳) 오원 장승업 (1843–1897) 청동기와 화초가 있는 정물화 조선|傳 吾...   \n",
              "5     287958                         Bridge of Augustus at Nani   \n",
              "6     435869  Antoine Dominique Sauveur Aubert (born 1817), ...   \n",
              "7      55834                                               <NA>   \n",
              "8      45087                                               <NA>   \n",
              "9      56883                                               <NA>   \n",
              "\n",
              "                         ml_generate_text_llm_result  \\\n",
              "0  This text is already in English. It appears to...   \n",
              "1  The phrase \"Portrait of a Man\" is already in E...   \n",
              "2  The phrase \"[Snow Crystal]\" is already in Engl...   \n",
              "3  The phrase \"Newhaven Fisherman\" is already in ...   \n",
              "4  Here's the English translation:\n",
              "\n",
              "\"Attributed t...   \n",
              "5  I apologize, but I'm not sure which language \"...   \n",
              "6  This title is already in English. It describes...   \n",
              "7                                               <NA>   \n",
              "8                                               <NA>   \n",
              "9                                               <NA>   \n",
              "\n",
              "                                              prompt  \n",
              "0  translate this into English: Addie Card, 12 ye...  \n",
              "1     translate this into English: Portrait of a Man  \n",
              "2        translate this into English: [Snow Crystal]  \n",
              "3    translate this into English: Newhaven Fisherman  \n",
              "4  translate this into English: 전(傳) 오원 장승업 (1843...  \n",
              "5  translate this into English: Bridge of Augustu...  \n",
              "6  translate this into English: Antoine Dominique...  \n",
              "7                                               <NA>  \n",
              "8                                               <NA>  \n",
              "9                                               <NA>  \n",
              "\n",
              "[10 rows x 4 columns]"
            ]
          },
          "execution_count": 11,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "output_df=df.drop(columns=[\"input_prompt\"]).join(result.drop(columns=\"ml_generate_text_status\"))\n",
        "output_df"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 12,
      "metadata": {
        "id": "ej70vFMvelsg"
      },
      "outputs": [
        {
          "data": {
            "text/html": [
              "Query job 8c3f1d21-9033-4224-b6f3-4f2414f4ed18 is DONE. 4.5 kB processed. <a target=\"_blank\" href=\"https://console.cloud.google.com/bigquery?project=bigframes-dev&j=bq:us-east5:8c3f1d21-9033-4224-b6f3-4f2414f4ed18&page=queryresults\">Open Job</a>"
            ],
            "text/plain": [
              "<IPython.core.display.HTML object>"
            ]
          },
          "metadata": {},
          "output_type": "display_data"
        },
        {
          "data": {
            "text/plain": [
              "'bigframes-dev.garrettwu_us_east5.museum_art_translate'"
            ]
          },
          "execution_count": 12,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# prompt: load the dataframe output to another Bigquery table\n",
        "\n",
        "# @title Save results to BigQuery\n",
        "\n",
        "output_df.to_gbq(\"bigframes-dev.garrettwu_us_east5.museum_art_translate\", if_exists=\"replace\") # replace with your table"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": []
    }
  ],
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "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.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
