{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Google Cloud LlamaIndex on Vertex AI for RAG\n", "\n", "In this notebook, we will show you how to get started with the [Vertex AI RAG API](https://cloud.google.com/vertex-ai/generative-ai/docs/llamaindex-on-vertexai).\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Installation" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install llama-index-llms-gemini\n", "%pip install llama-index-indices-managed-vertexai" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install llama-index\n", "%pip install google-cloud-aiplatform==1.53.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup\n", "\n", "Follow the steps in this documentation to create a Google Cloud project and enable the Vertex AI API.\n", "\n", "https://cloud.google.com/vertex-ai/docs/start/cloud-environment" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Authenticating your notebook environment\n", "\n", "* If you are using **Colab** to run this notebook, run the cell below and continue.\n", "* If you are using **Vertex AI Workbench**, check out the setup instructions [here](https://github.com/GoogleCloudPlatform/generative-ai/tree/main/setup-env)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "# Additional authentication is required for Google Colab\n", "if \"google.colab\" in sys.modules:\n", " # Authenticate user to Google Cloud\n", " from google.colab import auth\n", "\n", " auth.authenticate_user()\n", "\n", " ! gcloud config set project {PROJECT_ID}\n", " ! gcloud auth application-default login -q" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Download Data" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!mkdir -p 'data/paul_graham/'\n", "!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic Usage\n", "\n", "A `corpus` is a collection of `document`s. A `document` is a body of text that is broken into `chunk`s." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Set up LLM for RAG" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from llama_index.core import Settings\n", "from llama_index.llms.vertex import Vertex\n", "\n", "vertex_gemini = Vertex(\n", " model=\"gemini-1.5-pro-preview-0514\",\n", " temperature=0,\n", " context_window=100000,\n", " additional_kwargs={},\n", ")\n", "\n", "Settings.llm = vertex_gemini" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from llama_index.indices.managed.vertexai import VertexAIIndex\n", "\n", "# TODO(developer): Replace these values with your project information\n", "project_id = \"YOUR_PROJECT_ID\"\n", "location = \"us-central1\"\n", "\n", "# Optional: If creating a new corpus\n", "corpus_display_name = \"my-corpus\"\n", "corpus_description = \"Vertex AI Corpus for LlamaIndex\"\n", "\n", "# Create a corpus or provide an existing corpus ID\n", "index = VertexAIIndex(\n", " project_id,\n", " location,\n", " corpus_display_name=corpus_display_name,\n", " corpus_description=corpus_description,\n", ")\n", "print(f\"Newly created corpus name is {index.corpus_name}.\")\n", "\n", "# Upload local file\n", "file_name = index.insert_file(\n", " file_path=\"data/paul_graham/paul_graham_essay.txt\",\n", " metadata={\n", " \"display_name\": \"paul_graham_essay\",\n", " \"description\": \"Paul Graham essay\",\n", " },\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's check that what we've ingested." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(index.list_files())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's ask the index a question." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Querying.\n", "query_engine = index.as_query_engine()\n", "response = query_engine.query(\"What did Paul Graham do growing up?\")\n", "\n", "# Show response.\n", "print(f\"Response is {response.response}\")\n", "\n", "# Show cited passages that were used to construct the response.\n", "for cited_text in [node.text for node in response.source_nodes]:\n", " print(f\"Cited text: {cited_text}\")\n", "\n", "# Show answerability. 0 means not answerable from the passages.\n", "# 1 means the model is certain the answer can be provided from the passages.\n", "if response.metadata:\n", " print(\n", " f\"Answerability: {response.metadata.get('answerable_probability', 0)}\"\n", " )" ] } ], "metadata": { "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3" } }, "nbformat": 4, "nbformat_minor": 2 }