Use ContextChatEngine & QueryEngine at the same time #14876
Replies: 1 comment 2 replies
-
Hey @LouisTeilliais! I'm here to assist you while waiting for a human maintainer. I can help you solve bugs, answer questions, and become a contributor. To integrate
Here's an example to illustrate these steps: from llama_index.core import (
VectorStoreIndex,
SimpleDirectoryReader,
StorageContext,
SummaryIndex
)
from llama_index.core.tools import QueryEngineTool
from llama_index.core.query_engine import ToolRetrieverRouterQueryEngine
from llama_index.core.objects import ObjectIndex
from llama_index.core.chat_engine import ContextChatEngine
from llama_index.core.retrievers import RouterRetriever
from llama_index.core.selectors import LLMSingleSelector
# Step 1: Load documents
documents1 = SimpleDirectoryReader("./data/documents1").load_data()
documents2 = SimpleDirectoryReader("./data/documents2").load_data()
# Step 2: Convert documents to nodes and create indexes
nodes1 = Settings.node_parser.get_nodes_from_documents(documents1)
nodes2 = Settings.node_parser.get_nodes_from_documents(documents2)
storage_context1 = StorageContext.from_defaults()
storage_context1.docstore.add_documents(nodes1)
storage_context2 = StorageContext.from_defaults()
storage_context2.docstore.add_documents(nodes2)
summary_index1 = SummaryIndex(nodes1, storage_context=storage_context1)
vector_index1 = VectorStoreIndex(nodes1, storage_context=storage_context1)
summary_index2 = SummaryIndex(nodes2, storage_context=storage_context2)
vector_index2 = VectorStoreIndex(nodes2, storage_context=storage_context2)
# Step 3: Convert indexes to query engines and then to tools
list_query_engine1 = summary_index1.as_query_engine(response_mode="tree_summarize", use_async=True)
vector_query_engine1 = vector_index1.as_query_engine(response_mode="tree_summarize", use_async=True)
list_query_engine2 = summary_index2.as_query_engine(response_mode="tree_summarize", use_async=True)
vector_query_engine2 = vector_index2.as_query_engine(response_mode="tree_summarize", use_async=True)
list_tool1 = QueryEngineTool.from_defaults(query_engine=list_query_engine1, description="Tool for summary index 1.")
vector_tool1 = QueryEngineTool.from_defaults(query_engine=vector_query_engine1, description="Tool for vector index 1.")
list_tool2 = QueryEngineTool.from_defaults(query_engine=list_query_engine2, description="Tool for summary index 2.")
vector_tool2 = QueryEngineTool.from_defaults(query_engine=vector_query_engine2, description="Tool for vector index 2.")
# Step 4: Create ObjectIndex and RouterQueryEngine
obj_index = ObjectIndex.from_objects([list_tool1, vector_tool1, list_tool2, vector_tool2], index_cls=VectorStoreIndex)
router_query_engine = ToolRetrieverRouterQueryEngine(obj_index.as_retriever())
# Create ContextChatEngine
context_chat_engine = ContextChatEngine.from_defaults(retriever=RouterRetriever(
selector=LLMSingleSelector.from_defaults(),
retriever_tools=[list_tool1, vector_tool1, list_tool2, vector_tool2]
))
# Now you can use the router_query_engine and context_chat_engine to handle queries
response = router_query_engine.query("What is a biography of the author's life?")
print(str(response))
response = context_chat_engine.chat("What did the author do during his time in college?")
print(str(response)) This setup allows |
Beta Was this translation helpful? Give feedback.
-
Hello,
I'm working on project that has a RouterQueryEngine based on PDF files and PostgreSQL DB.
I want to use ContextChatEngine so that I can have a chat that can hold a real conversation.
With ContextChatEngine i can hold a real conversation but not on all documents & I want to make it work with a QueryEngineTool but i can't.
The problem is that I think that ContextChatEngine only takes into account the first document, while I give it all my vectors. Does ContextChatEngine only work on one document at a time?
The second problem is that I have on one side an object of type ContextChatEngine and on the other with the SQL tool a QueryEngineTool. So I can no longer make them work together in a RouterQueryEngine. I can’t find a solution to change "tool" depending on the context.
Here is my code:
cv.py :
sql_tool.py
chat.py
If someone already has these problems I am a taker of all solutions 🙏🏻
Beta Was this translation helpful? Give feedback.
All reactions