|
| 1 | +"""Simple Voice Agent with Memory: Personal Food Assistant. |
| 2 | +A food assistant that remembers your dietary preferences and speaks recommendations |
| 3 | +Powered by Agno + Cartesia + Mem0 |
| 4 | +
|
| 5 | +export MEM0_API_KEY=your_mem0_api_key |
| 6 | +export OPENAI_API_KEY=your_openai_api_key |
| 7 | +export CARTESIA_API_KEY=your_cartesia_api_key |
| 8 | +""" |
| 9 | + |
| 10 | +from textwrap import dedent |
| 11 | +from agno.agent import Agent |
| 12 | +from agno.models.openai import OpenAIChat |
| 13 | +from agno.tools.cartesia import CartesiaTools |
| 14 | +from agno.utils.audio import write_audio_to_file |
| 15 | +from mem0 import MemoryClient |
| 16 | + |
| 17 | +memory_client = MemoryClient() |
| 18 | +USER_ID = "food_user_01" |
| 19 | + |
| 20 | +# Agent instructions |
| 21 | +agent_instructions = dedent( |
| 22 | + """Follow these steps SEQUENTIALLY to provide personalized food recommendations with voice: |
| 23 | + 1. Analyze the user's food request and identify what type of recommendation they need. |
| 24 | + 2. Consider their dietary preferences, restrictions, and cooking habits from memory context. |
| 25 | + 3. Generate a personalized food recommendation based on their stored preferences. |
| 26 | + 4. Analyze the appropriate tone for the response (helpful, enthusiastic, cautious for allergies). |
| 27 | + 5. Call `list_voices` to retrieve available voices. |
| 28 | + 6. Select a voice that matches the helpful, friendly tone. |
| 29 | + 7. Call `text_to_speech` to generate the final audio recommendation. |
| 30 | + """ |
| 31 | +) |
| 32 | + |
| 33 | +# Simple agent that remembers food preferences |
| 34 | +food_agent = Agent( |
| 35 | + name="Personal Food Assistant", |
| 36 | + description="Provides personalized food recommendations with memory and generates voice responses using Cartesia TTS tools.", |
| 37 | + instructions=agent_instructions, |
| 38 | + model=OpenAIChat(id="gpt-4o"), |
| 39 | + tools=[CartesiaTools(voice_localize_enabled=True)], |
| 40 | + show_tool_calls=True, |
| 41 | +) |
| 42 | + |
| 43 | + |
| 44 | +def get_food_recommendation(user_query: str, user_id): |
| 45 | + """Get food recommendation with memory context""" |
| 46 | + |
| 47 | + # Search memory for relevant food preferences |
| 48 | + memories_result = memory_client.search( |
| 49 | + query=user_query, |
| 50 | + user_id=user_id, |
| 51 | + limit=5 |
| 52 | + ) |
| 53 | + |
| 54 | + # Add memory context to the message |
| 55 | + memories = [f"- {result['memory']}" for result in memories_result] |
| 56 | + memory_context = "Memories about user that might be relevant:\n" + "\n".join(memories) |
| 57 | + |
| 58 | + # Combine memory context with user request |
| 59 | + full_request = f""" |
| 60 | + {memory_context} |
| 61 | +
|
| 62 | + User: {user_query} |
| 63 | +
|
| 64 | + Answer the user query based on provided context and create a voice note. |
| 65 | + """ |
| 66 | + |
| 67 | + # Generate response with voice (same pattern as translator) |
| 68 | + food_agent.print_response(full_request) |
| 69 | + response = food_agent.run_response |
| 70 | + |
| 71 | + # Save audio file |
| 72 | + if response.audio: |
| 73 | + import time |
| 74 | + timestamp = int(time.time()) |
| 75 | + filename = f"food_recommendation_{timestamp}.mp3" |
| 76 | + write_audio_to_file( |
| 77 | + response.audio[0].base64_audio, |
| 78 | + filename=filename, |
| 79 | + ) |
| 80 | + print(f"Audio saved as {filename}") |
| 81 | + |
| 82 | + return response.content |
| 83 | + |
| 84 | + |
| 85 | +def initialize_food_memory(user_id): |
| 86 | + """Initialize memory with food preferences""" |
| 87 | + messages = [ |
| 88 | + { |
| 89 | + "role": "user", |
| 90 | + "content": "Hi, I'm Sarah. I'm vegetarian and lactose intolerant. I love spicy food, especially Thai and Indian cuisine.", |
| 91 | + }, |
| 92 | + { |
| 93 | + "role": "assistant", |
| 94 | + "content": "Hello Sarah! I've noted that you're vegetarian, lactose intolerant, and love spicy Thai and Indian food.", |
| 95 | + }, |
| 96 | + { |
| 97 | + "role": "user", |
| 98 | + "content": "I prefer quick breakfasts since I'm always rushing, but I like cooking elaborate dinners. I also meal prep on Sundays.", |
| 99 | + }, |
| 100 | + { |
| 101 | + "role": "assistant", |
| 102 | + "content": "Got it! Quick breakfasts, elaborate dinners, and Sunday meal prep. I'll remember this for future recommendations.", |
| 103 | + }, |
| 104 | + { |
| 105 | + "role": "user", |
| 106 | + "content": "I'm trying to eat more protein. I like quinoa, lentils, chickpeas, and tofu. I hate mushrooms though.", |
| 107 | + }, |
| 108 | + { |
| 109 | + "role": "assistant", |
| 110 | + "content": "Perfect! I'll focus on protein-rich options like quinoa, lentils, chickpeas, and tofu, and avoid mushrooms.", |
| 111 | + }, |
| 112 | + ] |
| 113 | + |
| 114 | + memory_client.add(messages, user_id=user_id) |
| 115 | + print("Food preferences stored in memory") |
| 116 | + |
| 117 | + |
| 118 | +# Initialize the memory for the user once in order for the agent to learn the user preference |
| 119 | +initialize_food_memory(user_id=USER_ID) |
| 120 | + |
| 121 | +print(get_food_recommendation("Which type of restaurants should I go tonight for dinner and cuisines preferred?", user_id=USER_ID)) |
| 122 | +# OUTPUT: 🎵 Audio saved as food_recommendation_1750162610.mp3 |
| 123 | +# For dinner tonight, considering your love for healthy spic optionsy, you could try a nice Thai, Indian, or Mexican restaurant. |
| 124 | +# You might find dishes with quinoa, chickpeas, tofu, and fresh herbs delightful. Enjoy your dinner! |
0 commit comments