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 | --- |
| #2 | title: OpenAI Agents SDK |
| #3 | --- |
| #4 | |
| #5 | Integrate [**Mem0**](https://github.com/mem0ai/mem0) with [OpenAI Agents SDK](https://github.com/openai/openai-agents-python), a lightweight framework for building multi-agent workflows. This integration enables agents to access persistent memory across conversations, enhancing context retention and personalization. |
| #6 | |
| #7 | ## Overview |
| #8 | |
| #9 | 1. Store and retrieve memories from Mem0 within OpenAI agents |
| #10 | 2. Multi-agent workflows with shared memory |
| #11 | 3. Retrieve relevant memories for past conversations |
| #12 | 4. Personalized responses based on user history |
| #13 | |
| #14 | ## Prerequisites |
| #15 | |
| #16 | Before setting up Mem0 with OpenAI Agents SDK, ensure you have: |
| #17 | |
| #18 | 1. Installed the required packages: |
| #19 | ```bash |
| #20 | pip install openai-agents mem0ai |
| #21 | ``` |
| #22 | |
| #23 | 2. Valid API keys: |
| #24 | - [Mem0 API Key](https://app.mem0.ai/dashboard/api-keys) |
| #25 | - [OpenAI API Key](https://platform.openai.com/api-keys) |
| #26 | |
| #27 | ## Basic Integration Example |
| #28 | |
| #29 | The following example demonstrates how to create an OpenAI agent with Mem0 memory integration: |
| #30 | |
| #31 | ```python |
| #32 | import os |
| #33 | from agents import Agent, Runner, function_tool |
| #34 | from mem0 import MemoryClient |
| #35 | |
| #36 | # Set up environment variables |
| #37 | os.environ["OPENAI_API_KEY"] = "your-openai-api-key" |
| #38 | os.environ["MEM0_API_KEY"] = "your-mem0-api-key" |
| #39 | |
| #40 | # Initialize Mem0 client |
| #41 | mem0 = MemoryClient() |
| #42 | |
| #43 | # Define memory tools for the agent |
| #44 | @function_tool |
| #45 | def search_memory(query: str, user_id: str) -> str: |
| #46 | """Search through past conversations and memories""" |
| #47 | memories = mem0.search(query, user_id=user_id, limit=3) |
| #48 | if memories and memories.get('results'): |
| #49 | return "\n".join([f"- {mem['memory']}" for mem in memories['results']]) |
| #50 | return "No relevant memories found." |
| #51 | |
| #52 | @function_tool |
| #53 | def save_memory(content: str, user_id: str) -> str: |
| #54 | """Save important information to memory""" |
| #55 | mem0.add([{"role": "user", "content": content}], user_id=user_id) |
| #56 | return "Information saved to memory." |
| #57 | |
| #58 | # Create agent with memory capabilities |
| #59 | agent = Agent( |
| #60 | name="Personal Assistant", |
| #61 | instructions="""You are a helpful personal assistant with memory capabilities. |
| #62 | Use the search_memory tool to recall past conversations and user preferences. |
| #63 | Use the save_memory tool to store important information about the user. |
| #64 | Always personalize your responses based on available memory.""", |
| #65 | tools=[search_memory, save_memory], |
| #66 | model="gpt-4.1-nano-2025-04-14" |
| #67 | ) |
| #68 | |
| #69 | def chat_with_agent(user_input: str, user_id: str) -> str: |
| #70 | """ |
| #71 | Handle user input with automatic memory integration. |
| #72 | |
| #73 | Args: |
| #74 | user_input: The user's message |
| #75 | user_id: Unique identifier for the user |
| #76 | |
| #77 | Returns: |
| #78 | The agent's response |
| #79 | """ |
| #80 | # Run the agent (it will automatically use memory tools when needed) |
| #81 | result = Runner.run_sync(agent, user_input) |
| #82 | |
| #83 | return result.final_output |
| #84 | |
| #85 | # Example usage |
| #86 | if __name__ == "__main__": |
| #87 | |
| #88 | # preferences will be saved in memory (using save_memory tool) |
| #89 | response_1 = chat_with_agent( |
| #90 | "I love Italian food and I'm planning a trip to Rome next month", |
| #91 | user_id="alice" |
| #92 | ) |
| #93 | print(response_1) |
| #94 | |
| #95 | # memory will be retrieved using search_memory tool to answer the user query |
| #96 | response_2 = chat_with_agent( |
| #97 | "Give me some recommendations for food", |
| #98 | user_id="alice" |
| #99 | ) |
| #100 | print(response_2) |
| #101 | ``` |
| #102 | |
| #103 | ## Multi-Agent Workflow with Handoffs |
| #104 | |
| #105 | Create multiple specialized agents with proper handoffs and shared memory: |
| #106 | |
| #107 | ```python |
| #108 | from agents import Agent, Runner, handoffs, function_tool |
| #109 | |
| #110 | # Specialized agents |
| #111 | travel_agent = Agent( |
| #112 | name="Travel Planner", |
| #113 | instructions="""You are a travel planning specialist. Use get_user_context to |
| #114 | understand the user's travel preferences and history before making recommendations. |
| #115 | After providing your response, use store_conversation to save important details.""", |
| #116 | tools=[search_memory, save_memory], |
| #117 | model="gpt-4.1-nano-2025-04-14" |
| #118 | ) |
| #119 | |
| #120 | health_agent = Agent( |
| #121 | name="Health Advisor", |
| #122 | instructions="""You are a health and wellness advisor. Use get_user_context to |
| #123 | understand the user's health goals and dietary preferences. |
| #124 | After providing advice, use store_conversation to save relevant information.""", |
| #125 | tools=[search_memory, save_memory], |
| #126 | model="gpt-4.1-nano-2025-04-14" |
| #127 | ) |
| #128 | |
| #129 | # Triage agent with handoffs |
| #130 | triage_agent = Agent( |
| #131 | name="Personal Assistant", |
| #132 | instructions="""You are a helpful personal assistant that routes requests to specialists. |
| #133 | For travel-related questions (trips, hotels, flights, destinations), hand off to the Travel Planner. |
| #134 | For health-related questions (fitness, diet, wellness, exercise), hand off to the Health Advisor. |
| #135 | For general questions, handle them directly using available tools.""", |
| #136 | handoffs=[travel_agent, health_agent], |
| #137 | model="gpt-4.1-nano-2025-04-14" |
| #138 | ) |
| #139 | |
| #140 | def chat_with_handoffs(user_input: str, user_id: str) -> str: |
| #141 | """ |
| #142 | Handle user input with automatic agent handoffs and memory integration. |
| #143 | |
| #144 | Args: |
| #145 | user_input: The user's message |
| #146 | user_id: Unique identifier for the user |
| #147 | |
| #148 | Returns: |
| #149 | The agent's response |
| #150 | """ |
| #151 | # Run the triage agent (it will automatically handoff when needed) |
| #152 | result = Runner.run_sync(triage_agent, user_input) |
| #153 | |
| #154 | # Store the original conversation in memory |
| #155 | conversation = [ |
| #156 | {"role": "user", "content": user_input}, |
| #157 | {"role": "assistant", "content": result.final_output} |
| #158 | ] |
| #159 | mem0.add(conversation, user_id=user_id) |
| #160 | |
| #161 | return result.final_output |
| #162 | |
| #163 | # Example usage |
| #164 | response = chat_with_handoffs("Plan a healthy meal for my Italy trip", user_id="alex") |
| #165 | print(response) |
| #166 | ``` |
| #167 | |
| #168 | ## Quick Start Chat Interface |
| #169 | |
| #170 | Simple interactive chat with memory: |
| #171 | |
| #172 | ```python |
| #173 | def interactive_chat(): |
| #174 | """Interactive chat interface with memory and handoffs""" |
| #175 | user_id = input("Enter your user ID: ") or "demo_user" |
| #176 | print(f"Chat started for user: {user_id}") |
| #177 | print("Type 'quit' to exit\n") |
| #178 | |
| #179 | while True: |
| #180 | user_input = input("You: ") |
| #181 | if user_input.lower() == 'quit': |
| #182 | break |
| #183 | |
| #184 | response = chat_with_handoffs(user_input, user_id) |
| #185 | print(f"Assistant: {response}\n") |
| #186 | |
| #187 | if __name__ == "__main__": |
| #188 | interactive_chat() |
| #189 | ``` |
| #190 | |
| #191 | ## Key Features |
| #192 | |
| #193 | ### 1. Automatic Memory Integration |
| #194 | - **Tool-Based Memory**: Agents use function tools to search and save memories |
| #195 | - **Conversation Storage**: All interactions are automatically stored |
| #196 | - **Context Retrieval**: Agents can access relevant past conversations |
| #197 | |
| #198 | ### 2. Multi-Agent Memory Sharing |
| #199 | - **Shared Context**: Multiple agents access the same memory store |
| #200 | - **Specialized Agents**: Create domain-specific agents with shared memory |
| #201 | - **Seamless Handoffs**: Agents maintain context across handoffs |
| #202 | |
| #203 | ### 3. Flexible Memory Operations |
| #204 | - **Retrieve Capabilities**: Retrieve relevant memories from previous conversations |
| #205 | - **User Segmentation**: Organize memories by user ID |
| #206 | - **Memory Management**: Built-in tools for saving and retrieving information |
| #207 | |
| #208 | ## Configuration Options |
| #209 | |
| #210 | Customize memory behavior: |
| #211 | |
| #212 | ```python |
| #213 | # Configure memory search |
| #214 | memories = mem0.search( |
| #215 | query="travel preferences", |
| #216 | user_id="alex", |
| #217 | limit=5 # Number of memories to retrieve |
| #218 | ) |
| #219 | |
| #220 | # Add metadata to memories |
| #221 | mem0.add( |
| #222 | messages=[{"role": "user", "content": "I prefer luxury hotels"}], |
| #223 | user_id="alex", |
| #224 | metadata={"category": "travel", "importance": "high"} |
| #225 | ) |
| #226 | ``` |
| #227 | |
| #228 | <CardGroup cols={2}> |
| #229 | <Card title="OpenAI Tool Calls Cookbook" icon="wrench" href="/cookbooks/integrations/openai-tool-calls"> |
| #230 | Learn how to integrate Mem0 with OpenAI function calling |
| #231 | </Card> |
| #232 | <Card title="Agents SDK Tool Cookbook" icon="cube" href="/cookbooks/integrations/agents-sdk-tool"> |
| #233 | Build agents with OpenAI SDK tools |
| #234 | </Card> |
| #235 | </CardGroup> |
| #236 | |
| #237 |