mirror of
https://github.com/hwchase17/langchain
synced 2024-11-06 03:20:49 +00:00
Improve ConversationKGMemory and its function load_memory_variables (#1999)
1. Removed the `summaries` dictionary in favor of directly appending to the summary_strings list, which avoids the unnecessary double-loop. 2. Simplified the logic for populating the `context` variable. Co-created with GPT-4 @agihouse
This commit is contained in:
parent
a5bf8c9b9d
commit
c33e055f17
@ -41,27 +41,23 @@ class ConversationKGMemory(BaseChatMemory, BaseModel):
|
||||
def load_memory_variables(self, inputs: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""Return history buffer."""
|
||||
entities = self._get_current_entities(inputs)
|
||||
summaries = {}
|
||||
|
||||
summary_strings = []
|
||||
for entity in entities:
|
||||
knowledge = self.kg.get_entity_knowledge(entity)
|
||||
if knowledge:
|
||||
summaries[entity] = ". ".join(knowledge) + "."
|
||||
summary = f"On {entity}: {'. '.join(knowledge)}."
|
||||
summary_strings.append(summary)
|
||||
context: Union[str, List]
|
||||
if summaries:
|
||||
summary_strings = [
|
||||
f"On {entity}: {summary}" for entity, summary in summaries.items()
|
||||
if not summary_strings:
|
||||
context = [] if self.return_messages else ""
|
||||
elif self.return_messages:
|
||||
context = [
|
||||
self.summary_message_cls(content=text) for text in summary_strings
|
||||
]
|
||||
if self.return_messages:
|
||||
context = [
|
||||
self.summary_message_cls(content=text) for text in summary_strings
|
||||
]
|
||||
else:
|
||||
context = "\n".join(summary_strings)
|
||||
else:
|
||||
if self.return_messages:
|
||||
context = []
|
||||
else:
|
||||
context = ""
|
||||
context = "\n".join(summary_strings)
|
||||
|
||||
return {self.memory_key: context}
|
||||
|
||||
@property
|
||||
|
Loading…
Reference in New Issue
Block a user