# Define key to pass information about entities into prompt.    memory_key: str = "entities"    def clear(self):        self.entities = {}    @property    def memory_variables(self) -> List[str]:        """Define the variables we are providing to the prompt."""        return [self.memory_key]    def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:        """Load the memory variables, in this case the entity key."""        # Get the input text and run through spaCy        doc = nlp(inputs[list(inputs.keys())[0]])        # Extract known information about entities, if they exist.        entities = [            self.entities[str(ent)] for ent in doc.ents if str(ent) in self.entities        ]        # Return combined information about entities to put into context.        return {self.memory_key: "\n".join(entities)}    def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:        """Save context from this conversation to buffer."""        # Get the input text and run through spaCy        text = inputs[list(inputs.keys())[0]]        doc = nlp(text)        # For each entity that was mentioned, save this information to the dictionary.        for ent in doc.ents:            ent_str = str(ent)            if ent_str in self.entities:                self.entities[ent_str] += f"\n{text}"            else:                self.entities[ent_str] = textWe now define a prompt that takes in information about entities as well as user input.from langchain.prompts.prompt import PromptTemplatetemplate = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. You are provided with information about entities the Human mentions, if relevant.Relevant entity information:{entities}Conversation:Human: {input}AI:"""prompt = PromptTemplate(input_variables=["entities",
Although there are a few predefined types of memory in LangChain, it is highly possible you will want to add your own type of memory that is optimal for your application. This notebook covers how to do that.
Although there are a few predefined types of memory in LangChain, it is highly possible you will want to add your own type of memory that is optimal for your application. This notebook covers how to do that. ->: # Define key to pass information about entities into prompt.    memory_key: str = "entities"    def clear(self):        self.entities = {}    @property    def memory_variables(self) -> List[str]:        """Define the variables we are providing to the prompt."""        return [self.memory_key]    def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, str]:        """Load the memory variables, in this case the entity key."""        # Get the input text and run through spaCy        doc = nlp(inputs[list(inputs.keys())[0]])        # Extract known information about entities, if they exist.        entities = [            self.entities[str(ent)] for ent in doc.ents if str(ent) in self.entities        ]        # Return combined information about entities to put into context.        return {self.memory_key: "\n".join(entities)}    def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> None:        """Save context from this conversation to buffer."""        # Get the input text and run through spaCy        text = inputs[list(inputs.keys())[0]]        doc = nlp(text)        # For each entity that was mentioned, save this information to the dictionary.        for ent in doc.ents:            ent_str = str(ent)            if ent_str in self.entities:                self.entities[ent_str] += f"\n{text}"            else:                self.entities[ent_str] = textWe now define a prompt that takes in information about entities as well as user input.from langchain.prompts.prompt import PromptTemplatetemplate = """The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know. You are provided with information about entities the Human mentions, if relevant.Relevant entity information:{entities}Conversation:Human: {input}AI:"""prompt = PromptTemplate(input_variables=["entities",