Google Cloud Vertex AI 上的 LlamaIndex 用于 RAG¶
在本笔记本中,我们将向您展示如何开始使用 Vertex AI RAG API。
安装¶
In [ ]
已复制!
%pip install llama-index-llms-gemini
%pip install llama-index-indices-managed-vertexai
%pip install llama-index-llms-gemini %pip install llama-index-indices-managed-vertexai
In [ ]
已复制!
%pip install llama-index
%pip install google-cloud-aiplatform==1.53.0
%pip install llama-index %pip install google-cloud-aiplatform==1.53.0
设置¶
按照此文档中的步骤创建 Google Cloud 项目并启用 Vertex AI API。
https://cloud.google.com/vertex-ai/docs/start/cloud-environment
In [ ]
已复制!
import sys
# Additional authentication is required for Google Colab
if "google.colab" in sys.modules:
# Authenticate user to Google Cloud
from google.colab import auth
auth.authenticate_user()
! gcloud config set project {PROJECT_ID}
! gcloud auth application-default login -q
import sys # Additional authentication is required for Google Colab if "google.colab" in sys.modules: # Authenticate user to Google Cloud from google.colab import auth auth.authenticate_user() ! gcloud config set project {PROJECT_ID} ! gcloud auth application-default login -q
下载数据¶
In [ ]
已复制!
!mkdir -p 'data/paul_graham/'
!wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
!mkdir -p 'data/paul_graham/' !wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'
基本用法¶
一个 corpus
是一个 document
的集合。一个 document
是一段文本,被分割成 chunk
s。
为 RAG 设置 LLM¶
In [ ]
已复制!
from llama_index.core import Settings
from llama_index.llms.vertex import Vertex
vertex_gemini = Vertex(
model="gemini-1.5-pro-preview-0514",
temperature=0,
context_window=100000,
additional_kwargs={},
)
Settings.llm = vertex_gemini
from llama_index.core import Settings from llama_index.llms.vertex import Vertex vertex_gemini = Vertex( model="gemini-1.5-pro-preview-0514", temperature=0, context_window=100000, additional_kwargs={}, ) Settings.llm = vertex_gemini
In [ ]
已复制!
from llama_index.indices.managed.vertexai import VertexAIIndex
# TODO(developer): Replace these values with your project information
project_id = "YOUR_PROJECT_ID"
location = "us-central1"
# Optional: If creating a new corpus
corpus_display_name = "my-corpus"
corpus_description = "Vertex AI Corpus for LlamaIndex"
# Create a corpus or provide an existing corpus ID
index = VertexAIIndex(
project_id,
location,
corpus_display_name=corpus_display_name,
corpus_description=corpus_description,
)
print(f"Newly created corpus name is {index.corpus_name}.")
# Upload local file
file_name = index.insert_file(
file_path="data/paul_graham/paul_graham_essay.txt",
metadata={
"display_name": "paul_graham_essay",
"description": "Paul Graham essay",
},
)
from llama_index.indices.managed.vertexai import VertexAIIndex # TODO(developer): Replace these values with your project information project_id = "YOUR_PROJECT_ID" location = "us-central1" # Optional: If creating a new corpus corpus_display_name = "my-corpus" corpus_description = "Vertex AI Corpus for LlamaIndex" # Create a corpus or provide an existing corpus ID index = VertexAIIndex( project_id, location, corpus_display_name=corpus_display_name, corpus_description=corpus_description, ) print(f"Newly created corpus name is {index.corpus_name}.") # Upload local file file_name = index.insert_file( file_path="data/paul_graham/paul_graham_essay.txt", metadata={ "display_name": "paul_graham_essay", "description": "Paul Graham essay", }, )
让我们检查一下我们已摄取的内容。
In [ ]
已复制!
print(index.list_files())
print(index.list_files())
让我们向索引提问。
In [ ]
已复制!
# Querying.
query_engine = index.as_query_engine()
response = query_engine.query("What did Paul Graham do growing up?")
# Show response.
print(f"Response is {response.response}")
# Show cited passages that were used to construct the response.
for cited_text in [node.text for node in response.source_nodes]:
print(f"Cited text: {cited_text}")
# Show answerability. 0 means not answerable from the passages.
# 1 means the model is certain the answer can be provided from the passages.
if response.metadata:
print(
f"Answerability: {response.metadata.get('answerable_probability', 0)}"
)
# Querying. query_engine = index.as_query_engine() response = query_engine.query("What did Paul Graham do growing up?") # Show response. print(f"Response is {response.response}") # Show cited passages that were used to construct the response. for cited_text in [node.text for node in response.source_nodes]: print(f"Cited text: {cited_text}") # Show answerability. 0 means not answerable from the passages. # 1 means the model is certain the answer can be provided from the passages. if response.metadata: print( f"Answerability: {response.metadata.get('answerable_probability', 0)}" )