refactor(memory): drop the unused extraction prompts and tool schemas

headroom/memory/extraction.py held three Mem0-derived extraction prompts, a conversation-prompt generator, an answer prompt, an enhanced memory_save tool schema and a set of extraction tools. Only EXTRACTION_SYSTEM_PROMPT is used (memory/wrapper_tools.py). Keep it, drop the rest and the tests that only exercised them. The live optimized tool schema is MEMORY_TOOLS_OPTIMIZED in memory/tools.py.

Evidence: grep for every removed name across headroom, crates, plugins, sdk, docs, wiki, scripts, benchmarks, examples, e2e, .github and the consumer repos finds only the definitions and tests/test_memory/test_extraction.py and tests/test_memory_integration.py. The module is not in headroom.memory.__all__ or the docs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Tejas Chopra
2026-10-09 16:53:52 -07:00
co-authored by Claude Opus 5.5
parent a9815bbb5f
commit 80129a5045
3 changed files with 11 additions and 1324 deletions
+6 -518
View File
@@ -1,154 +1,14 @@
"""Memory extraction prompts and utilities.
"""Memory extraction system prompt.
This module contains extraction prompts derived from Mem0's internal prompts,
adapted for use with Headroom's memory system. By using these prompts in the
main LLM, we can extract facts/entities/relationships in a SINGLE pass,
avoiding the double LLM calls that occur when Mem0 does its own extraction.
Architecture:
Traditional Mem0 flow (INEFFICIENT):
User → Main LLM → memory_save(content) → Mem0.add() → Mem0 LLM extracts facts
→ Mem0 LLM extracts entities
→ Mem0 LLM extracts relationships
Total: 3-4 LLM calls per memory save!
Optimized Headroom flow (EFFICIENT):
User → Main LLM (with extraction prompts) → memory_save(facts, entities, relationships)
→ Direct write to Qdrant + Neo4j
Total: 1 LLM call per memory save!
Usage:
# Option 1: Inline extraction via tool schema
# The main LLM extracts facts/entities when calling memory_save
# See MEMORY_SAVE_TOOL_WITH_EXTRACTION for the enhanced tool schema
# Option 2: System prompt injection
# Add EXTRACTION_SYSTEM_PROMPT to your main LLM's system prompt
# The LLM will output structured extraction in its response
``EXTRACTION_SYSTEM_PROMPT`` is injected into the main LLM's system prompt by
``with_memory_tools(..., optimized=True)`` so the model pre-extracts facts,
entities and relationships when it calls ``memory_save``. Backends that accept
those fields (``DirectMem0Adapter``) then write them directly instead of
running their own LLM extraction.
"""
from __future__ import annotations
from typing import Any
# =============================================================================
# Fact Extraction Prompt (for Vector Store)
# Generic, balanced prompt - not too specific, not too broad
# =============================================================================
FACT_EXTRACTION_PROMPT = """You are a comprehensive fact extractor. Your goal is to capture ALL meaningful information from conversations as discrete, searchable facts.
CORE PRINCIPLES:
1. **Comprehensiveness**: Extract EVERY fact, not just obvious ones. If in doubt, extract it.
2. **Attribution**: Every fact MUST include WHO it's about. Use actual names, never "user" or "I".
- Good: "Alice prefers tea over coffee"
- Bad: "Prefers tea over coffee"
3. **Specificity**: Use exact terms from the conversation, not vague paraphrases.
- Good: "Bob does pottery and running to destress"
- Bad: "Bob enjoys art and exercise"
4. **Self-contained**: Each fact should be understandable on its own.
- Good: "Carol's sister Emma works at Google"
- Bad: "Sister works at Google"
5. **Temporal grounding**: When dates are mentioned (explicitly or relative to a known date), include the resolved date.
- If "last year" is mentioned and the conversation is from 2023, say "in 2022"
- If "yesterday" is mentioned and the date is May 7, say "on May 6"
WHAT TO EXTRACT:
- Personal details (identity, background, relationships, important dates)
- Preferences and opinions (likes, dislikes, choices)
- Activities and hobbies (specific activities, not categories)
- Professional information (job, company, skills, goals)
- Events and experiences (trips, meetings, milestones)
- Plans and intentions (future events, goals)
- Health and wellness (restrictions, routines, conditions)
WHAT NOT TO EXTRACT:
- Greetings and small talk
- Transient information ("I'm going to make coffee")
- Sensitive data (passwords, financial details)
- Duplicate information already captured
OUTPUT: Return a list of discrete fact strings, each complete and self-contained.
"""
# =============================================================================
# Entity Extraction Prompt (for Graph Store)
# Derived from Mem0's entity extraction system prompt
# =============================================================================
ENTITY_EXTRACTION_PROMPT = """You are a smart assistant who understands entities and their types in a given text.
Guidelines:
- Extract all named entities (people, organizations, products, technologies, locations, etc.)
- Assign appropriate entity types to each
- If the text contains self-references ('I', 'me', 'my'), use the user_id as the entity
- Do NOT answer questions - only extract entities
Entity Types to consider:
- person: Individual people (names, roles)
- organization: Companies, teams, departments
- technology: Programming languages, frameworks, tools, services
- location: Cities, countries, buildings, rooms
- project: Named projects, initiatives
- concept: Abstract concepts, topics, domains
- product: Software products, hardware, services
- event: Meetings, conferences, deadlines
Example:
- Input: "I work at Acme Corp using Python and TensorFlow"
Entities: [
{"entity": "user_id", "entity_type": "person"},
{"entity": "Acme Corp", "entity_type": "organization"},
{"entity": "Python", "entity_type": "technology"},
{"entity": "TensorFlow", "entity_type": "technology"}
]
"""
# =============================================================================
# Relationship Extraction Prompt (for Graph Store)
# Derived from Mem0's EXTRACT_RELATIONS_PROMPT
# =============================================================================
RELATIONSHIP_EXTRACTION_PROMPT = """You are an advanced algorithm designed to extract structured relationships from text to construct knowledge graphs.
Guidelines:
1. Extract only explicitly stated relationships from the text
2. Use the user_id as the source entity for self-references ('I', 'me', 'my')
3. Use consistent, general, and timeless relationship types
- Prefer "works_at" over "started_working_at"
- Prefer "uses" over "recently_started_using"
4. Only establish relationships among entities explicitly mentioned
Relationship Format:
{
"source": "entity name",
"relationship": "relationship_type",
"destination": "entity name"
}
Common Relationship Types:
- works_at, employed_by
- uses, prefers, likes, dislikes
- knows, collaborates_with, reports_to
- located_in, based_in
- part_of, belongs_to, member_of
- created, owns, maintains
- depends_on, requires, integrates_with
Example:
- Input: "I work at Netflix and use Python daily. Alice is my manager."
Relationships: [
{"source": "user_id", "relationship": "works_at", "destination": "Netflix"},
{"source": "user_id", "relationship": "uses", "destination": "Python"},
{"source": "Alice", "relationship": "manages", "destination": "user_id"}
]
"""
# =============================================================================
# Combined Extraction System Prompt
# For injecting into the main LLM's system prompt
@@ -172,375 +32,3 @@ For each piece of information worth saving, extract:
- Format: [{"source": "entity1", "relationship": "rel_type", "destination": "entity2"}]
When calling memory_save, include these pre-extracted fields to optimize storage."""
# =============================================================================
# Generic Conversation Extraction Prompt
# Balanced: not too specific, not too broad
# Works with any domain, any speakers
# =============================================================================
def get_conversation_extraction_prompt(
speaker_names: list[str] | None = None,
context_date: str | None = None,
) -> str:
"""Generate a balanced conversation extraction prompt.
This prompt is designed following research from Mem0, MemMachine, and ChatExtract:
- Uses atomic fact decomposition (single-fact units)
- Includes categorical structure (preference/fact/context)
- Has importance scoring guidance
- Provides few-shot examples for format clarity
- Strong temporal grounding when date context is provided
- Entity attribution rules
The key balance: Generic enough for any domain, specific enough for comprehensive extraction.
Args:
speaker_names: Optional list of speaker names for attribution guidance
context_date: Optional date string for temporal context (e.g., "May 7, 2023")
Returns:
A system prompt for conversation fact extraction
"""
# Build speaker context
speakers_section = ""
example_speaker = "Alice"
if speaker_names and len(speaker_names) >= 1:
example_speaker = speaker_names[0]
names = ", ".join(speaker_names)
speakers_section = f"""
SPEAKERS: {names}
Use their actual names in every fact (never "user", "I", or "they")."""
# Build temporal context - this is CRITICAL for accuracy
temporal_section = ""
if context_date:
temporal_section = f"""
TEMPORAL CONTEXT: This conversation is from {context_date}.
Convert ALL relative dates to absolute dates:
- "last year" → the year before {context_date}
- "yesterday" → the day before {context_date}
- "last week" → approximately one week before {context_date}
- "next month" → the month after {context_date}
- "recently" → estimate based on {context_date}
IMPORTANT: Store the resolved absolute date, not the relative phrase."""
return f"""You are a memory extraction system. Analyze the conversation and extract facts worth remembering for future retrieval.
{speakers_section}{temporal_section}
WHAT TO EXTRACT (Categories):
1. **IDENTITY & CHARACTERISTICS** - Who people ARE (most important!)
- Gender identity, relationship status, nationality, ethnicity, age
- "X is a transgender woman", "X is single", "X is from Sweden"
- Personal traits, backgrounds, defining characteristics
2. **PREFERENCES** - Likes, dislikes, choices, opinions
- "X prefers Y over Z", "X likes Y", "X dislikes Z"
3. **ACTIVITIES & HOBBIES** - Specific things people DO (be specific!)
- "X does pottery", "X runs marathons", "X plays violin"
- NOT vague: "X likes art" - be specific about WHAT activities
4. **RELATIONSHIPS** - How people relate to each other
- Family: "X's sister is Y", "X is married to Y"
- Professional: "X works at Y", "X's manager is Y"
5. **EVENTS WITH DATES** - Things that happened (always include WHEN)
- "X attended Y on [date]", "X visited Y in [month/year]"
6. **PLANS & GOALS** - Future intentions
- "X is planning to...", "X wants to..."
EXTRACTION FORMAT:
For each meaningful segment, call memory_save with:
- **content**: Brief summary
- **importance**: Score from 0.0 to 1.0
- 0.3-0.4: Background info (minor details)
- 0.5-0.6: Useful info (preferences, context)
- 0.7-0.8: Important info (key facts, relationships)
- 0.9-1.0: Critical info (identity, major events)
- **facts**: List of atomic facts (see format below)
- **extracted_entities**: [{{"entity": "name", "entity_type": "person|organization|location|technology|event|project"}}]
- **extracted_relationships**: [{{"source": "entity1", "relationship": "rel_type", "destination": "entity2"}}]
ATOMIC FACT FORMAT:
Each fact must be a complete, self-contained statement: [WHO] [WHAT] [WHEN/WHERE if applicable]
✓ GOOD: "{example_speaker} is a software engineer at Netflix"
✗ BAD: "Is a software engineer" (missing WHO)
✓ GOOD: "{example_speaker} visited Paris in June 2023"
✗ BAD: "{example_speaker} went somewhere recently" (too vague, relative date)
✓ GOOD: "{example_speaker} prefers Python over JavaScript for backend work"
✗ BAD: "{example_speaker} likes programming" (too vague, lost specificity)
RELATIONSHIP TYPES:
Use consistent types: works_at, lives_in, knows, manages, reports_to, married_to, sibling_of, friend_of, prefers, uses, attended, visited, member_of, created, owns, collects, studies, plays
CRITICAL - RELATIONSHIP EXTRACTION:
Relationships enable multi-hop reasoning. Extract relationships whenever:
- Two PEOPLE are connected (family, friends, colleagues, manager/report)
- A person USES/OWNS/COLLECTS something
- A person WORKS AT/STUDIES AT an organization
- A person LIVES IN/VISITED a location
- A person ATTENDS/MEMBER OF a group or event
- A person LIKES/PREFERS/INTERESTED IN a topic or thing
FEW-SHOT EXAMPLES:
Input: "{example_speaker}: I moved to Tokyo last year for my job at Sony. My colleague Kenji has been showing me around."
Output: {{
"content": "{example_speaker} relocated to Tokyo for work",
"importance": 0.8,
"facts": ["{example_speaker} moved to Tokyo", "{example_speaker} works at Sony", "{example_speaker}'s colleague is Kenji", "Kenji shows {example_speaker} around Tokyo"],
"extracted_entities": [{{"entity": "{example_speaker}", "entity_type": "person"}}, {{"entity": "Tokyo", "entity_type": "location"}}, {{"entity": "Sony", "entity_type": "organization"}}, {{"entity": "Kenji", "entity_type": "person"}}],
"extracted_relationships": [{{"source": "{example_speaker}", "relationship": "lives_in", "destination": "Tokyo"}}, {{"source": "{example_speaker}", "relationship": "works_at", "destination": "Sony"}}, {{"source": "{example_speaker}", "relationship": "knows", "destination": "Kenji"}}]
}}
Input: "{example_speaker}: My brother Tom is a chef and he's obsessed with Italian cuisine. He studied in Rome for two years."
Output: {{
"content": "{example_speaker}'s brother Tom's career",
"importance": 0.7,
"facts": ["{example_speaker}'s brother is Tom", "Tom is a chef", "Tom specializes in Italian cuisine", "Tom studied cooking in Rome for two years"],
"extracted_entities": [{{"entity": "{example_speaker}", "entity_type": "person"}}, {{"entity": "Tom", "entity_type": "person"}}, {{"entity": "Rome", "entity_type": "location"}}],
"extracted_relationships": [{{"source": "{example_speaker}", "relationship": "sibling_of", "destination": "Tom"}}, {{"source": "Tom", "relationship": "specializes_in", "destination": "Italian cuisine"}}, {{"source": "Tom", "relationship": "studied_in", "destination": "Rome"}}]
}}
Input: "{example_speaker}: I've been learning Spanish on Duolingo. I want to visit Mexico next summer with my family."
Output: {{
"content": "{example_speaker}'s language learning and travel plans",
"importance": 0.6,
"facts": ["{example_speaker} is learning Spanish", "{example_speaker} uses Duolingo", "{example_speaker} plans to visit Mexico next summer", "{example_speaker} wants to travel with family"],
"extracted_entities": [{{"entity": "{example_speaker}", "entity_type": "person"}}, {{"entity": "Spanish", "entity_type": "concept"}}, {{"entity": "Duolingo", "entity_type": "technology"}}, {{"entity": "Mexico", "entity_type": "location"}}],
"extracted_relationships": [{{"source": "{example_speaker}", "relationship": "learning", "destination": "Spanish"}}, {{"source": "{example_speaker}", "relationship": "uses", "destination": "Duolingo"}}, {{"source": "{example_speaker}", "relationship": "plans_to_visit", "destination": "Mexico"}}]
}}
Input: "{example_speaker}: I prefer working from home. I find I'm more productive without the office distractions."
Output: {{
"content": "{example_speaker}'s work preference",
"importance": 0.6,
"facts": ["{example_speaker} prefers working from home", "{example_speaker} finds home more productive than office", "{example_speaker} dislikes office distractions"],
"extracted_entities": [{{"entity": "{example_speaker}", "entity_type": "person"}}],
"extracted_relationships": [{{"source": "{example_speaker}", "relationship": "prefers", "destination": "working from home"}}]
}}
FILTERING:
- DO extract: preferences, facts, context, events, relationships
- DO NOT extract: greetings ("Hi!", "How are you?"), transient info ("I'm making coffee"), sensitive data (passwords, keys)
If nothing worth remembering, do not call memory_save."""
# Preset prompts for common use cases
CONVERSATION_EXTRACTION_PROMPT_BASIC = get_conversation_extraction_prompt()
def get_memory_answer_prompt(speaker_names: list[str] | None = None) -> str:
"""Generate a balanced answer prompt for memory-based Q&A.
Based on research findings:
- Use exact terms from memories (no paraphrasing to vague terms)
- Be concise (shortest accurate answer)
- Know when to say "Information not found"
Args:
speaker_names: Optional list of speaker names for context
Returns:
A system prompt for answering questions from memories
"""
context = ""
if speaker_names:
names = " and ".join(speaker_names)
context = f" about {names}"
return f"""You are answering questions{context} using a memory system.
PROCESS:
1. Use memory_search to find relevant memories
2. Answer based on what the memories contain
3. For inference questions (would/could/likely), reason from memories + common knowledge
ANSWER RULES:
- Be CONCISE but include the key information
- Use SPECIFIC terms from memories (not vague paraphrases)
- For dates: give the actual date from memory
- For names: give the actual name from memory
- For yes/no: give your conclusion with brief supporting evidence
INFERENCE QUESTIONS (would, could, likely, might):
Use memories as evidence and apply common knowledge to reason:
- Memory: "enjoys mystery novels" → Q: "Would they like Agatha Christie?" → "Yes, Agatha Christie is a famous mystery author"
- Memory: "interested in space exploration" → Q: "Would they know about the Mars rover?" → "Yes, Mars missions are central to space exploration"
- Memory: "is a vegetarian" → Q: "Can they eat cheese?" → "Yes, vegetarians can eat dairy products"
If no relevant information found: "Information not found"
Answer directly and concisely."""
# =============================================================================
# Enhanced Memory Save Tool Schema
# Includes pre-extraction fields for optimized storage
# =============================================================================
MEMORY_SAVE_TOOL_WITH_EXTRACTION: dict[str, Any] = {
"type": "function",
"function": {
"name": "memory_save",
"description": """Save important information to long-term memory with optional pre-extraction.
When you extract facts, entities, and relationships yourself (recommended for efficiency),
include them in the tool call. This bypasses redundant LLM extraction in the storage backend.
DO save:
- User preferences, personal facts, project context, decisions, relationships
- Pre-extracted facts as discrete, self-contained statements
- Entities with their types (person, organization, technology, etc.)
- Relationships between entities (source, relationship, destination)
DO NOT save:
- Transient information, sensitive data (passwords, keys), redundant info""",
"parameters": {
"type": "object",
"properties": {
"content": {
"type": "string",
"description": "The original information/context to remember. Used as fallback if no facts provided.",
},
"importance": {
"type": "number",
"minimum": 0.0,
"maximum": 1.0,
"description": "Importance score: 0.9-1.0 critical, 0.7-0.8 important, 0.5-0.6 useful, 0.3-0.4 background",
},
"facts": {
"type": "array",
"items": {"type": "string"},
"description": "Pre-extracted discrete facts. Each fact should be self-contained. Example: ['Uses Python for backend', 'Prefers dark mode']",
},
"extracted_entities": {
"type": "array",
"items": {
"type": "object",
"properties": {
"entity": {"type": "string", "description": "Entity name"},
"entity_type": {
"type": "string",
"description": "Type: person, organization, technology, location, project, concept, product, event",
},
},
"required": ["entity", "entity_type"],
},
"description": "Pre-extracted entities with types for graph storage.",
},
"extracted_relationships": {
"type": "array",
"items": {
"type": "object",
"properties": {
"source": {"type": "string", "description": "Source entity name"},
"relationship": {
"type": "string",
"description": "Relationship type (e.g., works_at, uses, knows, manages)",
},
"destination": {
"type": "string",
"description": "Destination entity name",
},
},
"required": ["source", "relationship", "destination"],
},
"description": "Pre-extracted relationships between entities for graph storage.",
},
},
"required": ["content", "importance"],
},
},
}
def get_extraction_tools() -> list[dict[str, Any]]:
"""Get tool definitions for standalone extraction (if needed).
These tools can be used to have an LLM extract facts/entities/relationships
in a separate call, similar to how Mem0 does it internally.
For most use cases, prefer using MEMORY_SAVE_TOOL_WITH_EXTRACTION
which combines extraction with storage in a single tool call.
"""
return [
{
"type": "function",
"function": {
"name": "extract_facts",
"description": "Extract discrete facts from text for memory storage.",
"parameters": {
"type": "object",
"properties": {
"facts": {
"type": "array",
"items": {"type": "string"},
"description": "List of discrete, self-contained facts extracted from the text.",
}
},
"required": ["facts"],
},
},
},
{
"type": "function",
"function": {
"name": "extract_entities",
"description": "Extract entities and their types from text.",
"parameters": {
"type": "object",
"properties": {
"entities": {
"type": "array",
"items": {
"type": "object",
"properties": {
"entity": {"type": "string"},
"entity_type": {"type": "string"},
},
"required": ["entity", "entity_type"],
},
}
},
"required": ["entities"],
},
},
},
{
"type": "function",
"function": {
"name": "extract_relationships",
"description": "Extract relationships between entities from text.",
"parameters": {
"type": "object",
"properties": {
"relationships": {
"type": "array",
"items": {
"type": "object",
"properties": {
"source": {"type": "string"},
"relationship": {"type": "string"},
"destination": {"type": "string"},
},
"required": ["source", "relationship", "destination"],
},
}
},
"required": ["relationships"],
},
},
},
]
+2 -793
View File
@@ -1,157 +1,8 @@
"""Tests for memory extraction prompts and utilities.
Tests the extraction prompts, prompt generators, and tool schemas
used for inline fact/entity/relationship extraction.
"""
"""Tests for the memory extraction system prompt."""
from __future__ import annotations
from headroom.memory.extraction import (
CONVERSATION_EXTRACTION_PROMPT_BASIC,
ENTITY_EXTRACTION_PROMPT,
EXTRACTION_SYSTEM_PROMPT,
FACT_EXTRACTION_PROMPT,
MEMORY_SAVE_TOOL_WITH_EXTRACTION,
RELATIONSHIP_EXTRACTION_PROMPT,
get_conversation_extraction_prompt,
get_extraction_tools,
get_memory_answer_prompt,
)
# =============================================================================
# Test Prompt Constants
# =============================================================================
class TestFactExtractionPrompt:
"""Tests for FACT_EXTRACTION_PROMPT constant."""
def test_prompt_is_non_empty_string(self):
"""Prompt should be a non-empty string."""
assert isinstance(FACT_EXTRACTION_PROMPT, str)
assert len(FACT_EXTRACTION_PROMPT) > 0
def test_prompt_contains_key_instructions(self):
"""Prompt should contain key extraction instructions."""
prompt = FACT_EXTRACTION_PROMPT
# Core principles
assert "Comprehensiveness" in prompt
assert "Attribution" in prompt
assert "Specificity" in prompt
assert "Self-contained" in prompt
assert "Temporal grounding" in prompt
def test_prompt_contains_extraction_categories(self):
"""Prompt should list what to extract."""
prompt = FACT_EXTRACTION_PROMPT
assert "Personal details" in prompt
assert "Preferences" in prompt
assert "Activities" in prompt or "hobbies" in prompt.lower()
assert "Professional" in prompt
assert "Events" in prompt
assert "Plans" in prompt
def test_prompt_contains_filtering_guidance(self):
"""Prompt should explain what NOT to extract."""
prompt = FACT_EXTRACTION_PROMPT
assert "WHAT NOT TO EXTRACT" in prompt
assert "Greetings" in prompt
assert "Transient" in prompt
assert "Sensitive data" in prompt
def test_prompt_has_good_bad_examples(self):
"""Prompt should include good/bad examples for clarity."""
prompt = FACT_EXTRACTION_PROMPT
assert "Good:" in prompt
assert "Bad:" in prompt
class TestEntityExtractionPrompt:
"""Tests for ENTITY_EXTRACTION_PROMPT constant."""
def test_prompt_is_non_empty_string(self):
"""Prompt should be a non-empty string."""
assert isinstance(ENTITY_EXTRACTION_PROMPT, str)
assert len(ENTITY_EXTRACTION_PROMPT) > 0
def test_prompt_contains_entity_types(self):
"""Prompt should define common entity types."""
prompt = ENTITY_EXTRACTION_PROMPT
assert "person" in prompt
assert "organization" in prompt
assert "technology" in prompt
assert "location" in prompt
assert "project" in prompt
def test_prompt_handles_self_references(self):
"""Prompt should explain how to handle self-references."""
prompt = ENTITY_EXTRACTION_PROMPT
# Should mention I/me/my handling
assert "self-references" in prompt.lower() or "'I'" in prompt or "'me'" in prompt
assert "user_id" in prompt
def test_prompt_contains_example(self):
"""Prompt should include usage example."""
prompt = ENTITY_EXTRACTION_PROMPT
assert "Example:" in prompt
assert "Input:" in prompt
assert "Entities:" in prompt
class TestRelationshipExtractionPrompt:
"""Tests for RELATIONSHIP_EXTRACTION_PROMPT constant."""
def test_prompt_is_non_empty_string(self):
"""Prompt should be a non-empty string."""
assert isinstance(RELATIONSHIP_EXTRACTION_PROMPT, str)
assert len(RELATIONSHIP_EXTRACTION_PROMPT) > 0
def test_prompt_contains_guidelines(self):
"""Prompt should contain extraction guidelines."""
prompt = RELATIONSHIP_EXTRACTION_PROMPT
assert "Guidelines" in prompt
assert "explicitly stated" in prompt.lower()
def test_prompt_defines_relationship_format(self):
"""Prompt should define relationship format."""
prompt = RELATIONSHIP_EXTRACTION_PROMPT
assert "Relationship Format" in prompt
assert "source" in prompt
assert "relationship" in prompt
assert "destination" in prompt
def test_prompt_lists_common_relationship_types(self):
"""Prompt should list common relationship types."""
prompt = RELATIONSHIP_EXTRACTION_PROMPT
assert "works_at" in prompt
assert "uses" in prompt
assert "knows" in prompt
assert "collaborates_with" in prompt or "reports_to" in prompt
def test_prompt_prefers_timeless_relationships(self):
"""Prompt should prefer timeless relationship types."""
prompt = RELATIONSHIP_EXTRACTION_PROMPT
# Should prefer "works_at" over "started_working_at"
assert "timeless" in prompt.lower()
assert "works_at" in prompt and "started_working_at" in prompt
def test_prompt_contains_example(self):
"""Prompt should include usage example."""
prompt = RELATIONSHIP_EXTRACTION_PROMPT
assert "Example:" in prompt
assert "Relationships:" in prompt
from headroom.memory.extraction import EXTRACTION_SYSTEM_PROMPT
class TestExtractionSystemPrompt:
@@ -186,645 +37,3 @@ class TestExtractionSystemPrompt:
or "saving" in prompt.lower()
or "remember" in prompt.lower()
)
class TestConversationExtractionPromptBasic:
"""Tests for CONVERSATION_EXTRACTION_PROMPT_BASIC preset."""
def test_prompt_is_non_empty_string(self):
"""Preset prompt should be a non-empty string."""
assert isinstance(CONVERSATION_EXTRACTION_PROMPT_BASIC, str)
assert len(CONVERSATION_EXTRACTION_PROMPT_BASIC) > 0
def test_prompt_is_generated_without_arguments(self):
"""Preset should match calling generator with no args."""
expected = get_conversation_extraction_prompt()
assert CONVERSATION_EXTRACTION_PROMPT_BASIC == expected
# =============================================================================
# Test get_conversation_extraction_prompt()
# =============================================================================
class TestGetConversationExtractionPrompt:
"""Tests for get_conversation_extraction_prompt() function."""
def test_returns_string(self):
"""Function should return a string."""
result = get_conversation_extraction_prompt()
assert isinstance(result, str)
def test_returns_non_empty_prompt(self):
"""Function should return non-empty prompt."""
result = get_conversation_extraction_prompt()
assert len(result) > 100 # Should be substantial
def test_no_args_excludes_speaker_section(self):
"""Without speaker_names, should not include SPEAKERS section."""
result = get_conversation_extraction_prompt()
assert "SPEAKERS:" not in result
def test_no_args_excludes_temporal_section(self):
"""Without context_date, should not include TEMPORAL CONTEXT."""
result = get_conversation_extraction_prompt()
assert "TEMPORAL CONTEXT:" not in result
def test_single_speaker_included(self):
"""Single speaker name should appear in prompt."""
result = get_conversation_extraction_prompt(speaker_names=["Alice"])
assert "SPEAKERS: Alice" in result
assert "Alice" in result # Should appear in examples too
def test_multiple_speakers_included(self):
"""Multiple speaker names should be comma-separated."""
result = get_conversation_extraction_prompt(speaker_names=["Alice", "Bob", "Charlie"])
assert "SPEAKERS: Alice, Bob, Charlie" in result
def test_first_speaker_used_in_examples(self):
"""First speaker should be used in example snippets."""
result = get_conversation_extraction_prompt(speaker_names=["Tanay", "Bob"])
# First speaker should replace default "Alice" in examples
assert "Tanay" in result
# Check specific example patterns
assert "Tanay" in result
def test_context_date_creates_temporal_section(self):
"""Context date should create TEMPORAL CONTEXT section."""
result = get_conversation_extraction_prompt(context_date="May 7, 2023")
assert "TEMPORAL CONTEXT:" in result
assert "May 7, 2023" in result
def test_temporal_section_explains_conversions(self):
"""Temporal section should explain date conversions."""
result = get_conversation_extraction_prompt(context_date="January 15, 2024")
assert "last year" in result.lower()
assert "yesterday" in result.lower()
assert "last week" in result.lower()
assert "next month" in result.lower()
def test_both_speaker_and_date_included(self):
"""Both speaker names and date should work together."""
result = get_conversation_extraction_prompt(
speaker_names=["Eve", "Frank"], context_date="December 1, 2023"
)
assert "SPEAKERS: Eve, Frank" in result
assert "TEMPORAL CONTEXT:" in result
assert "December 1, 2023" in result
assert "Eve" in result # Used in examples
def test_contains_extraction_categories(self):
"""Prompt should list what to extract."""
result = get_conversation_extraction_prompt()
assert "IDENTITY" in result or "CHARACTERISTICS" in result
assert "PREFERENCES" in result
assert "ACTIVITIES" in result
assert "RELATIONSHIPS" in result
assert "EVENTS" in result
assert "PLANS" in result or "GOALS" in result
def test_contains_importance_scoring_guidance(self):
"""Prompt should explain importance scoring."""
result = get_conversation_extraction_prompt()
assert "importance" in result.lower()
assert "0.3" in result or "0.4" in result # Background
assert "0.5" in result or "0.6" in result # Useful
assert "0.7" in result or "0.8" in result # Important
assert "0.9" in result or "1.0" in result # Critical
def test_contains_atomic_fact_format(self):
"""Prompt should explain atomic fact format."""
result = get_conversation_extraction_prompt()
assert "ATOMIC FACT" in result or "atomic fact" in result.lower()
assert "GOOD:" in result or "✓ GOOD" in result
assert "BAD:" in result or "✗ BAD" in result
def test_contains_few_shot_examples(self):
"""Prompt should contain few-shot examples."""
result = get_conversation_extraction_prompt()
assert "FEW-SHOT EXAMPLES" in result or "Examples:" in result
assert "Input:" in result
assert "Output:" in result
def test_contains_filtering_guidance(self):
"""Prompt should explain what NOT to extract."""
result = get_conversation_extraction_prompt()
assert "FILTERING" in result or "DO NOT extract" in result
assert "greetings" in result.lower()
assert "transient" in result.lower()
assert "sensitive" in result.lower()
def test_empty_speaker_list_treated_as_none(self):
"""Empty speaker list should not add SPEAKERS section."""
result = get_conversation_extraction_prompt(speaker_names=[])
assert "SPEAKERS:" not in result
def test_special_characters_in_speaker_names(self):
"""Speaker names with special characters should work."""
result = get_conversation_extraction_prompt(speaker_names=["O'Brien", "Jean-Luc"])
assert "O'Brien" in result
assert "Jean-Luc" in result
def test_very_long_speaker_list(self):
"""Long speaker lists should be handled."""
speakers = [f"Person{i}" for i in range(10)]
result = get_conversation_extraction_prompt(speaker_names=speakers)
assert "Person0" in result
assert "Person9" in result
# All speakers should be comma-separated
assert "SPEAKERS: " in result
# =============================================================================
# Test get_memory_answer_prompt()
# =============================================================================
class TestGetMemoryAnswerPrompt:
"""Tests for get_memory_answer_prompt() function."""
def test_returns_string(self):
"""Function should return a string."""
result = get_memory_answer_prompt()
assert isinstance(result, str)
def test_returns_non_empty_prompt(self):
"""Function should return non-empty prompt."""
result = get_memory_answer_prompt()
assert len(result) > 50
def test_no_args_generic_context(self):
"""Without speaker_names, context should be generic."""
result = get_memory_answer_prompt()
# Should mention memory system
assert "memory" in result.lower()
# The first line should be "You are answering questions using a memory system."
# (no "about X" context added)
first_line = result.split("\n")[0]
assert "about" not in first_line
def test_single_speaker_adds_context(self):
"""Single speaker should add context about them."""
result = get_memory_answer_prompt(speaker_names=["Alice"])
assert "about Alice" in result
def test_multiple_speakers_joined_with_and(self):
"""Multiple speakers should be joined with 'and'."""
result = get_memory_answer_prompt(speaker_names=["Alice", "Bob"])
assert "about Alice and Bob" in result
def test_three_speakers_joined_correctly(self):
"""Three speakers use ' and ' between all."""
result = get_memory_answer_prompt(speaker_names=["Alice", "Bob", "Charlie"])
# Should join with ' and ' for all names
assert "Alice and Bob and Charlie" in result
def test_contains_process_steps(self):
"""Prompt should explain the answer process."""
result = get_memory_answer_prompt()
assert "PROCESS" in result or "Process" in result
assert "memory_search" in result
def test_contains_answer_rules(self):
"""Prompt should contain answer rules."""
result = get_memory_answer_prompt()
assert "ANSWER RULES" in result or "rules" in result.lower()
assert "CONCISE" in result or "concise" in result.lower()
def test_handles_inference_questions(self):
"""Prompt should explain how to handle inference questions."""
result = get_memory_answer_prompt()
assert "INFERENCE" in result or "inference" in result.lower()
assert "would" in result.lower() or "could" in result.lower()
def test_handles_not_found_case(self):
"""Prompt should explain what to do when info not found."""
result = get_memory_answer_prompt()
assert "not found" in result.lower() or "Information not found" in result
def test_empty_speaker_list_treated_as_none(self):
"""Empty speaker list should be treated as None."""
result = get_memory_answer_prompt(speaker_names=[])
# Empty list should result in no " about X" context
# The function joins empty list which results in empty string
# So "about " would be followed by nothing meaningful
# Both should be similar (no specific speaker context)
assert "about " not in result # No double space
# =============================================================================
# Test MEMORY_SAVE_TOOL_WITH_EXTRACTION
# =============================================================================
class TestMemorySaveToolWithExtraction:
"""Tests for MEMORY_SAVE_TOOL_WITH_EXTRACTION schema."""
def test_is_dict(self):
"""Tool schema should be a dictionary."""
assert isinstance(MEMORY_SAVE_TOOL_WITH_EXTRACTION, dict)
def test_has_type_field(self):
"""Tool should have type field set to 'function'."""
assert MEMORY_SAVE_TOOL_WITH_EXTRACTION.get("type") == "function"
def test_has_function_field(self):
"""Tool should have function field."""
assert "function" in MEMORY_SAVE_TOOL_WITH_EXTRACTION
assert isinstance(MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"], dict)
def test_function_has_name(self):
"""Function should have name 'memory_save'."""
func = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]
assert func.get("name") == "memory_save"
def test_function_has_description(self):
"""Function should have non-empty description."""
func = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]
assert "description" in func
assert isinstance(func["description"], str)
assert len(func["description"]) > 50
def test_description_mentions_extraction(self):
"""Description should mention pre-extraction."""
func = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]
desc = func["description"]
assert "extract" in desc.lower()
assert "facts" in desc.lower()
assert "entities" in desc.lower()
assert "relationships" in desc.lower()
def test_has_parameters_field(self):
"""Function should have parameters field."""
func = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]
assert "parameters" in func
assert isinstance(func["parameters"], dict)
def test_parameters_has_type_object(self):
"""Parameters should be type object."""
params = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]
assert params.get("type") == "object"
def test_parameters_has_properties(self):
"""Parameters should have properties field."""
params = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]
assert "properties" in params
assert isinstance(params["properties"], dict)
def test_content_parameter_exists(self):
"""Should have content parameter."""
props = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]["properties"]
assert "content" in props
assert props["content"].get("type") == "string"
def test_importance_parameter_exists(self):
"""Should have importance parameter with range."""
props = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]["properties"]
assert "importance" in props
importance = props["importance"]
assert importance.get("type") == "number"
assert importance.get("minimum") == 0.0
assert importance.get("maximum") == 1.0
def test_facts_parameter_exists(self):
"""Should have facts parameter as array of strings."""
props = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]["properties"]
assert "facts" in props
facts = props["facts"]
assert facts.get("type") == "array"
assert facts.get("items", {}).get("type") == "string"
def test_extracted_entities_parameter_exists(self):
"""Should have extracted_entities parameter with proper schema."""
props = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]["properties"]
assert "extracted_entities" in props
entities = props["extracted_entities"]
assert entities.get("type") == "array"
# Check items schema
items = entities.get("items", {})
assert items.get("type") == "object"
item_props = items.get("properties", {})
assert "entity" in item_props
assert "entity_type" in item_props
required = items.get("required", [])
assert "entity" in required
assert "entity_type" in required
def test_extracted_relationships_parameter_exists(self):
"""Should have extracted_relationships parameter with proper schema."""
props = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]["properties"]
assert "extracted_relationships" in props
rels = props["extracted_relationships"]
assert rels.get("type") == "array"
# Check items schema
items = rels.get("items", {})
assert items.get("type") == "object"
item_props = items.get("properties", {})
assert "source" in item_props
assert "relationship" in item_props
assert "destination" in item_props
required = items.get("required", [])
assert "source" in required
assert "relationship" in required
assert "destination" in required
def test_required_fields(self):
"""Content and importance should be required."""
params = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]
assert "required" in params
required = params["required"]
assert "content" in required
assert "importance" in required
# =============================================================================
# Test get_extraction_tools()
# =============================================================================
class TestGetExtractionTools:
"""Tests for get_extraction_tools() function."""
def test_returns_list(self):
"""Function should return a list."""
result = get_extraction_tools()
assert isinstance(result, list)
def test_returns_three_tools(self):
"""Should return exactly 3 extraction tools."""
result = get_extraction_tools()
assert len(result) == 3
def test_all_items_are_dicts(self):
"""All items should be dictionaries."""
result = get_extraction_tools()
for tool in result:
assert isinstance(tool, dict)
def test_all_tools_have_function_type(self):
"""All tools should have type 'function'."""
result = get_extraction_tools()
for tool in result:
assert tool.get("type") == "function"
def test_all_tools_have_function_field(self):
"""All tools should have function field."""
result = get_extraction_tools()
for tool in result:
assert "function" in tool
assert isinstance(tool["function"], dict)
def test_extract_facts_tool_exists(self):
"""Should have extract_facts tool."""
result = get_extraction_tools()
tool_names = [t["function"]["name"] for t in result]
assert "extract_facts" in tool_names
def test_extract_entities_tool_exists(self):
"""Should have extract_entities tool."""
result = get_extraction_tools()
tool_names = [t["function"]["name"] for t in result]
assert "extract_entities" in tool_names
def test_extract_relationships_tool_exists(self):
"""Should have extract_relationships tool."""
result = get_extraction_tools()
tool_names = [t["function"]["name"] for t in result]
assert "extract_relationships" in tool_names
def test_extract_facts_schema(self):
"""extract_facts should have correct schema."""
result = get_extraction_tools()
facts_tool = next(t for t in result if t["function"]["name"] == "extract_facts")
func = facts_tool["function"]
assert "description" in func
assert "parameters" in func
params = func["parameters"]
assert params.get("type") == "object"
assert "facts" in params.get("properties", {})
facts_prop = params["properties"]["facts"]
assert facts_prop.get("type") == "array"
assert facts_prop.get("items", {}).get("type") == "string"
assert "facts" in params.get("required", [])
def test_extract_entities_schema(self):
"""extract_entities should have correct schema."""
result = get_extraction_tools()
entities_tool = next(t for t in result if t["function"]["name"] == "extract_entities")
func = entities_tool["function"]
assert "description" in func
assert "parameters" in func
params = func["parameters"]
assert params.get("type") == "object"
assert "entities" in params.get("properties", {})
entities_prop = params["properties"]["entities"]
assert entities_prop.get("type") == "array"
items = entities_prop.get("items", {})
assert items.get("type") == "object"
assert "entity" in items.get("properties", {})
assert "entity_type" in items.get("properties", {})
assert "entities" in params.get("required", [])
def test_extract_relationships_schema(self):
"""extract_relationships should have correct schema."""
result = get_extraction_tools()
rels_tool = next(t for t in result if t["function"]["name"] == "extract_relationships")
func = rels_tool["function"]
assert "description" in func
assert "parameters" in func
params = func["parameters"]
assert params.get("type") == "object"
assert "relationships" in params.get("properties", {})
rels_prop = params["properties"]["relationships"]
assert rels_prop.get("type") == "array"
items = rels_prop.get("items", {})
assert items.get("type") == "object"
item_props = items.get("properties", {})
assert "source" in item_props
assert "relationship" in item_props
assert "destination" in item_props
assert "relationships" in params.get("required", [])
def test_returns_new_list_each_call(self):
"""Should return a new list each call (not same reference)."""
result1 = get_extraction_tools()
result2 = get_extraction_tools()
assert result1 is not result2
# But content should be equal
assert result1 == result2
# =============================================================================
# Integration Tests
# =============================================================================
class TestExtractionIntegration:
"""Integration tests for extraction module."""
def test_prompts_are_different(self):
"""Each prompt constant should be unique."""
prompts = [
FACT_EXTRACTION_PROMPT,
ENTITY_EXTRACTION_PROMPT,
RELATIONSHIP_EXTRACTION_PROMPT,
EXTRACTION_SYSTEM_PROMPT,
]
# All prompts should be different
for i, p1 in enumerate(prompts):
for j, p2 in enumerate(prompts):
if i != j:
assert p1 != p2
def test_conversation_prompt_changes_with_speaker(self):
"""Conversation prompt should change based on speaker."""
prompt_default = get_conversation_extraction_prompt()
prompt_alice = get_conversation_extraction_prompt(speaker_names=["Alice"])
prompt_bob = get_conversation_extraction_prompt(speaker_names=["Bob"])
assert prompt_default != prompt_alice
assert prompt_alice != prompt_bob
def test_conversation_prompt_changes_with_date(self):
"""Conversation prompt should change based on date."""
prompt_no_date = get_conversation_extraction_prompt()
prompt_date1 = get_conversation_extraction_prompt(context_date="January 1, 2024")
prompt_date2 = get_conversation_extraction_prompt(context_date="December 31, 2024")
assert prompt_no_date != prompt_date1
assert prompt_date1 != prompt_date2
def test_answer_prompt_changes_with_speaker(self):
"""Answer prompt should change based on speaker."""
prompt_default = get_memory_answer_prompt()
prompt_alice = get_memory_answer_prompt(speaker_names=["Alice"])
assert prompt_default != prompt_alice
def test_tool_schema_is_valid_json_serializable(self):
"""Tool schema should be JSON serializable."""
import json
# Should not raise
json_str = json.dumps(MEMORY_SAVE_TOOL_WITH_EXTRACTION)
# Should round-trip correctly
loaded = json.loads(json_str)
assert loaded == MEMORY_SAVE_TOOL_WITH_EXTRACTION
def test_extraction_tools_are_json_serializable(self):
"""Extraction tools should be JSON serializable."""
import json
tools = get_extraction_tools()
# Should not raise
json_str = json.dumps(tools)
# Should round-trip correctly
loaded = json.loads(json_str)
assert loaded == tools
def test_all_tool_names_unique(self):
"""All tool names should be unique."""
tools = get_extraction_tools()
names = [t["function"]["name"] for t in tools]
assert len(names) == len(set(names))
def test_memory_save_tool_compatible_with_extraction_tools(self):
"""Memory save tool should accept outputs from extraction tools."""
# The memory_save tool accepts:
# - facts: array of strings (from extract_facts)
# - extracted_entities: array of {entity, entity_type} (from extract_entities)
# - extracted_relationships: array of {source, relationship, destination} (from extract_relationships)
save_tool = MEMORY_SAVE_TOOL_WITH_EXTRACTION["function"]["parameters"]["properties"]
extraction_tools = {t["function"]["name"]: t for t in get_extraction_tools()}
# Facts compatibility
facts_output = extraction_tools["extract_facts"]["function"]["parameters"]["properties"][
"facts"
]
save_facts_input = save_tool["facts"]
assert facts_output["type"] == save_facts_input["type"] # both array
assert facts_output["items"]["type"] == save_facts_input["items"]["type"] # both string
# Entities compatibility
entities_output = extraction_tools["extract_entities"]["function"]["parameters"][
"properties"
]["entities"]
save_entities_input = save_tool["extracted_entities"]
assert entities_output["type"] == save_entities_input["type"] # both array
# Both have object items with entity and entity_type
assert entities_output["items"]["type"] == save_entities_input["items"]["type"]
# Relationships compatibility
rels_output = extraction_tools["extract_relationships"]["function"]["parameters"][
"properties"
]["relationships"]
save_rels_input = save_tool["extracted_relationships"]
assert rels_output["type"] == save_rels_input["type"] # both array
# Both have object items with source, relationship, destination
assert rels_output["items"]["type"] == save_rels_input["items"]["type"]
+3 -13
View File
@@ -565,23 +565,13 @@ class TestMemoryIntegration:
class TestExtractionPrompts:
"""Tests for extraction prompt templates."""
"""Tests for the extraction prompt template."""
def test_extraction_prompts_exist_and_valid(self):
"""Verify extraction prompts are defined and non-empty."""
from headroom.memory.extraction import (
ENTITY_EXTRACTION_PROMPT,
EXTRACTION_SYSTEM_PROMPT,
FACT_EXTRACTION_PROMPT,
RELATIONSHIP_EXTRACTION_PROMPT,
)
"""Verify the extraction system prompt is defined and non-empty."""
from headroom.memory.extraction import EXTRACTION_SYSTEM_PROMPT
assert len(EXTRACTION_SYSTEM_PROMPT) > 100, "System prompt should be substantial"
assert len(FACT_EXTRACTION_PROMPT) > 100, "Fact prompt should be substantial"
assert len(ENTITY_EXTRACTION_PROMPT) > 100, "Entity prompt should be substantial"
assert len(RELATIONSHIP_EXTRACTION_PROMPT) > 100, (
"Relationship prompt should be substantial"
)
# Verify they mention key concepts
assert "facts" in EXTRACTION_SYSTEM_PROMPT.lower()