Source: https://github.com/aipoch/medical-research-skills
Knowledge Base Search
When to Use
- You need to find specific facts, definitions, or procedures from a local knowledge base and return the exact source location.
- You must provide traceable citations (file path + page/paragraph/section) for audit, compliance, or review.
- You need to verify the original wording of a claim in the source document (quote-level validation).
- You want to compare how multiple local documents discuss the same topic and identify differences.
- You need to assemble supporting snippets for a report, FAQ, or internal knowledge response using only local materials.
Key Features
- Supports multiple retrieval approaches: direct file search, index-based search, and PageIndex-style location mapping.
- Query strategy guidance: keyword splitting, synonym expansion, and optional filters (time range, file type, tags).
- Relevance-oriented result ranking and filtering to keep the most supportive evidence first.
- Outputs verifiable hit snippets with precise citation locations (file + page/paragraph/section when available).
- Enforces local-only boundaries: searches only within authorized directories and does not modify source content.
Dependencies
glob (>= 10.0.0): file path pattern matching
grep (>= 3.11): in-file text searching
- Local knowledge base index files (one or more of: filename index, content index, vector index, PageIndex mapping)
assets/hit_list_template.csv: standardized hit list output template
- Optional reference:
references/guide.md (output formats, checklists, inspection points)
Example Usage
The following example demonstrates an end-to-end local search workflow and produces a CSV hit list compatible with assets/hit_list_template.csv.
Inputs
- Knowledge base root:
./kb/
- Query:
How do we rotate API keys?
- Filters: file types
md,pdf, time range 2024-01-01..2026-12-31
Steps
Confirm index and scope
- Ensure the search scope is limited to authorized paths (e.g.,
./kb/).
- Identify available indices:
- filename/content index (fast keyword search)
- vector index (semantic retrieval)
- PageIndex mapping (page/paragraph location resolution)
Build the query
- Keywords:
rotate, API key, key rotation
- Synonyms/variants:
credential rotation, token rotation, regenerate key
- Filters:
- file type:
*.md, *.pdf
- time range:
2024-01-01..2026-12-31 (if metadata exists)
Execute search (local-only)
- Path discovery (example):
glob("./kb/**/*.md")
glob("./kb/**/*.pdf")
- Content search (example):
grep -RIn "API key\|key rotation\|rotate" ./kb/
Filter and rank results
- Keep hits that directly answer the question (procedure, policy, steps, constraints).
- Rank by:
- term proximity (e.g., “rotate” near “API key”)
- section relevance (e.g., “Security”, “Credentials”, “Operations”)
- coverage (hits that include prerequisites + steps + verification)
Output citations and hit list
- For each hit, output:
file_path
location (page number for PDFs; heading/paragraph index for Markdown; PageIndex if available)
snippet (verbatim excerpt supporting the conclusion)
notes (why it is relevant; any assumptions)
- Save as
hit_list.csv using assets/hit_list_template.csv columns.
Example Output (CSV rows)
file_path,location,snippet,relevance_score,notes
kb/security/credential_policy.pdf,page 12,"API keys must be rotated every 90 days... Rotation requires...",0.92,"Direct policy + rotation interval + procedure reference."
kb/runbooks/api_key_rotation.md,section 'Procedure' ¶3,"To rotate an API key: (1) create a new key... (2) update services... (3) revoke old key...",0.89,"Step-by-step operational runbook."
kb/audit/controls.md,heading 'Key Management' ¶2,"Evidence of rotation includes change tickets and key revocation logs...",0.81,"Provides verification/evidence requirements."
Implementation Details
Retrieval Workflow
Index confirmation
- Determine knowledge base root paths and last update time (if available).
- Detect which indices exist:
- filename index: quick narrowing by file names
- content index: inverted index / grep-like scanning
- vector index: semantic similarity retrieval
- PageIndex: mapping from document offsets to page/paragraph identifiers
Query strategy
- Tokenize the question into:
- core entities (e.g., “API key”)
- actions (e.g., “rotate”, “revoke”, “regenerate”)
- constraints (e.g., “every 90 days”, “approval required”)
- Expand with synonyms and variants.
- Apply filters when metadata exists:
- time range
- file type
- tags/collections
Result filtering and ranking
- Remove low-signal hits (navigation, boilerplate, unrelated mentions).
- Rank by a weighted score (example):
- Keyword match (exact phrase > partial): 0.45
- Proximity (terms close together): 0.20
- Section importance (titles like “Procedure/Policy”): 0.20
- Coverage (answers include steps + constraints + verification): 0.15
- Keep the original text snippet verbatim for verification.
Citation and location resolution
- Markdown/text:
- use heading + paragraph index (or line range) as the primary locator
- PDF:
- use page number; optionally include bounding text around the hit
- PageIndex (if present):
- map internal offsets to stable
page/paragraph identifiers
Constraints and Limitations
- Search only within user-authorized local directories.
- Do not modify source documents.
- Do not execute scripts or arbitrary code.
- Do not access network resources or external APIs.
- If indices are missing/corrupted, fall back to direct file scanning; if scanning is not possible, report the limitation and required remediation (re-indexing).
1---2name: knowledge-base-search3description: Search and locate relevant content within a local knowledge base (files, indices, or PageIndex). Use when you need verifiable citations (file + page/paragraph) to support answers from local sources.4license: MIT5---6> **Source**: [https://github.com/aipoch/medical-research-skills](https://github.com/aipoch/medical-research-skills)
7
8# Knowledge Base Search
9
10## When to Use
11- You need to find specific facts, definitions, or procedures from a local knowledge base and return the exact source location.
12- You must provide traceable citations (file path + page/paragraph/section) for audit, compliance, or review.
13- You need to verify the original wording of a claim in the source document (quote-level validation).
14- You want to compare how multiple local documents discuss the same topic and identify differences.
15- You need to assemble supporting snippets for a report, FAQ, or internal knowledge response using only local materials.
16
17## Key Features
18- Supports multiple retrieval approaches: direct file search, index-based search, and PageIndex-style location mapping.
19- Query strategy guidance: keyword splitting, synonym expansion, and optional filters (time range, file type, tags).
20- Relevance-oriented result ranking and filtering to keep the most supportive evidence first.
21- Outputs verifiable hit snippets with precise citation locations (file + page/paragraph/section when available).
22- Enforces local-only boundaries: searches only within authorized directories and does not modify source content.
23
24## Dependencies
25- `glob` (>= 10.0.0): file path pattern matching
26- `grep` (>= 3.11): in-file text searching
27- Local knowledge base index files (one or more of: filename index, content index, vector index, PageIndex mapping)
28- `assets/hit_list_template.csv`: standardized hit list output template
29- Optional reference: `references/guide.md` (output formats, checklists, inspection points)
30
31## Example Usage
32The following example demonstrates an end-to-end local search workflow and produces a CSV hit list compatible with `assets/hit_list_template.csv`.
33
34### Inputs
35- Knowledge base root: `./kb/`
36- Query: `How do we rotate API keys?`
37- Filters: file types `md,pdf`, time range `2024-01-01..2026-12-31`
38
39### Steps
401. **Confirm index and scope**
41 - Ensure the search scope is limited to authorized paths (e.g., `./kb/`).
42 - Identify available indices:
43 - filename/content index (fast keyword search)
44 - vector index (semantic retrieval)
45 - PageIndex mapping (page/paragraph location resolution)
46
472. **Build the query**
48 - Keywords: `rotate`, `API key`, `key rotation`
49 - Synonyms/variants: `credential rotation`, `token rotation`, `regenerate key`
50 - Filters:
51 - file type: `*.md`, `*.pdf`
52 - time range: `2024-01-01..2026-12-31` (if metadata exists)
53
543. **Execute search (local-only)**
55 - Path discovery (example):
56 - `glob("./kb/**/*.md")`
57 - `glob("./kb/**/*.pdf")`
58 - Content search (example):
59 - `grep -RIn "API key\|key rotation\|rotate" ./kb/`
60
614. **Filter and rank results**
62 - Keep hits that directly answer the question (procedure, policy, steps, constraints).
63 - Rank by:
64 - term proximity (e.g., “rotate” near “API key”)
65 - section relevance (e.g., “Security”, “Credentials”, “Operations”)
66 - coverage (hits that include prerequisites + steps + verification)
67
685. **Output citations and hit list**
69 - For each hit, output:
70 - `file_path`
71 - `location` (page number for PDFs; heading/paragraph index for Markdown; PageIndex if available)
72 - `snippet` (verbatim excerpt supporting the conclusion)
73 - `notes` (why it is relevant; any assumptions)
74 - Save as `hit_list.csv` using `assets/hit_list_template.csv` columns.
75
76### Example Output (CSV rows)
77```csv
78file_path,location,snippet,relevance_score,notes
79kb/security/credential_policy.pdf,page 12,"API keys must be rotated every 90 days... Rotation requires...",0.92,"Direct policy + rotation interval + procedure reference."
80kb/runbooks/api_key_rotation.md,section 'Procedure' ¶3,"To rotate an API key: (1) create a new key... (2) update services... (3) revoke old key...",0.89,"Step-by-step operational runbook."
81kb/audit/controls.md,heading 'Key Management' ¶2,"Evidence of rotation includes change tickets and key revocation logs...",0.81,"Provides verification/evidence requirements."
82```
83
84## Implementation Details
85### Retrieval Workflow
861. **Index confirmation**
87 - Determine knowledge base root paths and last update time (if available).
88 - Detect which indices exist:
89 - filename index: quick narrowing by file names
90 - content index: inverted index / grep-like scanning
91 - vector index: semantic similarity retrieval
92 - PageIndex: mapping from document offsets to page/paragraph identifiers
93
942. **Query strategy**
95 - Tokenize the question into:
96 - core entities (e.g., “API key”)
97 - actions (e.g., “rotate”, “revoke”, “regenerate”)
98 - constraints (e.g., “every 90 days”, “approval required”)
99 - Expand with synonyms and variants.
100 - Apply filters when metadata exists:
101 - time range
102 - file type
103 - tags/collections
104
1053. **Result filtering and ranking**
106 - Remove low-signal hits (navigation, boilerplate, unrelated mentions).
107 - Rank by a weighted score (example):
108 - **Keyword match** (exact phrase > partial): 0.45
109 - **Proximity** (terms close together): 0.20
110 - **Section importance** (titles like “Procedure/Policy”): 0.20
111 - **Coverage** (answers include steps + constraints + verification): 0.15
112 - Keep the original text snippet verbatim for verification.
113
1144. **Citation and location resolution**
115 - Markdown/text:
116 - use heading + paragraph index (or line range) as the primary locator
117 - PDF:
118 - use page number; optionally include bounding text around the hit
119 - PageIndex (if present):
120 - map internal offsets to stable `page/paragraph` identifiers
121
122### Constraints and Limitations
123- Search only within user-authorized local directories.
124- Do not modify source documents.
125- Do not execute scripts or arbitrary code.
126- Do not access network resources or external APIs.
127- If indices are missing/corrupted, fall back to direct file scanning; if scanning is not possible, report the limitation and required remediation (re-indexing).