Conversation Knowledge Graph Memory

Ricardo Reis
3 min readMay 23, 2023

“Conversation Knowledge Graph Memory” é uma estrutura de memória baseada em grafos de conhecimento em uma conversação com uma AI. Esse tipo de memória usa um gráfico de conhecimento para recriar a memória.

Um gráfico de conhecimento, também conhecido como rede semântica, representa uma rede de entidades do mundo real, ou seja, objetos, eventos, situações ou conceitos — e ilustra a relação entre eles

No código a seguir, é criada uma instância de “ConversationKGMemory” que usa um “Language Model” (neste caso, a classe “OpenAI” é usada como um LLM). Essa memória é usada para salvar e carregar contextos de conversação, que são representados como dicionários de “input” e “output”.

from langchain.memory import ConversationKGMemory
from langchain.llms import OpenAI
llm = OpenAI(temperature=0)
memory = ConversationKGMemory(llm=llm)
memory.save_context({"input": "say hi to sam"}, {"ouput": "who is sam"})
memory.save_context({"input": "sam is a friend"}, {"ouput": "okay"})
memory.load_memory_variables({"input": 'who is sam'})
{'history': 'On Sam: Sam is friend.'}

Também podemos obter o histórico como uma lista de mensagens (isso é útil se você estiver usando isso com um modelo de bate-papo).

memory = ConversationKGMemory(llm=llm, return_messages=True)
memory.save_context({"input": "say hi to sam"}, {"ouput": "who is sam"})
memory.save_context({"input": "sam is a friend"}, {"ouput": "okay"})
memory.load_memory_variables({"input": 'who is sam'})
{'history': [SystemMessage(content='On Sam: Sam is friend.', additional_kwargs={})]}

Também podemos obter de forma mais modular as entidades atuais de uma nova mensagem (usará as mensagens anteriores como contexto).

memory.get_current_entities("what's Sams favorite color?")
['Sam']

Usando em uma chain

Vamos agora usar isso em uma chain!

llm = OpenAI(temperature=0)
from langchain.prompts.prompt import PromptTemplate
from langchain.chains import ConversationChain
template = """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. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate.
Relevant Information:{history}Conversation:
Human: {input}
AI:"""
prompt = PromptTemplate(
input_variables=["history", "input"], template=template
)
conversation_with_kg = ConversationChain(
llm=llm,
verbose=True,
prompt=prompt,
memory=ConversationKGMemory(llm=llm)
)
conversation_with_kg.predict(input="Hi, what's up?")
> Entering new ConversationChain chain...
Prompt after formatting:
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. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate.
Relevant Information:Conversation:
Human: Hi, what's up?
AI:
> Finished chain.
" Hi there! I'm doing great. I'm currently in the process of learning about the world around me. I'm learning about different cultures, languages, and customs. It's really fascinating! How about you?"
conversation_with_kg.predict(input="My name is James and I'm helping Will. He's an engineer.")
> Entering new ConversationChain chain...
Prompt after formatting:
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. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate.
Relevant Information:Conversation:
Human: My name is James and I'm helping Will. He's an engineer.
AI:
> Finished chain.
" Hi James, it's nice to meet you. I'm an AI and I understand you're helping Will, the engineer. What kind of engineering does he do?"
conversation_with_kg.predict(input="What do you know about Will?")
> Entering new ConversationChain chain...
Prompt after formatting:
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. The AI ONLY uses information contained in the "Relevant Information" section and does not hallucinate.
Relevant Information:On Will: Will is an engineer.Conversation:
Human: What do you know about Will?
AI:
> Finished chain.
' Will is an engineer.'

--

--