|
29 | 29 | "id": "JAPoU8Sm5E6e" |
30 | 30 | }, |
31 | 31 | "source": [ |
32 | | - "# Intro to Building a Scalable and Modular RAG System with RAG Engine in Vertex AI \n", |
| 32 | + "# Intro to Building a Scalable and Modular RAG System with RAG Engine on Gemini Enterprise Agent Platform\n", |
33 | 33 | "\n", |
34 | 34 | "<table align=\"left\">\n", |
35 | 35 | " <td style=\"text-align: center\">\n", |
|
38 | 38 | " </a>\n", |
39 | 39 | " </td>\n", |
40 | 40 | " <td style=\"text-align: center\">\n", |
41 | | - " <a href=\"https://console.cloud.google.com/vertex-ai/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Frag-engine%2Fintro_rag_engine.ipynb\">\n", |
| 41 | + " <a href=\"https://console.cloud.google.com/agent-platform/colab/import/https:%2F%2Fraw.githubusercontent.com%2FGoogleCloudPlatform%2Fgenerative-ai%2Fmain%2Fgemini%2Frag-engine%2Fintro_rag_engine.ipynb\">\n", |
42 | 42 | " <img width=\"32px\" src=\"https://lh3.googleusercontent.com/JmcxdQi-qOpctIvWKgPtrzZdJJK-J3sWE1RsfjZNwshCFgE_9fULcNpuXYTilIR2hjwN\" alt=\"Google Cloud Colab Enterprise logo\"><br> Open in Colab Enterprise\n", |
43 | 43 | " </a>\n", |
44 | 44 | " </td>\n", |
45 | 45 | " <td style=\"text-align: center\">\n", |
46 | | - " <a href=\"https://console.cloud.google.com/vertex-ai/workbench/deploy-notebook?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/gemini/rag-engine/intro_rag_engine.ipynb\">\n", |
47 | | - " <img src=\"https://www.gstatic.com/images/branding/gcpiconscolors/vertexai/v1/32px.svg\" alt=\"Vertex AI logo\"><br> Open in Vertex AI Workbench\n", |
| 46 | + " <a href=\"https://console.cloud.google.com/agent-platform/workbench/instances?download_url=https://raw.githubusercontent.com/GoogleCloudPlatform/generative-ai/main/gemini/rag-engine/intro_rag_engine.ipynb\">\n", |
| 47 | + " <img width=\"32px\" src=\"https://storage.googleapis.com/github-repo/workbench-icon.svg\" alt=\"Workbench logo\"><br> Open in Workbench\n", |
48 | 48 | " </a>\n", |
49 | 49 | " </td>\n", |
50 | 50 | " <td style=\"text-align: center\">\n", |
|
56 | 56 | "\n", |
57 | 57 | "<div style=\"clear: both;\"></div>\n", |
58 | 58 | "\n", |
| 59 | + "<p>\n", |
59 | 60 | "<b>Share to:</b>\n", |
60 | 61 | "\n", |
61 | 62 | "<a href=\"https://www.linkedin.com/sharing/share-offsite/?url=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/rag-engine/intro_rag_engine.ipynb\" target=\"_blank\">\n", |
|
76 | 77 | "\n", |
77 | 78 | "<a href=\"https://www.facebook.com/sharer/sharer.php?u=https%3A//github.com/GoogleCloudPlatform/generative-ai/blob/main/gemini/rag-engine/intro_rag_engine.ipynb\" target=\"_blank\">\n", |
78 | 79 | " <img width=\"20px\" src=\"https://upload.wikimedia.org/wikipedia/commons/5/51/Facebook_f_logo_%282019%29.svg\" alt=\"Facebook logo\">\n", |
79 | | - "</a> " |
| 80 | + "</a>\n", |
| 81 | + "</p> " |
80 | 82 | ] |
81 | 83 | }, |
82 | 84 | { |
|
109 | 111 | "have, a better context is provided. The improved context along with the query\n", |
110 | 112 | "enhances the quality of the LLM's response.\n", |
111 | 113 | "\n", |
112 | | - "The following concepts are key to understanding Vertex AI RAG Engine. These concepts are listed in the order of the\n", |
| 114 | + "The following concepts are key to understanding Agent Platform RAG Engine. These concepts are listed in the order of the\n", |
113 | 115 | "retrieval-augmented generation (RAG) process.\n", |
114 | 116 | "\n", |
115 | 117 | "1. **Data ingestion**: Intake data from different data sources. For example,\n", |
|
135 | 137 | " original user query as a guide for the generative AI model to generate\n", |
136 | 138 | " factually grounded and relevant responses.\n", |
137 | 139 | "\n", |
138 | | - "For more information, refer to the public documentation for [Vertex AI RAG Engine](https://cloud.google.com/vertex-ai/generative-ai/docs/rag-overview)." |
| 140 | + "For more information, refer to the public documentation for [Agent Platform RAG Engine](https://docs.cloud.google.com/gemini-enterprise-agent-platform/build/rag-engine/rag-overview)." |
139 | 141 | ] |
140 | 142 | }, |
141 | 143 | { |
|
153 | 155 | "id": "No17Cw5hgx12" |
154 | 156 | }, |
155 | 157 | "source": [ |
156 | | - "### Install Vertex AI SDK and Google Gen AI SDK\n" |
| 158 | + "### Install Agent Platform SDK and Google Gen AI SDK\n" |
157 | 159 | ] |
158 | 160 | }, |
159 | 161 | { |
|
200 | 202 | "id": "DF4l8DTdWgPY" |
201 | 203 | }, |
202 | 204 | "source": [ |
203 | | - "### Set Google Cloud project information and initialize Vertex AI SDK\n", |
| 205 | + "### Set Google Cloud project information\n", |
204 | 206 | "\n", |
205 | | - "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).\n", |
| 207 | + "To get started using Agent Platform, you must have an existing Google Cloud project and [enable the Agent Platform API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).\n", |
206 | 208 | "\n", |
207 | | - "Learn more about [setting up a project and a development environment](https://cloud.google.com/vertex-ai/docs/start/cloud-environment).\n", |
| 209 | + "Learn more about [setting up a project](https://docs.cloud.google.com/resource-manager/docs/creating-managing-projects) and a [development environment](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment).\n", |
208 | 210 | "\n", |
209 | | - "See [supported regions](https://cloud.google.com/vertex-ai/generative-ai/docs/rag-engine/rag-overview#supported-regions) for location options.\n" |
| 211 | + "See [supported regions](https://cloud.google.com/vertex-ai/generative-ai/docs/rag-engine/rag-overview#supported-regions) for location options." |
210 | 212 | ] |
211 | 213 | }, |
212 | 214 | { |
|
220 | 222 | "# Use the environment variable if the user doesn't provide Project ID.\n", |
221 | 223 | "import os\n", |
222 | 224 | "\n", |
223 | | - "import vertexai\n", |
224 | | - "from google import genai\n", |
| 225 | + "import agentplatform\n", |
225 | 226 | "\n", |
226 | 227 | "# fmt: off\n", |
227 | 228 | "PROJECT_ID = \"[your-project-id]\" # @param {type: \"string\", placeholder: \"[your-project-id]\", isTemplate: true}\n", |
|
230 | 231 | " PROJECT_ID = str(os.environ.get(\"GOOGLE_CLOUD_PROJECT\"))\n", |
231 | 232 | "\n", |
232 | 233 | "# See https://cloud.google.com/vertex-ai/generative-ai/docs/rag-engine/rag-overview#supported-regions for location options.\n", |
233 | | - "vertexai.init(project=PROJECT_ID, location=\"us-east1\")\n", |
234 | | - "client = genai.Client(vertexai=True, project=PROJECT_ID, location=\"global\")" |
| 234 | + "client = agentplatform.Client(project=PROJECT_ID, location=\"us-east1\")" |
235 | 235 | ] |
236 | 236 | }, |
237 | 237 | { |
|
252 | 252 | "outputs": [], |
253 | 253 | "source": [ |
254 | 254 | "from IPython.display import Markdown, display\n", |
255 | | - "from google.genai.types import GenerateContentConfig, Retrieval, Tool, VertexRagStore\n", |
256 | | - "from vertexai import rag" |
| 255 | + "from agentplatform import types\n", |
| 256 | + "from google import genai\n", |
| 257 | + "from google.genai import types as genai_types" |
257 | 258 | ] |
258 | 259 | }, |
259 | 260 | { |
|
273 | 274 | }, |
274 | 275 | "outputs": [], |
275 | 276 | "source": [ |
276 | | - "# Currently supports Google first-party embedding models\n", |
277 | | - "# fmt: off\n", |
278 | | - "EMBEDDING_MODEL = \"publishers/google/models/text-embedding-005\" # @param {type:\"string\", isTemplate: true}\n", |
279 | | - "# fmt: on\n", |
280 | | - "\n", |
281 | | - "rag_corpus = rag.create_corpus(\n", |
282 | | - " display_name=\"my-rag-corpus\",\n", |
283 | | - " backend_config=rag.RagVectorDbConfig(\n", |
284 | | - " rag_embedding_model_config=rag.RagEmbeddingModelConfig(\n", |
285 | | - " vertex_prediction_endpoint=rag.VertexPredictionEndpoint(\n", |
286 | | - " publisher_model=EMBEDDING_MODEL\n", |
287 | | - " )\n", |
288 | | - " )\n", |
289 | | - " ),\n", |
| 277 | + "rag_corpus = client.rag.create_corpus(\n", |
| 278 | + " rag_corpus=types.RagCorpus(\n", |
| 279 | + " display_name=\"my-rag-corpus\",\n", |
| 280 | + " )\n", |
290 | 281 | ")" |
291 | 282 | ] |
292 | 283 | }, |
|
307 | 298 | }, |
308 | 299 | "outputs": [], |
309 | 300 | "source": [ |
310 | | - "rag.list_corpora()" |
| 301 | + "client.rag.list_corpora()" |
311 | 302 | ] |
312 | 303 | }, |
313 | 304 | { |
|
365 | 356 | }, |
366 | 357 | "outputs": [], |
367 | 358 | "source": [ |
368 | | - "rag_file = rag.upload_file(\n", |
| 359 | + "rag_file = client.rag.upload_file(\n", |
369 | 360 | " corpus_name=rag_corpus.name,\n", |
370 | 361 | " path=\"test.md\",\n", |
371 | 362 | " display_name=\"test.md\",\n", |
372 | | - " description=\"my test file\",\n", |
373 | 363 | ")" |
374 | 364 | ] |
375 | 365 | }, |
|
395 | 385 | "outputs": [], |
396 | 386 | "source": [ |
397 | 387 | "INPUT_GCS_BUCKET = (\n", |
398 | | - " \"gs://cloud-samples-data/gen-app-builder/search/alphabet-investor-pdfs/\"\n", |
| 388 | + " \"gs://cloud-samples-data/gen-app-builder/search/alphabet-investor-pdfs/*\"\n", |
399 | 389 | ")\n", |
400 | 390 | "\n", |
401 | | - "response = rag.import_files(\n", |
402 | | - " corpus_name=rag_corpus.name,\n", |
403 | | - " paths=[INPUT_GCS_BUCKET],\n", |
404 | | - " # Optional\n", |
405 | | - " transformation_config=rag.TransformationConfig(\n", |
406 | | - " chunking_config=rag.ChunkingConfig(chunk_size=1024, chunk_overlap=100)\n", |
| 391 | + "response = client.rag.import_files(\n", |
| 392 | + " name=rag_corpus.name,\n", |
| 393 | + " import_config=types.ImportRagFilesConfig(\n", |
| 394 | + " gcs_source=types.GcsSource(uris=[INPUT_GCS_BUCKET]),\n", |
| 395 | + " rag_file_transformation_config=types.RagFileTransformationConfig(\n", |
| 396 | + " rag_file_chunking_config=types.RagFileChunkingConfig(\n", |
| 397 | + " chunk_size=1024,\n", |
| 398 | + " chunk_overlap=100,\n", |
| 399 | + " )\n", |
| 400 | + " ), # optional\n", |
| 401 | + " max_embedding_requests_per_min=900, # optional\n", |
407 | 402 | " ),\n", |
408 | | - " max_embedding_requests_per_min=900, # Optional\n", |
409 | 403 | ")" |
410 | 404 | ] |
411 | 405 | }, |
|
433 | 427 | }, |
434 | 428 | "outputs": [], |
435 | 429 | "source": [ |
436 | | - "response = rag.import_files(\n", |
437 | | - " corpus_name=rag_corpus.name,\n", |
438 | | - " paths=[\"https://drive.google.com/drive/folders/{folder_id}\"],\n", |
439 | | - " # Optional\n", |
440 | | - " transformation_config=rag.TransformationConfig(\n", |
441 | | - " chunking_config=rag.ChunkingConfig(chunk_size=512, chunk_overlap=50)\n", |
| 430 | + "response = client.rag.import_files(\n", |
| 431 | + " name=rag_corpus.name,\n", |
| 432 | + " import_config=types.ImportRagFilesConfig(\n", |
| 433 | + " google_drive_source=types.GoogleDriveSource(\n", |
| 434 | + " resource_ids=[\n", |
| 435 | + " types.GoogleDriveSourceResourceId(\n", |
| 436 | + " resource_id=\"https://drive.google.com/drive/folders/{folder_id}\",\n", |
| 437 | + " resource_type=types.ResourceType.RESOURCE_TYPE_FOLDER,\n", |
| 438 | + " )\n", |
| 439 | + " ]\n", |
| 440 | + " ),\n", |
| 441 | + " rag_file_transformation_config=types.RagFileTransformationConfig(\n", |
| 442 | + " rag_file_chunking_config=types.RagFileChunkingConfig(\n", |
| 443 | + " chunk_size=512,\n", |
| 444 | + " chunk_overlap=50,\n", |
| 445 | + " )\n", |
| 446 | + " ), # optional\n", |
442 | 447 | " ),\n", |
443 | 448 | ")" |
444 | 449 | ] |
|
461 | 466 | "outputs": [], |
462 | 467 | "source": [ |
463 | 468 | "# Direct context retrieval\n", |
464 | | - "response = rag.retrieval_query(\n", |
465 | | - " rag_resources=[\n", |
466 | | - " rag.RagResource(\n", |
467 | | - " rag_corpus=rag_corpus.name,\n", |
468 | | - " # Optional: supply IDs from `rag.list_files()`.\n", |
469 | | - " # rag_file_ids=[\"rag-file-1\", \"rag-file-2\", ...],\n", |
470 | | - " )\n", |
471 | | - " ],\n", |
472 | | - " rag_retrieval_config=rag.RagRetrievalConfig(\n", |
473 | | - " top_k=10, # Optional\n", |
474 | | - " filter=rag.Filter(\n", |
475 | | - " vector_distance_threshold=0.5, # Optional\n", |
| 469 | + "response = client.rag.retrieve_contexts(\n", |
| 470 | + " vertex_rag_store=genai_types.VertexRagStore(\n", |
| 471 | + " rag_resources=[\n", |
| 472 | + " genai_types.VertexRagStoreRagResource(rag_corpus=rag_corpus.name)\n", |
| 473 | + " ]\n", |
| 474 | + " ),\n", |
| 475 | + " query=types.RagQuery(\n", |
| 476 | + " text=\"What is RAG and why it is helpful?\",\n", |
| 477 | + " rag_retrieval_config=types.RagRetrievalConfig(\n", |
| 478 | + " top_k=10,\n", |
| 479 | + " filter=types.RagRetrievalConfigFilter(vector_distance_threshold=0.5),\n", |
476 | 480 | " ),\n", |
477 | 481 | " ),\n", |
478 | | - " text=\"What is RAG and why it is helpful?\",\n", |
479 | 482 | ")\n", |
480 | 483 | "print(response)\n", |
481 | 484 | "\n", |
|
501 | 504 | "outputs": [], |
502 | 505 | "source": [ |
503 | 506 | "# Create a tool for the RAG Corpus\n", |
504 | | - "rag_retrieval_tool = Tool(\n", |
505 | | - " retrieval=Retrieval(\n", |
506 | | - " vertex_rag_store=VertexRagStore(\n", |
| 507 | + "rag_retrieval_tool = genai_types.Tool(\n", |
| 508 | + " retrieval=genai_types.Retrieval(\n", |
| 509 | + " vertex_rag_store=genai_types.VertexRagStore(\n", |
507 | 510 | " rag_corpora=[rag_corpus.name],\n", |
508 | 511 | " similarity_top_k=10,\n", |
509 | 512 | " vector_distance_threshold=0.5,\n", |
|
536 | 539 | "cell_type": "code", |
537 | 540 | "execution_count": null, |
538 | 541 | "metadata": { |
539 | | - "id": "124b36be8d5b" |
| 542 | + "id": "1f02122723b6" |
540 | 543 | }, |
541 | 544 | "outputs": [], |
542 | 545 | "source": [ |
543 | | - "response = client.models.generate_content(\n", |
544 | | - " model=MODEL_ID,\n", |
545 | | - " contents=\"What is RAG?\",\n", |
546 | | - " config=GenerateContentConfig(tools=[rag_retrieval_tool]),\n", |
547 | | - ")\n", |
548 | | - "\n", |
549 | | - "display(Markdown(response.text))" |
550 | | - ] |
551 | | - }, |
552 | | - { |
553 | | - "cell_type": "markdown", |
554 | | - "metadata": { |
555 | | - "id": "0268fe43d41c" |
556 | | - }, |
557 | | - "source": [ |
558 | | - "### Generate Content with Llama3 using RAG Retrieval Tool" |
| 546 | + "# Create a Gen AI SDK client\n", |
| 547 | + "genai_client = genai.Client(project=PROJECT_ID, location=\"your-location\")" |
559 | 548 | ] |
560 | 549 | }, |
561 | 550 | { |
562 | 551 | "cell_type": "code", |
563 | 552 | "execution_count": null, |
564 | 553 | "metadata": { |
565 | | - "id": "f6e67ee7968c" |
| 554 | + "id": "124b36be8d5b" |
566 | 555 | }, |
567 | 556 | "outputs": [], |
568 | 557 | "source": [ |
569 | | - "from vertexai import generative_models\n", |
570 | | - "\n", |
571 | | - "# Load tool into Llama model\n", |
572 | | - "rag_retrieval_tool = generative_models.Tool.from_retrieval(\n", |
573 | | - " retrieval=rag.Retrieval(\n", |
574 | | - " source=rag.VertexRagStore(\n", |
575 | | - " rag_resources=[rag.RagResource(rag_corpus=rag_corpus.name)],\n", |
576 | | - " rag_retrieval_config=rag.RagRetrievalConfig(\n", |
577 | | - " top_k=10, # Optional\n", |
578 | | - " filter=rag.Filter(\n", |
579 | | - " vector_distance_threshold=0.5, # Optional\n", |
580 | | - " ),\n", |
581 | | - " ),\n", |
582 | | - " ),\n", |
583 | | - " )\n", |
| 558 | + "response = genai_client.models.generate_content(\n", |
| 559 | + " model=MODEL_ID,\n", |
| 560 | + " contents=\"What is RAG?\",\n", |
| 561 | + " config=genai_types.GenerateContentConfig(tools=[rag_retrieval_tool]),\n", |
584 | 562 | ")\n", |
585 | 563 | "\n", |
586 | | - "llama_model = generative_models.GenerativeModel(\n", |
587 | | - " # your self-deployed endpoint for Llama3\n", |
588 | | - " \"projects/{project}/locations/{location}/endpoints/{endpoint_resource_id}\",\n", |
589 | | - " tools=[rag_retrieval_tool],\n", |
590 | | - ")" |
591 | | - ] |
592 | | - }, |
593 | | - { |
594 | | - "cell_type": "code", |
595 | | - "execution_count": null, |
596 | | - "metadata": { |
597 | | - "id": "c6d710b6dece" |
598 | | - }, |
599 | | - "outputs": [], |
600 | | - "source": [ |
601 | | - "response = llama_model.generate_content(\"What is RAG?\")\n", |
602 | | - "\n", |
603 | 564 | "display(Markdown(response.text))" |
604 | 565 | ] |
605 | 566 | } |
|
0 commit comments