@zimezone/z-command 1.1.0 → 1.1.1

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.
Files changed (182) hide show
  1. package/package.json +4 -1
  2. package/templates/agents/api-documenter.agent.md +161 -0
  3. package/templates/agents/architect-review.agent.md +146 -0
  4. package/templates/agents/arm-cortex-expert.agent.md +288 -0
  5. package/templates/agents/backend-architect.agent.md +309 -0
  6. package/templates/agents/backend-security-coder.agent.md +152 -0
  7. package/templates/agents/bash-pro.agent.md +285 -0
  8. package/templates/agents/c-pro.agent.md +35 -0
  9. package/templates/agents/c4-code.agent.md +320 -0
  10. package/templates/agents/c4-component.agent.md +227 -0
  11. package/templates/agents/c4-container.agent.md +248 -0
  12. package/templates/agents/c4-context.agent.md +235 -0
  13. package/templates/agents/conductor-validator.agent.md +245 -0
  14. package/templates/agents/csharp-pro.agent.md +38 -0
  15. package/templates/agents/customer-support.agent.md +148 -0
  16. package/templates/agents/database-admin.agent.md +142 -0
  17. package/templates/agents/database-architect.agent.md +238 -0
  18. package/templates/agents/database-optimizer.agent.md +144 -0
  19. package/templates/agents/debugger.agent.md +30 -0
  20. package/templates/agents/deployment-engineer.agent.md +0 -0
  21. package/templates/agents/devops-troubleshooter.agent.md +138 -0
  22. package/templates/agents/django-pro.agent.md +159 -0
  23. package/templates/agents/docs-architect.agent.md +77 -0
  24. package/templates/agents/dotnet-architect.agent.md +175 -0
  25. package/templates/agents/dx-optimizer.agent.md +63 -0
  26. package/templates/agents/elixir-pro.agent.md +38 -0
  27. package/templates/agents/error-detective.agent.md +32 -0
  28. package/templates/agents/event-sourcing-architect.agent.md +42 -0
  29. package/templates/agents/fastapi-pro.agent.md +171 -0
  30. package/templates/agents/firmware-analyst.agent.md +330 -0
  31. package/templates/agents/frontend-security-coder.agent.md +149 -0
  32. package/templates/agents/haskell-pro.agent.md +37 -0
  33. package/templates/agents/hr-pro.agent.md +105 -0
  34. package/templates/agents/incident-responder.agent.md +190 -0
  35. package/templates/agents/ios-developer.agent.md +198 -0
  36. package/templates/agents/java-pro.agent.md +156 -0
  37. package/templates/agents/javascript-pro.agent.md +35 -0
  38. package/templates/agents/julia-pro.agent.md +187 -0
  39. package/templates/agents/legal-advisor.agent.md +49 -0
  40. package/templates/agents/malware-analyst.agent.md +272 -0
  41. package/templates/agents/mermaid-expert.agent.md +39 -0
  42. package/templates/agents/minecraft-bukkit-pro.agent.md +104 -0
  43. package/templates/agents/mobile-security-coder.agent.md +163 -0
  44. package/templates/agents/monorepo-architect.agent.md +44 -0
  45. package/templates/agents/observability-engineer.agent.md +228 -0
  46. package/templates/agents/performance-engineer.agent.md +167 -0
  47. package/templates/agents/php-pro.agent.md +43 -0
  48. package/templates/agents/posix-shell-pro.agent.md +284 -0
  49. package/templates/agents/quant-analyst.agent.md +32 -0
  50. package/templates/agents/reference-builder.agent.md +167 -0
  51. package/templates/agents/reverse-engineer.agent.md +202 -0
  52. package/templates/agents/risk-manager.agent.md +41 -0
  53. package/templates/agents/ruby-pro.agent.md +35 -0
  54. package/templates/agents/rust-pro.agent.md +156 -0
  55. package/templates/agents/sales-automator.agent.md +35 -0
  56. package/templates/agents/scala-pro.agent.md +60 -0
  57. package/templates/agents/search-specialist.agent.md +59 -0
  58. package/templates/agents/security-auditor.agent.md +138 -0
  59. package/templates/agents/seo-authority-builder.agent.md +116 -0
  60. package/templates/agents/seo-cannibalization-detector.agent.md +103 -0
  61. package/templates/agents/seo-content-auditor.agent.md +63 -0
  62. package/templates/agents/seo-content-planner.agent.md +88 -0
  63. package/templates/agents/seo-content-refresher.agent.md +98 -0
  64. package/templates/agents/seo-content-writer.agent.md +76 -0
  65. package/templates/agents/seo-keyword-strategist.agent.md +75 -0
  66. package/templates/agents/seo-meta-optimizer.agent.md +72 -0
  67. package/templates/agents/seo-snippet-hunter.agent.md +94 -0
  68. package/templates/agents/seo-structure-architect.agent.md +88 -0
  69. package/templates/agents/service-mesh-expert.agent.md +41 -0
  70. package/templates/agents/sql-pro.agent.md +146 -0
  71. package/templates/agents/tdd-orchestrator.agent.md +183 -0
  72. package/templates/agents/temporal-python-pro.agent.md +349 -0
  73. package/templates/agents/terraform-specialist.agent.md +137 -0
  74. package/templates/agents/test-automator.agent.md +203 -0
  75. package/templates/agents/threat-modeling-expert.agent.md +44 -0
  76. package/templates/agents/tutorial-engineer.agent.md +118 -0
  77. package/templates/agents/ui-ux-designer.agent.md +188 -0
  78. package/templates/agents/ui-visual-validator.agent.md +192 -0
  79. package/templates/agents/vector-database-engineer.agent.md +43 -0
  80. package/templates/skills/angular-migration/SKILL.md +410 -0
  81. package/templates/skills/api-design-principles/SKILL.md +528 -0
  82. package/templates/skills/api-design-principles/assets/api-design-checklist.md +155 -0
  83. package/templates/skills/api-design-principles/assets/rest-api-template.py +182 -0
  84. package/templates/skills/api-design-principles/references/graphql-schema-design.md +583 -0
  85. package/templates/skills/api-design-principles/references/rest-best-practices.md +408 -0
  86. package/templates/skills/architecture-decision-records/SKILL.md +428 -0
  87. package/templates/skills/architecture-patterns/SKILL.md +494 -0
  88. package/templates/skills/async-python-patterns/SKILL.md +694 -0
  89. package/templates/skills/auth-implementation-patterns/SKILL.md +634 -0
  90. package/templates/skills/changelog-automation/SKILL.md +552 -0
  91. package/templates/skills/code-review-excellence/SKILL.md +520 -0
  92. package/templates/skills/competitive-landscape/SKILL.md +479 -0
  93. package/templates/skills/context-driven-development/SKILL.md +385 -0
  94. package/templates/skills/cost-optimization/SKILL.md +274 -0
  95. package/templates/skills/cqrs-implementation/SKILL.md +554 -0
  96. package/templates/skills/data-quality-frameworks/SKILL.md +587 -0
  97. package/templates/skills/data-storytelling/SKILL.md +453 -0
  98. package/templates/skills/database-migration/SKILL.md +424 -0
  99. package/templates/skills/dbt-transformation-patterns/SKILL.md +561 -0
  100. package/templates/skills/debugging-strategies/SKILL.md +527 -0
  101. package/templates/skills/defi-protocol-templates/SKILL.md +454 -0
  102. package/templates/skills/dependency-upgrade/SKILL.md +409 -0
  103. package/templates/skills/deployment-pipeline-design/SKILL.md +359 -0
  104. package/templates/skills/distributed-tracing/SKILL.md +438 -0
  105. package/templates/skills/dotnet-backend-patterns/SKILL.md +815 -0
  106. package/templates/skills/dotnet-backend-patterns/assets/repository-template.cs +523 -0
  107. package/templates/skills/dotnet-backend-patterns/assets/service-template.cs +336 -0
  108. package/templates/skills/dotnet-backend-patterns/references/dapper-patterns.md +544 -0
  109. package/templates/skills/dotnet-backend-patterns/references/ef-core-best-practices.md +355 -0
  110. package/templates/skills/e2e-testing-patterns/SKILL.md +547 -0
  111. package/templates/skills/employment-contract-templates/SKILL.md +507 -0
  112. package/templates/skills/error-handling-patterns/SKILL.md +636 -0
  113. package/templates/skills/event-store-design/SKILL.md +437 -0
  114. package/templates/skills/fastapi-templates/SKILL.md +567 -0
  115. package/templates/skills/git-advanced-workflows/SKILL.md +400 -0
  116. package/templates/skills/github-actions-templates/SKILL.md +333 -0
  117. package/templates/skills/go-concurrency-patterns/SKILL.md +655 -0
  118. package/templates/skills/grafana-dashboards/SKILL.md +369 -0
  119. package/templates/skills/helm-chart-scaffolding/SKILL.md +544 -0
  120. package/templates/skills/helm-chart-scaffolding/assets/Chart.yaml.template +42 -0
  121. package/templates/skills/helm-chart-scaffolding/assets/values.yaml.template +185 -0
  122. package/templates/skills/helm-chart-scaffolding/references/chart-structure.md +500 -0
  123. package/templates/skills/helm-chart-scaffolding/scripts/validate-chart.sh +244 -0
  124. package/templates/skills/javascript-testing-patterns/SKILL.md +1025 -0
  125. package/templates/skills/langchain-architecture/SKILL.md +338 -0
  126. package/templates/skills/llm-evaluation/SKILL.md +471 -0
  127. package/templates/skills/microservices-patterns/SKILL.md +595 -0
  128. package/templates/skills/modern-javascript-patterns/SKILL.md +911 -0
  129. package/templates/skills/monorepo-management/SKILL.md +622 -0
  130. package/templates/skills/nextjs-app-router-patterns/SKILL.md +544 -0
  131. package/templates/skills/nodejs-backend-patterns/SKILL.md +1020 -0
  132. package/templates/skills/nx-workspace-patterns/SKILL.md +452 -0
  133. package/templates/skills/openapi-spec-generation/SKILL.md +1028 -0
  134. package/templates/skills/paypal-integration/SKILL.md +467 -0
  135. package/templates/skills/pci-compliance/SKILL.md +466 -0
  136. package/templates/skills/postgresql/SKILL.md +204 -0
  137. package/templates/skills/projection-patterns/SKILL.md +490 -0
  138. package/templates/skills/prometheus-configuration/SKILL.md +392 -0
  139. package/templates/skills/prompt-engineering-patterns/SKILL.md +201 -0
  140. package/templates/skills/prompt-engineering-patterns/assets/few-shot-examples.json +106 -0
  141. package/templates/skills/prompt-engineering-patterns/assets/prompt-template-library.md +246 -0
  142. package/templates/skills/prompt-engineering-patterns/references/chain-of-thought.md +399 -0
  143. package/templates/skills/prompt-engineering-patterns/references/few-shot-learning.md +369 -0
  144. package/templates/skills/prompt-engineering-patterns/references/prompt-optimization.md +414 -0
  145. package/templates/skills/prompt-engineering-patterns/references/prompt-templates.md +470 -0
  146. package/templates/skills/prompt-engineering-patterns/references/system-prompts.md +189 -0
  147. package/templates/skills/prompt-engineering-patterns/scripts/optimize-prompt.py +279 -0
  148. package/templates/skills/python-packaging/SKILL.md +870 -0
  149. package/templates/skills/python-performance-optimization/SKILL.md +869 -0
  150. package/templates/skills/python-testing-patterns/SKILL.md +907 -0
  151. package/templates/skills/rag-implementation/SKILL.md +403 -0
  152. package/templates/skills/react-modernization/SKILL.md +513 -0
  153. package/templates/skills/react-native-architecture/SKILL.md +671 -0
  154. package/templates/skills/react-state-management/SKILL.md +429 -0
  155. package/templates/skills/risk-metrics-calculation/SKILL.md +555 -0
  156. package/templates/skills/rust-async-patterns/SKILL.md +517 -0
  157. package/templates/skills/secrets-management/SKILL.md +346 -0
  158. package/templates/skills/security-requirement-extraction/SKILL.md +677 -0
  159. package/templates/skills/shellcheck-configuration/SKILL.md +454 -0
  160. package/templates/skills/similarity-search-patterns/SKILL.md +558 -0
  161. package/templates/skills/slo-implementation/SKILL.md +329 -0
  162. package/templates/skills/sql-optimization-patterns/SKILL.md +493 -0
  163. package/templates/skills/stripe-integration/SKILL.md +442 -0
  164. package/templates/skills/tailwind-design-system/SKILL.md +666 -0
  165. package/templates/skills/temporal-python-testing/SKILL.md +158 -0
  166. package/templates/skills/temporal-python-testing/resources/integration-testing.md +455 -0
  167. package/templates/skills/temporal-python-testing/resources/local-setup.md +553 -0
  168. package/templates/skills/temporal-python-testing/resources/replay-testing.md +462 -0
  169. package/templates/skills/temporal-python-testing/resources/unit-testing.md +328 -0
  170. package/templates/skills/terraform-module-library/SKILL.md +249 -0
  171. package/templates/skills/terraform-module-library/references/aws-modules.md +63 -0
  172. package/templates/skills/threat-mitigation-mapping/SKILL.md +745 -0
  173. package/templates/skills/track-management/SKILL.md +593 -0
  174. package/templates/skills/typescript-advanced-types/SKILL.md +717 -0
  175. package/templates/skills/uv-package-manager/SKILL.md +831 -0
  176. package/templates/skills/vector-index-tuning/SKILL.md +521 -0
  177. package/templates/skills/wcag-audit-patterns/SKILL.md +555 -0
  178. package/templates/skills/workflow-orchestration-patterns/SKILL.md +316 -0
  179. package/templates/skills/workflow-patterns/SKILL.md +623 -0
  180. package/templates/agents/game-developer.agent.md +0 -57
  181. package/templates/agents/kubernetes-specialist.agent.md +0 -56
  182. package/templates/agents/market-researcher.agent.md +0 -47
@@ -0,0 +1,558 @@
1
+ ---
2
+ name: similarity-search-patterns
3
+ description: Implement efficient similarity search with vector databases. Use when building semantic search, implementing nearest neighbor queries, or optimizing retrieval performance.
4
+ ---
5
+
6
+ # Similarity Search Patterns
7
+
8
+ Patterns for implementing efficient similarity search in production systems.
9
+
10
+ ## When to Use This Skill
11
+
12
+ - Building semantic search systems
13
+ - Implementing RAG retrieval
14
+ - Creating recommendation engines
15
+ - Optimizing search latency
16
+ - Scaling to millions of vectors
17
+ - Combining semantic and keyword search
18
+
19
+ ## Core Concepts
20
+
21
+ ### 1. Distance Metrics
22
+
23
+ | Metric | Formula | Best For |
24
+ |--------|---------|----------|
25
+ | **Cosine** | 1 - (A·B)/(‖A‖‖B‖) | Normalized embeddings |
26
+ | **Euclidean (L2)** | √Σ(a-b)² | Raw embeddings |
27
+ | **Dot Product** | A·B | Magnitude matters |
28
+ | **Manhattan (L1)** | Σ|a-b| | Sparse vectors |
29
+
30
+ ### 2. Index Types
31
+
32
+ ```
33
+ ┌─────────────────────────────────────────────────┐
34
+ │ Index Types │
35
+ ├─────────────┬───────────────┬───────────────────┤
36
+ │ Flat │ HNSW │ IVF+PQ │
37
+ │ (Exact) │ (Graph-based) │ (Quantized) │
38
+ ├─────────────┼───────────────┼───────────────────┤
39
+ │ O(n) search │ O(log n) │ O(√n) │
40
+ │ 100% recall │ ~95-99% │ ~90-95% │
41
+ │ Small data │ Medium-Large │ Very Large │
42
+ └─────────────┴───────────────┴───────────────────┘
43
+ ```
44
+
45
+ ## Templates
46
+
47
+ ### Template 1: Pinecone Implementation
48
+
49
+ ```python
50
+ from pinecone import Pinecone, ServerlessSpec
51
+ from typing import List, Dict, Optional
52
+ import hashlib
53
+
54
+ class PineconeVectorStore:
55
+ def __init__(
56
+ self,
57
+ api_key: str,
58
+ index_name: str,
59
+ dimension: int = 1536,
60
+ metric: str = "cosine"
61
+ ):
62
+ self.pc = Pinecone(api_key=api_key)
63
+
64
+ # Create index if not exists
65
+ if index_name not in self.pc.list_indexes().names():
66
+ self.pc.create_index(
67
+ name=index_name,
68
+ dimension=dimension,
69
+ metric=metric,
70
+ spec=ServerlessSpec(cloud="aws", region="us-east-1")
71
+ )
72
+
73
+ self.index = self.pc.Index(index_name)
74
+
75
+ def upsert(
76
+ self,
77
+ vectors: List[Dict],
78
+ namespace: str = ""
79
+ ) -> int:
80
+ """
81
+ Upsert vectors.
82
+ vectors: [{"id": str, "values": List[float], "metadata": dict}]
83
+ """
84
+ # Batch upsert
85
+ batch_size = 100
86
+ total = 0
87
+
88
+ for i in range(0, len(vectors), batch_size):
89
+ batch = vectors[i:i + batch_size]
90
+ self.index.upsert(vectors=batch, namespace=namespace)
91
+ total += len(batch)
92
+
93
+ return total
94
+
95
+ def search(
96
+ self,
97
+ query_vector: List[float],
98
+ top_k: int = 10,
99
+ namespace: str = "",
100
+ filter: Optional[Dict] = None,
101
+ include_metadata: bool = True
102
+ ) -> List[Dict]:
103
+ """Search for similar vectors."""
104
+ results = self.index.query(
105
+ vector=query_vector,
106
+ top_k=top_k,
107
+ namespace=namespace,
108
+ filter=filter,
109
+ include_metadata=include_metadata
110
+ )
111
+
112
+ return [
113
+ {
114
+ "id": match.id,
115
+ "score": match.score,
116
+ "metadata": match.metadata
117
+ }
118
+ for match in results.matches
119
+ ]
120
+
121
+ def search_with_rerank(
122
+ self,
123
+ query: str,
124
+ query_vector: List[float],
125
+ top_k: int = 10,
126
+ rerank_top_n: int = 50,
127
+ namespace: str = ""
128
+ ) -> List[Dict]:
129
+ """Search and rerank results."""
130
+ # Over-fetch for reranking
131
+ initial_results = self.search(
132
+ query_vector,
133
+ top_k=rerank_top_n,
134
+ namespace=namespace
135
+ )
136
+
137
+ # Rerank with cross-encoder or LLM
138
+ reranked = self._rerank(query, initial_results)
139
+
140
+ return reranked[:top_k]
141
+
142
+ def _rerank(self, query: str, results: List[Dict]) -> List[Dict]:
143
+ """Rerank results using cross-encoder."""
144
+ from sentence_transformers import CrossEncoder
145
+
146
+ model = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
147
+
148
+ pairs = [(query, r["metadata"]["text"]) for r in results]
149
+ scores = model.predict(pairs)
150
+
151
+ for result, score in zip(results, scores):
152
+ result["rerank_score"] = float(score)
153
+
154
+ return sorted(results, key=lambda x: x["rerank_score"], reverse=True)
155
+
156
+ def delete(self, ids: List[str], namespace: str = ""):
157
+ """Delete vectors by ID."""
158
+ self.index.delete(ids=ids, namespace=namespace)
159
+
160
+ def delete_by_filter(self, filter: Dict, namespace: str = ""):
161
+ """Delete vectors matching filter."""
162
+ self.index.delete(filter=filter, namespace=namespace)
163
+ ```
164
+
165
+ ### Template 2: Qdrant Implementation
166
+
167
+ ```python
168
+ from qdrant_client import QdrantClient
169
+ from qdrant_client.http import models
170
+ from typing import List, Dict, Optional
171
+
172
+ class QdrantVectorStore:
173
+ def __init__(
174
+ self,
175
+ url: str = "localhost",
176
+ port: int = 6333,
177
+ collection_name: str = "documents",
178
+ vector_size: int = 1536
179
+ ):
180
+ self.client = QdrantClient(url=url, port=port)
181
+ self.collection_name = collection_name
182
+
183
+ # Create collection if not exists
184
+ collections = self.client.get_collections().collections
185
+ if collection_name not in [c.name for c in collections]:
186
+ self.client.create_collection(
187
+ collection_name=collection_name,
188
+ vectors_config=models.VectorParams(
189
+ size=vector_size,
190
+ distance=models.Distance.COSINE
191
+ ),
192
+ # Optional: enable quantization for memory efficiency
193
+ quantization_config=models.ScalarQuantization(
194
+ scalar=models.ScalarQuantizationConfig(
195
+ type=models.ScalarType.INT8,
196
+ quantile=0.99,
197
+ always_ram=True
198
+ )
199
+ )
200
+ )
201
+
202
+ def upsert(self, points: List[Dict]) -> int:
203
+ """
204
+ Upsert points.
205
+ points: [{"id": str/int, "vector": List[float], "payload": dict}]
206
+ """
207
+ qdrant_points = [
208
+ models.PointStruct(
209
+ id=p["id"],
210
+ vector=p["vector"],
211
+ payload=p.get("payload", {})
212
+ )
213
+ for p in points
214
+ ]
215
+
216
+ self.client.upsert(
217
+ collection_name=self.collection_name,
218
+ points=qdrant_points
219
+ )
220
+ return len(points)
221
+
222
+ def search(
223
+ self,
224
+ query_vector: List[float],
225
+ limit: int = 10,
226
+ filter: Optional[models.Filter] = None,
227
+ score_threshold: Optional[float] = None
228
+ ) -> List[Dict]:
229
+ """Search for similar vectors."""
230
+ results = self.client.search(
231
+ collection_name=self.collection_name,
232
+ query_vector=query_vector,
233
+ limit=limit,
234
+ query_filter=filter,
235
+ score_threshold=score_threshold
236
+ )
237
+
238
+ return [
239
+ {
240
+ "id": r.id,
241
+ "score": r.score,
242
+ "payload": r.payload
243
+ }
244
+ for r in results
245
+ ]
246
+
247
+ def search_with_filter(
248
+ self,
249
+ query_vector: List[float],
250
+ must_conditions: List[Dict] = None,
251
+ should_conditions: List[Dict] = None,
252
+ must_not_conditions: List[Dict] = None,
253
+ limit: int = 10
254
+ ) -> List[Dict]:
255
+ """Search with complex filters."""
256
+ conditions = []
257
+
258
+ if must_conditions:
259
+ conditions.extend([
260
+ models.FieldCondition(
261
+ key=c["key"],
262
+ match=models.MatchValue(value=c["value"])
263
+ )
264
+ for c in must_conditions
265
+ ])
266
+
267
+ filter = models.Filter(must=conditions) if conditions else None
268
+
269
+ return self.search(query_vector, limit=limit, filter=filter)
270
+
271
+ def search_with_sparse(
272
+ self,
273
+ dense_vector: List[float],
274
+ sparse_vector: Dict[int, float],
275
+ limit: int = 10,
276
+ dense_weight: float = 0.7
277
+ ) -> List[Dict]:
278
+ """Hybrid search with dense and sparse vectors."""
279
+ # Requires collection with named vectors
280
+ results = self.client.search(
281
+ collection_name=self.collection_name,
282
+ query_vector=models.NamedVector(
283
+ name="dense",
284
+ vector=dense_vector
285
+ ),
286
+ limit=limit
287
+ )
288
+ return [{"id": r.id, "score": r.score, "payload": r.payload} for r in results]
289
+ ```
290
+
291
+ ### Template 3: pgvector with PostgreSQL
292
+
293
+ ```python
294
+ import asyncpg
295
+ from typing import List, Dict, Optional
296
+ import numpy as np
297
+
298
+ class PgVectorStore:
299
+ def __init__(self, connection_string: str):
300
+ self.connection_string = connection_string
301
+
302
+ async def init(self):
303
+ """Initialize connection pool and extension."""
304
+ self.pool = await asyncpg.create_pool(self.connection_string)
305
+
306
+ async with self.pool.acquire() as conn:
307
+ # Enable extension
308
+ await conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
309
+
310
+ # Create table
311
+ await conn.execute("""
312
+ CREATE TABLE IF NOT EXISTS documents (
313
+ id TEXT PRIMARY KEY,
314
+ content TEXT,
315
+ metadata JSONB,
316
+ embedding vector(1536)
317
+ )
318
+ """)
319
+
320
+ # Create index (HNSW for better performance)
321
+ await conn.execute("""
322
+ CREATE INDEX IF NOT EXISTS documents_embedding_idx
323
+ ON documents
324
+ USING hnsw (embedding vector_cosine_ops)
325
+ WITH (m = 16, ef_construction = 64)
326
+ """)
327
+
328
+ async def upsert(self, documents: List[Dict]):
329
+ """Upsert documents with embeddings."""
330
+ async with self.pool.acquire() as conn:
331
+ await conn.executemany(
332
+ """
333
+ INSERT INTO documents (id, content, metadata, embedding)
334
+ VALUES ($1, $2, $3, $4)
335
+ ON CONFLICT (id) DO UPDATE SET
336
+ content = EXCLUDED.content,
337
+ metadata = EXCLUDED.metadata,
338
+ embedding = EXCLUDED.embedding
339
+ """,
340
+ [
341
+ (
342
+ doc["id"],
343
+ doc["content"],
344
+ doc.get("metadata", {}),
345
+ np.array(doc["embedding"]).tolist()
346
+ )
347
+ for doc in documents
348
+ ]
349
+ )
350
+
351
+ async def search(
352
+ self,
353
+ query_embedding: List[float],
354
+ limit: int = 10,
355
+ filter_metadata: Optional[Dict] = None
356
+ ) -> List[Dict]:
357
+ """Search for similar documents."""
358
+ query = """
359
+ SELECT id, content, metadata,
360
+ 1 - (embedding <=> $1::vector) as similarity
361
+ FROM documents
362
+ """
363
+
364
+ params = [query_embedding]
365
+
366
+ if filter_metadata:
367
+ conditions = []
368
+ for key, value in filter_metadata.items():
369
+ params.append(value)
370
+ conditions.append(f"metadata->>'{key}' = ${len(params)}")
371
+ query += " WHERE " + " AND ".join(conditions)
372
+
373
+ query += f" ORDER BY embedding <=> $1::vector LIMIT ${len(params) + 1}"
374
+ params.append(limit)
375
+
376
+ async with self.pool.acquire() as conn:
377
+ rows = await conn.fetch(query, *params)
378
+
379
+ return [
380
+ {
381
+ "id": row["id"],
382
+ "content": row["content"],
383
+ "metadata": row["metadata"],
384
+ "score": row["similarity"]
385
+ }
386
+ for row in rows
387
+ ]
388
+
389
+ async def hybrid_search(
390
+ self,
391
+ query_embedding: List[float],
392
+ query_text: str,
393
+ limit: int = 10,
394
+ vector_weight: float = 0.5
395
+ ) -> List[Dict]:
396
+ """Hybrid search combining vector and full-text."""
397
+ async with self.pool.acquire() as conn:
398
+ rows = await conn.fetch(
399
+ """
400
+ WITH vector_results AS (
401
+ SELECT id, content, metadata,
402
+ 1 - (embedding <=> $1::vector) as vector_score
403
+ FROM documents
404
+ ORDER BY embedding <=> $1::vector
405
+ LIMIT $3 * 2
406
+ ),
407
+ text_results AS (
408
+ SELECT id, content, metadata,
409
+ ts_rank(to_tsvector('english', content),
410
+ plainto_tsquery('english', $2)) as text_score
411
+ FROM documents
412
+ WHERE to_tsvector('english', content) @@ plainto_tsquery('english', $2)
413
+ LIMIT $3 * 2
414
+ )
415
+ SELECT
416
+ COALESCE(v.id, t.id) as id,
417
+ COALESCE(v.content, t.content) as content,
418
+ COALESCE(v.metadata, t.metadata) as metadata,
419
+ COALESCE(v.vector_score, 0) * $4 +
420
+ COALESCE(t.text_score, 0) * (1 - $4) as combined_score
421
+ FROM vector_results v
422
+ FULL OUTER JOIN text_results t ON v.id = t.id
423
+ ORDER BY combined_score DESC
424
+ LIMIT $3
425
+ """,
426
+ query_embedding, query_text, limit, vector_weight
427
+ )
428
+
429
+ return [dict(row) for row in rows]
430
+ ```
431
+
432
+ ### Template 4: Weaviate Implementation
433
+
434
+ ```python
435
+ import weaviate
436
+ from weaviate.util import generate_uuid5
437
+ from typing import List, Dict, Optional
438
+
439
+ class WeaviateVectorStore:
440
+ def __init__(
441
+ self,
442
+ url: str = "http://localhost:8080",
443
+ class_name: str = "Document"
444
+ ):
445
+ self.client = weaviate.Client(url=url)
446
+ self.class_name = class_name
447
+ self._ensure_schema()
448
+
449
+ def _ensure_schema(self):
450
+ """Create schema if not exists."""
451
+ schema = {
452
+ "class": self.class_name,
453
+ "vectorizer": "none", # We provide vectors
454
+ "properties": [
455
+ {"name": "content", "dataType": ["text"]},
456
+ {"name": "source", "dataType": ["string"]},
457
+ {"name": "chunk_id", "dataType": ["int"]}
458
+ ]
459
+ }
460
+
461
+ if not self.client.schema.exists(self.class_name):
462
+ self.client.schema.create_class(schema)
463
+
464
+ def upsert(self, documents: List[Dict]):
465
+ """Batch upsert documents."""
466
+ with self.client.batch as batch:
467
+ batch.batch_size = 100
468
+
469
+ for doc in documents:
470
+ batch.add_data_object(
471
+ data_object={
472
+ "content": doc["content"],
473
+ "source": doc.get("source", ""),
474
+ "chunk_id": doc.get("chunk_id", 0)
475
+ },
476
+ class_name=self.class_name,
477
+ uuid=generate_uuid5(doc["id"]),
478
+ vector=doc["embedding"]
479
+ )
480
+
481
+ def search(
482
+ self,
483
+ query_vector: List[float],
484
+ limit: int = 10,
485
+ where_filter: Optional[Dict] = None
486
+ ) -> List[Dict]:
487
+ """Vector search."""
488
+ query = (
489
+ self.client.query
490
+ .get(self.class_name, ["content", "source", "chunk_id"])
491
+ .with_near_vector({"vector": query_vector})
492
+ .with_limit(limit)
493
+ .with_additional(["distance", "id"])
494
+ )
495
+
496
+ if where_filter:
497
+ query = query.with_where(where_filter)
498
+
499
+ results = query.do()
500
+
501
+ return [
502
+ {
503
+ "id": item["_additional"]["id"],
504
+ "content": item["content"],
505
+ "source": item["source"],
506
+ "score": 1 - item["_additional"]["distance"]
507
+ }
508
+ for item in results["data"]["Get"][self.class_name]
509
+ ]
510
+
511
+ def hybrid_search(
512
+ self,
513
+ query: str,
514
+ query_vector: List[float],
515
+ limit: int = 10,
516
+ alpha: float = 0.5 # 0 = keyword, 1 = vector
517
+ ) -> List[Dict]:
518
+ """Hybrid search combining BM25 and vector."""
519
+ results = (
520
+ self.client.query
521
+ .get(self.class_name, ["content", "source"])
522
+ .with_hybrid(query=query, vector=query_vector, alpha=alpha)
523
+ .with_limit(limit)
524
+ .with_additional(["score"])
525
+ .do()
526
+ )
527
+
528
+ return [
529
+ {
530
+ "content": item["content"],
531
+ "source": item["source"],
532
+ "score": item["_additional"]["score"]
533
+ }
534
+ for item in results["data"]["Get"][self.class_name]
535
+ ]
536
+ ```
537
+
538
+ ## Best Practices
539
+
540
+ ### Do's
541
+ - **Use appropriate index** - HNSW for most cases
542
+ - **Tune parameters** - ef_search, nprobe for recall/speed
543
+ - **Implement hybrid search** - Combine with keyword search
544
+ - **Monitor recall** - Measure search quality
545
+ - **Pre-filter when possible** - Reduce search space
546
+
547
+ ### Don'ts
548
+ - **Don't skip evaluation** - Measure before optimizing
549
+ - **Don't over-index** - Start with flat, scale up
550
+ - **Don't ignore latency** - P99 matters for UX
551
+ - **Don't forget costs** - Vector storage adds up
552
+
553
+ ## Resources
554
+
555
+ - [Pinecone Docs](https://docs.pinecone.io/)
556
+ - [Qdrant Docs](https://qdrant.tech/documentation/)
557
+ - [pgvector](https://github.com/pgvector/pgvector)
558
+ - [Weaviate Docs](https://weaviate.io/developers/weaviate)