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: 'Pipecat' |
| #3 | description: 'Integrate Mem0 with Pipecat for conversational memory in AI agents' |
| #4 | --- |
| #5 | |
| #6 | # Pipecat Integration |
| #7 | |
| #8 | Mem0 seamlessly integrates with [Pipecat](https://pipecat.ai), providing long-term memory capabilities for conversational AI agents. This integration allows your Pipecat-powered applications to remember past conversations and provide personalized responses based on user history. |
| #9 | |
| #10 | ## Installation |
| #11 | |
| #12 | To use Mem0 with Pipecat, install the required dependencies: |
| #13 | |
| #14 | ```bash |
| #15 | pip install "pipecat-ai[mem0]" |
| #16 | ``` |
| #17 | |
| #18 | You'll also need to set up your Mem0 API key as an environment variable: |
| #19 | |
| #20 | ```bash |
| #21 | export MEM0_API_KEY=your_mem0_api_key |
| #22 | ``` |
| #23 | |
| #24 | You can obtain a Mem0 API key by signing up at [mem0.ai](https://mem0.ai). |
| #25 | |
| #26 | ## Configuration |
| #27 | |
| #28 | Mem0 integration is provided through the `Mem0MemoryService` class in Pipecat. Here's how to configure it: |
| #29 | |
| #30 | ```python |
| #31 | from pipecat.services.mem0 import Mem0MemoryService |
| #32 | |
| #33 | memory = Mem0MemoryService( |
| #34 | api_key=os.getenv("MEM0_API_KEY"), # Your Mem0 API key |
| #35 | user_id="unique_user_id", # Unique identifier for the end user |
| #36 | agent_id="my_agent", # Identifier for the agent using the memory |
| #37 | run_id="session_123", # Optional: specific conversation session ID |
| #38 | params={ # Optional: configuration parameters |
| #39 | "search_limit": 10, # Maximum memories to retrieve per query |
| #40 | "search_threshold": 0.1, # Relevance threshold (0.0 to 1.0) |
| #41 | "system_prompt": "Here are your past memories:", # Custom prefix for memories |
| #42 | "add_as_system_message": True, # Add memories as system (True) or user (False) message |
| #43 | "position": 1, # Position in context to insert memories |
| #44 | } |
| #45 | ) |
| #46 | ``` |
| #47 | |
| #48 | ## Pipeline Integration |
| #49 | |
| #50 | The `Mem0MemoryService` should be positioned between your context aggregator and LLM service in the Pipecat pipeline: |
| #51 | |
| #52 | ```python |
| #53 | pipeline = Pipeline([ |
| #54 | transport.input(), |
| #55 | stt, # Speech-to-text for audio input |
| #56 | user_context, # User context aggregator |
| #57 | memory, # Mem0 Memory service enhances context here |
| #58 | llm, # LLM for response generation |
| #59 | tts, # Optional: Text-to-speech |
| #60 | transport.output(), |
| #61 | assistant_context # Assistant context aggregator |
| #62 | ]) |
| #63 | ``` |
| #64 | |
| #65 | ## Example: Voice Agent with Memory |
| #66 | |
| #67 | Here's a complete example of a Pipecat voice agent with Mem0 memory integration: |
| #68 | |
| #69 | ```python |
| #70 | import asyncio |
| #71 | import os |
| #72 | from fastapi import FastAPI, WebSocket |
| #73 | |
| #74 | from pipecat.frames.frames import TextFrame |
| #75 | from pipecat.pipeline.pipeline import Pipeline |
| #76 | from pipecat.pipeline.task import PipelineTask |
| #77 | from pipecat.pipeline.runner import PipelineRunner |
| #78 | from pipecat.services.mem0 import Mem0MemoryService |
| #79 | from pipecat.services.openai import OpenAILLMService, OpenAIUserContextAggregator, OpenAIAssistantContextAggregator |
| #80 | from pipecat.transports.network.fastapi_websocket import ( |
| #81 | FastAPIWebsocketTransport, |
| #82 | FastAPIWebsocketParams |
| #83 | ) |
| #84 | from pipecat.serializers.protobuf import ProtobufFrameSerializer |
| #85 | from pipecat.audio.vad.silero import SileroVADAnalyzer |
| #86 | from pipecat.services.whisper import WhisperSTTService |
| #87 | |
| #88 | app = FastAPI() |
| #89 | |
| #90 | @app.websocket("/chat") |
| #91 | async def websocket_endpoint(websocket: WebSocket): |
| #92 | await websocket.accept() |
| #93 | |
| #94 | # Basic setup with minimal configuration |
| #95 | user_id = "alice" |
| #96 | |
| #97 | # WebSocket transport |
| #98 | transport = FastAPIWebsocketTransport( |
| #99 | websocket=websocket, |
| #100 | params=FastAPIWebsocketParams( |
| #101 | audio_out_enabled=True, |
| #102 | vad_enabled=True, |
| #103 | vad_analyzer=SileroVADAnalyzer(), |
| #104 | vad_audio_passthrough=True, |
| #105 | serializer=ProtobufFrameSerializer(), |
| #106 | ) |
| #107 | ) |
| #108 | |
| #109 | # Core services |
| #110 | user_context = OpenAIUserContextAggregator() |
| #111 | assistant_context = OpenAIAssistantContextAggregator() |
| #112 | stt = WhisperSTTService(api_key=os.getenv("OPENAI_API_KEY")) |
| #113 | |
| #114 | # Memory service - the key component |
| #115 | memory = Mem0MemoryService( |
| #116 | api_key=os.getenv("MEM0_API_KEY"), |
| #117 | user_id=user_id, |
| #118 | agent_id="fastapi_memory_bot" |
| #119 | ) |
| #120 | |
| #121 | # LLM for response generation |
| #122 | llm = OpenAILLMService( |
| #123 | api_key=os.getenv("OPENAI_API_KEY"), |
| #124 | model="gpt-3.5-turbo", |
| #125 | system_prompt="You are a helpful assistant that remembers past conversations." |
| #126 | ) |
| #127 | |
| #128 | # Simple pipeline |
| #129 | pipeline = Pipeline([ |
| #130 | transport.input(), |
| #131 | stt, # Speech-to-text for audio input |
| #132 | user_context, |
| #133 | memory, # Memory service enhances context here |
| #134 | llm, |
| #135 | transport.output(), |
| #136 | assistant_context |
| #137 | ]) |
| #138 | |
| #139 | # Run the pipeline |
| #140 | runner = PipelineRunner() |
| #141 | task = PipelineTask(pipeline) |
| #142 | |
| #143 | # Event handlers for WebSocket connections |
| #144 | @transport.event_handler("on_client_connected") |
| #145 | async def on_client_connected(transport, client): |
| #146 | # Send welcome message when client connects |
| #147 | await task.queue_frame(TextFrame("Hello! I'm a memory bot. I'll remember our conversation.")) |
| #148 | |
| #149 | @transport.event_handler("on_client_disconnected") |
| #150 | async def on_client_disconnected(transport, client): |
| #151 | # Clean up when client disconnects |
| #152 | await task.cancel() |
| #153 | |
| #154 | await runner.run(task) |
| #155 | |
| #156 | if __name__ == "__main__": |
| #157 | import uvicorn |
| #158 | uvicorn.run(app, host="0.0.0.0", port=8000) |
| #159 | ``` |
| #160 | |
| #161 | ## How It Works |
| #162 | |
| #163 | When integrated with Pipecat, Mem0 provides two key functionalities: |
| #164 | |
| #165 | ### 1. Message Storage |
| #166 | |
| #167 | All conversation messages are automatically stored in Mem0 for future reference: |
| #168 | - Captures the full message history from context frames |
| #169 | - Associates messages with the specified user, agent, and run IDs |
| #170 | - Stores metadata to enable efficient retrieval |
| #171 | |
| #172 | ### 2. Memory Retrieval |
| #173 | |
| #174 | When a new user message is detected: |
| #175 | 1. The message is used as a search query to find relevant past memories |
| #176 | 2. Relevant memories are retrieved from Mem0's database |
| #177 | 3. Memories are formatted and added to the conversation context |
| #178 | 4. The enhanced context is passed to the LLM for response generation |
| #179 | |
| #180 | ## Additional Configuration Options |
| #181 | |
| #182 | ### Memory Search Parameters |
| #183 | |
| #184 | You can customize how memories are retrieved and used: |
| #185 | |
| #186 | ```python |
| #187 | memory = Mem0MemoryService( |
| #188 | api_key=os.getenv("MEM0_API_KEY"), |
| #189 | user_id="user123", |
| #190 | params={ |
| #191 | "search_limit": 5, # Retrieve up to 5 memories |
| #192 | "search_threshold": 0.2, # Higher threshold for more relevant matches |
| #193 | } |
| #194 | ) |
| #195 | ``` |
| #196 | |
| #197 | ### Memory Presentation Options |
| #198 | |
| #199 | Control how memories are presented to the LLM: |
| #200 | |
| #201 | ```python |
| #202 | memory = Mem0MemoryService( |
| #203 | api_key=os.getenv("MEM0_API_KEY"), |
| #204 | user_id="user123", |
| #205 | params={ |
| #206 | "system_prompt": "Previous conversations with this user:", |
| #207 | "add_as_system_message": True, # Add as system message instead of user message |
| #208 | "position": 0, # Insert at the beginning of the context |
| #209 | } |
| #210 | ) |
| #211 | ``` |
| #212 | |
| #213 | <CardGroup cols={2}> |
| #214 | <Card title="LiveKit Integration" icon="video" href="/integrations/livekit"> |
| #215 | Build real-time voice and video agents |
| #216 | </Card> |
| #217 | <Card title="ElevenLabs Integration" icon="volume" href="/integrations/elevenlabs"> |
| #218 | Create conversational voice agents |
| #219 | </Card> |
| #220 | </CardGroup> |
| #221 |