import os
from agents import Agent, Runner, function_tool
from supermemory import Supermemory
from dotenv import load_dotenv
load_dotenv()
class SupportAgent:
def __init__(self):
self.memory = Supermemory()
def get_customer_context(self, customer_id: str, issue: str) -> dict:
"""Pull customer profile and past support interactions."""
result = self.memory.profile(
container_tag=customer_id,
q=issue,
threshold=0.5
)
return {
"profile": result.profile.static or [],
"recent": result.profile.dynamic or [],
"history": [m.memory for m in (result.search_results.results or [])[:3]]
}
def build_instructions(self, context: dict) -> str:
"""Turn customer context into agent instructions."""
parts = ["You are a customer support agent."]
if context["profile"]:
parts.append(f"Customer info: {', '.join(context['profile'])}")
if context["recent"]:
parts.append(f"Recent activity: {', '.join(context['recent'])}")
if context["history"]:
parts.append(f"Past issues: {'; '.join(context['history'])}")
parts.append("Be helpful and reference past interactions when relevant.")
return "\n\n".join(parts)
@function_tool
def escalate_to_human(self, reason: str) -> str:
"""Escalate the issue to a human agent.
Args:
reason: Why escalation is needed
"""
return f"Escalated: {reason}. A human agent will follow up."
@function_tool
def check_order_status(self, order_id: str) -> str:
"""Check the status of an order.
Args:
order_id: The order identifier
"""
# In reality, this would call your order system
return f"Order {order_id}: Shipped, arriving Thursday"
def create_agent(self, context: dict) -> Agent:
return Agent(
name="support",
instructions=self.build_instructions(context),
tools=[self.escalate_to_human, self.check_order_status],
model="gpt-4o"
)
async def handle(self, customer_id: str, message: str) -> str:
"""Handle a support request."""
context = self.get_customer_context(customer_id, message)
agent = self.create_agent(context)
result = await Runner.run(agent, message)
# Store the interaction
self.memory.add(
content=f"Support request: {message}\nResolution: {result.final_output}",
container_tag=customer_id,
metadata={"type": "support", "resolved": True}
)
return result.final_output
async def main():
support = SupportAgent()
# Add some customer context
support.memory.add(
content="Premium customer since 2021. Prefers email communication.",
container_tag="customer_456"
)
response = await support.handle(
"customer_456",
"My order hasn't arrived yet. Order ID is ORD-789."
)
print(response)
if __name__ == "__main__":
import asyncio
asyncio.run(main())