|
| 1 | +# Copyright 2026 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# [START aiplatform_genai_embedding_code_retrieval] |
| 16 | +import os |
| 17 | + |
| 18 | +from google import genai |
| 19 | + |
| 20 | +# TODO (Developer) set the following environment variables. |
| 21 | +PROJECT_ID = os.getenv("PROJECT_ID") |
| 22 | +LOCATION_ID = os.getenv("LOCATION_ID", "us-central1") |
| 23 | +MODEL_NAME = os.getenv("MODEL_NAME", "gemini-embedding-001") |
| 24 | + |
| 25 | +QUERY_LINES = ["Retrieve a function that adds two numbers"] |
| 26 | +CODE_RETRIEVAL_QUERY = "CODE_RETRIEVAL_QUERY" |
| 27 | +RETRIEVAL_DOCUMENT = "RETRIEVAL_DOCUMENT" |
| 28 | +SOURCE_CODE = [ |
| 29 | + "def func(a, b): return a + b", |
| 30 | + "def func(a, b): return a - b", |
| 31 | + "def func(a, b): return (a ** 2 + b ** 2) ** 0.5", |
| 32 | +] |
| 33 | + |
| 34 | + |
| 35 | +def embed_test() -> ( |
| 36 | + tuple[genai.types.EmbedContentResponse, genai.types.EmbedContentResponse] |
| 37 | +): |
| 38 | + """Generates embeddings for source code indexing and code search queries using the Gemini API. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + tuple[genai.types.EmbedContentResponse, genai.types.EmbedContentResponse]: A tuple containing |
| 42 | + the final source code indexing response and search query embedding response. |
| 43 | + """ |
| 44 | + client = genai.Client(enterprise=True, project=PROJECT_ID, location=LOCATION_ID) |
| 45 | + |
| 46 | + # Index Source Code |
| 47 | + for line in SOURCE_CODE: |
| 48 | + config = genai.types.EmbedContentConfig(task_type=RETRIEVAL_DOCUMENT) |
| 49 | + |
| 50 | + index_response = client.models.embed_content( |
| 51 | + model=MODEL_NAME, contents=line, config=config |
| 52 | + ) |
| 53 | + |
| 54 | + print( |
| 55 | + f"Task: {RETRIEVAL_DOCUMENT} | " |
| 56 | + f"Vector length: {len(index_response.embeddings)} | " |
| 57 | + f"Preview: {index_response.embeddings[:3]}..." |
| 58 | + ) |
| 59 | + |
| 60 | + # Embed Search Prompts |
| 61 | + for line in QUERY_LINES: |
| 62 | + config = genai.types.EmbedContentConfig(task_type=CODE_RETRIEVAL_QUERY) |
| 63 | + |
| 64 | + query_response = client.models.embed_content( |
| 65 | + model=MODEL_NAME, contents=line, config=config |
| 66 | + ) |
| 67 | + |
| 68 | + print( |
| 69 | + f"Task: {CODE_RETRIEVAL_QUERY} | " |
| 70 | + f"Vector length: {len(query_response.embeddings)} | " |
| 71 | + f"Preview: {query_response.embeddings[:3]}..." |
| 72 | + ) |
| 73 | + |
| 74 | + return index_response, query_response |
| 75 | + |
| 76 | + |
| 77 | +# [END aiplatform_genai_embedding_code_retrieval] |
0 commit comments