@patricio0312rev/skillset 0.1.0

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 (115) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/LICENSE +21 -0
  3. package/README.md +176 -0
  4. package/bin/cli.js +37 -0
  5. package/package.json +55 -0
  6. package/src/commands/init.js +301 -0
  7. package/src/index.js +168 -0
  8. package/src/lib/config.js +200 -0
  9. package/src/lib/generator.js +166 -0
  10. package/src/utils/display.js +95 -0
  11. package/src/utils/readme.js +196 -0
  12. package/src/utils/tool-specific.js +233 -0
  13. package/templates/ai-engineering/agent-orchestration-planner/ SKILL.md +266 -0
  14. package/templates/ai-engineering/cost-latency-optimizer/ SKILL.md +270 -0
  15. package/templates/ai-engineering/doc-to-vector-dataset-generator/ SKILL.md +239 -0
  16. package/templates/ai-engineering/evaluation-harness/ SKILL.md +219 -0
  17. package/templates/ai-engineering/guardrails-safety-filter-builder/ SKILL.md +226 -0
  18. package/templates/ai-engineering/llm-debugger/ SKILL.md +283 -0
  19. package/templates/ai-engineering/prompt-regression-tester/ SKILL.md +216 -0
  20. package/templates/ai-engineering/prompt-template-builder/ SKILL.md +393 -0
  21. package/templates/ai-engineering/rag-pipeline-builder/ SKILL.md +244 -0
  22. package/templates/ai-engineering/tool-function-schema-designer/ SKILL.md +219 -0
  23. package/templates/architecture/adr-writer/ SKILL.md +250 -0
  24. package/templates/architecture/api-versioning-deprecation-planner/ SKILL.md +331 -0
  25. package/templates/architecture/domain-model-boundaries-mapper/ SKILL.md +300 -0
  26. package/templates/architecture/migration-planner/ SKILL.md +376 -0
  27. package/templates/architecture/performance-budget-setter/ SKILL.md +318 -0
  28. package/templates/architecture/reliability-strategy-builder/ SKILL.md +286 -0
  29. package/templates/architecture/rfc-generator/ SKILL.md +362 -0
  30. package/templates/architecture/scalability-playbook/ SKILL.md +279 -0
  31. package/templates/architecture/system-design-generator/ SKILL.md +339 -0
  32. package/templates/architecture/tech-debt-prioritizer/ SKILL.md +329 -0
  33. package/templates/backend/api-contract-normalizer/ SKILL.md +487 -0
  34. package/templates/backend/api-endpoint-generator/ SKILL.md +415 -0
  35. package/templates/backend/auth-module-builder/ SKILL.md +99 -0
  36. package/templates/backend/background-jobs-designer/ SKILL.md +166 -0
  37. package/templates/backend/caching-strategist/ SKILL.md +190 -0
  38. package/templates/backend/error-handling-standardizer/ SKILL.md +174 -0
  39. package/templates/backend/rate-limiting-abuse-protection/ SKILL.md +147 -0
  40. package/templates/backend/rbac-permissions-builder/ SKILL.md +158 -0
  41. package/templates/backend/service-layer-extractor/ SKILL.md +269 -0
  42. package/templates/backend/webhook-receiver-hardener/ SKILL.md +211 -0
  43. package/templates/ci-cd/artifact-sbom-publisher/ SKILL.md +236 -0
  44. package/templates/ci-cd/caching-strategy-optimizer/ SKILL.md +195 -0
  45. package/templates/ci-cd/deployment-checklist-generator/ SKILL.md +381 -0
  46. package/templates/ci-cd/github-actions-pipeline-creator/ SKILL.md +348 -0
  47. package/templates/ci-cd/monorepo-ci-optimizer/ SKILL.md +298 -0
  48. package/templates/ci-cd/preview-environments-builder/ SKILL.md +187 -0
  49. package/templates/ci-cd/quality-gates-enforcer/ SKILL.md +342 -0
  50. package/templates/ci-cd/release-automation-builder/ SKILL.md +281 -0
  51. package/templates/ci-cd/rollback-workflow-builder/ SKILL.md +372 -0
  52. package/templates/ci-cd/secrets-env-manager/ SKILL.md +242 -0
  53. package/templates/db-management/backup-restore-runbook-generator/ SKILL.md +505 -0
  54. package/templates/db-management/data-integrity-auditor/ SKILL.md +505 -0
  55. package/templates/db-management/data-retention-archiving-planner/ SKILL.md +430 -0
  56. package/templates/db-management/data-seeding-fixtures-builder/ SKILL.md +375 -0
  57. package/templates/db-management/db-performance-watchlist/ SKILL.md +425 -0
  58. package/templates/db-management/etl-sync-job-builder/ SKILL.md +457 -0
  59. package/templates/db-management/multi-tenant-safety-checker/ SKILL.md +398 -0
  60. package/templates/db-management/prisma-migration-assistant/ SKILL.md +379 -0
  61. package/templates/db-management/schema-consistency-checker/ SKILL.md +440 -0
  62. package/templates/db-management/sql-query-optimizer/ SKILL.md +324 -0
  63. package/templates/foundation/changelog-writer/ SKILL.md +431 -0
  64. package/templates/foundation/code-formatter-installer/ SKILL.md +320 -0
  65. package/templates/foundation/codebase-summarizer/ SKILL.md +360 -0
  66. package/templates/foundation/dependency-doctor/ SKILL.md +163 -0
  67. package/templates/foundation/dev-environment-bootstrapper/ SKILL.md +259 -0
  68. package/templates/foundation/dev-onboarding-builder/ SKILL.md +556 -0
  69. package/templates/foundation/docs-starter-kit/ SKILL.md +574 -0
  70. package/templates/foundation/explaining-code/SKILL.md +13 -0
  71. package/templates/foundation/git-hygiene-enforcer/ SKILL.md +455 -0
  72. package/templates/foundation/project-scaffolder/ SKILL.md +65 -0
  73. package/templates/foundation/project-scaffolder/references/templates.md +126 -0
  74. package/templates/foundation/repo-structure-linter/ SKILL.md +0 -0
  75. package/templates/foundation/repo-structure-linter/references/conventions.md +98 -0
  76. package/templates/frontend/animation-micro-interaction-pack/ SKILL.md +41 -0
  77. package/templates/frontend/component-scaffold-generator/ SKILL.md +562 -0
  78. package/templates/frontend/design-to-component-translator/ SKILL.md +547 -0
  79. package/templates/frontend/form-wizard-builder/ SKILL.md +553 -0
  80. package/templates/frontend/frontend-refactor-planner/ SKILL.md +37 -0
  81. package/templates/frontend/i18n-frontend-implementer/ SKILL.md +44 -0
  82. package/templates/frontend/modal-drawer-system/ SKILL.md +377 -0
  83. package/templates/frontend/page-layout-builder/ SKILL.md +630 -0
  84. package/templates/frontend/state-ux-flow-builder/ SKILL.md +23 -0
  85. package/templates/frontend/table-builder/ SKILL.md +350 -0
  86. package/templates/performance/alerting-dashboard-builder/ SKILL.md +162 -0
  87. package/templates/performance/backend-latency-profiler-helper/ SKILL.md +108 -0
  88. package/templates/performance/caching-cdn-strategy-planner/ SKILL.md +150 -0
  89. package/templates/performance/capacity-planning-helper/ SKILL.md +242 -0
  90. package/templates/performance/core-web-vitals-tuner/ SKILL.md +126 -0
  91. package/templates/performance/incident-runbook-generator/ SKILL.md +162 -0
  92. package/templates/performance/load-test-scenario-builder/ SKILL.md +256 -0
  93. package/templates/performance/observability-setup/ SKILL.md +232 -0
  94. package/templates/performance/postmortem-writer/ SKILL.md +203 -0
  95. package/templates/performance/structured-logging-standardizer/ SKILL.md +122 -0
  96. package/templates/security/auth-security-reviewer/ SKILL.md +428 -0
  97. package/templates/security/dependency-vulnerability-triage/ SKILL.md +495 -0
  98. package/templates/security/input-validation-sanitization-auditor/ SKILL.md +76 -0
  99. package/templates/security/pii-redaction-logging-policy-builder/ SKILL.md +65 -0
  100. package/templates/security/rbac-policy-tester/ SKILL.md +80 -0
  101. package/templates/security/secrets-scanner/ SKILL.md +462 -0
  102. package/templates/security/secure-headers-csp-builder/ SKILL.md +404 -0
  103. package/templates/security/security-incident-playbook-generator/ SKILL.md +76 -0
  104. package/templates/security/security-pr-checklist-skill/ SKILL.md +62 -0
  105. package/templates/security/threat-model-generator/ SKILL.md +394 -0
  106. package/templates/testing/contract-testing-builder/ SKILL.md +492 -0
  107. package/templates/testing/coverage-strategist/ SKILL.md +436 -0
  108. package/templates/testing/e2e-test-builder/ SKILL.md +382 -0
  109. package/templates/testing/flaky-test-detective/ SKILL.md +416 -0
  110. package/templates/testing/integration-test-builder/ SKILL.md +525 -0
  111. package/templates/testing/mocking-assistant/ SKILL.md +383 -0
  112. package/templates/testing/snapshot-test-refactorer/ SKILL.md +375 -0
  113. package/templates/testing/test-data-factory-builder/ SKILL.md +449 -0
  114. package/templates/testing/test-reporting-triage-skill/ SKILL.md +469 -0
  115. package/templates/testing/unit-test-generator/ SKILL.md +548 -0
@@ -0,0 +1,393 @@
1
+ ---
2
+ name: prompt-template-builder
3
+ description: Creates reusable prompt templates with strict output contracts, style rules, few-shot examples, and do/don't guidelines. Provides system/user prompt files, variable placeholders, output formatting instructions, and quality criteria. Use when building "prompt templates", "LLM prompts", "AI system prompts", or "prompt engineering".
4
+ ---
5
+
6
+ # Prompt Template Builder
7
+
8
+ Build robust, reusable prompt templates with clear contracts and consistent outputs.
9
+
10
+ ## Core Components
11
+
12
+ **System Prompt**: Role, persona, constraints, output format
13
+ **User Prompt**: Task, context, variables, examples
14
+ **Few-Shot Examples**: Input/output pairs demonstrating desired behavior
15
+ **Output Contract**: Strict format specification (JSON schema, Markdown structure)
16
+ **Style Rules**: Tone, verbosity, formatting preferences
17
+ **Guardrails**: Do's and don'ts, safety constraints
18
+
19
+ ## System Prompt Template
20
+
21
+ ````markdown
22
+ # System Prompt: Code Review Assistant
23
+
24
+ You are an expert code reviewer specializing in {language} and {framework}. Your role is to provide constructive, actionable feedback on code quality, best practices, and potential issues.
25
+
26
+ ## Output Format
27
+
28
+ Provide your review in the following JSON structure:
29
+
30
+ ```json
31
+ {
32
+ "summary": "Brief 1-2 sentence overview",
33
+ "issues": [
34
+ {
35
+ "severity": "critical|major|minor",
36
+ "line": number,
37
+ "message": "Description of the issue",
38
+ "suggestion": "How to fix it"
39
+ }
40
+ ],
41
+ "strengths": ["List of positive aspects"],
42
+ "overall_score": 1-10
43
+ }
44
+ ```
45
+ ````
46
+
47
+ ## Style Guidelines
48
+
49
+ - Be constructive and specific
50
+ - Cite line numbers for issues
51
+ - Provide actionable suggestions
52
+ - Balance criticism with praise
53
+ - Use professional, respectful tone
54
+
55
+ ## Constraints
56
+
57
+ - Do NOT suggest unnecessary refactors
58
+ - Do focus on correctness, security, performance
59
+ - Do NOT be overly pedantic about style
60
+ - Do consider the context and project requirements
61
+
62
+ ````
63
+
64
+ ## User Prompt Template with Variables
65
+
66
+ ```typescript
67
+ // prompt-templates/code-review.ts
68
+ export const codeReviewPrompt = (variables: {
69
+ language: string;
70
+ framework: string;
71
+ code: string;
72
+ context?: string;
73
+ }) => `
74
+ Please review the following ${variables.language} code:
75
+
76
+ ${variables.context ? `Context: ${variables.context}\n` : ''}
77
+
78
+ \`\`\`${variables.language}
79
+ ${variables.code}
80
+ \`\`\`
81
+
82
+ Provide a thorough code review following the output format specified in the system prompt.
83
+ `;
84
+
85
+ // Usage
86
+ const prompt = codeReviewPrompt({
87
+ language: 'typescript',
88
+ framework: 'React',
89
+ code: userSubmittedCode,
90
+ context: 'This is a production component for user authentication',
91
+ });
92
+ ````
93
+
94
+ ## Few-Shot Examples
95
+
96
+ ````markdown
97
+ # Few-Shot Examples
98
+
99
+ ## Example 1: Good Code
100
+
101
+ **Input:**
102
+
103
+ ```typescript
104
+ function calculateTotal(items: Item[]): number {
105
+ return items.reduce((sum, item) => sum + item.price, 0);
106
+ }
107
+ ```
108
+ ````
109
+
110
+ **Output:**
111
+
112
+ ```json
113
+ {
114
+ "summary": "Clean, type-safe implementation with no issues found.",
115
+ "issues": [],
116
+ "strengths": [
117
+ "Type safety with TypeScript",
118
+ "Functional approach with reduce",
119
+ "Clear, descriptive naming"
120
+ ],
121
+ "overall_score": 9
122
+ }
123
+ ```
124
+
125
+ ## Example 2: Code with Issues
126
+
127
+ **Input:**
128
+
129
+ ```typescript
130
+ function calc(arr) {
131
+ let total = 0;
132
+ for (var i = 0; i < arr.length; i++) {
133
+ total = total + arr[i].price;
134
+ }
135
+ return total;
136
+ }
137
+ ```
138
+
139
+ **Output:**
140
+
141
+ ```json
142
+ {
143
+ "summary": "Functional but has type safety and modern syntax issues.",
144
+ "issues": [
145
+ {
146
+ "severity": "major",
147
+ "line": 1,
148
+ "message": "Missing type annotations for parameters and return value",
149
+ "suggestion": "Add types: function calc(arr: Item[]): number"
150
+ },
151
+ {
152
+ "severity": "minor",
153
+ "line": 3,
154
+ "message": "Using 'var' instead of 'let' or 'const'",
155
+ "suggestion": "Replace 'var' with 'let': for (let i = 0; ...)"
156
+ }
157
+ ],
158
+ "strengths": ["Logic is correct", "Handles empty array case"],
159
+ "overall_score": 6
160
+ }
161
+ ```
162
+
163
+ ````
164
+
165
+ ## Output Contracts
166
+
167
+ ```typescript
168
+ // Define strict output schema
169
+ import { z } from 'zod';
170
+
171
+ export const codeReviewSchema = z.object({
172
+ summary: z.string().min(10).max(200),
173
+ issues: z.array(z.object({
174
+ severity: z.enum(['critical', 'major', 'minor']),
175
+ line: z.number().int().positive(),
176
+ message: z.string(),
177
+ suggestion: z.string(),
178
+ })),
179
+ strengths: z.array(z.string()),
180
+ overall_score: z.number().int().min(1).max(10),
181
+ });
182
+
183
+ // Validate LLM output
184
+ export const parseCodeReview = (output: string) => {
185
+ try {
186
+ const parsed = JSON.parse(output);
187
+ return codeReviewSchema.parse(parsed);
188
+ } catch (error) {
189
+ throw new Error('Invalid code review output format');
190
+ }
191
+ };
192
+ ````
193
+
194
+ ## Template Variables
195
+
196
+ ```typescript
197
+ export interface PromptVariables {
198
+ // Required
199
+ required_field: string;
200
+
201
+ // Optional with defaults
202
+ optional_field?: string;
203
+
204
+ // Constrained values
205
+ severity_level: "low" | "medium" | "high";
206
+
207
+ // Numeric with ranges
208
+ max_tokens: number; // 1-4096
209
+ }
210
+
211
+ export const buildPrompt = (vars: PromptVariables): string => {
212
+ // Validate variables
213
+ if (!vars.required_field) {
214
+ throw new Error("required_field is required");
215
+ }
216
+
217
+ // Set defaults
218
+ const optional = vars.optional_field ?? "default value";
219
+
220
+ // Build prompt
221
+ return `Task: ${vars.required_field}
222
+ Options: ${optional}
223
+ Severity: ${vars.severity_level}`;
224
+ };
225
+ ```
226
+
227
+ ## Style Rules
228
+
229
+ ```markdown
230
+ ## Tone Guidelines
231
+
232
+ - **Professional**: Formal language, no slang
233
+ - **Friendly**: Conversational but respectful
234
+ - **Technical**: Precise terminology, assume expertise
235
+ - **Educational**: Explain concepts, teach as you go
236
+
237
+ ## Verbosity Levels
238
+
239
+ - **Concise**: 1-2 sentences, bullet points
240
+ - **Standard**: 1 paragraph per point
241
+ - **Detailed**: Full explanations with examples
242
+ - **Comprehensive**: Deep dive with references
243
+
244
+ ## Formatting Preferences
245
+
246
+ - Use markdown headers for structure
247
+ - Bold important terms
248
+ - Code blocks for technical content
249
+ - Lists for enumeration
250
+ - Tables for comparisons
251
+ ```
252
+
253
+ ## Do's and Don'ts
254
+
255
+ ```markdown
256
+ ## Do's
257
+
258
+ ✓ Provide specific, actionable feedback
259
+ ✓ Include code examples when relevant
260
+ ✓ Reference line numbers for issues
261
+ ✓ Suggest concrete improvements
262
+ ✓ Balance criticism with praise
263
+ ✓ Consider context and constraints
264
+
265
+ ## Don'ts
266
+
267
+ ✗ Don't be vague ("this is bad")
268
+ ✗ Don't suggest unnecessary rewrites
269
+ ✗ Don't ignore security issues
270
+ ✗ Don't be overly pedantic
271
+ ✗ Don't assume unlimited resources
272
+ ✗ Don't make assumptions without context
273
+ ```
274
+
275
+ ## Prompt Chaining
276
+
277
+ ```typescript
278
+ // Multi-step prompts
279
+ export const chainedPrompts = {
280
+ step1_analyze: (code: string) => `
281
+ Analyze this code and identify potential issues:
282
+ ${code}
283
+
284
+ List issues in JSON array format with severity and description.
285
+ `,
286
+
287
+ step2_suggest: (issues: Issue[]) => `
288
+ Given these code issues:
289
+ ${JSON.stringify(issues)}
290
+
291
+ Provide detailed fix suggestions for each issue.
292
+ `,
293
+
294
+ step3_summarize: (suggestions: Suggestion[]) => `
295
+ Summarize these code review suggestions into a final report:
296
+ ${JSON.stringify(suggestions)}
297
+ `,
298
+ };
299
+
300
+ // Execute chain
301
+ const issues = await llm(chainedPrompts.step1_analyze(code));
302
+ const suggestions = await llm(chainedPrompts.step2_suggest(issues));
303
+ const report = await llm(chainedPrompts.step3_summarize(suggestions));
304
+ ```
305
+
306
+ ## Version Control
307
+
308
+ ```typescript
309
+ // Track prompt versions
310
+ export const PROMPT_VERSIONS = {
311
+ "v1.0": {
312
+ system: "Original system prompt...",
313
+ user: (vars) => `Original user prompt...`,
314
+ deprecated: false,
315
+ },
316
+ "v1.1": {
317
+ system: "Improved system prompt with better constraints...",
318
+ user: (vars) => `Updated user prompt...`,
319
+ deprecated: false,
320
+ changes: "Added JSON schema validation, improved examples",
321
+ },
322
+ "v1.0-deprecated": {
323
+ system: "...",
324
+ user: (vars) => `...`,
325
+ deprecated: true,
326
+ deprecation_reason: "Replaced by v1.1 with better output format",
327
+ },
328
+ };
329
+
330
+ // Use specific version
331
+ const prompt = PROMPT_VERSIONS["v1.1"];
332
+ ```
333
+
334
+ ## Testing Prompts
335
+
336
+ ```typescript
337
+ // Test cases for prompt validation
338
+ const testCases = [
339
+ {
340
+ input: { code: "function test() {}", language: "javascript" },
341
+ expected: {
342
+ hasIssues: false,
343
+ scoreRange: [8, 10],
344
+ },
345
+ },
346
+ {
347
+ input: { code: "func test(arr) { return arr[0] }", language: "javascript" },
348
+ expected: {
349
+ hasIssues: true,
350
+ minIssues: 2,
351
+ severities: ["major", "minor"],
352
+ },
353
+ },
354
+ ];
355
+
356
+ // Run tests
357
+ for (const test of testCases) {
358
+ const output = await llm(buildPrompt(test.input));
359
+ const parsed = parseCodeReview(output);
360
+
361
+ if (test.expected.hasIssues) {
362
+ assert(parsed.issues.length >= test.expected.minIssues);
363
+ }
364
+ if (test.expected.scoreRange) {
365
+ assert(parsed.overall_score >= test.expected.scoreRange[0]);
366
+ assert(parsed.overall_score <= test.expected.scoreRange[1]);
367
+ }
368
+ }
369
+ ```
370
+
371
+ ## Best Practices
372
+
373
+ 1. **Clear instructions**: Be explicit about what you want
374
+ 2. **Output contracts**: Define strict schemas
375
+ 3. **Few-shot examples**: Show, don't just tell
376
+ 4. **Variable validation**: Check inputs before building prompts
377
+ 5. **Version tracking**: Maintain prompt history
378
+ 6. **Test thoroughly**: Validate against edge cases
379
+ 7. **Iterate**: Improve based on real outputs
380
+ 8. **Document constraints**: Explain limitations
381
+
382
+ ## Output Checklist
383
+
384
+ - [ ] System prompt with role and constraints
385
+ - [ ] User prompt template with variables
386
+ - [ ] Output format specification (JSON schema)
387
+ - [ ] 3+ few-shot examples (good and bad)
388
+ - [ ] Style guidelines documented
389
+ - [ ] Do's and don'ts list
390
+ - [ ] Variable validation logic
391
+ - [ ] Output parsing/validation
392
+ - [ ] Test cases for prompt
393
+ - [ ] Version tracking system
@@ -0,0 +1,244 @@
1
+ ---
2
+ name: rag-pipeline-builder
3
+ description: Designs retrieval-augmented generation pipelines for document-based AI assistants. Includes chunking strategies, metadata schemas, retrieval algorithms, reranking, and evaluation plans. Use when building "RAG systems", "document search", "semantic search", or "knowledge bases".
4
+ ---
5
+
6
+ # RAG Pipeline Builder
7
+
8
+ Design end-to-end RAG pipelines for accurate document retrieval and generation.
9
+
10
+ ## Pipeline Architecture
11
+
12
+ ```
13
+ Documents → Chunking → Embedding → Vector Store → Retrieval → Reranking → Generation
14
+ ```
15
+
16
+ ## Chunking Strategy
17
+
18
+ ```python
19
+ # Semantic chunking (recommended)
20
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
21
+
22
+ splitter = RecursiveCharacterTextSplitter(
23
+ chunk_size=1000, # Characters per chunk
24
+ chunk_overlap=200, # Overlap between chunks
25
+ separators=["\n\n", "\n", ". ", " ", ""],
26
+ length_function=len,
27
+ )
28
+
29
+ chunks = splitter.split_text(document.text)
30
+
31
+ # Add metadata to each chunk
32
+ for i, chunk in enumerate(chunks):
33
+ chunks[i] = {
34
+ "text": chunk,
35
+ "metadata": {
36
+ "source": document.filename,
37
+ "page": calculate_page(i),
38
+ "chunk_id": f"{document.id}_chunk_{i}",
39
+ }
40
+ }
41
+ ```
42
+
43
+ ## Metadata Schema
44
+
45
+ ```typescript
46
+ interface ChunkMetadata {
47
+ // Source information
48
+ document_id: string;
49
+ source: string;
50
+ url?: string;
51
+
52
+ // Location
53
+ page?: number;
54
+ section?: string;
55
+ chunk_index: number;
56
+
57
+ // Content classification
58
+ content_type: "text" | "code" | "table" | "list";
59
+ language?: string;
60
+
61
+ // Timestamps
62
+ created_at: Date;
63
+ updated_at: Date;
64
+
65
+ // Retrieval optimization
66
+ keywords: string[];
67
+ summary?: string;
68
+ importance_score?: number;
69
+ }
70
+ ```
71
+
72
+ ## Vector Store Setup
73
+
74
+ ```python
75
+ # Pinecone example
76
+ import pinecone
77
+ from langchain.vectorstores import Pinecone
78
+ from langchain.embeddings import OpenAIEmbeddings
79
+
80
+ pinecone.init(api_key="...", environment="...")
81
+
82
+ embeddings = OpenAIEmbeddings(model="text-embedding-3-small")
83
+
84
+ vectorstore = Pinecone.from_documents(
85
+ documents=chunks,
86
+ embedding=embeddings,
87
+ index_name="knowledge-base",
88
+ namespace="production",
89
+ )
90
+ ```
91
+
92
+ ## Retrieval Strategies
93
+
94
+ ```python
95
+ # Hybrid search (dense + sparse)
96
+ def hybrid_retrieval(query: str, k: int = 5):
97
+ # Dense retrieval (semantic)
98
+ dense_results = vectorstore.similarity_search(query, k=k*2)
99
+
100
+ # Sparse retrieval (keyword - BM25)
101
+ sparse_results = bm25_search(query, k=k*2)
102
+
103
+ # Combine and rerank
104
+ combined = reciprocal_rank_fusion(dense_results, sparse_results)
105
+
106
+ return combined[:k]
107
+
108
+ # Metadata filtering
109
+ results = vectorstore.similarity_search(
110
+ query,
111
+ k=5,
112
+ filter={
113
+ "content_type": "code",
114
+ "language": "python",
115
+ }
116
+ )
117
+ ```
118
+
119
+ ## Reranking
120
+
121
+ ```python
122
+ from sentence_transformers import CrossEncoder
123
+
124
+ reranker = CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')
125
+
126
+ def rerank_results(query: str, results: List[Document], top_k: int = 3):
127
+ # Score each result against query
128
+ pairs = [(query, doc.page_content) for doc in results]
129
+ scores = reranker.predict(pairs)
130
+
131
+ # Sort by score
132
+ scored_results = list(zip(results, scores))
133
+ scored_results.sort(key=lambda x: x[1], reverse=True)
134
+
135
+ return [doc for doc, score in scored_results[:top_k]]
136
+ ```
137
+
138
+ ## Query Enhancement
139
+
140
+ ```python
141
+ # Query expansion
142
+ def expand_query(query: str) -> str:
143
+ expansion_prompt = f"""
144
+ Generate 3 alternative phrasings of this query:
145
+ "{query}"
146
+
147
+ Return as JSON array of strings.
148
+ """
149
+ alternatives = llm(expansion_prompt)
150
+ return [query] + alternatives
151
+
152
+ # Multi-query retrieval
153
+ def multi_query_retrieval(query: str, k: int = 5):
154
+ queries = expand_query(query)
155
+ all_results = []
156
+
157
+ for q in queries:
158
+ results = vectorstore.similarity_search(q, k=k)
159
+ all_results.extend(results)
160
+
161
+ # Deduplicate and rerank
162
+ unique_results = deduplicate(all_results)
163
+ return rerank_results(query, unique_results, k)
164
+ ```
165
+
166
+ ## Evaluation Plan
167
+
168
+ ```python
169
+ # Define golden dataset
170
+ golden_dataset = [
171
+ {
172
+ "query": "How do I authenticate users?",
173
+ "expected_docs": ["auth_guide.md", "user_management.md"],
174
+ "relevant_chunks": ["chunk_123", "chunk_456"],
175
+ },
176
+ ]
177
+
178
+ # Metrics
179
+ def evaluate_retrieval(dataset):
180
+ results = {
181
+ "precision": [],
182
+ "recall": [],
183
+ "mrr": [], # Mean Reciprocal Rank
184
+ "ndcg": [] # Normalized Discounted Cumulative Gain
185
+ }
186
+
187
+ for item in dataset:
188
+ retrieved = retrieval_fn(item["query"])
189
+ retrieved_ids = [doc.metadata["chunk_id"] for doc in retrieved]
190
+
191
+ # Calculate metrics
192
+ relevant = set(item["relevant_chunks"])
193
+ retrieved_set = set(retrieved_ids)
194
+
195
+ precision = len(relevant & retrieved_set) / len(retrieved_set)
196
+ recall = len(relevant & retrieved_set) / len(relevant)
197
+
198
+ results["precision"].append(precision)
199
+ results["recall"].append(recall)
200
+
201
+ return {k: sum(v)/len(v) for k, v in results.items()}
202
+ ```
203
+
204
+ ## Context Window Management
205
+
206
+ ```python
207
+ def fit_context_window(chunks: List[Document], max_tokens: int = 4000):
208
+ """Select chunks that fit in context window"""
209
+ total_tokens = 0
210
+ selected_chunks = []
211
+
212
+ for chunk in chunks:
213
+ chunk_tokens = count_tokens(chunk.page_content)
214
+ if total_tokens + chunk_tokens <= max_tokens:
215
+ selected_chunks.append(chunk)
216
+ total_tokens += chunk_tokens
217
+ else:
218
+ break
219
+
220
+ return selected_chunks
221
+ ```
222
+
223
+ ## Best Practices
224
+
225
+ 1. **Chunk size**: 500-1000 chars for general text
226
+ 2. **Overlap**: 10-20% overlap between chunks
227
+ 3. **Metadata**: Rich metadata for filtering
228
+ 4. **Hybrid search**: Combine semantic + keyword
229
+ 5. **Reranking**: Cross-encoder for final ranking
230
+ 6. **Evaluation**: Golden dataset with metrics
231
+ 7. **Context management**: Don't exceed model limits
232
+
233
+ ## Output Checklist
234
+
235
+ - [ ] Chunking strategy defined
236
+ - [ ] Metadata schema documented
237
+ - [ ] Vector store configured
238
+ - [ ] Retrieval algorithm implemented
239
+ - [ ] Reranking pipeline added
240
+ - [ ] Query enhancement (optional)
241
+ - [ ] Context window management
242
+ - [ ] Evaluation dataset created
243
+ - [ ] Metrics implementation
244
+ - [ ] Performance baseline established