GoogleCloudPlatform / applied-ai-engineering-samples

This repository compiles code samples and notebooks demonstrating how to use Generative AI on Google Cloud Vertex AI.
https://cloud.google.com/ai/generative-ai
Apache License 2.0
312 stars 104 forks source link

Build grounded rag app with vertex with VertexAIRank example confusion #73

Open kikiWhite opened 1 month ago

kikiWhite commented 1 month ago

In the part Step 4. Query in Real Time and Check Grounding of genai-on-vertex-ai/retrieval_augmented_generation/diy_rag_with_vertexai_apis/build_grounded_rag_app_with_vertex.ipynb

The notebook mentioned that this step will "pass retrieved documents to the reranker API via the VertexAIRank" But I only saw the usage of VertexAI class and a basic retriever with the following code:

retriever = vector_store.as_retriever(search_kwargs={"k": 5})

llm = VertexAI(model_name="gemini-1.5-pro-001", max_output_tokens=1024)
template = """
Answer the question based only on the following context:
{context}

Question:
{query}
"""

I feel a bit confused. If I am wrong or I miss something, please correct me! Thank you for your attention to this matter : )

RajeshThallam commented 1 month ago

@kikiWhite Thanks for the feedback. Please check the step 3.1 in the notebook where reranker is added as part of contextual compression retriever.

from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
from langchain_google_community import VertexAIRank

# Instantiate the VertexAIReranker with the SDK manager
reranker = VertexAIRank(
    project_id=PROJECT_ID,
    location_id="global",
    ranking_config="default_ranking_config",
    title_field="source",  # metadata field to preserve with reranked results
    top_n=5,
)

basic_retriever = vector_store.as_retriever(
    search_kwargs={"k": 5}
)  # fetch top 5 documents

# Create the ContextualCompressionRetriever with the VertexAIRanker as a Reranker
retriever_with_reranker = ContextualCompressionRetriever(
    base_compressor=reranker, base_retriever=basic_retriever
)

Does that help?

kikiWhite commented 1 month ago

@RajeshThallam Thank you, Rajesh. Everything is clear now. I understand the use of the contextual compression retriever in step 3.1. I just wanted to kindly point out that step 4 mentioned the use of VertexAIRank, but it wasn't actually used in that step. Just wanted to bring it to your attention. Thanks again for your help!