MemSync’s semantic search goes beyond keyword matching to understand the meaning and context of your queries. This powerful feature uses vector embeddings and advanced ranking to find the most relevant memories for any situation.
MemSync understands natural language, allowing for conversational search:
Copy
queries = [ "What is the user passionate about?", "Tell me about their family", "What skills is the user learning?", "What are their career goals?", "How does the user stay healthy?", "What challenges is the user facing?"]for query in queries: memories = search_memories(query, limit=5)
# Search only career-related memoriescareer_search = { "query": "What projects is the user working on?", "categories": ["career"], "limit": 10}# Search across multiple categories learning_search = { "query": "What is the user studying?", "categories": ["learning", "career"], "limit": 15}# Search health and wellnesswellness_search = { "query": "How does the user take care of themselves?", "categories": ["health", "interests"], "limit": 8}
# Search within a specific conversation threadthread_search = { "query": "What did we discuss about the project?", "thread_id": "conversation-123", "limit": 10}# Search across specific agent's interactionsagent_search = { "query": "What preferences has the user mentioned?", "agent_id": "my-chatbot", "limit": 15}
Specific queries often work better than generic ones:
Copy
# Less effective"user info"# More effective"What is the user's professional background?""What activities does the user enjoy on weekends?""What goals is the user working towards?"
Context-Rich Queries
Include context to get more relevant results:
Copy
# Basic"user skills"# Context-rich"What technical skills does the user have for their software engineering career?""What cooking skills is the user developing for healthy eating?"
Question Format
Question-format queries often perform well:
Copy
effective_queries = [ "What does the user do for fun?", "Where does the user work?", "What is the user learning right now?", "How does the user prefer to work?", "What challenges is the user facing?"]
Intent-Based Queries
Focus on the intent behind your search:
Copy
# For personalization"What would make this user happy?""What motivates the user?"# For recommendations"What kind of content would interest the user?""What products might the user want to buy?"# For context understanding"What is important to the user right now?""What is the user struggling with?"
{ "user_bio": "Software engineer passionate about AI and outdoor activities", "memories": [ { "id": "mem_123", "memory": "Works as a Senior Software Engineer at Google focusing on ML infrastructure", "categories": ["career"], "type": "semantic", "vector_distance": 0.12, // Lower = more similar "rerank_score": 0.95, // Higher = more relevant "source": "chat", "created_at": "2024-03-20T10:00:00Z", "updated_at": "2024-03-20T10:00:00Z", "agent_id": "my-chatbot", "thread_id": "conv-456" } ]}
def interpret_results(memories): for memory in memories: relevance = "high" if memory.vector_distance < 0.3 else "medium" if memory.vector_distance < 0.6 else "low" print(f"Relevance: {relevance} - {memory.memory}") # Rerank score available if reranking was used if memory.rerank_score: quality = "excellent" if memory.rerank_score > 0.8 else "good" if memory.rerank_score > 0.6 else "fair" print(f"Quality: {quality}")
# Step 1: Broad discoveryoverview = search_memories("Tell me about this user", limit=20)# Step 2: Focus on interesting categoriesif has_career_memories(overview): career_details = search_memories( "What are the user's professional goals and challenges?", categories=["career", "learning"], limit=10 )# Step 3: Deep dive into specific areasif has_current_projects(career_details): project_details = search_memories( "What specific projects is the user working on right now?", categories=["career"], limit=5 )
def get_user_context(user_id): searches = { "professional": "What is the user's career background and goals?", "personal": "What does the user enjoy doing in their free time?", "current_focus": "What is the user currently working on or learning?", "preferences": "What are the user's preferences and dislikes?", "relationships": "Who is important to the user?" } context = {} for aspect, query in searches.items(): context[aspect] = search_memories(query, limit=8) return context
def contextual_search(current_message, conversation_history): # Extract topics from current conversation topics = extract_topics(conversation_history) # Build contextual query if "work" in topics: query = f"What has the user said about work and career? Context: {current_message}" elif "hobbies" in topics: query = f"What are the user's interests and hobbies? Context: {current_message}" else: query = f"What's relevant to: {current_message}" return search_memories(query, limit=10, rerank=True)
# Use reranking for complex queriescomplex_query = search_memories( "How does the user balance work and personal life priorities?", rerank=True, # Worth the extra processing limit=10)# Skip reranking for simple category searchessimple_query = search_memories( "user career", categories=["career"], rerank=False, # Category filtering is sufficient limit=8)
def recommend_content(user_id): # Find interests and preferences interests = search_memories( "What topics and activities interest the user?", categories=["interests", "learning"], limit=10 ) # Find current goals goals = search_memories( "What is the user trying to achieve or learn?", categories=["learning", "career", "health"], limit=8 ) return generate_recommendations(interests, goals)
def analyze_user_profile(user_id): analyses = { "expertise": search_memories("What is the user an expert in?", limit=5), "learning": search_memories("What is the user currently learning?", limit=5), "challenges": search_memories("What challenges is the user facing?", limit=5), "motivations": search_memories("What motivates and drives the user?", limit=5) } return {key: [m.memory for m in memories] for key, memories in analyses.items()}
# Try broader queriesbroad_query = search_memories("user information", limit=20)# Check if user has any memoriesall_memories = get_all_memories(user_id, limit=50)# Try different phrasingsalternative_queries = [ "What does the user do?", "User background", "Tell me about this person"]
Irrelevant Results
Possible causes:
Query too broad or ambiguous
Need reranking enabled
Categories not specified
Solutions:
Copy
# Use more specific queriesspecific_query = search_memories( "What is the user's current job title and company?", categories=["career"], limit=5)# Enable rerankingreranked_query = search_memories( "user work experience", rerank=True, limit=10)
Outdated Results
Possible causes:
Old episodic memories ranking high
Need to update or clean old memories
Solutions:
Copy
# Focus on recent memories by using current contextcurrent_query = search_memories( "What is the user currently doing and working on right now?", limit=10)# Check memory timestamps and update as neededmemories = search_memories("user status", limit=20)recent_memories = [m for m in memories if is_recent(m.updated_at)]