@techwavedev/agi-agent-kit 1.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +59 -0
- package/README.md +147 -0
- package/bin/init.js +471 -0
- package/package.json +36 -0
- package/templates/.agent/agents/backend-specialist.md +263 -0
- package/templates/.agent/agents/code-archaeologist.md +106 -0
- package/templates/.agent/agents/database-architect.md +226 -0
- package/templates/.agent/agents/debugger.md +225 -0
- package/templates/.agent/agents/devops-engineer.md +242 -0
- package/templates/.agent/agents/documentation-writer.md +104 -0
- package/templates/.agent/agents/explorer-agent.md +73 -0
- package/templates/.agent/agents/frontend-specialist.md +556 -0
- package/templates/.agent/agents/game-developer.md +162 -0
- package/templates/.agent/agents/mobile-developer.md +377 -0
- package/templates/.agent/agents/orchestrator.md +416 -0
- package/templates/.agent/agents/penetration-tester.md +188 -0
- package/templates/.agent/agents/performance-optimizer.md +187 -0
- package/templates/.agent/agents/product-manager.md +112 -0
- package/templates/.agent/agents/project-planner.md +403 -0
- package/templates/.agent/agents/qa-automation-engineer.md +109 -0
- package/templates/.agent/agents/security-auditor.md +170 -0
- package/templates/.agent/agents/seo-specialist.md +111 -0
- package/templates/.agent/agents/test-engineer.md +158 -0
- package/templates/.agent/rules/GEMINI.md +253 -0
- package/templates/.agent/workflows/brainstorm.md +113 -0
- package/templates/.agent/workflows/create.md +59 -0
- package/templates/.agent/workflows/debug.md +103 -0
- package/templates/.agent/workflows/deploy.md +176 -0
- package/templates/.agent/workflows/enhance.md +63 -0
- package/templates/.agent/workflows/orchestrate.md +237 -0
- package/templates/.agent/workflows/plan.md +89 -0
- package/templates/.agent/workflows/preview.md +81 -0
- package/templates/.agent/workflows/status.md +86 -0
- package/templates/.agent/workflows/test.md +144 -0
- package/templates/.agent/workflows/ui-ux-pro-max.md +296 -0
- package/templates/base/.env.example +54 -0
- package/templates/base/AGENTS.md +463 -0
- package/templates/base/requirements.txt +6 -0
- package/templates/base/skill-creator/LICENSE.txt +202 -0
- package/templates/base/skill-creator/SKILL_skillcreator.md +389 -0
- package/templates/base/skill-creator/references/output-patterns.md +82 -0
- package/templates/base/skill-creator/references/workflows.md +28 -0
- package/templates/base/skill-creator/scripts/init_skill.py +304 -0
- package/templates/base/skill-creator/scripts/package_skill.py +110 -0
- package/templates/base/skill-creator/scripts/quick_validate.py +95 -0
- package/templates/base/skill-creator/scripts/update_catalog.py +371 -0
- package/templates/skills/core/README.md +21 -0
- package/templates/skills/core/documentation/SKILL.md +351 -0
- package/templates/skills/core/documentation/references/best_practices.md +201 -0
- package/templates/skills/core/documentation/scripts/analyze_code.py +307 -0
- package/templates/skills/core/documentation/scripts/detect_changes.py +460 -0
- package/templates/skills/core/documentation/scripts/generate_changelog.py +312 -0
- package/templates/skills/core/documentation/scripts/sync_docs.py +272 -0
- package/templates/skills/core/documentation/scripts/update_skill_docs.py +366 -0
- package/templates/skills/core/pdf-reader/SKILL.md +104 -0
- package/templates/skills/core/pdf-reader/references/pdf_libraries.md +83 -0
- package/templates/skills/core/pdf-reader/scripts/extract_text.py +295 -0
- package/templates/skills/core/qdrant-memory/SKILL.md +435 -0
- package/templates/skills/core/qdrant-memory/references/advanced_patterns.md +375 -0
- package/templates/skills/core/qdrant-memory/references/collection_schemas.md +229 -0
- package/templates/skills/core/qdrant-memory/references/complete_guide.md +724 -0
- package/templates/skills/core/qdrant-memory/references/embedding_models.md +325 -0
- package/templates/skills/core/qdrant-memory/scripts/benchmark_token_savings.py +640 -0
- package/templates/skills/core/qdrant-memory/scripts/embedding_utils.py +323 -0
- package/templates/skills/core/qdrant-memory/scripts/hybrid_search.py +214 -0
- package/templates/skills/core/qdrant-memory/scripts/init_collection.py +193 -0
- package/templates/skills/core/qdrant-memory/scripts/memory_retrieval.py +345 -0
- package/templates/skills/core/qdrant-memory/scripts/semantic_cache.py +282 -0
- package/templates/skills/core/qdrant-memory/scripts/test_skill.py +655 -0
- package/templates/skills/core/webcrawler/SKILL.md +292 -0
- package/templates/skills/core/webcrawler/references/advanced_crawling.md +181 -0
- package/templates/skills/core/webcrawler/scripts/crawl_docs.py +532 -0
- package/templates/skills/core/webcrawler/scripts/extract_page.py +189 -0
- package/templates/skills/core/webcrawler/scripts/filter_docs.py +200 -0
- package/templates/skills/knowledge/api-patterns/SKILL.md +81 -0
- package/templates/skills/knowledge/api-patterns/api-style.md +42 -0
- package/templates/skills/knowledge/api-patterns/auth.md +24 -0
- package/templates/skills/knowledge/api-patterns/documentation.md +26 -0
- package/templates/skills/knowledge/api-patterns/graphql.md +41 -0
- package/templates/skills/knowledge/api-patterns/rate-limiting.md +31 -0
- package/templates/skills/knowledge/api-patterns/response.md +37 -0
- package/templates/skills/knowledge/api-patterns/rest.md +40 -0
- package/templates/skills/knowledge/api-patterns/scripts/api_validator.py +211 -0
- package/templates/skills/knowledge/api-patterns/security-testing.md +122 -0
- package/templates/skills/knowledge/api-patterns/trpc.md +41 -0
- package/templates/skills/knowledge/api-patterns/versioning.md +22 -0
- package/templates/skills/knowledge/app-builder/SKILL.md +75 -0
- package/templates/skills/knowledge/app-builder/agent-coordination.md +71 -0
- package/templates/skills/knowledge/app-builder/feature-building.md +53 -0
- package/templates/skills/knowledge/app-builder/project-detection.md +34 -0
- package/templates/skills/knowledge/app-builder/scaffolding.md +118 -0
- package/templates/skills/knowledge/app-builder/tech-stack.md +40 -0
- package/templates/skills/knowledge/app-builder/templates/SKILL.md +39 -0
- package/templates/skills/knowledge/app-builder/templates/astro-static/TEMPLATE.md +76 -0
- package/templates/skills/knowledge/app-builder/templates/chrome-extension/TEMPLATE.md +92 -0
- package/templates/skills/knowledge/app-builder/templates/cli-tool/TEMPLATE.md +88 -0
- package/templates/skills/knowledge/app-builder/templates/electron-desktop/TEMPLATE.md +88 -0
- package/templates/skills/knowledge/app-builder/templates/express-api/TEMPLATE.md +83 -0
- package/templates/skills/knowledge/app-builder/templates/flutter-app/TEMPLATE.md +90 -0
- package/templates/skills/knowledge/app-builder/templates/monorepo-turborepo/TEMPLATE.md +90 -0
- package/templates/skills/knowledge/app-builder/templates/nextjs-fullstack/TEMPLATE.md +82 -0
- package/templates/skills/knowledge/app-builder/templates/nextjs-saas/TEMPLATE.md +100 -0
- package/templates/skills/knowledge/app-builder/templates/nextjs-static/TEMPLATE.md +106 -0
- package/templates/skills/knowledge/app-builder/templates/nuxt-app/TEMPLATE.md +101 -0
- package/templates/skills/knowledge/app-builder/templates/python-fastapi/TEMPLATE.md +83 -0
- package/templates/skills/knowledge/app-builder/templates/react-native-app/TEMPLATE.md +93 -0
- package/templates/skills/knowledge/architecture/SKILL.md +55 -0
- package/templates/skills/knowledge/architecture/context-discovery.md +43 -0
- package/templates/skills/knowledge/architecture/examples.md +94 -0
- package/templates/skills/knowledge/architecture/pattern-selection.md +68 -0
- package/templates/skills/knowledge/architecture/patterns-reference.md +50 -0
- package/templates/skills/knowledge/architecture/trade-off-analysis.md +77 -0
- package/templates/skills/knowledge/bash-linux/SKILL.md +199 -0
- package/templates/skills/knowledge/behavioral-modes/SKILL.md +242 -0
- package/templates/skills/knowledge/brainstorming/SKILL.md +163 -0
- package/templates/skills/knowledge/brainstorming/dynamic-questioning.md +350 -0
- package/templates/skills/knowledge/clean-code/SKILL.md +201 -0
- package/templates/skills/knowledge/code-review-checklist/SKILL.md +109 -0
- package/templates/skills/knowledge/database-design/SKILL.md +52 -0
- package/templates/skills/knowledge/database-design/database-selection.md +43 -0
- package/templates/skills/knowledge/database-design/indexing.md +39 -0
- package/templates/skills/knowledge/database-design/migrations.md +48 -0
- package/templates/skills/knowledge/database-design/optimization.md +36 -0
- package/templates/skills/knowledge/database-design/orm-selection.md +30 -0
- package/templates/skills/knowledge/database-design/schema-design.md +56 -0
- package/templates/skills/knowledge/database-design/scripts/schema_validator.py +172 -0
- package/templates/skills/knowledge/deployment-procedures/SKILL.md +241 -0
- package/templates/skills/knowledge/doc.md +177 -0
- package/templates/skills/knowledge/documentation-templates/SKILL.md +194 -0
- package/templates/skills/knowledge/frontend-design/SKILL.md +396 -0
- package/templates/skills/knowledge/frontend-design/animation-guide.md +331 -0
- package/templates/skills/knowledge/frontend-design/color-system.md +311 -0
- package/templates/skills/knowledge/frontend-design/decision-trees.md +418 -0
- package/templates/skills/knowledge/frontend-design/motion-graphics.md +306 -0
- package/templates/skills/knowledge/frontend-design/scripts/accessibility_checker.py +183 -0
- package/templates/skills/knowledge/frontend-design/scripts/ux_audit.py +722 -0
- package/templates/skills/knowledge/frontend-design/typography-system.md +345 -0
- package/templates/skills/knowledge/frontend-design/ux-psychology.md +541 -0
- package/templates/skills/knowledge/frontend-design/visual-effects.md +383 -0
- package/templates/skills/knowledge/game-development/2d-games/SKILL.md +119 -0
- package/templates/skills/knowledge/game-development/3d-games/SKILL.md +135 -0
- package/templates/skills/knowledge/game-development/SKILL.md +167 -0
- package/templates/skills/knowledge/game-development/game-art/SKILL.md +185 -0
- package/templates/skills/knowledge/game-development/game-audio/SKILL.md +190 -0
- package/templates/skills/knowledge/game-development/game-design/SKILL.md +129 -0
- package/templates/skills/knowledge/game-development/mobile-games/SKILL.md +108 -0
- package/templates/skills/knowledge/game-development/multiplayer/SKILL.md +132 -0
- package/templates/skills/knowledge/game-development/pc-games/SKILL.md +144 -0
- package/templates/skills/knowledge/game-development/vr-ar/SKILL.md +123 -0
- package/templates/skills/knowledge/game-development/web-games/SKILL.md +150 -0
- package/templates/skills/knowledge/geo-fundamentals/SKILL.md +156 -0
- package/templates/skills/knowledge/geo-fundamentals/scripts/geo_checker.py +289 -0
- package/templates/skills/knowledge/i18n-localization/SKILL.md +154 -0
- package/templates/skills/knowledge/i18n-localization/scripts/i18n_checker.py +241 -0
- package/templates/skills/knowledge/intelligent-routing/SKILL.md +334 -0
- package/templates/skills/knowledge/lint-and-validate/SKILL.md +45 -0
- package/templates/skills/knowledge/lint-and-validate/scripts/lint_runner.py +172 -0
- package/templates/skills/knowledge/lint-and-validate/scripts/type_coverage.py +173 -0
- package/templates/skills/knowledge/mcp-builder/SKILL.md +176 -0
- package/templates/skills/knowledge/mobile-design/SKILL.md +394 -0
- package/templates/skills/knowledge/mobile-design/decision-trees.md +516 -0
- package/templates/skills/knowledge/mobile-design/mobile-backend.md +491 -0
- package/templates/skills/knowledge/mobile-design/mobile-color-system.md +420 -0
- package/templates/skills/knowledge/mobile-design/mobile-debugging.md +122 -0
- package/templates/skills/knowledge/mobile-design/mobile-design-thinking.md +357 -0
- package/templates/skills/knowledge/mobile-design/mobile-navigation.md +458 -0
- package/templates/skills/knowledge/mobile-design/mobile-performance.md +767 -0
- package/templates/skills/knowledge/mobile-design/mobile-testing.md +356 -0
- package/templates/skills/knowledge/mobile-design/mobile-typography.md +433 -0
- package/templates/skills/knowledge/mobile-design/platform-android.md +666 -0
- package/templates/skills/knowledge/mobile-design/platform-ios.md +561 -0
- package/templates/skills/knowledge/mobile-design/scripts/mobile_audit.py +670 -0
- package/templates/skills/knowledge/mobile-design/touch-psychology.md +537 -0
- package/templates/skills/knowledge/nextjs-best-practices/SKILL.md +203 -0
- package/templates/skills/knowledge/nodejs-best-practices/SKILL.md +333 -0
- package/templates/skills/knowledge/parallel-agents/SKILL.md +175 -0
- package/templates/skills/knowledge/performance-profiling/SKILL.md +143 -0
- package/templates/skills/knowledge/performance-profiling/scripts/lighthouse_audit.py +76 -0
- package/templates/skills/knowledge/plan-writing/SKILL.md +152 -0
- package/templates/skills/knowledge/powershell-windows/SKILL.md +167 -0
- package/templates/skills/knowledge/python-patterns/SKILL.md +441 -0
- package/templates/skills/knowledge/react-patterns/SKILL.md +198 -0
- package/templates/skills/knowledge/red-team-tactics/SKILL.md +199 -0
- package/templates/skills/knowledge/seo-fundamentals/SKILL.md +129 -0
- package/templates/skills/knowledge/seo-fundamentals/scripts/seo_checker.py +219 -0
- package/templates/skills/knowledge/server-management/SKILL.md +161 -0
- package/templates/skills/knowledge/systematic-debugging/SKILL.md +109 -0
- package/templates/skills/knowledge/tailwind-patterns/SKILL.md +269 -0
- package/templates/skills/knowledge/tdd-workflow/SKILL.md +149 -0
- package/templates/skills/knowledge/testing-patterns/SKILL.md +178 -0
- package/templates/skills/knowledge/testing-patterns/scripts/test_runner.py +219 -0
- package/templates/skills/knowledge/vulnerability-scanner/SKILL.md +276 -0
- package/templates/skills/knowledge/vulnerability-scanner/checklists.md +121 -0
- package/templates/skills/knowledge/vulnerability-scanner/scripts/security_scan.py +458 -0
- package/templates/skills/knowledge/webapp-testing/SKILL.md +187 -0
- package/templates/skills/knowledge/webapp-testing/scripts/playwright_runner.py +173 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
# Advanced Patterns Reference
|
|
2
|
+
|
|
3
|
+
This document covers advanced RAG optimization patterns for maximum token savings.
|
|
4
|
+
|
|
5
|
+
## Pattern 1: Hierarchical Memory
|
|
6
|
+
|
|
7
|
+
Store memories at multiple granularities for optimal retrieval.
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
┌─────────────────────────────────────────────┐
|
|
11
|
+
│ SUMMARY LEVEL (Low detail, fast lookup) │
|
|
12
|
+
│ "We discussed database architecture" │
|
|
13
|
+
├─────────────────────────────────────────────┤
|
|
14
|
+
│ CHUNK LEVEL (Medium detail) │
|
|
15
|
+
│ "Decided PostgreSQL for ACID compliance" │
|
|
16
|
+
├─────────────────────────────────────────────┤
|
|
17
|
+
│ DETAIL LEVEL (Full context) │
|
|
18
|
+
│ "After evaluating MongoDB, PostgreSQL, │
|
|
19
|
+
│ and MySQL, we chose PostgreSQL because..."│
|
|
20
|
+
└─────────────────────────────────────────────┘
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
### Implementation
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
def store_hierarchical(content: str, metadata: dict):
|
|
27
|
+
"""Store at multiple granularities."""
|
|
28
|
+
|
|
29
|
+
# Level 1: Full content
|
|
30
|
+
store_memory(content, type="detail", **metadata)
|
|
31
|
+
|
|
32
|
+
# Level 2: Summary (use LLM to summarize)
|
|
33
|
+
summary = summarize(content)
|
|
34
|
+
store_memory(summary, type="summary", parent_id=detail_id, **metadata)
|
|
35
|
+
|
|
36
|
+
# Level 3: Keywords/tags for fast lookup
|
|
37
|
+
keywords = extract_keywords(content)
|
|
38
|
+
store_memory(" ".join(keywords), type="index", parent_id=detail_id, **metadata)
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### Retrieval Strategy
|
|
42
|
+
|
|
43
|
+
1. First search `type=index` for fast matching
|
|
44
|
+
2. If match found, retrieve parent `type=summary`
|
|
45
|
+
3. If more detail needed, retrieve `type=detail`
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Pattern 2: Sliding Window Chunking
|
|
50
|
+
|
|
51
|
+
Optimal chunk size for context retrieval.
|
|
52
|
+
|
|
53
|
+
### Chunk Size Guidelines
|
|
54
|
+
|
|
55
|
+
| Content Type | Optimal Chunk | Overlap |
|
|
56
|
+
| --------------- | -------------- | ---------- |
|
|
57
|
+
| Code | 500-800 tokens | 100 tokens |
|
|
58
|
+
| Documentation | 300-500 tokens | 50 tokens |
|
|
59
|
+
| Conversation | 200-300 tokens | 25 tokens |
|
|
60
|
+
| Technical specs | 400-600 tokens | 75 tokens |
|
|
61
|
+
|
|
62
|
+
### Implementation
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
def chunk_text(text: str, chunk_size: int = 500, overlap: int = 50) -> list:
|
|
66
|
+
"""Split text into overlapping chunks."""
|
|
67
|
+
words = text.split()
|
|
68
|
+
chunks = []
|
|
69
|
+
|
|
70
|
+
for i in range(0, len(words), chunk_size - overlap):
|
|
71
|
+
chunk = " ".join(words[i:i + chunk_size])
|
|
72
|
+
chunks.append(chunk)
|
|
73
|
+
|
|
74
|
+
return chunks
|
|
75
|
+
|
|
76
|
+
def store_chunked(document: str, metadata: dict):
|
|
77
|
+
"""Store document as overlapping chunks."""
|
|
78
|
+
chunks = chunk_text(document)
|
|
79
|
+
|
|
80
|
+
for i, chunk in enumerate(chunks):
|
|
81
|
+
store_memory(
|
|
82
|
+
chunk,
|
|
83
|
+
type="chunk",
|
|
84
|
+
chunk_index=i,
|
|
85
|
+
total_chunks=len(chunks),
|
|
86
|
+
**metadata
|
|
87
|
+
)
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
---
|
|
91
|
+
|
|
92
|
+
## Pattern 3: Query Expansion
|
|
93
|
+
|
|
94
|
+
Improve recall by expanding queries.
|
|
95
|
+
|
|
96
|
+
### Synonym Expansion
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
def expand_query(query: str) -> list:
|
|
100
|
+
"""Generate query variations for better recall."""
|
|
101
|
+
variations = [query]
|
|
102
|
+
|
|
103
|
+
# Add variations
|
|
104
|
+
variations.append(query.lower())
|
|
105
|
+
variations.append(rephrase_with_llm(query))
|
|
106
|
+
|
|
107
|
+
return variations
|
|
108
|
+
|
|
109
|
+
def search_with_expansion(query: str, top_k: int = 5):
|
|
110
|
+
"""Search with multiple query variations, dedupe results."""
|
|
111
|
+
all_results = {}
|
|
112
|
+
|
|
113
|
+
for variation in expand_query(query):
|
|
114
|
+
results = retrieve_context(variation, top_k=top_k)
|
|
115
|
+
for r in results["chunks"]:
|
|
116
|
+
# Keep highest scoring version
|
|
117
|
+
if r["content"] not in all_results or r["score"] > all_results[r["content"]]["score"]:
|
|
118
|
+
all_results[r["content"]] = r
|
|
119
|
+
|
|
120
|
+
return sorted(all_results.values(), key=lambda x: x["score"], reverse=True)[:top_k]
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## Pattern 4: Relevance Feedback Loop
|
|
126
|
+
|
|
127
|
+
Improve retrieval quality over time.
|
|
128
|
+
|
|
129
|
+
### Implicit Feedback
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
def log_usage(query: str, retrieved: list, used_chunks: list):
|
|
133
|
+
"""Log which retrieved chunks were actually used."""
|
|
134
|
+
for chunk in retrieved:
|
|
135
|
+
was_used = chunk["content"] in used_chunks
|
|
136
|
+
store_feedback(
|
|
137
|
+
query=query,
|
|
138
|
+
chunk_id=chunk["id"],
|
|
139
|
+
was_used=was_used,
|
|
140
|
+
score=chunk["score"]
|
|
141
|
+
)
|
|
142
|
+
|
|
143
|
+
def boost_frequently_used(results: list) -> list:
|
|
144
|
+
"""Boost chunks that are frequently used."""
|
|
145
|
+
for result in results:
|
|
146
|
+
usage_rate = get_usage_rate(result["id"])
|
|
147
|
+
result["boosted_score"] = result["score"] * (1 + usage_rate * 0.2)
|
|
148
|
+
|
|
149
|
+
return sorted(results, key=lambda x: x["boosted_score"], reverse=True)
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Explicit Feedback
|
|
153
|
+
|
|
154
|
+
```json
|
|
155
|
+
{
|
|
156
|
+
"tool": "qdrant_update_memory",
|
|
157
|
+
"arguments": {
|
|
158
|
+
"id": "memory_123",
|
|
159
|
+
"payload": {
|
|
160
|
+
"relevance_score": 0.95,
|
|
161
|
+
"last_useful": "2026-01-22T12:00:00Z",
|
|
162
|
+
"usage_count": 15
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
---
|
|
169
|
+
|
|
170
|
+
## Pattern 5: Context Window Packing
|
|
171
|
+
|
|
172
|
+
Maximize information density in the context window.
|
|
173
|
+
|
|
174
|
+
### Token Budget Allocation
|
|
175
|
+
|
|
176
|
+
```
|
|
177
|
+
Total Budget: 4000 tokens
|
|
178
|
+
├── System Prompt: 500 tokens (fixed)
|
|
179
|
+
├── Retrieved Context: 2000 tokens (variable)
|
|
180
|
+
├── User Query: 200 tokens (variable)
|
|
181
|
+
└── Response Space: 1300 tokens (reserved)
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
### Packing Algorithm
|
|
185
|
+
|
|
186
|
+
```python
|
|
187
|
+
def pack_context(chunks: list, token_budget: int = 2000) -> str:
|
|
188
|
+
"""Pack most relevant chunks within token budget."""
|
|
189
|
+
packed = []
|
|
190
|
+
current_tokens = 0
|
|
191
|
+
|
|
192
|
+
# Sort by relevance score
|
|
193
|
+
sorted_chunks = sorted(chunks, key=lambda x: x["score"], reverse=True)
|
|
194
|
+
|
|
195
|
+
for chunk in sorted_chunks:
|
|
196
|
+
chunk_tokens = chunk["token_estimate"]
|
|
197
|
+
if current_tokens + chunk_tokens <= token_budget:
|
|
198
|
+
packed.append(chunk["content"])
|
|
199
|
+
current_tokens += chunk_tokens
|
|
200
|
+
else:
|
|
201
|
+
# Try to fit partial chunk
|
|
202
|
+
remaining = token_budget - current_tokens
|
|
203
|
+
if remaining > 100: # Worth including partial
|
|
204
|
+
words = chunk["content"].split()[:remaining]
|
|
205
|
+
packed.append(" ".join(words) + "...")
|
|
206
|
+
break
|
|
207
|
+
|
|
208
|
+
return "\n\n---\n\n".join(packed)
|
|
209
|
+
```
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Pattern 6: Semantic Deduplication
|
|
214
|
+
|
|
215
|
+
Avoid storing redundant information.
|
|
216
|
+
|
|
217
|
+
```python
|
|
218
|
+
def should_store(new_content: str, threshold: float = 0.9) -> bool:
|
|
219
|
+
"""Check if similar content already exists."""
|
|
220
|
+
similar = retrieve_context(
|
|
221
|
+
new_content,
|
|
222
|
+
top_k=1,
|
|
223
|
+
score_threshold=threshold
|
|
224
|
+
)
|
|
225
|
+
|
|
226
|
+
if similar["total_chunks"] > 0:
|
|
227
|
+
print(f"Similar content exists (score: {similar['chunks'][0]['score']})")
|
|
228
|
+
return False
|
|
229
|
+
|
|
230
|
+
return True
|
|
231
|
+
|
|
232
|
+
def store_if_unique(content: str, **metadata):
|
|
233
|
+
"""Only store if semantically unique."""
|
|
234
|
+
if should_store(content):
|
|
235
|
+
return store_memory(content, **metadata)
|
|
236
|
+
else:
|
|
237
|
+
return {"status": "skipped", "reason": "duplicate"}
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
---
|
|
241
|
+
|
|
242
|
+
## Pattern 7: Time-Decayed Relevance
|
|
243
|
+
|
|
244
|
+
Recent information is often more relevant.
|
|
245
|
+
|
|
246
|
+
### Decay Function
|
|
247
|
+
|
|
248
|
+
```python
|
|
249
|
+
import math
|
|
250
|
+
from datetime import datetime, timedelta
|
|
251
|
+
|
|
252
|
+
def time_decay_score(base_score: float, timestamp: str, half_life_days: int = 30) -> float:
|
|
253
|
+
"""Apply exponential decay based on age."""
|
|
254
|
+
age = datetime.utcnow() - datetime.fromisoformat(timestamp)
|
|
255
|
+
age_days = age.total_seconds() / 86400
|
|
256
|
+
|
|
257
|
+
decay = math.exp(-0.693 * age_days / half_life_days) # ln(2) ≈ 0.693
|
|
258
|
+
return base_score * decay
|
|
259
|
+
|
|
260
|
+
def retrieve_with_recency(query: str, top_k: int = 5):
|
|
261
|
+
"""Retrieve and re-rank with time decay."""
|
|
262
|
+
results = retrieve_context(query, top_k=top_k * 2)
|
|
263
|
+
|
|
264
|
+
for chunk in results["chunks"]:
|
|
265
|
+
chunk["decayed_score"] = time_decay_score(
|
|
266
|
+
chunk["score"],
|
|
267
|
+
chunk["timestamp"]
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
# Re-rank by decayed score
|
|
271
|
+
results["chunks"] = sorted(
|
|
272
|
+
results["chunks"],
|
|
273
|
+
key=lambda x: x["decayed_score"],
|
|
274
|
+
reverse=True
|
|
275
|
+
)[:top_k]
|
|
276
|
+
|
|
277
|
+
return results
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
---
|
|
281
|
+
|
|
282
|
+
## Pattern 8: Multi-Collection Routing
|
|
283
|
+
|
|
284
|
+
Route queries to specialized collections.
|
|
285
|
+
|
|
286
|
+
```python
|
|
287
|
+
COLLECTION_ROUTES = {
|
|
288
|
+
"code": "code_patterns",
|
|
289
|
+
"error": "error_solutions",
|
|
290
|
+
"decision": "decisions",
|
|
291
|
+
"api": "api_documentation",
|
|
292
|
+
"default": "agent_memory"
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
def classify_query(query: str) -> str:
|
|
296
|
+
"""Classify query to route to appropriate collection."""
|
|
297
|
+
# Simple keyword-based routing
|
|
298
|
+
if any(kw in query.lower() for kw in ["code", "function", "class", "implement"]):
|
|
299
|
+
return "code"
|
|
300
|
+
if any(kw in query.lower() for kw in ["error", "exception", "failed", "bug"]):
|
|
301
|
+
return "error"
|
|
302
|
+
if any(kw in query.lower() for kw in ["decision", "decided", "chose", "why"]):
|
|
303
|
+
return "decision"
|
|
304
|
+
return "default"
|
|
305
|
+
|
|
306
|
+
def routed_search(query: str, **kwargs):
|
|
307
|
+
"""Search the appropriate collection based on query type."""
|
|
308
|
+
query_type = classify_query(query)
|
|
309
|
+
collection = COLLECTION_ROUTES[query_type]
|
|
310
|
+
|
|
311
|
+
return retrieve_context(query, collection=collection, **kwargs)
|
|
312
|
+
```
|
|
313
|
+
|
|
314
|
+
---
|
|
315
|
+
|
|
316
|
+
## Token Savings Calculations
|
|
317
|
+
|
|
318
|
+
### Semantic Cache ROI
|
|
319
|
+
|
|
320
|
+
```python
|
|
321
|
+
def calculate_cache_roi(
|
|
322
|
+
cache_hit_rate: float,
|
|
323
|
+
avg_query_tokens: int,
|
|
324
|
+
avg_response_tokens: int,
|
|
325
|
+
embedding_cost_per_1k: float = 0.00002,
|
|
326
|
+
llm_input_cost_per_1k: float = 0.01,
|
|
327
|
+
llm_output_cost_per_1k: float = 0.03
|
|
328
|
+
):
|
|
329
|
+
"""Calculate cost savings from semantic caching."""
|
|
330
|
+
|
|
331
|
+
# Cost with caching (embedding only on cache hit)
|
|
332
|
+
cache_cost = embedding_cost_per_1k * avg_query_tokens / 1000
|
|
333
|
+
|
|
334
|
+
# Cost without caching (full LLM call)
|
|
335
|
+
llm_cost = (
|
|
336
|
+
llm_input_cost_per_1k * avg_query_tokens / 1000 +
|
|
337
|
+
llm_output_cost_per_1k * avg_response_tokens / 1000
|
|
338
|
+
)
|
|
339
|
+
|
|
340
|
+
# Blended cost with cache
|
|
341
|
+
blended_cost = (cache_hit_rate * cache_cost) + ((1 - cache_hit_rate) * llm_cost)
|
|
342
|
+
|
|
343
|
+
savings = (llm_cost - blended_cost) / llm_cost * 100
|
|
344
|
+
|
|
345
|
+
return {
|
|
346
|
+
"cache_cost_per_query": cache_cost,
|
|
347
|
+
"llm_cost_per_query": llm_cost,
|
|
348
|
+
"blended_cost_per_query": blended_cost,
|
|
349
|
+
"savings_percentage": f"{savings:.1f}%"
|
|
350
|
+
}
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Context Reduction ROI
|
|
354
|
+
|
|
355
|
+
```python
|
|
356
|
+
def calculate_context_roi(
|
|
357
|
+
original_context_tokens: int,
|
|
358
|
+
retrieved_tokens: int,
|
|
359
|
+
llm_input_cost_per_1k: float = 0.01
|
|
360
|
+
):
|
|
361
|
+
"""Calculate savings from context reduction."""
|
|
362
|
+
|
|
363
|
+
original_cost = llm_input_cost_per_1k * original_context_tokens / 1000
|
|
364
|
+
optimized_cost = llm_input_cost_per_1k * retrieved_tokens / 1000
|
|
365
|
+
|
|
366
|
+
tokens_saved = original_context_tokens - retrieved_tokens
|
|
367
|
+
savings = (original_cost - optimized_cost) / original_cost * 100
|
|
368
|
+
|
|
369
|
+
return {
|
|
370
|
+
"original_tokens": original_context_tokens,
|
|
371
|
+
"optimized_tokens": retrieved_tokens,
|
|
372
|
+
"tokens_saved": tokens_saved,
|
|
373
|
+
"savings_percentage": f"{savings:.1f}%"
|
|
374
|
+
}
|
|
375
|
+
```
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# Collection Schemas Reference
|
|
2
|
+
|
|
3
|
+
This document provides detailed collection schemas for different memory use cases.
|
|
4
|
+
|
|
5
|
+
## Standard Agent Memory Collection
|
|
6
|
+
|
|
7
|
+
Optimized for general-purpose agent memory storage.
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
PUT /collections/agent_memory
|
|
11
|
+
{
|
|
12
|
+
"vectors": {
|
|
13
|
+
"size": 1536,
|
|
14
|
+
"distance": "Cosine"
|
|
15
|
+
},
|
|
16
|
+
"optimizers_config": {
|
|
17
|
+
"indexing_threshold": 10000,
|
|
18
|
+
"memmap_threshold": 20000
|
|
19
|
+
},
|
|
20
|
+
"hnsw_config": {
|
|
21
|
+
"m": 16,
|
|
22
|
+
"ef_construct": 100,
|
|
23
|
+
"full_scan_threshold": 10000
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Payload Schema
|
|
29
|
+
|
|
30
|
+
| Field | Type | Purpose | Indexed |
|
|
31
|
+
| ------------- | --------- | ------------------------- | -------------- |
|
|
32
|
+
| `content` | text | The actual memory content | No (too large) |
|
|
33
|
+
| `type` | keyword | Memory category | Yes |
|
|
34
|
+
| `project` | keyword | Project association | Yes |
|
|
35
|
+
| `timestamp` | datetime | Creation time | Yes |
|
|
36
|
+
| `tags` | keyword[] | Categorical tags | Yes |
|
|
37
|
+
| `model` | keyword | Embedding model used | Yes |
|
|
38
|
+
| `token_count` | integer | Token estimate | Yes |
|
|
39
|
+
| `user_id` | keyword | User association | Yes (optional) |
|
|
40
|
+
| `session_id` | keyword | Session tracking | Yes (optional) |
|
|
41
|
+
|
|
42
|
+
### Memory Types
|
|
43
|
+
|
|
44
|
+
| Type | Description | Typical Retention |
|
|
45
|
+
| -------------- | ------------------------------ | ----------------- |
|
|
46
|
+
| `decision` | Architectural/design decisions | Permanent |
|
|
47
|
+
| `code` | Code patterns and snippets | 90 days |
|
|
48
|
+
| `error` | Error resolutions | 60 days |
|
|
49
|
+
| `conversation` | Key conversation points | 30 days |
|
|
50
|
+
| `technical` | Technical documentation | Permanent |
|
|
51
|
+
| `cache` | Semantic cache entries | 7 days |
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Semantic Cache Collection
|
|
56
|
+
|
|
57
|
+
Optimized for fast similarity lookups with high precision.
|
|
58
|
+
|
|
59
|
+
```json
|
|
60
|
+
PUT /collections/semantic_cache
|
|
61
|
+
{
|
|
62
|
+
"vectors": {
|
|
63
|
+
"size": 1536,
|
|
64
|
+
"distance": "Cosine"
|
|
65
|
+
},
|
|
66
|
+
"optimizers_config": {
|
|
67
|
+
"indexing_threshold": 5000,
|
|
68
|
+
"memmap_threshold": 10000
|
|
69
|
+
},
|
|
70
|
+
"hnsw_config": {
|
|
71
|
+
"m": 32,
|
|
72
|
+
"ef_construct": 200,
|
|
73
|
+
"full_scan_threshold": 5000
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Cache Payload Schema
|
|
79
|
+
|
|
80
|
+
| Field | Type | Purpose |
|
|
81
|
+
| ------------- | -------- | ------------------------------ |
|
|
82
|
+
| `query` | keyword | Original query (for debugging) |
|
|
83
|
+
| `response` | text | Cached LLM response |
|
|
84
|
+
| `timestamp` | datetime | Cache entry time |
|
|
85
|
+
| `model` | keyword | Model that generated response |
|
|
86
|
+
| `token_count` | integer | Response token count |
|
|
87
|
+
| `ttl_days` | integer | Time-to-live in days |
|
|
88
|
+
|
|
89
|
+
### Cache Hit Thresholds
|
|
90
|
+
|
|
91
|
+
| Similarity | Confidence | Action |
|
|
92
|
+
| ---------- | ---------- | ----------------------------- |
|
|
93
|
+
| ≥ 0.95 | Very High | Return cached, log hit |
|
|
94
|
+
| 0.90-0.94 | High | Return cached with disclaimer |
|
|
95
|
+
| 0.85-0.89 | Medium | Offer cached, allow refresh |
|
|
96
|
+
| < 0.85 | Low | Proceed to LLM |
|
|
97
|
+
|
|
98
|
+
---
|
|
99
|
+
|
|
100
|
+
## Multi-Tenant Collection
|
|
101
|
+
|
|
102
|
+
For applications serving multiple users/projects.
|
|
103
|
+
|
|
104
|
+
```json
|
|
105
|
+
PUT /collections/multi_tenant_memory
|
|
106
|
+
{
|
|
107
|
+
"vectors": {
|
|
108
|
+
"size": 1536,
|
|
109
|
+
"distance": "Cosine"
|
|
110
|
+
},
|
|
111
|
+
"optimizers_config": {
|
|
112
|
+
"indexing_threshold": 20000
|
|
113
|
+
},
|
|
114
|
+
"hnsw_config": {
|
|
115
|
+
"m": 16,
|
|
116
|
+
"ef_construct": 128
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Tenant Isolation Patterns
|
|
122
|
+
|
|
123
|
+
**Pattern 1: Payload Filtering**
|
|
124
|
+
|
|
125
|
+
```json
|
|
126
|
+
{
|
|
127
|
+
"filter": {
|
|
128
|
+
"must": [{ "key": "tenant_id", "match": { "value": "tenant_123" } }]
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
**Pattern 2: Collection per Tenant**
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
tenant_123_memory/
|
|
137
|
+
tenant_456_memory/
|
|
138
|
+
tenant_789_memory/
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Recommended: Shard Key Optimization
|
|
142
|
+
|
|
143
|
+
For large multi-tenant deployments:
|
|
144
|
+
|
|
145
|
+
```json
|
|
146
|
+
{
|
|
147
|
+
"shard_number": 4,
|
|
148
|
+
"replication_factor": 2,
|
|
149
|
+
"write_consistency_factor": 1
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
---
|
|
154
|
+
|
|
155
|
+
## Code Patterns Collection
|
|
156
|
+
|
|
157
|
+
Optimized for code snippet retrieval.
|
|
158
|
+
|
|
159
|
+
```json
|
|
160
|
+
PUT /collections/code_patterns
|
|
161
|
+
{
|
|
162
|
+
"vectors": {
|
|
163
|
+
"size": 1536,
|
|
164
|
+
"distance": "Cosine"
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Code Payload Schema
|
|
170
|
+
|
|
171
|
+
| Field | Type | Purpose |
|
|
172
|
+
| -------------- | ------- | -------------------------------- |
|
|
173
|
+
| `code` | text | The actual code |
|
|
174
|
+
| `language` | keyword | Programming language |
|
|
175
|
+
| `framework` | keyword | Framework (React, FastAPI, etc.) |
|
|
176
|
+
| `pattern_type` | keyword | Pattern category |
|
|
177
|
+
| `description` | text | What the code does |
|
|
178
|
+
| `file_path` | keyword | Original file location |
|
|
179
|
+
| `project` | keyword | Source project |
|
|
180
|
+
|
|
181
|
+
### Pattern Types
|
|
182
|
+
|
|
183
|
+
- `api_endpoint`
|
|
184
|
+
- `database_query`
|
|
185
|
+
- `authentication`
|
|
186
|
+
- `error_handling`
|
|
187
|
+
- `data_transformation`
|
|
188
|
+
- `testing`
|
|
189
|
+
- `configuration`
|
|
190
|
+
|
|
191
|
+
---
|
|
192
|
+
|
|
193
|
+
## Indexing Best Practices
|
|
194
|
+
|
|
195
|
+
### When to Create Payload Indexes
|
|
196
|
+
|
|
197
|
+
```json
|
|
198
|
+
PUT /collections/{collection}/index
|
|
199
|
+
{
|
|
200
|
+
"field_name": "type",
|
|
201
|
+
"field_schema": "keyword"
|
|
202
|
+
}
|
|
203
|
+
```
|
|
204
|
+
|
|
205
|
+
**Index when**:
|
|
206
|
+
|
|
207
|
+
- Field is frequently filtered
|
|
208
|
+
- Field has high cardinality but limited unique values
|
|
209
|
+
- Field is used in range queries
|
|
210
|
+
|
|
211
|
+
**Don't index**:
|
|
212
|
+
|
|
213
|
+
- Large text fields (content, code)
|
|
214
|
+
- Fields rarely used in filters
|
|
215
|
+
- Fields with extremely high cardinality (UUIDs used once)
|
|
216
|
+
|
|
217
|
+
### HNSW Parameter Tuning
|
|
218
|
+
|
|
219
|
+
| Parameter | Low Value | High Value | Trade-off |
|
|
220
|
+
| -------------- | --------- | ---------- | ---------------------- |
|
|
221
|
+
| `m` | 4 | 64 | Memory vs. Recall |
|
|
222
|
+
| `ef_construct` | 50 | 500 | Index time vs. Quality |
|
|
223
|
+
| `ef` (search) | 50 | 500 | Speed vs. Accuracy |
|
|
224
|
+
|
|
225
|
+
**Recommended defaults**:
|
|
226
|
+
|
|
227
|
+
- Development: `m=16, ef_construct=100`
|
|
228
|
+
- Production: `m=32, ef_construct=200`
|
|
229
|
+
- High-precision: `m=48, ef_construct=400`
|