-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[Question]: How to build multiple as_chat_engine from multiple documents? #13772
Comments
To build multiple Here's a sample implementation: from llama_index.core.base.llms.types import ChatMessage
from llama_index.core.llms.llm import LLM
from llama_index.core.memory import ChatMemoryBuffer
from llama_index.core.chat_engine.condense_plus_context import CondensePlusContextChatEngine
from llama_index.core.indices.base_retriever import BaseRetriever
# Mock LLM class for demonstration purposes
class MockLLM(LLM):
def chat(self, messages):
return ChatMessage(content="This is a mock response.", role="assistant")
async def achat(self, messages):
return ChatMessage(content="This is a mock async response.", role="assistant")
# Mock Retriever class for demonstration purposes
class MockRetriever(BaseRetriever):
def retrieve(self, message):
return []
async def aretrieve(self, message):
return []
# Function to create a chat engine for a user session
def create_chat_engine(user_id):
llm = MockLLM()
retriever = MockRetriever()
memory = ChatMemoryBuffer()
chat_engine = CondensePlusContextChatEngine(
retriever=retriever,
llm=llm,
memory=memory
)
return chat_engine
# Dictionary to store chat engines for each user session
user_sessions = {}
# Function to handle user messages
def handle_user_message(user_id, message):
if user_id not in user_sessions:
user_sessions[user_id] = create_chat_engine(user_id)
chat_engine = user_sessions[user_id]
response = chat_engine.chat(message)
return response.response
# Example usage
user_id = "user123"
message = "Hello, how are you?"
response = handle_user_message(user_id, message)
print(response) # Output: This is a mock response. In this example:
This setup ensures that each user session maintains its own chat history and the model can provide valid answers based on the user's questions [1][2][3].
|
Question Validation
Question
I have 500 documents and want to use as_chat_engine to keep history chat in each session from different users . The model can choose correct indexs from user questions with valid answer. Could you recommend or suggest how to write program.
The text was updated successfully, but these errors were encountered: