UPDATE MEMORY.PY WITH RESPOECT TO ALL THE CODE
Browse files- my_memory_logic.py +22 -26
my_memory_logic.py
CHANGED
|
@@ -1,48 +1,42 @@
|
|
| 1 |
# my_memory_logic.py
|
|
|
|
| 2 |
import os
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
from langchain_core.chat_history import BaseChatMessageHistory
|
| 7 |
from langchain_community.chat_message_histories import ChatMessageHistory
|
| 8 |
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 9 |
|
| 10 |
-
# We'll assume you have a `rag_chain` from your pipeline code or can import it.
|
| 11 |
-
# For example:
|
| 12 |
-
# from pipeline import rag_chain
|
| 13 |
-
|
| 14 |
-
# For demonstration, let's just define a dummy "rag_chain" that returns "answer".
|
| 15 |
-
# In your real code, import your actual chain.
|
| 16 |
-
class DummyRagChain:
|
| 17 |
-
def invoke(self, inputs):
|
| 18 |
-
# returns a dictionary with "answer"
|
| 19 |
-
return {"answer": f"Dummy answer to '{inputs['input']}'."}
|
| 20 |
-
|
| 21 |
-
rag_chain = DummyRagChain()
|
| 22 |
-
|
| 23 |
###############################################################################
|
| 24 |
-
# 1) We'll keep an in-memory store
|
| 25 |
###############################################################################
|
| 26 |
-
store = {} # { "abc123": ChatMessageHistory(...) }
|
| 27 |
|
| 28 |
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
| 29 |
"""
|
| 30 |
Retrieve or create a ChatMessageHistory object for the given session_id.
|
|
|
|
| 31 |
"""
|
| 32 |
if session_id not in store:
|
| 33 |
store[session_id] = ChatMessageHistory()
|
| 34 |
return store[session_id]
|
| 35 |
|
| 36 |
###############################################################################
|
| 37 |
-
# 2) Create the RunnableWithMessageHistory (
|
| 38 |
###############################################################################
|
| 39 |
-
#
|
|
|
|
| 40 |
conversational_rag_chain = RunnableWithMessageHistory(
|
| 41 |
-
rag_chain, #
|
| 42 |
-
get_session_history, #
|
| 43 |
-
input_messages_key="input",
|
| 44 |
-
history_messages_key="chat_history",
|
| 45 |
-
output_messages_key="answer"
|
| 46 |
)
|
| 47 |
|
| 48 |
###############################################################################
|
|
@@ -50,9 +44,11 @@ conversational_rag_chain = RunnableWithMessageHistory(
|
|
| 50 |
###############################################################################
|
| 51 |
def run_with_session_memory(user_query: str, session_id: str) -> str:
|
| 52 |
"""
|
| 53 |
-
A
|
| 54 |
-
with a
|
| 55 |
"""
|
|
|
|
|
|
|
| 56 |
response = conversational_rag_chain.invoke(
|
| 57 |
{"input": user_query},
|
| 58 |
config={
|
|
|
|
| 1 |
# my_memory_logic.py
|
| 2 |
+
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# Import your actual RAG chain (or pipeline) from pipeline.py
|
| 6 |
+
# We'll assume `rag_chain` is exposed by pipeline.py
|
| 7 |
+
from pipeline import rag_chain
|
| 8 |
+
|
| 9 |
+
# We'll import the session-based classes from langchain_core
|
| 10 |
+
# If they're in different modules, adjust accordingly.
|
| 11 |
from langchain_core.chat_history import BaseChatMessageHistory
|
| 12 |
from langchain_community.chat_message_histories import ChatMessageHistory
|
| 13 |
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
###############################################################################
|
| 16 |
+
# 1) We'll keep an in-memory store mapping session_id -> ChatMessageHistory
|
| 17 |
###############################################################################
|
| 18 |
+
store = {} # e.g., { "abc123": ChatMessageHistory(...) }
|
| 19 |
|
| 20 |
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
| 21 |
"""
|
| 22 |
Retrieve or create a ChatMessageHistory object for the given session_id.
|
| 23 |
+
This ensures each session_id has its own conversation history.
|
| 24 |
"""
|
| 25 |
if session_id not in store:
|
| 26 |
store[session_id] = ChatMessageHistory()
|
| 27 |
return store[session_id]
|
| 28 |
|
| 29 |
###############################################################################
|
| 30 |
+
# 2) Create the RunnableWithMessageHistory (session-based chain)
|
| 31 |
###############################################################################
|
| 32 |
+
# This wraps your `rag_chain` so it automatically reads/writes
|
| 33 |
+
# conversation history from get_session_history for each session.
|
| 34 |
conversational_rag_chain = RunnableWithMessageHistory(
|
| 35 |
+
rag_chain, # the main chain from pipeline.py
|
| 36 |
+
get_session_history, # fetches or creates ChatMessageHistory by session_id
|
| 37 |
+
input_messages_key="input", # key in the dict for user's new query
|
| 38 |
+
history_messages_key="chat_history", # key for existing chat logs
|
| 39 |
+
output_messages_key="answer" # key for final output
|
| 40 |
)
|
| 41 |
|
| 42 |
###############################################################################
|
|
|
|
| 44 |
###############################################################################
|
| 45 |
def run_with_session_memory(user_query: str, session_id: str) -> str:
|
| 46 |
"""
|
| 47 |
+
A helper that calls our `conversational_rag_chain`
|
| 48 |
+
with a given session_id. Returns the final 'answer'.
|
| 49 |
"""
|
| 50 |
+
# We invoke the chain with the user query;
|
| 51 |
+
# the chain automatically updates the session’s chat history.
|
| 52 |
response = conversational_rag_chain.invoke(
|
| 53 |
{"input": user_query},
|
| 54 |
config={
|