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: Build a Node.js Companion |
| #3 | description: "Build a JavaScript fitness coach that remembers user goals run after run." |
| #4 | --- |
| #5 | |
| #6 | |
| #7 | You can create a personalized AI Companion using Mem0. This guide will walk you through the necessary steps and provide the complete code to get you started. |
| #8 | |
| #9 | ## Overview |
| #10 | |
| #11 | The Personalized AI Companion leverages Mem0 to retain information across interactions, enabling a tailored learning experience. It creates memories for each user interaction and integrates with OpenAI's GPT models to provide detailed and context-aware responses to user queries. |
| #12 | |
| #13 | ## Setup |
| #14 | |
| #15 | Before you begin, ensure you have Node.js installed and create a new project. Install the required dependencies using npm: |
| #16 | |
| #17 | ```bash |
| #18 | npm install openai mem0ai |
| #19 | ``` |
| #20 | |
| #21 | ## Full Code Example |
| #22 | |
| #23 | Below is the complete code to create and interact with an AI Companion using Mem0: |
| #24 | |
| #25 | ```javascript |
| #26 | import { OpenAI } from 'openai'; |
| #27 | import { Memory } from 'mem0ai/oss'; |
| #28 | import * as readline from 'readline'; |
| #29 | |
| #30 | const openaiClient = new OpenAI(); |
| #31 | const memory = new Memory(); |
| #32 | |
| #33 | async function chatWithMemories(message, userId = "default_user") { |
| #34 | const relevantMemories = await memory.search(message, { userId: userId }); |
| #35 | |
| #36 | const memoriesStr = relevantMemories.results |
| #37 | .map(entry => `- ${entry.memory}`) |
| #38 | .join('\n'); |
| #39 | |
| #40 | const systemPrompt = `You are a helpful AI. Answer the question based on query and memories. |
| #41 | User Memories: |
| #42 | ${memoriesStr}`; |
| #43 | |
| #44 | const messages = [ |
| #45 | { role: "system", content: systemPrompt }, |
| #46 | { role: "user", content: message } |
| #47 | ]; |
| #48 | |
| #49 | const response = await openaiClient.chat.completions.create({ |
| #50 | model: "gpt-4.1-nano-2025-04-14", |
| #51 | messages: messages |
| #52 | }); |
| #53 | |
| #54 | const assistantResponse = response.choices[0].message.content || ""; |
| #55 | |
| #56 | messages.push({ role: "assistant", content: assistantResponse }); |
| #57 | await memory.add(messages, { userId: userId }); |
| #58 | |
| #59 | return assistantResponse; |
| #60 | } |
| #61 | |
| #62 | async function main() { |
| #63 | const rl = readline.createInterface({ |
| #64 | input: process.stdin, |
| #65 | output: process.stdout |
| #66 | }); |
| #67 | |
| #68 | console.log("Chat with AI (type 'exit' to quit)"); |
| #69 | |
| #70 | const askQuestion = () => { |
| #71 | return new Promise((resolve) => { |
| #72 | rl.question("You: ", (input) => { |
| #73 | resolve(input.trim()); |
| #74 | }); |
| #75 | }); |
| #76 | }; |
| #77 | |
| #78 | try { |
| #79 | while (true) { |
| #80 | const userInput = await askQuestion(); |
| #81 | |
| #82 | if (userInput.toLowerCase() === 'exit') { |
| #83 | console.log("Goodbye!"); |
| #84 | rl.close(); |
| #85 | break; |
| #86 | } |
| #87 | |
| #88 | const response = await chatWithMemories(userInput, "sample_user"); |
| #89 | console.log(`AI: ${response}`); |
| #90 | } |
| #91 | } catch (error) { |
| #92 | console.error("An error occurred:", error); |
| #93 | rl.close(); |
| #94 | } |
| #95 | } |
| #96 | |
| #97 | main().catch(console.error); |
| #98 | ``` |
| #99 | |
| #100 | ### Key Components |
| #101 | |
| #102 | 1. **Initialization** |
| #103 | - The code initializes both OpenAI and Mem0 Memory clients |
| #104 | - Uses Node.js's built-in readline module for command-line interaction |
| #105 | |
| #106 | 2. **Memory Management (chatWithMemories function)** |
| #107 | - Retrieves relevant memories using Mem0's search functionality |
| #108 | - Constructs a system prompt that includes past memories |
| #109 | - Makes API calls to OpenAI for generating responses |
| #110 | - Stores new interactions in memory |
| #111 | |
| #112 | 3. **Interactive Chat Interface (main function)** |
| #113 | - Creates a command-line interface for user interaction |
| #114 | - Handles user input and displays AI responses |
| #115 | - Includes graceful exit functionality |
| #116 | |
| #117 | ### Environment Setup |
| #118 | |
| #119 | Make sure to set up your environment variables: |
| #120 | ```bash |
| #121 | export OPENAI_API_KEY=your_api_key |
| #122 | ``` |
| #123 | |
| #124 | ### Conclusion |
| #125 | |
| #126 | This implementation demonstrates how to create an AI Companion that maintains context across conversations using Mem0's memory capabilities. The system automatically stores and retrieves relevant information, creating a more personalized and context-aware interaction experience. |
| #127 | |
| #128 | As users interact with the system, Mem0's memory system continuously learns and adapts, making future responses more relevant and personalized. This setup is ideal for creating long-term learning AI assistants that can maintain context and provide increasingly personalized responses over time. |
| #129 | |
| #130 | --- |
| #131 | |
| #132 | <CardGroup cols={2}> |
| #133 | <Card title="Partition Memories by Entity" icon="layers" href="/cookbooks/essentials/entity-partitioning-playbook"> |
| #134 | Separate user, agent, and session context to keep your companion consistent. |
| #135 | </Card> |
| #136 | <Card title="Quickstart Demo with Mem0" icon="rocket" href="/cookbooks/companions/quickstart-demo"> |
| #137 | Run the full showcase app to see memory-powered companions in action. |
| #138 | </Card> |
| #139 | </CardGroup> |
| #140 |