Skip to content

Commit

Permalink
Migrate to Gen AI SDK: semantic_matching
Browse files Browse the repository at this point in the history
  • Loading branch information
takumiohym committed Feb 19, 2025
1 parent 290caa1 commit d528c30
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 873 deletions.
34 changes: 17 additions & 17 deletions notebooks/vertex_genai/labs/semantic_matching_with_gemini.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
"tags": []
},
"source": [
"# Semantic Matching with the Gemini API\n",
"# Semantic Matching with Gemini\n",
"\n",
"**Learning Objectives**\n",
" 1. Learn how to identify matching product descriptions\n",
" 1. Learn how to design a prompt for semantic matching\n",
" 2. Learn how to evaluate performance of a prompt for semantic matching\n",
" 1. Learn how to query the Vertex Gemini API\n",
" 1. Learn how to use Gemini with Google Gen AI SDK\n",
" \n",
"Semantic matching is the problem of classifying a pair of entities $(x_1, x_2)$ as being a good match or not. So it is a classification setup that is a very flexible: Namely, it comprises general information retrieval (where the first entity can be a textual query and the second entity can be a paragraph for instance), entity resolutions, or database-record fuzzy-matching. In this notebook we will focus on matching textual descriptions of retail products. More specifically:\n",
" \n",
Expand Down Expand Up @@ -46,12 +46,10 @@
"import random\n",
"\n",
"import pandas as pd\n",
"import vertexai\n",
"from vertexai.generative_models import GenerationConfig, GenerativeModel\n",
"from google import genai\n",
"from google.genai.types import GenerateContentConfig\n",
"\n",
"pd.options.display.max_colwidth = 1000\n",
"\n",
"vertexai.init()"
"pd.options.display.max_colwidth = 1000"
]
},
{
Expand Down Expand Up @@ -325,7 +323,7 @@
"id": "kPOcktSQn1Q0"
},
"source": [
"We start by instanciating our client to the `gemini-1.0-pro` version of Gemini which is a large language model (LLM) developed by Google."
"We start by instanciating our client using the Gen AI SDK. We'll use the `gemini-2.0-flash-001` version of Gemini which is a large language model (LLM) developed by Google."
]
},
{
Expand All @@ -337,7 +335,9 @@
},
"outputs": [],
"source": [
"generation_model = GenerativeModel(\"gemini-1.0-pro\")"
"MODEL = \"gemini-2.0-flash-001\"\n",
"\n",
"client = genai.Client(vertexai=True, location=\"us-central1\")"
]
},
{
Expand All @@ -355,7 +355,7 @@
"source": [
"### Exercise\n",
"\n",
"Complete the function below so that it queries the Gemini API using the `generation_model` client.\n",
"Complete the function below so that it queries Gemini using the Gen AI SDK client.\n",
"\n",
"**Hint:** Jump to the cell after next to see how this function is used."
]
Expand All @@ -372,7 +372,7 @@
"def are_products_matching(d1, d2, prompt):\n",
" prompt = None # TODO: Substitute d1 and d2 in the parametrized prompt\n",
" answer = None # TODO: Call the Gemini API with the prompt\n",
" return answer.text"
" return answer.text.strip()"
]
},
{
Expand Down Expand Up @@ -767,15 +767,15 @@
"provenance": []
},
"environment": {
"kernel": "python3",
"name": "tf2-gpu.2-12.m123",
"kernel": "conda-base-py",
"name": "workbench-notebooks.m128",
"type": "gcloud",
"uri": "us-docker.pkg.dev/deeplearning-platform-release/gcr.io/tf2-gpu.2-12:m123"
"uri": "us-docker.pkg.dev/deeplearning-platform-release/gcr.io/workbench-notebooks:m128"
},
"kernelspec": {
"display_name": "Python 3 (Local)",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
"name": "conda-base-py"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -787,7 +787,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
"version": "3.10.16"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit d528c30

Please sign in to comment.