Untitled

 avatar
unknown
plain_text
a month ago
1.7 kB
6
Indexable
class RAGWithContext:
    def __init__(self, rag_system, window_size=3):
        """
        Set up the RAG system with memory for previous questions.
        
        :param rag_system: Your existing RAG system.
        :param window_size: How many previous questions to remember (default is 3).
        """
        self.rag_system = rag_system
        self.window_size = window_size
        self.history = []  # This will store the previous questions

    def ask(self, question):
        """
        Ask a question and get an answer using context from previous questions.
        
        :param question: Your current question.
        :return: The answer based on the context and current question.
        """
        # Add the new question to the history
        self.history.append(question)
        
        # Keep only the last 'window_size' questions
        if len(self.history) > self.window_size:
            self.history.pop(0)  # Remove the oldest question
        
        # Combine the history into a context string
        context = " [SEP] ".join(self.history)
        
        # Send the context to your RAG system to get an answer
        answer = self.rag_system.generate_answer(context)
        
        return answer

# Example usage (assuming 'rag_system' is your RAG system):
rag_with_context = RAGWithContext(rag_system, window_size=3)

# Ask some questions
print(rag_with_context.ask("What is the capital of France?"))
# Might output: "The capital of France is Paris."

print(rag_with_context.ask("What about Spain?"))
# Might output: "The capital of Spain is Madrid."

print(rag_with_context.ask("And Italy?"))
# Might output: "The capital of Italy is Rome."
Editor is loading...
Leave a Comment