Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Llamaindex managed Vertex AI index needs to be updated. #14637

Closed
wadave opened this issue Jul 8, 2024 · 3 comments · Fixed by #14641
Closed

[Bug]: Llamaindex managed Vertex AI index needs to be updated. #14637

wadave opened this issue Jul 8, 2024 · 3 comments · Fixed by #14641
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized

Comments

@wadave
Copy link
Contributor

wadave commented Jul 8, 2024

Bug Description

I tried this https://docs.llamaindex.ai/en/stable/examples/managed/VertexAIDemo/ but it gave errors.

print(index.list_files()) gave list_files() got an unexpected keyword argument 'corpus'

It seems Vertex AI RAG api (from vertexai.preview import rag) evolved. The latest RAG API actually uses 'corpus_name' instead of 'corpus'.

https://github.com/run-llama/llama_index/tree/main/llama-index-integrations/indices/llama-index-indices-managed-vertexai/llama_index/indices/managed/vertexai needs to be updated.

query_engine = index.as_query_engine() gave Could not load OpenAI model. If you intended to use OpenAI, please check your OPENAI_API_KEY. at beginning, after I set up Settings.llm = vertex_gemini (nor sure if this is the right way), it gave AttributeError: Unknown field for RetrieveContextsResponse: rag_contexts

Version

0.10.52

Steps to Reproduce

Run this notebook: https://docs.llamaindex.ai/en/stable/examples/managed/VertexAIDemo/
The errors will be reproducced.

Relevant Logs/Tracbacks

No response

@wadave wadave added bug Something isn't working triage Issue needs to be triaged/prioritized labels Jul 8, 2024
Copy link

dosubot bot commented Jul 8, 2024

Hey there, @wadave! I'm here to help you with any bugs, questions, or becoming a contributor. Let's squash those bugs together! 🤖

To address the issues with the list_files() and as_query_engine() methods in the LlamaIndex managed Vertex AI index, you can update the code as follows:

  1. Update list_files() Method:
    Ensure the list_files() method correctly lists all files in the index by using the corpus_name argument:

    def list_files(self) -> Sequence[str]:
        """List all files in the index."""
        files = []
        with telemetry.tool_context_manager(self._user_agent):
            for file in rag.list_files(corpus=self.corpus_name):
                files.append(file.name)
        return files
  2. Update as_query_engine() Method:
    Ensure the as_query_engine() method correctly returns a RetrieverQueryEngine:

    def as_query_engine(self, **kwargs: Any) -> BaseQueryEngine:
        from llama_index.core.query_engine.retriever_query_engine import (
            RetrieverQueryEngine,
        )
    
        kwargs["retriever"] = self.as_retriever(**kwargs)
        return RetrieverQueryEngine.from_args(**kwargs)
  3. Handle rag_contexts Attribute:
    Modify the VertexAIRetriever class to handle the rag_contexts attribute in the RetrieveContextsResponse to avoid the AttributeError:

    from typing import Any, List, Optional
    
    from llama_index.core.base.base_retriever import BaseRetriever
    from llama_index.core.schema import NodeWithScore, QueryBundle, TextNode
    
    from google.cloud.aiplatform import telemetry
    from vertexai.preview import rag
    
    class VertexAIRetriever(BaseRetriever):
        def __init__(
            self,
            corpus_name: str,
            similarity_top_k: Optional[int] = None,
            vector_distance_threshold: Optional[float] = 0.3,
            user_agent: Optional[str] = None,
            **kwargs: Any,
        ) -> None:
            """Initialize the Vertex AI Retriever."""
            self.rag_resources = [rag.RagResource(rag_corpus=corpus_name)]
            self._similarity_top_k = similarity_top_k
            self._vector_distance_threshold = vector_distance_threshold
            self._user_agent = user_agent or "llama-index/0.0.0"
    
        def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
            """Retrieve from the platform."""
            with telemetry.tool_context_manager(self._user_agent):
                response = rag.retrieval_query(
                    text=query_bundle.query_str,
                    rag_resources=self.rag_resources,
                    similarity_top_k=self._similarity_top_k,
                    vector_distance_threshold=self._vector_distance_threshold,
                )
    
            if response.rag_contexts:
                return [
                    NodeWithScore(node=TextNode(text=context.text), score=context.distance)
                    for context in response.rag_contexts.contexts
                ]
            else:
                return []
    
        async def _aretrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
            """Asynchronously retrieve from the platform."""
            return self._retrieve(query_bundle=query_bundle)
  4. Dependencies:
    Ensure you have the necessary dependencies in your requirements.txt or setup.py file:

    llama-index-llms-gemini
    llama-index-indices-managed-vertexai
    llama-index
    google-cloud-aiplatform==1.53.0
    llama-index-vector-stores-vertexaivectorsearch
    llama-index-core
    llama-index-llms-vertex
    

These updates should resolve the issues you encountered with the list_files() and as_query_engine() methods, as well as the AttributeError related to the rag_contexts attribute [1][2][3].

To continue talking to Dosu, mention @dosu.

@wadave
Copy link
Contributor Author

wadave commented Jul 8, 2024

Hi Bot, it should be 'response.contexts.contexts', not response.rag_contexts.contexts

Copy link

dosubot bot commented Jul 8, 2024

Thank you for the correction. Here is the updated code for the VertexAIRetriever class to handle the contexts attribute correctly:

from typing import Any, List, Optional

from llama_index.core.base.base_retriever import BaseRetriever
from llama_index.core.schema import NodeWithScore, QueryBundle, TextNode

from google.cloud.aiplatform import telemetry
from vertexai.preview import rag

class VertexAIRetriever(BaseRetriever):
    def __init__(
        self,
        corpus_name: str,
        similarity_top_k: Optional[int] = None,
        vector_distance_threshold: Optional[float] = 0.3,
        user_agent: Optional[str] = None,
        **kwargs: Any,
    ) -> None:
        """Initialize the Vertex AI Retriever."""
        self.rag_resources = [rag.RagResource(rag_corpus=corpus_name)]
        self._similarity_top_k = similarity_top_k
        self._vector_distance_threshold = vector_distance_threshold
        self._user_agent = user_agent or "llama-index/0.0.0"

    def _retrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
        """Retrieve from the platform."""
        with telemetry.tool_context_manager(self._user_agent):
            response = rag.retrieval_query(
                text=query_bundle.query_str,
                rag_resources=self.rag_resources,
                similarity_top_k=self._similarity_top_k,
                vector_distance_threshold=self._vector_distance_threshold,
            )

        if response.contexts:
            return [
                NodeWithScore(node=TextNode(text=context.text), score=context.distance)
                for context in response.contexts.contexts
            ]
        else:
            return []

    async def _aretrieve(self, query_bundle: QueryBundle) -> List[NodeWithScore]:
        """Asynchronously retrieve from the platform."""
        return self._retrieve(query_bundle=query_bundle)

This should correctly handle the contexts attribute in the RetrieveContextsResponse to avoid the AttributeError.

To continue talking to Dosu, mention @dosu.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage Issue needs to be triaged/prioritized
1 participant