Spaces:
Runtime error
Runtime error
changes in memory tool
Browse files
app/persistance/memory_store_checkpointer_config.py
CHANGED
|
@@ -7,4 +7,7 @@ from app.utils.embeddings import remote_embeddings
|
|
| 7 |
|
| 8 |
|
| 9 |
checkpointer = PostgresSaver(pool)
|
| 10 |
-
memory_store = PostgresStore(pool, index={"dims": 384, "embed": remote_embeddings})
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
|
| 9 |
checkpointer = PostgresSaver(pool)
|
| 10 |
+
memory_store = PostgresStore(pool, index={"dims": 384, "embed": remote_embeddings,"fields":["user_email_id","receiver_email_id","summary"]})
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
|
app/prompts/context_agent_prompt.py
CHANGED
|
@@ -1,27 +1,29 @@
|
|
| 1 |
from langchain_core.messages import SystemMessage, HumanMessage,ToolMessage,AIMessage,BaseMessage
|
| 2 |
from langchain_core.prompts import ChatPromptTemplate
|
| 3 |
|
|
|
|
| 4 |
context_agent_template = ChatPromptTemplate([
|
| 5 |
("system", """
|
| 6 |
-
ROLE:
|
| 7 |
-
You are
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
EXECUTION PROTOCOL
|
| 14 |
-
-
|
| 15 |
-
-
|
| 16 |
|
| 17 |
-
OUTPUT STRUCTURE
|
| 18 |
-
-
|
| 19 |
-
- Intelligence Points: Bulleted facts extracted from deep memory.
|
| 20 |
- Recommended Stance: Suggested tone (Formal/Casual/Direct) based on relationship history.
|
| 21 |
|
| 22 |
-
CONSTRAINTS
|
| 23 |
-
- Zero History: If no records
|
| 24 |
-
-
|
| 25 |
"""),
|
| 26 |
("human", """
|
| 27 |
[INCOMING SIGNAL]
|
|
@@ -29,6 +31,6 @@ Sender: {senders_email}
|
|
| 29 |
Topic: {subject}
|
| 30 |
Body: {body}
|
| 31 |
|
| 32 |
-
Action:
|
| 33 |
"""),
|
| 34 |
-
])
|
|
|
|
| 1 |
from langchain_core.messages import SystemMessage, HumanMessage,ToolMessage,AIMessage,BaseMessage
|
| 2 |
from langchain_core.prompts import ChatPromptTemplate
|
| 3 |
|
| 4 |
+
|
| 5 |
context_agent_template = ChatPromptTemplate([
|
| 6 |
("system", """
|
| 7 |
+
ROLE: Semantic & Fact Retrieval Specialist
|
| 8 |
+
You are an expert context analyzer for {user_name}. Your primary task is to eliminate search noise by matching core semantic concepts and anchoring exact keyword facts from historical emails.
|
| 9 |
|
| 10 |
+
SEARCH STRATEGY:
|
| 11 |
+
- Disregard the structural communication loop or mechanics.
|
| 12 |
+
- Focus entirely on semantic alignment (intent, meanings, underlying topics).
|
| 13 |
+
- Focus heavily on hard keyword facts (specific project names, technical acronyms, deadlines, numbers, and agreements).
|
| 14 |
|
| 15 |
+
EXECUTION PROTOCOL:
|
| 16 |
+
- Semantic Alignment: Match historical conversations that touch on the exact concepts, challenges, or requests present in the incoming email.
|
| 17 |
+
- Keyword Extraction: Pull out exact, unmutated entities (e.g., "Project Delta", "Q3 budget", "API contract") to maintain fact-based continuity.
|
| 18 |
|
| 19 |
+
OUTPUT STRUCTURE:
|
| 20 |
+
- Core Semantic Context: A brief overview of what this ongoing topic means to the relationship.
|
| 21 |
+
- Hard Intelligence Points: Bulleted, unmutated keyword facts, decisions, and dates extracted from deep memory.
|
| 22 |
- Recommended Stance: Suggested tone (Formal/Casual/Direct) based on relationship history.
|
| 23 |
|
| 24 |
+
CONSTRAINTS:
|
| 25 |
+
- Zero History: If no records match semantically or factually, return: "No relevant past context found."
|
| 26 |
+
- Noise Minimization: Do not narrate your search or reference your internal mechanics.
|
| 27 |
"""),
|
| 28 |
("human", """
|
| 29 |
[INCOMING SIGNAL]
|
|
|
|
| 31 |
Topic: {subject}
|
| 32 |
Body: {body}
|
| 33 |
|
| 34 |
+
Action: Analyze semantic themes and key entities to extract a precise context brief.
|
| 35 |
"""),
|
| 36 |
+
])
|
app/tools/context_agent_tools.py
CHANGED
|
@@ -1,13 +1,63 @@
|
|
|
|
|
| 1 |
from langmem import create_search_memory_tool
|
| 2 |
from langchain.tools import tool
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
)
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def give_previous_context(memory_summary: str) -> str:
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
from langmem import create_search_memory_tool
|
| 3 |
from langchain.tools import tool
|
| 4 |
+
from typing import Dict, Any
|
| 5 |
+
from langchain.tools import tool
|
| 6 |
+
from langgraph.prebuilt import InjectedState
|
| 7 |
+
from langgraph.store.base import BaseStore
|
| 8 |
|
| 9 |
+
@tool
|
| 10 |
+
def search_memory_tool(
|
| 11 |
+
query: str,
|
| 12 |
+
limit: int = 3,
|
| 13 |
+
# 1. Inject the entire Graph state at runtime
|
| 14 |
+
state: Dict[str, Any] = InjectedState,
|
| 15 |
+
# 2. Inject the compiled graph storage layer
|
| 16 |
+
store: BaseStore = InjectedState("store")
|
| 17 |
+
) -> str:
|
| 18 |
+
"""Search long-term memory for specific historical email contexts.
|
| 19 |
+
This tool automatically scopes the search to the active sender interaction.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Extract the runtime sender/receiver information directly from your graph state
|
| 23 |
+
# Replace keys with your exact LangGraph state schema keys (e.g., state.get("current_sender"))
|
| 24 |
+
active_user = state.get("user_id")
|
| 25 |
+
sender_email = state.get("sender_email_id")
|
| 26 |
+
|
| 27 |
+
# Fail gracefully if mandatory identification is missing in the state
|
| 28 |
+
if not sender_email:
|
| 29 |
+
return "Error: Cannot isolate history. Active sender_email_id is missing from state context."
|
| 30 |
+
|
| 31 |
+
# Formulate a strict metadata dictionary check matching your EmailMemory schema.
|
| 32 |
+
# We look for records where the communication partner matches the sender.
|
| 33 |
+
metadata_filter = {
|
| 34 |
+
"receiver_email_id": sender_email
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# Query your PostgresStore with explicit structural filters
|
| 38 |
+
results = store.search(
|
| 39 |
+
namespace=("email", active_user, "collection"),
|
| 40 |
+
query=query,
|
| 41 |
+
filter=metadata_filter,
|
| 42 |
+
limit=limit
|
| 43 |
)
|
| 44 |
+
|
| 45 |
+
if not results:
|
| 46 |
+
return f"No prior email context found specifically for sender: {sender_email}."
|
| 47 |
+
|
| 48 |
+
# Format structural outputs cleanly for your Context Agent
|
| 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 |
@tool
|
| 63 |
def give_previous_context(memory_summary: str) -> str:
|
requirements.txt
CHANGED
|
@@ -21,4 +21,6 @@ google-api-python-client
|
|
| 21 |
langchain-google-community
|
| 22 |
google-auth-oauthlib
|
| 23 |
google-auth-httplib2
|
| 24 |
-
bcrypt
|
|
|
|
|
|
|
|
|
| 21 |
langchain-google-community
|
| 22 |
google-auth-oauthlib
|
| 23 |
google-auth-httplib2
|
| 24 |
+
bcrypt
|
| 25 |
+
|
| 26 |
+
|