mirror of
https://github.com/GoogleCloudPlatform/vertex-ai-samples.git
synced 2026-09-26 14:42:04 +00:00
refactor(egen): follows new template+sentence and other minor corrections (#3073)
* chore: restructures according to template, contracts text and cells, organizes headings * refactor: removes the IS_TESTING conditions for steps involving redis instance * chore: addresses review comments * fix: adds back the IS_TESTING conditions for redis commands to skip in the test environment * chore: removes duplicate comment --------- Co-authored-by: krishr2d2 <krishna.movva@egen.ai>
This commit is contained in:
co-authored by
krishr2d2
parent
7e8bf8a126
commit
bd10dede25
+195
-288
@@ -30,23 +30,26 @@
|
||||
},
|
||||
"source": [
|
||||
"# Using Vertex AI Vector Search for StackOverflow Questions\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"<table align=\"left\">\n",
|
||||
" <td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://colab.research.google.com/github/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/matching_engine/sdk_matching_engine_create_stack_overflow_embeddings.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Colab logo\"> Run in Colab\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/colab-logo-32px.png\" alt=\"Google Colaboratory logo\"><br> Open in Colab\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/matching_engine/sdk_matching_engine_create_stack_overflow_embeddings.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\">\n",
|
||||
" View on GitHub\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fvertex-ai-samples%2Fmain%2Fnotebooks%2Fofficial%2Fmatching_engine%2Fsdk_matching_engine_create_stack_overflow_embeddings.ipynb\">\n",
|
||||
" <img width=\"32px\" src=\"https://cloud.google.com/ml-engine/images/colab-enterprise-logo-32px.png\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td>\n",
|
||||
" </td> \n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/vertex-ai-samples/main/notebooks/official/matching_engine/sdk_matching_engine_create_stack_overflow_embeddings.ipynb\">\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\">\n",
|
||||
" Open in Vertex AI Workbench\n",
|
||||
" <img src=\"https://lh3.googleusercontent.com/UiNooY4LUgW_oTvpsNhPpQzsstV5W8F7rYgxgGBD85cWJoLmrOzhVs_ksK_vgx40SHs7jCqkTkCk=e14-rj-sc0xffffff-h130-w32\" alt=\"Vertex AI logo\"><br> Open in Workbench\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
" <td style=\"text-align: center\">\n",
|
||||
" <a href=\"https://github.com/GoogleCloudPlatform/vertex-ai-samples/blob/main/notebooks/official/matching_engine/sdk_matching_engine_create_stack_overflow_embeddings.ipynb\">\n",
|
||||
" <img src=\"https://cloud.google.com/ml-engine/images/github-logo-32px.png\" alt=\"GitHub logo\"><br> View on GitHub\n",
|
||||
" </a>\n",
|
||||
" </td>\n",
|
||||
"</table>"
|
||||
@@ -102,15 +105,22 @@
|
||||
"> Stack Overflow is the largest online community for programmers to learn, share their knowledge, and advance their careers. Updated on a quarterly basis, this BigQuery dataset includes an archive of Stack Overflow content, including posts, votes, tags, and badges. This dataset is updated to mirror the Stack Overflow content on the Internet Archive, and is also available through the Stack Exchange Data Explorer."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "d0be1c1c229a"
|
||||
},
|
||||
"source": [
|
||||
"## Get started"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "f0f1bea346db"
|
||||
},
|
||||
"source": [
|
||||
"## Installation\n",
|
||||
"\n",
|
||||
"Install the latest version of Cloud Storage, BigQuery, and the Vertex AI SDK for Python."
|
||||
"### Install Vertex AI SDK for Python and other required packages\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -121,54 +131,19 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Install the packages\n",
|
||||
"! pip3 install --upgrade google-cloud-aiplatform==1.35.0 \\\n",
|
||||
"# Install the google-cloud packages\n",
|
||||
"! pip3 install --upgrade google-cloud-aiplatform \\\n",
|
||||
" google-cloud-storage \\\n",
|
||||
" 'google-cloud-bigquery[pandas]'"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "1ae34c2a9ce7"
|
||||
},
|
||||
"source": [
|
||||
"Install the latest version of tensorflow and tensorflow_text to encode embeddings"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "f329eff0fe39"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Install the packages\n",
|
||||
" 'google-cloud-bigquery[pandas]' -q\n",
|
||||
"\n",
|
||||
"# Install the latest version of tensorflow packages\n",
|
||||
"! pip3 install --upgrade tensorflow \\\n",
|
||||
" tensorflow_text \\\n",
|
||||
" tensorflow-hub"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "54ac7ebac10b"
|
||||
},
|
||||
"source": [
|
||||
"Install the latest version of Redis for low-latency data retrieval"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "e3dd53b3c06c"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Install the redis package\n",
|
||||
"! pip install --upgrade redis"
|
||||
" tensorflow-hub -q\n",
|
||||
"\n",
|
||||
"# Install the redis and tqdm packages\n",
|
||||
"! pip install --upgrade redis \\\n",
|
||||
" tqdm -q"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -177,7 +152,9 @@
|
||||
"id": "5b08ba354c6e"
|
||||
},
|
||||
"source": [
|
||||
"### Colab only: Uncomment the following cell to restart the kernel."
|
||||
"### Restart runtime (Colab only)\n",
|
||||
"\n",
|
||||
"To use the newly installed packages, you must restart the runtime on Google Colab."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -188,11 +165,53 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# import IPython\n",
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"# app = IPython.Application.instance()\n",
|
||||
"# app.kernel.do_shutdown(True)"
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" import IPython\n",
|
||||
"\n",
|
||||
" app = IPython.Application.instance()\n",
|
||||
" app.kernel.do_shutdown(True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "ffcde4d56c00"
|
||||
},
|
||||
"source": [
|
||||
"<div class=\"alert alert-block alert-warning\">\n",
|
||||
"<b>⚠️ The kernel is going to restart. Wait until it's finished before continuing to the next step. ⚠️</b>\n",
|
||||
"</div>\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "7176ea64999b"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your notebook environment (Colab only)\n",
|
||||
"\n",
|
||||
"Authenticate your environment on Google Colab.\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "7de6ef0fac42"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import sys\n",
|
||||
"\n",
|
||||
"if \"google.colab\" in sys.modules:\n",
|
||||
"\n",
|
||||
" from google.colab import auth\n",
|
||||
"\n",
|
||||
" auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -201,13 +220,9 @@
|
||||
"id": "dd28c9e4f067"
|
||||
},
|
||||
"source": [
|
||||
"## Before you begin\n",
|
||||
"#### Set your project ID\n",
|
||||
"### Set Google Cloud project information\n",
|
||||
"\n",
|
||||
"If you don't know your project ID, try the following:\n",
|
||||
"* Run `gcloud config list`.\n",
|
||||
"* Run `gcloud projects list`.\n",
|
||||
"* See the support page: [Locate the project ID](https://support.google.com/googleapi/answer/7014113)"
|
||||
"Learn more about how to [set up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -218,137 +233,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"PROJECT_ID = \"[your-project-id]\"\n",
|
||||
"\n",
|
||||
"# Set the project id\n",
|
||||
"! gcloud config set project {PROJECT_ID}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "4f4512bf63b3"
|
||||
},
|
||||
"source": [
|
||||
"#### Region\n",
|
||||
"\n",
|
||||
"You can also change the `REGION` variable used by Vertex AI. Learn more about [Vertex AI regions](https://cloud.google.com/vertex-ai/docs/general/locations)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "474be5183c27"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"REGION = \"us-central1\" # @param {type: \"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "949271bfebe3"
|
||||
},
|
||||
"source": [
|
||||
"### Authenticate your Google Cloud account\n",
|
||||
"\n",
|
||||
"Depending on your Jupyter environment, you may have to manually authenticate. Follow the relevant instructions below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "b65b4ce80d9a"
|
||||
},
|
||||
"source": [
|
||||
"**1. Vertex AI Workbench**\n",
|
||||
"* Do nothing since you're already authenticated."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "985cdbfe7372"
|
||||
},
|
||||
"source": [
|
||||
"**2. Local JupyterLab instance, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "fbc9cd30cc4b"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# ! gcloud auth login"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "79efab26ad02"
|
||||
},
|
||||
"source": [
|
||||
"**3. Colab, uncomment and run:**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "a336a05c6149"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# from google.colab import auth\n",
|
||||
"# auth.authenticate_user()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "0c0a44fa330f"
|
||||
},
|
||||
"source": [
|
||||
"**4. Service account or other**\n",
|
||||
"* See how to grant Cloud Storage permissions to your service account at https://cloud.google.com/storage/docs/gsutil/commands/iam#ch-examples."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "d3uj8x73nDX_"
|
||||
},
|
||||
"source": [
|
||||
"* Authentication: Rerun the `gcloud auth login` command in the Vertex AI Workbench notebook terminal when you are logged out and need the credential again."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "hhq5zEbGg0XX"
|
||||
},
|
||||
"source": [
|
||||
"### Colab only: Uncomment the following cell to restart the kernel."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "EzrelQZ22IZj"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Automatically restart kernel after installs so that your environment can access the new packages\n",
|
||||
"# import IPython\n",
|
||||
"\n",
|
||||
"# app = IPython.Application.instance()\n",
|
||||
"# app.kernel.do_shutdown(True)"
|
||||
"PROJECT_ID = \"[your-project-id]\" # @param {type:\"string\"}\n",
|
||||
"LOCATION = \"us-central1\" # @param {type:\"string\"}"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -390,7 +276,63 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"! gsutil mb -l {REGION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
"! gsutil mb -l {LOCATION} -p {PROJECT_ID} {BUCKET_URI}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "e45533d84985"
|
||||
},
|
||||
"source": [
|
||||
"### Initialize Vertex AI SDK for Python\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). "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "b519c49740cb"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=LOCATION, staging_bucket=BUCKET_URI)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "44b8dbd3f108"
|
||||
},
|
||||
"source": [
|
||||
"### Import the required libraries"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "bf6f53cd87ee"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"import os\n",
|
||||
"import tempfile\n",
|
||||
"from typing import List\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"import redis\n",
|
||||
"import tensorflow as tf\n",
|
||||
"import tensorflow_hub as hub\n",
|
||||
"# Registers the ops.\n",
|
||||
"import tensorflow_text as text # noqa: F401\n",
|
||||
"from google.cloud import bigquery\n",
|
||||
"from tqdm.auto import tqdm"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -401,9 +343,18 @@
|
||||
"source": [
|
||||
"## Prepare the data\n",
|
||||
"\n",
|
||||
"You'll use the [Stack Overflow dataset](https://console.cloud.google.com/marketplace/product/stack-exchange/stack-overflow) of question and answers hosted on BigQuery.\n",
|
||||
"For this tutorial, use the [Stack Overflow dataset](https://console.cloud.google.com/marketplace/product/stack-exchange/stack-overflow) of question and answers hosted on BigQuery.\n",
|
||||
"\n",
|
||||
"> This public dataset is hosted in Google BigQuery and is included in BigQuery's 1TB/mo of free tier processing. This means that each user receives 1TB of free BigQuery processing every month, which can be used to run queries on this public dataset."
|
||||
"> This public dataset is hosted in Google BigQuery and is included in BigQuery's 1TB/mo of free tier processing. Each user receives 1TB of free BigQuery processing every month, which can be used to run queries on this public dataset."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"id": "62f6feea28a2"
|
||||
},
|
||||
"source": [
|
||||
"Fetch the dataset from the BigQuery source."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -415,8 +366,6 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%%time\n",
|
||||
"from google.cloud import bigquery\n",
|
||||
"\n",
|
||||
"client = bigquery.Client(project=PROJECT_ID)\n",
|
||||
"\n",
|
||||
"NUM_ROWS = 1000\n",
|
||||
@@ -470,13 +419,13 @@
|
||||
"id": "1124422cc200"
|
||||
},
|
||||
"source": [
|
||||
"#### Instantiate the text encoding model\n",
|
||||
"### Instantiate the text encoding model\n",
|
||||
"\n",
|
||||
"Use the [sentence-t5 encoder](https://tfhub.dev/google/sentence-t5/st5-base/1) developed by Google for converting text to embeddings.\n",
|
||||
"\n",
|
||||
"> The sentence-T5 family of models encode text into high-dimensional vectors that can be used for text classification, semantic similarity, clustering and other natural language processing tasks.\n",
|
||||
">\n",
|
||||
"> Our model is built on top of T5 (i.e. the Text-To-Text Transfer Transformer). It is trained on a variety of data sources and initialized from pre-trained T5 models with different model sizes. The input is variable-length English text and the output is a 768-dimensional vector. The sentence-T5 base model employs a 12-layer transformer architecture as the T5 base model does."
|
||||
"> The model is built on top of T5 (i.e., the Text-To-Text Transfer Transformer). It's trained on a variety of data sources and initialized from pre-trained T5 models with different model sizes. The input is variable-length English text and the output is a 768-dimensional vector. The sentence-T5 base model employs a 12-layer transformer architecture as does the T5 base model."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -487,11 +436,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tensorflow as tf\n",
|
||||
"import tensorflow_hub as hub\n",
|
||||
"# Registers the ops.\n",
|
||||
"import tensorflow_text as text # noqa: F401\n",
|
||||
"\n",
|
||||
"hub_url = \"https://tfhub.dev/google/sentence-t5/st5-base/1\"\n",
|
||||
"\n",
|
||||
"encoder = hub.KerasLayer(hub_url)"
|
||||
@@ -503,7 +447,7 @@
|
||||
"id": "43088937e820"
|
||||
},
|
||||
"source": [
|
||||
"#### Defining an encoding function\n",
|
||||
"### Define an encoding function\n",
|
||||
"\n",
|
||||
"Define a function, to be used later, that takes sentences and converts them to embeddings."
|
||||
]
|
||||
@@ -516,12 +460,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from typing import List\n",
|
||||
"\n",
|
||||
"import numpy as np\n",
|
||||
"from tqdm.auto import tqdm\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def encode_text_to_embedding(\n",
|
||||
" text_encoder: hub.KerasLayer, sentences: List[str], batch_size: int = 100\n",
|
||||
") -> np.ndarray:\n",
|
||||
@@ -595,6 +533,7 @@
|
||||
"source": [
|
||||
"question_index = 0\n",
|
||||
"\n",
|
||||
"# Print the query question\n",
|
||||
"print(f\"Query question = {questions[question_index]}\")\n",
|
||||
"scores = np.dot(question_embeddings[question_index], question_embeddings.T)\n",
|
||||
"\n",
|
||||
@@ -611,11 +550,11 @@
|
||||
"id": "aQIQSyF9GtSv"
|
||||
},
|
||||
"source": [
|
||||
"#### Save the train split in JSONL format.\n",
|
||||
"### Save the train split in JSONL format.\n",
|
||||
"\n",
|
||||
"The data must be formatted in JSONL format, which means each embedding dictionary is written as a JSON string on its own line.\n",
|
||||
"\n",
|
||||
"See more information in the docs at [Input data format and structure](https://cloud.google.com/vertex-ai/docs/matching-engine/match-eng-setup#input-data-format)."
|
||||
"See more information in the docs for [input data format and structure](https://cloud.google.com/vertex-ai/docs/matching-engine/match-eng-setup#input-data-format)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -626,8 +565,6 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import tempfile\n",
|
||||
"\n",
|
||||
"# Create temporary file to write embeddings to\n",
|
||||
"embeddings_file = tempfile.NamedTemporaryFile(suffix=\".json\", delete=False)\n",
|
||||
"\n",
|
||||
@@ -642,10 +579,10 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import json\n",
|
||||
"\n",
|
||||
"# Set batch size\n",
|
||||
"BATCH_SIZE = 100\n",
|
||||
"\n",
|
||||
"# Create embeddings and write to a file\n",
|
||||
"with open(embeddings_file.name, \"a\") as f:\n",
|
||||
" for i in tqdm(range(0, len(questions), BATCH_SIZE)):\n",
|
||||
" id_chunk = ids[i : i + BATCH_SIZE]\n",
|
||||
@@ -728,20 +665,7 @@
|
||||
"source": [
|
||||
"Create the ANN index configuration:\n",
|
||||
"\n",
|
||||
"To learn more about configuring the index, see [Input data format and structure](https://cloud.google.com/vertex-ai/docs/vector-search/setup/setup).\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "Y4zooldkGoM4"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from google.cloud import aiplatform\n",
|
||||
"\n",
|
||||
"aiplatform.init(project=PROJECT_ID, location=REGION, staging_bucket=BUCKET_URI)"
|
||||
"To learn more about configuring the index, see [Vector Search input data format and structure](https://cloud.google.com/vertex-ai/docs/vector-search/setup/setup).\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -782,7 +706,7 @@
|
||||
"id": "0f1a9fbecabb"
|
||||
},
|
||||
"source": [
|
||||
"Using the resource name, you can retrieve an existing MatchingEngineIndex."
|
||||
"Using the resource name, you can retrieve an existing MatchingEngineIndex resource."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -804,7 +728,7 @@
|
||||
"source": [
|
||||
"## Setup VPC peering network\n",
|
||||
"\n",
|
||||
"To use a Vector Search index, you setup a VPC peering network between your project and the Vertex AI Vector Search service project. This eliminates additional hops in network traffic and allows using efficient gRPC protocol.\n",
|
||||
"To use a Vector Search index, set up a VPC peering network between your project and the Vertex AI Vector Search service project. This eliminates additional hops in network traffic and allows using efficient gRPC protocol.\n",
|
||||
"\n",
|
||||
"Learn more about [VPC peering](https://cloud.google.com/vertex-ai/docs/general/vpc-peering).\n",
|
||||
"\n",
|
||||
@@ -819,9 +743,9 @@
|
||||
"source": [
|
||||
"### Create VPC peering\n",
|
||||
"\n",
|
||||
"For simplicity, we setup VPC peering to the ucaip-haystack-vpc-network network. You can create a different network for your project.\n",
|
||||
"For simplicity, set up VPC peering to the `ucaip-haystack-vpc-network` network. You can create a different network for your project.\n",
|
||||
"\n",
|
||||
"If you setup VPC peering with any other network, make sure that the network already exists and that your VM is running on that network."
|
||||
"If you set up VPC peering with any other network, make sure that the network already exists and that your VM is running on that network."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -833,10 +757,10 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# This is for display only; you can name the range anything.\n",
|
||||
"NETWORK = \"ucaip-haystack-vpc-network\" # @param {type:\"string\"}\n",
|
||||
"PEERING_RANGE_NAME = \"vertex-ai-prediction-peering-range\"\n",
|
||||
"NETWORK = \"ucaip-haystack-vpc-network\"\n",
|
||||
"\n",
|
||||
"# NOTE: `prefix-length=16` means a CIDR block with mask /16 will be\n",
|
||||
"# NOTE: `prefix-length=16` means a CIDR block with mask /16 is\n",
|
||||
"# reserved for use by Google services, such as Vertex AI.\n",
|
||||
"! gcloud compute addresses create $PEERING_RANGE_NAME \\\n",
|
||||
" --global \\\n",
|
||||
@@ -856,14 +780,14 @@
|
||||
"\n",
|
||||
"Next, create the connection for VPC peering.\n",
|
||||
"\n",
|
||||
"*Note:* If you get a PERMISSION DENIED, you may not have the neccessary role 'Compute Network Admin' set for your default service account. In the Cloud Console, do the following steps.\n",
|
||||
"**Note:** If you get a PERMISSION DENIED, you may not have the neccessary 'Compute Network Admin' role set for your default service account. In the Cloud Console, do the following:\n",
|
||||
"\n",
|
||||
"1. Goto `IAM & Admin`.\n",
|
||||
"1. Goto **IAM & Admin**.\n",
|
||||
"2. Find your service account.\n",
|
||||
"3. Click edit icon.\n",
|
||||
"4. Select `Add Another Role`.\n",
|
||||
"5. Enter 'Compute Network Admin'.\n",
|
||||
"6. Select `Save`."
|
||||
"4. Select **Add Another Role**.\n",
|
||||
"5. Enter **Compute Network Admin**.\n",
|
||||
"6. Select **Save**."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -922,18 +846,10 @@
|
||||
"source": [
|
||||
"# Retrieve the project number\n",
|
||||
"PROJECT_NUMBER = !gcloud projects list --filter=\"PROJECT_ID:'{PROJECT_ID}'\" --format='value(PROJECT_NUMBER)'\n",
|
||||
"PROJECT_NUMBER = PROJECT_NUMBER[0]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "cd58eb809f71"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"full_network_name = f\"projects/{PROJECT_NUMBER}/global/networks/{NETWORK}\""
|
||||
"PROJECT_NUMBER = PROJECT_NUMBER[0]\n",
|
||||
"\n",
|
||||
"full_network_name = f\"projects/{PROJECT_NUMBER}/global/networks/{NETWORK}\"\n",
|
||||
"print(full_network_name)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -986,6 +902,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Set a unique id for your deployed index\n",
|
||||
"DEPLOYED_INDEX_ID = \"deployed_index_id_unique\""
|
||||
]
|
||||
},
|
||||
@@ -997,6 +914,7 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Deploy your ANN index to the index endpoint\n",
|
||||
"my_index_endpoint = my_index_endpoint.deploy_index(\n",
|
||||
" index=tree_ah_index, deployed_index_id=DEPLOYED_INDEX_ID\n",
|
||||
")\n",
|
||||
@@ -1014,7 +932,7 @@
|
||||
"\n",
|
||||
"After you've built your indexes, you can query against the deployed index to find nearest neighbors.\n",
|
||||
"\n",
|
||||
"Note: For the DOT_PRODUCT_DISTANCE distance type, the \"distance\" property returned with each MatchNeighbor actually refers to the similarity."
|
||||
"**Note:** For the **DOT_PRODUCT_DISTANCE** distance type, the \"distance\" property returned with each MatchNeighbor actually refers to the similarity."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1086,13 +1004,13 @@
|
||||
},
|
||||
"source": [
|
||||
"## Storing and retrieving titles from a Redis data store\n",
|
||||
"When you productionize this code into a service, you'll need to convert the nearest nearest id's returned from Vertex AI Vector Search into data usable by downstream services.\n",
|
||||
"When you productionize this code into a service, you need to convert the nearest ids returned from Vertex AI Vector Search into usable data for downstream services.\n",
|
||||
"\n",
|
||||
"In this case, you'll need to convert the id's to titles.\n",
|
||||
"In this case, you need to convert the ids to titles.\n",
|
||||
"\n",
|
||||
"You can use Google Cloud's Memorystore to deploy a managed Redis instance to save the id-title key-value pairs.\n",
|
||||
"\n",
|
||||
"See more information on [Memorystore](https://cloud.google.com/memorystore/docs/redis/create-manage-instances?hl=en)"
|
||||
"See more information on [Memorystore](https://cloud.google.com/memorystore/docs/redis/create-manage-instances?hl=en)."
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1103,10 +1021,11 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Set a display name for your Redis instance\n",
|
||||
"REDIS_INSTANCE_NAME = \"stackoverflow-questions-unique\"\n",
|
||||
"\n",
|
||||
"# Create a Redis instance\n",
|
||||
"! gcloud redis instances create '{REDIS_INSTANCE_NAME}' --size=5 --region={REGION} --network={VPC_NETWORK_FULL} --connect-mode=private-service-access"
|
||||
"! gcloud redis instances create '{REDIS_INSTANCE_NAME}' --size=5 --region={LOCATION} --network={VPC_NETWORK_FULL} --connect-mode=private-service-access"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -1117,12 +1036,10 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"\n",
|
||||
"# Get host and port info\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" REDIS_HOST = ! gcloud redis instances list --filter=\"INSTANCE_NAME:'{REDIS_INSTANCE_NAME}'\" --region {REGION} --format='value(HOST)'\n",
|
||||
" REDIS_PORT = ! gcloud redis instances list --filter=\"INSTANCE_NAME:'{REDIS_INSTANCE_NAME}'\" --region {REGION} --format='value(PORT)'\n",
|
||||
" REDIS_HOST = ! gcloud redis instances list --filter=\"INSTANCE_NAME:'{REDIS_INSTANCE_NAME}'\" --region {LOCATION} --format='value(HOST)'\n",
|
||||
" REDIS_PORT = ! gcloud redis instances list --filter=\"INSTANCE_NAME:'{REDIS_INSTANCE_NAME}'\" --region {LOCATION} --format='value(PORT)'\n",
|
||||
"\n",
|
||||
" if isinstance(REDIS_HOST, list):\n",
|
||||
" REDIS_HOST = REDIS_HOST[0]\n",
|
||||
@@ -1143,8 +1060,6 @@
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Connect to the instance\n",
|
||||
"import redis\n",
|
||||
"\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT)"
|
||||
]
|
||||
@@ -1170,8 +1085,8 @@
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Verify that redis can retrieve the correct information\n",
|
||||
"if not os.getenv(\"IS_TESTING\"):\n",
|
||||
" # Verify that redis can retrieve the correct information\n",
|
||||
" [\n",
|
||||
" f\"Actual = {title}, Retrieved = {redis_client.get(str(id))}\"\n",
|
||||
" for id, title in list(zip(df.id, df.title))[:10]\n",
|
||||
@@ -1205,21 +1120,13 @@
|
||||
"# Delete indexes\n",
|
||||
"tree_ah_index.delete()\n",
|
||||
"\n",
|
||||
"# Delete cloud storage bucket\n",
|
||||
"delete_bucket = False\n",
|
||||
"if delete_bucket or os.getenv(\"IS_TESTING\"):\n",
|
||||
" ! gsutil rm -rf {BUCKET_URI}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"id": "d2fcf9468031"
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"if delete_bucket:\n",
|
||||
" ! gsutil rm -rf {BUCKET_URI}\n",
|
||||
"\n",
|
||||
"# Delete redis instance\n",
|
||||
"! gcloud redis instances delete '{REDIS_INSTANCE_NAME}' --region {REGION} --quiet"
|
||||
"! gcloud redis instances delete '{REDIS_INSTANCE_NAME}' --region {LOCATION} --quiet"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user