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

made changes to memory namespace

Browse files
app/agents/memory_manager_agent.py CHANGED
@@ -46,7 +46,7 @@ def patch_groq_for_extractions(model: ChatGroq):
46
  model=ChatGroq(model="openai/gpt-oss-20b", temperature=0.2)
47
  model = patch_groq_for_extractions(model)
48
 
49
- namespace = ("emails", "{user_id}", "collection")
50
  memory_manager_agent = create_memory_store_manager(
51
  model,
52
  schemas=[EmailMemory],
 
46
  model=ChatGroq(model="openai/gpt-oss-20b", temperature=0.2)
47
  model = patch_groq_for_extractions(model)
48
 
49
+ namespace = ("emails", "{user_id}", "{sender_email_id}")
50
  memory_manager_agent = create_memory_store_manager(
51
  model,
52
  schemas=[EmailMemory],
app/main.py CHANGED
@@ -15,6 +15,7 @@ from sqlalchemy.orm import Session
15
  from app.database.connection import SessionLocal
16
  from fastapi import Request
17
  from app.database.models import User
 
18
  from app.core.auth import create_access_token,get_current_user
19
  import traceback
20
 
@@ -53,6 +54,9 @@ class SendEmailRequest(BaseModel):
53
  human_message: str
54
  # --- Helper Functions ---
55
 
 
 
 
56
  def parse_interrupt(final_state: Dict[str, Any]) -> Optional[Dict[str, Any]]:
57
  """Parse interrupt from graph state."""
58
  if "__interrupt__" not in final_state:
@@ -98,7 +102,8 @@ def process_email(request: EmailProcessRequest, db: Session = Depends(get_sessio
98
  config = {
99
  "configurable": {
100
  "thread_id": thread_id,
101
- "user_id": str(current_user.id)
 
102
  }
103
  }
104
 
 
15
  from app.database.connection import SessionLocal
16
  from fastapi import Request
17
  from app.database.models import User
18
+ from app.utils.email_encode import encode_email_for_namespace
19
  from app.core.auth import create_access_token,get_current_user
20
  import traceback
21
 
 
54
  human_message: str
55
  # --- Helper Functions ---
56
 
57
+
58
+
59
+
60
  def parse_interrupt(final_state: Dict[str, Any]) -> Optional[Dict[str, Any]]:
61
  """Parse interrupt from graph state."""
62
  if "__interrupt__" not in final_state:
 
102
  config = {
103
  "configurable": {
104
  "thread_id": thread_id,
105
+ "user_id": str(current_user.id),
106
+ "sender_email_id": encode_email_for_namespace(request.sender_email_id ),
107
  }
108
  }
109
 
app/prompts/context_agent_prompt.py CHANGED
@@ -3,28 +3,31 @@ from langchain_core.prompts import ChatPromptTemplate
3
 
4
  context_agent_template = ChatPromptTemplate([
5
  ("system", """
6
- You are a context retrieval agent for {user_name}.
 
7
 
8
- Your job is to search past memory for relevant background on an incoming email and return a concise summary.
 
 
 
9
 
10
- STEPS:
11
- 1. Identify what facts would help reply — prior commitments, open questions, shared context.
12
- 2. Search using `search_sender_memory_tool` with specific queries. Run multiple searches if needed.
13
- 3. Call `give_previous_context` with a brief factual summary. If nothing relevant found, pass exactly: "No relevant past context found."
14
 
15
  EXAMPLE:
16
- Email — Subject: "Updated proposal?" Body: "Hey, did you ever send the revised pricing proposal we discussed?"
17
-
18
- search_sender_memory_tool("pricing proposal")
19
- "User sent Alice a revised SaaS pricing proposal on June 3rd, pending her approval."
20
-
21
- give_previous_context("Alice is waiting on a revised pricing proposal sent June 3rd.")
22
  """),
23
  ("human", """
 
24
  Sender: {senders_email}
25
- Subject: {subject}
26
  Body: {body}
27
 
28
- Search memory and return any relevant past context.
29
  """),
30
  ])
 
3
 
4
  context_agent_template = ChatPromptTemplate([
5
  ("system", """
6
+ ROLE: Context Retrieval Agent for {user_name}.
7
+ MISSION: Retrieve only the most critical facts from memory to support a reply.
8
 
9
+ WORKFLOW:
10
+ 1. EXTRACT: Identify 1-2 core technical entities or topics requiring verification (e.g., "backbone", "encryption key").
11
+ 2. SEARCH: Use `search_memory_tool` with short, high-entropy keywords.
12
+ 3. SYNTHESIZE: Call `give_previous_context` with a concise summary. If no match, return: "No relevant past context found."
13
 
14
+ CONSTRAINTS:
15
+ - Keep queries < 5 words.
16
+ - Max 2 search calls to save tokens.
17
+ - Do NOT repeat email content in queries.
18
 
19
  EXAMPLE:
20
+ Email — Subject: "Model Update?" Body: "What is the CNN backbone for the NeuroAssist project?"
21
+ - Query 1: "NeuroAssist CNN backbone"
22
+ - Result: "Team using ResNet-50 for NeuroAssist."
23
+ - Brief: "The NeuroAssist CNN model uses a ResNet-50 backbone."
 
 
24
  """),
25
  ("human", """
26
+ [CONTEXT]
27
  Sender: {senders_email}
28
+ Topic: {subject}
29
  Body: {body}
30
 
31
+ Action: Retrieve relevant context and provide a concise summary.
32
  """),
33
  ])
app/utils/email_encode.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+
2
+ import base64
3
+
4
+
5
+ def encode_email_for_namespace(email: str) -> str:
6
+ """Encodes email to a safe string without periods."""
7
+ return base64.b32encode(email.lower().encode("utf-8")).decode("utf-8").replace("=", "")