-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
88 lines (71 loc) · 2.83 KB
/
main.py
File metadata and controls
88 lines (71 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import warnings
import uuid
from agent import create_my_agent, ContextSchema
# Suppress Pydantic serialization warnings
warnings.filterwarnings("ignore", category=UserWarning, module="pydantic")
def parse_response(response):
"""Parse agent response and extract clean message with tools used"""
result = {"message": "", "tools_used": []}
if isinstance(response, dict):
messages = response.get("messages", [])
# Extract tools used from messages
for msg in messages:
if hasattr(msg, "type") and msg.type == "tool":
tool_name = getattr(msg, "name", "unknown")
if tool_name not in result["tools_used"]:
result["tools_used"].append(tool_name)
elif isinstance(msg, dict) and msg.get("type") == "tool":
tool_name = msg.get("name", "unknown")
if tool_name not in result["tools_used"]:
result["tools_used"].append(tool_name)
# Get final message
if messages:
last_message = messages[-1]
if hasattr(last_message, "content"):
result["message"] = last_message.content
elif isinstance(last_message, dict):
result["message"] = last_message.get("content", str(last_message))
else:
result["message"] = str(response)
else:
result["message"] = str(response)
return result
def main():
print("🤖 LangChain Deep Agent with Memory started!\n")
agent = create_my_agent()
# Create a thread ID for this conversation session
thread_id = str(uuid.uuid4())
print(f"🧵 Thread ID: {thread_id}\n")
# Test questions with context and memory
test_cases = [
{
"question": "Hi, my name is Ali",
"context": ContextSchema(),
},
{
"question": "Get supplier information",
"context": ContextSchema(supplierId="SUP-001"),
},
{
"question": "What is my name?", # Test memory recall
"context": ContextSchema(),
},
]
for test in test_cases:
print(f"❓ Question: {test['question']}")
print(f"📋 Context: {test['context']}")
try:
# Pass thread_id in config to enable memory persistence
response = agent.invoke(
{"messages": [{"role": "user", "content": test["question"]}]},
config={"configurable": {"thread_id": thread_id}},
context=test["context"],
)
parsed = parse_response(response)
if parsed["tools_used"]:
print(f"🔧 Tools used: {', '.join(parsed['tools_used'])}")
print(f"✅ Response: {parsed['message']}\n")
except Exception as e:
print(f"❌ Error: {e}\n")
if __name__ == "__main__":
main()