Use this file to discover all available pages before exploring further.
Build AI applications with LangChain that remember context across conversations. Supermemory handles memory storage, retrieval, and user profiling while LangChain manages your conversation flow.
Supermemory automatically maintains user profiles with two types of information:
Static facts: Long-term information about the user (preferences, expertise, background)
Dynamic context: Recent activity and current focus areas
# Fetch profile with optional searchresult = memory.profile( container_tag="user_123", q="optional search query" # Also returns relevant memories)print(result.profile.static) # ["User is a Python developer", "Prefers dark mode"]print(result.profile.dynamic) # ["Currently working on API integration", "Debugging auth issues"]
Content you add is automatically processed into searchable memories:
# Store a conversationmemory.add( content="User asked about async Python patterns. Explained asyncio basics.", container_tag="user_123", metadata={"topic": "python", "type": "conversation"})# Store a documentmemory.add( content="https://docs.python.org/3/library/asyncio.html", container_tag="user_123")
Here’s a full example of a code review assistant that learns from past reviews and adapts to the user’s coding style:
import osfrom typing import Optionalfrom langchain_openai import ChatOpenAIfrom langchain_core.messages import SystemMessage, HumanMessage, AIMessagefrom langchain_core.prompts import ChatPromptTemplatefrom supermemory import Supermemoryfrom dotenv import load_dotenvload_dotenv()class CodeReviewAssistant: def __init__(self): self.llm = ChatOpenAI(model="gpt-4o", temperature=0.3) self.memory = Supermemory() def get_context(self, user_id: str, code: str) -> str: """Retrieve user profile and relevant past reviews.""" # Get profile with search for similar code patterns result = self.memory.profile( container_tag=user_id, q=code[:500], # Use code snippet for semantic search threshold=0.6 ) static = result.profile.static or [] dynamic = result.profile.dynamic or [] memories = result.search_results.results if result.search_results else [] return f"""## Developer Profile{chr(10).join(f"- {fact}" for fact in static) if static else "New developer, no profile yet."}## Current Focus{chr(10).join(f"- {ctx}" for ctx in dynamic) if dynamic else "No recent context."}## Relevant Past Reviews{chr(10).join(f"- {m.memory}" for m in memories[:3]) if memories else "No similar reviews found."}""" def review(self, user_id: str, code: str, language: Optional[str] = None) -> str: """Review code with personalized feedback.""" context = self.get_context(user_id, code) prompt = ChatPromptTemplate.from_messages([ SystemMessage(content=f"""You are a code review assistant. Provide constructive feedbacktailored to the developer's experience level and preferences.{context}Guidelines:- Reference past feedback when relevant patterns appear- Adapt explanation depth to the developer's expertise- Focus on issues that matter most to this developer"""), HumanMessage(content=f"Review this {language or 'code'}:\n\n```\n{code}\n```") ]) chain = prompt | self.llm response = chain.invoke({}) # Store the review for future context self.memory.add( content=f"Code review feedback: {response.content[:500]}", container_tag=user_id, metadata={"type": "code_review", "language": language} ) return response.content def learn_preference(self, user_id: str, preference: str): """Store a coding preference or style guideline.""" self.memory.add( content=f"Developer preference: {preference}", container_tag=user_id, metadata={"type": "preference"} )# Usageif __name__ == "__main__": assistant = CodeReviewAssistant() user_id = "dev_alice" # Teach the assistant about preferences assistant.learn_preference(user_id, "Prefers functional programming patterns") assistant.learn_preference(user_id, "Values descriptive variable names over comments") # Review some code code = """def calc(x, y): r = [] for i in x: if i in y: r.append(i) return r """ review = assistant.review(user_id, code, language="python") print(review)
# Store meeting notes as separate memoriesnotes = [ "Decided to use PostgreSQL for the new service", "Timeline: MVP ready by end of Q2", "Alice will lead the database migration"]for note in notes: memory.add( content=note, container_tag="team_standup", metadata={"date": "2024-01-15", "type": "decision"} )