Gaykar commited on
Commit
b77da2b
·
1 Parent(s): 3d38571

changes to serach memory tool

Browse files
Files changed (1) hide show
  1. app/tools/context_agent_tools.py +8 -51
app/tools/context_agent_tools.py CHANGED
@@ -1,5 +1,5 @@
1
  from typing import Any
2
- # from langmem import create_search_memory_tool
3
  from typing import Dict, Any, Optional,Annotated
4
  from langchain.tools import tool,ToolRuntime
5
  from langgraph.prebuilt import InjectedState
@@ -7,56 +7,13 @@ from langgraph.store.base import BaseStore
7
  from typing import Literal
8
  # Correct import path
9
 
10
- @tool
11
- def search_sender_memory_tool(
12
- query: str,
13
- limit: int = 3,
14
- state: InjectedState = None, # Inject the LangGraph state here
15
- runtime: ToolRuntime = None # Retain your runtime for the store
16
- ) -> str:
17
- """Accepts a SINGLE string query to search the sender's history.
18
- Execute this tool multiple times if you need to search for different facts.
19
- """
20
-
21
- # 1. Pull values directly from the injected graph state safely
22
- if not state:
23
- return "Error: Graph state injection failed entirely."
24
-
25
- active_user = state.get("user_id")
26
- sender_email = state.get("sender_email_id")
27
-
28
- if not sender_email:
29
- return "Error: Cannot isolate history. Active sender_email_id is missing from state context."
30
-
31
- metadata_filter = {
32
- "content.receiver_email_id": sender_email
33
- }
34
-
35
- # 2. Access the BaseStore directly through runtime.store
36
- if not runtime or not runtime.store:
37
- return "Error: Runtime store context is missing."
38
-
39
- results = runtime.store.search(
40
- namespace=("email", active_user, "collection"),
41
- query=query,
42
- filter=metadata_filter,
43
- limit=limit
44
  )
45
-
46
- if not results:
47
- return f"No prior email context found specifically for sender: {sender_email}."
48
-
49
- formatted_memories = []
50
- for item in results:
51
- val = item.value
52
- formatted_memories.append(
53
- f"--- Past Interaction Summary ---\n"
54
- f"Sender: {val.get('user_email_id')}\n"
55
- f"Receiver: {val.get('receiver_email_id')}\n"
56
- f"Context Summary: {val.get('summary')}\n"
57
- )
58
-
59
- return "\n".join(formatted_memories)
60
 
61
 
62
 
@@ -70,4 +27,4 @@ def give_previous_context(memory_summary: str) -> str:
70
  """
71
  return memory_summary
72
 
73
- context_agent_tools=[search_sender_memory_tool,give_previous_context]
 
1
  from typing import Any
2
+ from langmem import create_search_memory_tool
3
  from typing import Dict, Any, Optional,Annotated
4
  from langchain.tools import tool,ToolRuntime
5
  from langgraph.prebuilt import InjectedState
 
7
  from typing import Literal
8
  # Correct import path
9
 
10
+ search_memory_tool = create_search_memory_tool(
11
+ namespace=(
12
+ "emails",
13
+ "{user_id}",
14
+ "{sender_email_id}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  )
16
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
 
19
 
 
27
  """
28
  return memory_summary
29
 
30
+ context_agent_tools=[search_memory_tool,give_previous_context]