|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "acd53f9d", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "# Copyright 2025 Google LLC\n", |
| 11 | + "#\n", |
| 12 | + "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", |
| 13 | + "# you may not use this file except in compliance with the License.\n", |
| 14 | + "# You may obtain a copy of the License at\n", |
| 15 | + "#\n", |
| 16 | + "# https://www.apache.org/licenses/LICENSE-2.0\n", |
| 17 | + "#\n", |
| 18 | + "# Unless required by applicable law or agreed to in writing, software\n", |
| 19 | + "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", |
| 20 | + "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", |
| 21 | + "# See the License for the specific language governing permissions and\n", |
| 22 | + "# limitations under the License." |
| 23 | + ] |
| 24 | + }, |
| 25 | + { |
| 26 | + "cell_type": "markdown", |
| 27 | + "id": "e75ce682", |
| 28 | + "metadata": {}, |
| 29 | + "source": [ |
| 30 | + "# BigQuery DataFrames (BigFrames) AI Functions\n", |
| 31 | + "\n", |
| 32 | + "<table align=\"left\">\n", |
| 33 | + "\n", |
| 34 | + " <td>\n", |
| 35 | + " <a href=\"https://colab.research.google.com/github/googleapis/python-bigquery-dataframes/blob/main/notebooks/generative_ai/ai_functions.ipynb\">\n", |
| 36 | + " <img src=\"https://raw.githubusercontent.com/googleapis/python-bigquery-dataframes/refs/heads/main/third_party/logo/colab-logo.png\" alt=\"Colab logo\"> Run in Colab\n", |
| 37 | + " </a>\n", |
| 38 | + " </td>\n", |
| 39 | + " <td>\n", |
| 40 | + " <a href=\"https://github.com/googleapis/python-bigquery-dataframes/blob/main/notebooks/generative_ai/ai_functions.ipynb\">\n", |
| 41 | + " <img src=\"https://raw.githubusercontent.com/googleapis/python-bigquery-dataframes/refs/heads/main/third_party/logo/github-logo.png\" width=\"32\" alt=\"GitHub logo\">\n", |
| 42 | + " View on GitHub\n", |
| 43 | + " </a>\n", |
| 44 | + " </td>\n", |
| 45 | + " <td>\n", |
| 46 | + " <a href=\"https://console.cloud.google.com/bigquery/import?url=https://github.com/googleapis/python-bigquery-dataframes/blob/main/notebooks/generative_ai/ai_functions.ipynb\">\n", |
| 47 | + " <img src=\"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTW1gvOovVlbZAIZylUtf5Iu8-693qS1w5NJw&s\" alt=\"BQ logo\" width=\"35\">\n", |
| 48 | + " Open in BQ Studio\n", |
| 49 | + " </a>\n", |
| 50 | + " </td>\n", |
| 51 | + "</table>" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "markdown", |
| 56 | + "id": "aee05821", |
| 57 | + "metadata": {}, |
| 58 | + "source": [ |
| 59 | + "This notebooks provides examples on how to use BigFrames AI functions, which are offered under the `bigframes.bigquery` package. " |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "markdown", |
| 64 | + "id": "1232f400", |
| 65 | + "metadata": {}, |
| 66 | + "source": [ |
| 67 | + "## Preparation\n", |
| 68 | + "\n", |
| 69 | + "First, set up your BigFrames environment" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 3, |
| 75 | + "id": "c9f924aa", |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [ |
| 79 | + "import bigframes.pandas as bpd \n", |
| 80 | + "\n", |
| 81 | + "PROJECT_ID = \"bigframes-dev\" # Your project ID here\n", |
| 82 | + "\n", |
| 83 | + "bpd.options.bigquery.project = PROJECT_ID\n", |
| 84 | + "bpd.options.bigquery.ordering_mode = \"partial\"\n", |
| 85 | + "bpd.options.display.progress_bar = None" |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "markdown", |
| 90 | + "id": "e2188773", |
| 91 | + "metadata": {}, |
| 92 | + "source": [ |
| 93 | + "## ai.generate\n", |
| 94 | + "\n", |
| 95 | + "The `ai.generate` function lets you analyze any combination of text and unstructured data from BigQuery. You can mix BigFrames/Pandas series with string literals as your prompt in the form of a tuple. Here is an example:" |
| 96 | + ] |
| 97 | + }, |
| 98 | + { |
| 99 | + "cell_type": "code", |
| 100 | + "execution_count": 5, |
| 101 | + "id": "471a47fe", |
| 102 | + "metadata": {}, |
| 103 | + "outputs": [ |
| 104 | + { |
| 105 | + "data": { |
| 106 | + "text/plain": [ |
| 107 | + "0 {'result': 'Salad\\n', 'full_response': '{\"cand...\n", |
| 108 | + "1 {'result': 'Hotdog\\n', 'full_response': '{\"can...\n", |
| 109 | + "dtype: struct<result: string, full_response: extension<dbjson<JSONArrowType>>, status: string>[pyarrow]" |
| 110 | + ] |
| 111 | + }, |
| 112 | + "execution_count": 5, |
| 113 | + "metadata": {}, |
| 114 | + "output_type": "execute_result" |
| 115 | + } |
| 116 | + ], |
| 117 | + "source": [ |
| 118 | + "import bigframes.bigquery as bbq\n", |
| 119 | + "\n", |
| 120 | + "ingredients1 = bpd.Series([\"Lettuce\", \"Sausage\"])\n", |
| 121 | + "ingredients2 = bpd.Series([\"Cucumber\", \"Long Bread\"])\n", |
| 122 | + "\n", |
| 123 | + "prompt = (\"What's the food made from \", ingredients1, \" and \", ingredients2, \" One word only\")\n", |
| 124 | + "bbq.ai.generate(prompt)" |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "markdown", |
| 129 | + "id": "03953835", |
| 130 | + "metadata": {}, |
| 131 | + "source": [ |
| 132 | + "The function returns a series of structs. The `'result'` field holds the answer, while more metadata can be found in the `'full_response'` field. The `'status'` field tells you whether LLM made a successful response for that specific row. " |
| 133 | + ] |
| 134 | + }, |
| 135 | + { |
| 136 | + "cell_type": "markdown", |
| 137 | + "id": "b606c51f", |
| 138 | + "metadata": {}, |
| 139 | + "source": [ |
| 140 | + "You can also include additional model parameters into your function call, as long as they satisfy the structure of `generateContent` [request body format](https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.endpoints/generateContent#request-body) For example:" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "code", |
| 145 | + "execution_count": null, |
| 146 | + "id": "4a3229a8", |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [ |
| 149 | + { |
| 150 | + "data": { |
| 151 | + "text/plain": [ |
| 152 | + "1 The food made from sausage and long bread is m...\n", |
| 153 | + "0 Lettuce and cucumber are commonly used togethe...\n", |
| 154 | + "Name: result, dtype: string" |
| 155 | + ] |
| 156 | + }, |
| 157 | + "execution_count": 7, |
| 158 | + "metadata": {}, |
| 159 | + "output_type": "execute_result" |
| 160 | + } |
| 161 | + ], |
| 162 | + "source": [ |
| 163 | + "model_params = {\n", |
| 164 | + " \"generationConfig\": {\"maxOutputTokens\": 1}\n", |
| 165 | + "}\n", |
| 166 | + "\n", |
| 167 | + "ingredients1 = bpd.Series([\"Lettuce\", \"Sausage\"])\n", |
| 168 | + "ingredients2 = bpd.Series([\"Cucumber\", \"Long Bread\"])\n", |
| 169 | + "\n", |
| 170 | + "prompt = (\"What's the food made from \", ingredients1, \" and \", ingredients2)\n", |
| 171 | + "bbq.ai.generate(prompt).struct.field(\"result\")" |
| 172 | + ] |
| 173 | + } |
| 174 | + ], |
| 175 | + "metadata": { |
| 176 | + "kernelspec": { |
| 177 | + "display_name": "venv (3.10.17)", |
| 178 | + "language": "python", |
| 179 | + "name": "python3" |
| 180 | + }, |
| 181 | + "language_info": { |
| 182 | + "codemirror_mode": { |
| 183 | + "name": "ipython", |
| 184 | + "version": 3 |
| 185 | + }, |
| 186 | + "file_extension": ".py", |
| 187 | + "mimetype": "text/x-python", |
| 188 | + "name": "python", |
| 189 | + "nbconvert_exporter": "python", |
| 190 | + "pygments_lexer": "ipython3", |
| 191 | + "version": "3.10.17" |
| 192 | + } |
| 193 | + }, |
| 194 | + "nbformat": 4, |
| 195 | + "nbformat_minor": 5 |
| 196 | +} |
0 commit comments