An AI that remembers user preferences and past conversations automatically.Without Memory:
Copy
// Day 1await llm.generate("I'm vegan and allergic to peanuts");// Day 2await llm.generate("Give me a recipe");// AI: "What dietary restrictions do you have?" ❌
With Memory:
Copy
// Day 1await generateWithMemory({ prompt: "I'm vegan", userId: "alice" });// Day 2await generateWithMemory({ prompt: "Give me a recipe", userId: "alice" });// AI: "Here's a vegan recipe: ..." ✅ Remembered automatically
// First conversation: User shares preferenceconst response1 = await generateTextWithMemory({ model: "openai:gpt-4", prompt: "I'm vegan and allergic to peanuts", userId: "alice", sessionId: "profile_setup"});console.log(response1.text);// Output: "Got it! I'll remember you're vegan and have a peanut allergy."// Later: Different session, AI remembersconst response2 = await generateTextWithMemory({ model: "openai:gpt-4", prompt: "Give me a dinner recipe", userId: "alice", sessionId: "cooking_monday"});console.log(response2.text);// Output: "Here's a vegan stir-fry without peanuts: ..."// ✅ Remembered from different conversation!
Memory works across:
Different sessions (profile_setup → cooking_monday)
Start with this simplified version to understand the basics:
Copy
import osfrom alchemyst_ai import AlchemystAIalchemyst = AlchemystAI(api_key=os.environ.get("ALCHEMYST_AI_API_KEY"))# Store a memoryalchemyst.v1.context.memory.add({ "user_id": "alice", "session_id": "preferences", "content": "User said: I'm vegan and allergic to peanuts"})print("✅ Memory stored!")# Later: Retrieve memoriesresult = alchemyst.v1.context.memory.search( user_id="alice", session_id="preferences")if result and hasattr(result, 'memories'): for memory in result.memories: print(f"Found: {memory.content}") # Output: Found: User said: I'm vegan and allergic to peanuts
Expected Output:
Copy
✅ Memory stored!Found: User said: I'm vegan and allergic to peanuts
Now integrate with OpenAI for complete chat functionality:
Copy
import osfrom alchemyst_ai import AlchemystAIimport openaialchemyst = AlchemystAI(api_key=os.environ.get("ALCHEMYST_AI_API_KEY"))openai_client = openai.OpenAI()def chat_with_memory(prompt: str, user_id: str, session_id: str): """Chat function that remembers past conversations""" # 1. Get past conversations memory = alchemyst.v1.context.memory.search( user_id=user_id, session_id=session_id, limit=10 ) # 2. Build message history messages = [{"role": "system", "content": "You are a helpful assistant."}] # Add past memories to context if memory and hasattr(memory, 'memories'): for mem in memory.memories: if hasattr(mem, 'content'): messages.append({"role": "assistant", "content": mem.content}) # Add current prompt messages.append({"role": "user", "content": prompt}) # 3. Generate response with full context response = openai_client.chat.completions.create( model="gpt-4", messages=messages ) assistant_message = response.choices[0].message.content # 4. Store this conversation for next time alchemyst.v1.context.memory.add({ "user_id": user_id, "session_id": session_id, "content": f"User: {prompt}\nAssistant: {assistant_message}" }) return assistant_message# Test itresponse = chat_with_memory( prompt="I'm vegan", user_id="alice", session_id="profile")print(response)# Output: "Got it! I'll remember you're vegan."response2 = chat_with_memory( prompt="Give me a recipe", user_id="alice", session_id="cooking")print(response2)# Output: "Here's a vegan pasta recipe: ..."
Expected Output:
Copy
Got it! I'll remember you're vegan.Here's a vegan pasta recipe: ...
import AlchemystAI from '@alchemystai/sdk';const client = new AlchemystAI({ apiKey: process.env.ALCHEMYST_AI_API_KEY,});// Update specific memoryawait client.v1.context.memory.update({ userId: "alice", sessionId: "profile_setup", messageId: "msg_001", content: "Updated: I'm vegan and gluten-free"});// Delete a specific conversationawait client.v1.context.memory.delete({ userId: "alice", sessionId: "profile_setup"});// Delete ALL memories for a user (use with caution!)await client.v1.context.memory.delete({ userId: "alice"});console.log("✅ Memory updated/deleted");
Copy
# Update specific memoryalchemyst.v1.context.memory.update( user_id="alice", session_id="profile_setup", message_id="msg_001", content="Updated: I'm vegan and gluten-free")# Delete a specific conversationalchemyst.v1.context.memory.delete( user_id="alice", session_id="profile_setup")# Delete ALL memories for a user (use with caution!)alchemyst.v1.context.memory.delete( user_id="alice")print("✅ Memory updated/deleted")
Handle group chats where multiple users participate in the same thread:
TypeScript
Python
Copy
// User 1 starts discussionawait generateTextWithMemory({ model: "openai:gpt-4", prompt: "What are React hooks best practices?", userId: "alice", sessionId: "team_discussion_001"});// User 2 joins same discussionawait generateTextWithMemory({ model: "openai:gpt-4", prompt: "Can you elaborate on useEffect?", userId: "bob", sessionId: "team_discussion_001" // ← Same session = shared context});// User 1 continues - AI has full thread contextawait generateTextWithMemory({ model: "openai:gpt-4", prompt: "What about custom hooks?", userId: "alice", sessionId: "team_discussion_001"});// AI has full thread context regardless of who asks
Copy
# User 1 starts discussionchat_with_memory( prompt="What are React hooks best practices?", user_id="alice", session_id="team_discussion_001")# User 2 joins same discussionchat_with_memory( prompt="Can you elaborate on useEffect?", user_id="bob", session_id="team_discussion_001" # ← Same session = shared context)# User 1 continues - AI has full thread contextchat_with_memory( prompt="What about custom hooks?", user_id="alice", session_id="team_discussion_001")# AI has full thread context regardless of who asks
Key insight: Using the same sessionId across different userId values creates a shared memory space for team conversations.
Found memories: 2Memory content: [ { content: "User: I'm vegan\nAssistant: Got it!" }, { content: "User: Give me a recipe\nAssistant: Here's a vegan..." }]
Too much irrelevant context
Symptoms: AI references unrelated past conversations or gets confused.Causes:
Threshold too low
Mixing unrelated conversations in same session
Fixes:1. Raise threshold:
Copy
similarityThreshold: 0.85 // More strict
2. Use separate sessions by topic:
Copy
// ✅ Good - separate by topicsessionId: "physics_homework"sessionId: "cooking_recipes"sessionId: "movie_recommendations"// ❌ Bad - everything mixedsessionId: "general_chat"
3. Limit memory retrieval:
Copy
// Only retrieve last 5 memories instead of 10limit: 5
Memory storage failures
Error Message:
Copy
{ "error": "Failed to store memory", "code": "STORAGE_ERROR"}
Common Causes:
Invalid API key
Rate limit exceeded
Content too large
Fixes:1. Verify API key:
Copy
console.log("API Key set:", !!process.env.ALCHEMYST_AI_API_KEY);// Should output: API Key set: true
2. Check rate limits:
Free tier: 100 operations/day
Pro tier: Unlimited
3. Reduce content size:
Copy
// Keep memory entries under 10KB eachconst content = longText.slice(0, 10000); // Truncate if needed
// ✅ Good - descriptive and structuredsessionId: "support_ticket_2024_02_001"sessionId: "recipe_planning_vegan_week_5"sessionId: "project_alpha_sprint_3_planning"// ❌ Bad - hard to debugsessionId: "session1"sessionId: "chat"sessionId: "abc123"
// Store a test memoryawait client.v1.context.memory.add({ userId: "test_user", sessionId: "test_session", content: "Test memory: The user likes pizza"});// Retrieve it immediatelyconst result = await client.v1.context.memory.search({ userId: "test_user", sessionId: "test_session"});console.log("Test passed:", result.memories?.length === 1);// Expected Output: Test passed: true