Phoenix21 commited on
Commit
b5e8391
·
verified ·
1 Parent(s): 7997061

Update my_memory_logic.py

Browse files
Files changed (1) hide show
  1. my_memory_logic.py +9 -22
my_memory_logic.py CHANGED
@@ -2,41 +2,28 @@
2
 
3
  import os
4
 
5
- # Import the "run_with_chain_context" function from pipeline.py
6
- # This function must accept a dict with { "input": ..., "chat_history": ... }
7
- # and return a dict with { "answer": ... }.
8
- from pipeline import run_with_chain_context
9
 
10
- # For session-based chat history
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) In-Memory Store: 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 for the given session_id.
23
- This ensures each session_id has its own conversation transcripts.
24
- """
25
  if session_id not in store:
26
  store[session_id] = ChatMessageHistory()
27
  return store[session_id]
28
 
29
  ###############################################################################
30
- # 2) Build a RunnableWithMessageHistory that wraps "run_with_chain_context"
31
  ###############################################################################
32
- # "run_with_chain_context" must be a function returning a dict,
33
- # e.g. { "answer": "... final string ..." }
34
- # input_messages_key -> "input"
35
- # history_messages_key -> "chat_history"
36
- # output_messages_key -> "answer"
37
-
38
  conversational_rag_chain = RunnableWithMessageHistory(
39
- run_with_chain_context, # from pipeline.py
40
  get_session_history,
41
  input_messages_key="input",
42
  history_messages_key="chat_history",
@@ -44,12 +31,12 @@ conversational_rag_chain = RunnableWithMessageHistory(
44
  )
45
 
46
  ###############################################################################
47
- # 3) A convenience function that calls our chain with session-based memory
48
  ###############################################################################
49
  def run_with_session_memory(user_query: str, session_id: str) -> str:
50
  """
51
- Calls the 'conversational_rag_chain' with a given session_id and user_query.
52
- This returns the final 'answer' from run_with_chain_context.
53
  """
54
  response = conversational_rag_chain.invoke(
55
  {"input": user_query},
 
2
 
3
  import os
4
 
5
+ # Import the PipelineRunnable from pipeline.py
6
+ from pipeline import pipeline_runnable
 
 
7
 
 
8
  from langchain_core.chat_history import BaseChatMessageHistory
9
  from langchain_community.chat_message_histories import ChatMessageHistory
10
  from langchain_core.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
  if session_id not in store:
19
  store[session_id] = ChatMessageHistory()
20
  return store[session_id]
21
 
22
  ###############################################################################
23
+ # 2) RunnableWithMessageHistory referencing pipeline_runnable
24
  ###############################################################################
 
 
 
 
 
 
25
  conversational_rag_chain = RunnableWithMessageHistory(
26
+ pipeline_runnable, # The Runnable from pipeline.py
27
  get_session_history,
28
  input_messages_key="input",
29
  history_messages_key="chat_history",
 
31
  )
32
 
33
  ###############################################################################
34
+ # 3) Convenience function to run a query with session-based memory
35
  ###############################################################################
36
  def run_with_session_memory(user_query: str, session_id: str) -> str:
37
  """
38
+ Calls our `conversational_rag_chain` with session_id,
39
+ returns the final 'answer' from pipeline_runnable.
40
  """
41
  response = conversational_rag_chain.invoke(
42
  {"input": user_query},