How can I save my onw chat history in my TXT flie? #24261
Replies: 1 comment 2 replies
-
To return the variables # Lib\site-packages\langchain\memory
def save_context(self, inputs: Dict[str, Any], outputs: Dict[str, str]) -> Dict[str, Any]:
"""Save context from this conversation to buffer and return variables A and B."""
input_str, output_str = self._get_input_output(inputs, outputs)
self.chat_memory.add_messages(
[HumanMessage(content=input_str), AIMessage(content=output_str)]
)
A = f"Input: {input_str}"
B = f"Output: {output_str}"
print(A)
print(B)
return {'A': A, 'B': B} This modification ensures that the Here is an example of how you might integrate this into your existing code: # my code
def chat_by_purpose_ET_memory_retrieval(self, who, content, Entity, source="wechat", signature=None):
self.Echat = ConversationChain(
llm=self.llm_zhipu_new,
verbose=True,
prompt=Wechat(var=content, entity=str(Entity)).Q_BAKE_TEMPLATE_END2END,
memory=ConversationEntityMemory(llm=self.llm_zhipu_air)
)
start_word = self.Q_bake_start.invoke({"name": who, "signature": signature}).content
self.wx.SendMsg_hotkey(start_word, who)
while True:
try:
friend_name, receive_msg = self.wx.GetAllMessage[-1][0], self.wx.GetAllMessage[-1][1]
if (friend_name == who) and (receive_msg != temp_msg):
print(f'【{who}】send:【{receive_msg}】')
temp_msg = receive_msg
# Entity
msg = self.Echat.predict(input=receive_msg)
if "Target_Down" in msg:
print("finish")
reply_msg = "thank you"
self.wx.SendMsg_hotkey(reply_msg, who)
return temp_msg, last_msg, content
break
else:
reply_msg = msg
pprint(f"reply_msg:【{reply_msg}】")
last_msg = reply_msg
self.wx.SendMsg_hotkey(reply_msg, who)
store_input = self.Echat.memory.entity_store.store
first_save = Memory_save(name=who, memory_entity=store_input, source=source)
print(first_save)
# Save context and get A and B
context_data = self.Echat.memory.save_context(inputs={'input': receive_msg}, outputs={'response': reply_msg})
A = context_data['A']
B = context_data['B']
# Save A and B to a TXT file
with open('chat_history.txt', 'a') as file:
file.write(f"{A}\n{B}\n")
# Entity
self.Echat.predict(response=reply_msg)
store_output = self.Echat.memory.entity_store.store
second_save = Memory_save(name=who, memory_entity=store_output, source=source)
print(second_save)
except Exception as e:
print(f"An error occurred: {e}")
pass In this example, the |
Beta Was this translation helpful? Give feedback.
-
Checked other resources
Commit to Help
Example Code
Description
How can I return the A and B from save_context in my code? so I can save this data in my way?
System Info
You are helpful assistant
Context:
{}
Current conversation:
Last line:
human: test
AI:
Input: test
Output:How can I help you?
Beta Was this translation helpful? Give feedback.
All reactions