Custom query component #15500
Unanswered
sumitbindra
asked this question in
Q&A
Replies: 1 comment 1 reply
-
To create a custom query component that returns both the query engine and context, and to resolve errors regarding the index not being initialized, you can follow these steps:
Here's an example of how you can modify your import os
from typing import Optional, Dict, Any, cast
from llama_index.core.base.base_query_engine import BaseQueryEngine, QueryEngineComponent
from llama_index.core.callbacks.base import CallbackManager
from llama_index.core.readers.file.base import SimpleDirectoryReader
from llama_index.core.storage import StorageContext, load_index_from_storage
from llama_index.core.indices.vector_store import VectorStoreIndex
class LoadQuestions(QueryEngineComponent):
def __init__(self):
super().__init__()
self.vector_retriever = None
self.index = None
def _load_or_create_index(self, table_name: Optional[str]):
print(f"\n\nRunning load or create index {table_name}\n\n")
dir_name = table_name.replace("_responses", "") if table_name else "default"
PERSIST_DIR = f"./surveys/{dir_name}/RAGFiles/quesindex"
if not os.path.exists(PERSIST_DIR):
reader = SimpleDirectoryReader(f"./surveys/{dir_name}")
documents = reader.load_data()
self.index = VectorStoreIndex.from_documents(documents)
self.index.storage_context.persist(persist_dir=PERSIST_DIR)
else:
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
self.index = cast(VectorStoreIndex, load_index_from_storage(storage_context))
self.vector_retriever = self.index.as_retriever(similarity_top_k=3)
def _get_question_context(self, query_str: str) -> str:
relevant_nodes = self.vector_retriever.retrieve(query_str) if self.vector_retriever else []
question_context = ""
if len(relevant_nodes) > 0:
question_context = "\nHere are questions that are relevant to this query\n"
for node in relevant_nodes:
question_context += str(node.get_content()) + "\n"
return question_context
def get_query_engine(self) -> BaseQueryEngine:
if self.index is None:
raise ValueError("Index has not been loaded. Run the component first.")
return self.index.as_query_engine()
def _run_component(self, **kwargs: Any) -> Dict[str, Any]:
query_str = kwargs.get('query_str')
table_name = kwargs.get('table_name')
if query_str is None:
raise ValueError("query_str cannot be None")
self._load_or_create_index(table_name)
question_context = self._get_question_context(query_str)
return {"quesindex": question_context}
@property
def input_keys(self) -> set:
return {"query_str", "table_name"}
@property
def optional_input_keys(self) -> set:
return set()
@property
def output_keys(self) -> set:
return {"quesindex"} Key Points:
This approach ensures that the index is properly initialized and the query engine can be used without errors [1][2]. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
How do I create a custom query component that returns both the query engine and context?
I tried it the following way but there are errors regarding the index not being initialized when I run the query engine itself:
This function converts the survey yaml file to a vector store that can be used as context with the SQL
class LoadQuestions(CustomQueryComponent):
Beta Was this translation helpful? Give feedback.
All reactions