Analyzer — Pattern Extraction & Swarm Generation
Processes conversational history to extract recurring intent patterns and generate personalized mini-agent swarms.
Content
Analyzer — Pattern Extraction & Swarm Generation
Processes conversational history to extract recurring intent patterns and generate personalized mini-agent swarms.
How It Works
- Loads session logs from
history/{user_id}/ - Batches sessions (10 at a time) and sends them to Gemini 3.1 Pro for pattern extraction
- Merges and deduplicates patterns across batches
- Filters to patterns appearing in >= 3 sessions
- Separates task patterns (what user wants) from behavioral patterns (how user communicates)
- Behavioral patterns →
user_style.json(shared style profile) - For each task pattern, generates:
- A trigger definition (
triggers.json) with structured attribute rules - A data-only mini-agent file (
agents/{pattern_name}.py) with its metadata and prompt(s) - Scope embeddings (768-dim,
text-embedding-005) for semantic matching
- Critic/revision pass validates each agent against history sessions (up to 3 rounds)
- Non-parametric quality ranking keeps all agents scoring >= 25/50 (5-dimension rubric, hard cap 30)
- Coverage validation checks for gaps after ranking and widens surviving agents
Key Components
analyze_history.py— Main entry pointpattern_extractor.py— LLM-based pattern clusteringswarm_generator.py— Generates mini-agent .py files, triggers, embeddings, runs critic/rankingagent_spec.py— Reads and writes agent files as data (never imports them) and supplies the one trustedexecute(). Tests:test_agent_spec.pytrigger_schema.py— Feature schema, extraction prompts, soft penalty matching logic, rule validationllm_util.py— Retry + model fallback wrapper for Google Cloud calls, andjson_for_prompt(), which puts conversation history into prompts with<and>escaped. Tests:test_prompt_data.py
Matching Logic (trigger_schema.py)
Domain and task_type mismatches apply soft scoring penalties (0.15 / 0.10) to embedding similarity instead of binary rejection. This allows cross-domain requests to match when semantic similarity is high. Keyword-based matching (require_any_keyword, exclude_keywords) has been removed — pure semantic matching only.
Usage
source .venv/bin/activate
# Analyze all users
python analyzer/analyze_history.py
# One user, dry run (show patterns only)
python analyzer/analyze_history.py --user user_1 --dry-run --verbose
# Full generation for one user
python analyzer/analyze_history.py --user user_1
# Skip critic pass (faster)
python analyzer/analyze_history.py --user user_1 --skip-critic
# Unit tests (no API calls)
python -m unittest analyzer.test_agent_spec analyzer.test_prompt_data
Output
swarms/{user_id}/
├── manifest.json # Index of all agents + metadata + critic/ranking results
├── triggers.json # Trigger rules + scope embeddings (768-dim) + _config
├── user_style.json # Behavioral pattern profile
└── agents/
├── pattern_name_1.py # Mini-agent data: AGENT_META + ENRICHED_PROMPT or STEPS
└── pattern_name_2.py # (quality-based count, typically 5-10)Source and license
- Source
- Google Cloud Generative AI
- Original
- GoogleCloudPlatform/generative-ai/agents/personalized-agent-swarms/analyzer
- License
- Apache-2.0