Update my_memory_logic.py
Browse files- my_memory_logic.py +20 -12
my_memory_logic.py
CHANGED
@@ -5,39 +5,47 @@ import os
|
|
5 |
# Import the PipelineRunnable from pipeline.py
|
6 |
from pipeline import pipeline_runnable
|
7 |
|
8 |
-
from
|
9 |
from langchain_community.chat_message_histories import ChatMessageHistory
|
10 |
-
from
|
11 |
|
12 |
###############################################################################
|
13 |
# 1) In-memory store: session_id -> ChatMessageHistory
|
14 |
###############################################################################
|
15 |
-
store = {} # e.g
|
16 |
|
17 |
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
|
|
|
|
|
|
|
|
18 |
if session_id not in store:
|
19 |
store[session_id] = ChatMessageHistory()
|
20 |
return store[session_id]
|
21 |
|
22 |
###############################################################################
|
23 |
-
# 2) RunnableWithMessageHistory
|
24 |
###############################################################################
|
|
|
|
|
25 |
conversational_rag_chain = RunnableWithMessageHistory(
|
26 |
-
pipeline_runnable,
|
27 |
-
get_session_history,
|
28 |
-
input_messages_key="input",
|
29 |
-
history_messages_key="chat_history",
|
30 |
-
output_messages_key="answer"
|
31 |
)
|
32 |
|
33 |
###############################################################################
|
34 |
-
# 3)
|
35 |
###############################################################################
|
36 |
def run_with_session_memory(user_query: str, session_id: str) -> str:
|
37 |
"""
|
38 |
-
|
39 |
-
|
40 |
"""
|
|
|
|
|
41 |
response = conversational_rag_chain.invoke(
|
42 |
{"input": user_query},
|
43 |
config={
|
|
|
5 |
# Import the PipelineRunnable from pipeline.py
|
6 |
from pipeline import pipeline_runnable
|
7 |
|
8 |
+
from langchain.schema import BaseChatMessageHistory
|
9 |
from langchain_community.chat_message_histories import ChatMessageHistory
|
10 |
+
from langchain.runnables.history import RunnableWithMessageHistory
|
11 |
|
12 |
###############################################################################
|
13 |
# 1) In-memory store: session_id -> ChatMessageHistory
|
14 |
###############################################################################
|
15 |
+
store = {} # e.g., { "abc123": ChatMessageHistory(...) }
|
16 |
|
17 |
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
18 |
+
"""
|
19 |
+
Retrieve or create a ChatMessageHistory object for the given session_id.
|
20 |
+
This ensures each session_id has its own conversation history.
|
21 |
+
"""
|
22 |
if session_id not in store:
|
23 |
store[session_id] = ChatMessageHistory()
|
24 |
return store[session_id]
|
25 |
|
26 |
###############################################################################
|
27 |
+
# 2) Create the RunnableWithMessageHistory (session-based chain)
|
28 |
###############################################################################
|
29 |
+
# This wraps your `pipeline_runnable` so it automatically reads/writes
|
30 |
+
# conversation history from get_session_history for each session.
|
31 |
conversational_rag_chain = RunnableWithMessageHistory(
|
32 |
+
pipeline_runnable, # the Runnable from pipeline.py
|
33 |
+
get_session_history, # fetches or creates ChatMessageHistory by session_id
|
34 |
+
input_messages_key="input", # key in the dict for user's new query
|
35 |
+
history_messages_key="chat_history", # key for existing chat logs
|
36 |
+
output_messages_key="answer" # key for final output
|
37 |
)
|
38 |
|
39 |
###############################################################################
|
40 |
+
# 3) A convenience function to run a query with session-based memory
|
41 |
###############################################################################
|
42 |
def run_with_session_memory(user_query: str, session_id: str) -> str:
|
43 |
"""
|
44 |
+
A helper that calls our `conversational_rag_chain`
|
45 |
+
with a given session_id. Returns the final 'answer'.
|
46 |
"""
|
47 |
+
# We invoke the chain with the user query;
|
48 |
+
# the chain automatically updates the session’s chat history.
|
49 |
response = conversational_rag_chain.invoke(
|
50 |
{"input": user_query},
|
51 |
config={
|