repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | from agents import Agent, Runner, enable_verbose_stdout_logging, function_tool |
| #2 | from dotenv import load_dotenv |
| #3 | |
| #4 | from mem0 import MemoryClient |
| #5 | |
| #6 | enable_verbose_stdout_logging() |
| #7 | |
| #8 | load_dotenv() |
| #9 | |
| #10 | # Initialize Mem0 client |
| #11 | mem0 = MemoryClient() |
| #12 | |
| #13 | |
| #14 | # Define memory tools for the agent |
| #15 | @function_tool |
| #16 | def search_memory(query: str, user_id: str) -> str: |
| #17 | """Search through past conversations and memories""" |
| #18 | memories = mem0.search(query, user_id=user_id, limit=3) |
| #19 | if memories: |
| #20 | return "\n".join([f"- {mem['memory']}" for mem in memories]) |
| #21 | return "No relevant memories found." |
| #22 | |
| #23 | |
| #24 | @function_tool |
| #25 | def save_memory(content: str, user_id: str) -> str: |
| #26 | """Save important information to memory""" |
| #27 | mem0.add([{"role": "user", "content": content}], user_id=user_id) |
| #28 | return "Information saved to memory." |
| #29 | |
| #30 | |
| #31 | # Specialized agents |
| #32 | travel_agent = Agent( |
| #33 | name="Travel Planner", |
| #34 | instructions="""You are a travel planning specialist. Use get_user_context to |
| #35 | understand the user's travel preferences and history before making recommendations. |
| #36 | After providing your response, use store_conversation to save important details.""", |
| #37 | tools=[search_memory, save_memory], |
| #38 | model="gpt-4.1-nano-2025-04-14", |
| #39 | ) |
| #40 | |
| #41 | health_agent = Agent( |
| #42 | name="Health Advisor", |
| #43 | instructions="""You are a health and wellness advisor. Use get_user_context to |
| #44 | understand the user's health goals and dietary preferences. |
| #45 | After providing advice, use store_conversation to save relevant information.""", |
| #46 | tools=[search_memory, save_memory], |
| #47 | model="gpt-4.1-nano-2025-04-14", |
| #48 | ) |
| #49 | |
| #50 | # Triage agent with handoffs |
| #51 | triage_agent = Agent( |
| #52 | name="Personal Assistant", |
| #53 | instructions="""You are a helpful personal assistant that routes requests to specialists. |
| #54 | For travel-related questions (trips, hotels, flights, destinations), hand off to Travel Planner. |
| #55 | For health-related questions (fitness, diet, wellness, exercise), hand off to Health Advisor. |
| #56 | For general questions, you can handle them directly using available tools.""", |
| #57 | handoffs=[travel_agent, health_agent], |
| #58 | model="gpt-4.1-nano-2025-04-14", |
| #59 | ) |
| #60 | |
| #61 | |
| #62 | def chat_with_handoffs(user_input: str, user_id: str) -> str: |
| #63 | """ |
| #64 | Handle user input with automatic agent handoffs and memory integration. |
| #65 | |
| #66 | Args: |
| #67 | user_input: The user's message |
| #68 | user_id: Unique identifier for the user |
| #69 | |
| #70 | Returns: |
| #71 | The agent's response |
| #72 | """ |
| #73 | # Run the triage agent (it will automatically handoffs when needed) |
| #74 | result = Runner.run_sync(triage_agent, user_input) |
| #75 | |
| #76 | # Store the original conversation in memory |
| #77 | conversation = [{"role": "user", "content": user_input}, {"role": "assistant", "content": result.final_output}] |
| #78 | mem0.add(conversation, user_id=user_id) |
| #79 | |
| #80 | return result.final_output |
| #81 | |
| #82 | |
| #83 | # Example usage |
| #84 | # response = chat_with_handoffs("Which places should I vist?", user_id="alex") |
| #85 | # print(response) |
| #86 |