|
| 1 | +# Toolpack CLI Knowledge Base |
| 2 | + |
| 3 | +This directory contains the documentation that powers the `knowledge_search` tool in Toolpack CLI. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The knowledge base uses RAG (Retrieval-Augmented Generation) to provide semantic search over CLI documentation. When users ask questions about Toolpack CLI features, the AI agent can use the `knowledge_search` tool to find relevant documentation. |
| 8 | + |
| 9 | +## Architecture |
| 10 | + |
| 11 | +- **Provider**: `PersistentKnowledgeProvider` - SQLite-backed storage at `~/.toolpack/knowledge/toolpack-cli.db` |
| 12 | +- **Embedder**: `OllamaEmbedder` with `nomic-embed-text` model (768 dimensions) |
| 13 | +- **Source**: `MarkdownSource` - Indexes all `.md` files in `docs/` folder |
| 14 | +- **Chunking**: Heading-based with 2000 token max, 200 token overlap |
| 15 | + |
| 16 | +## Documentation Structure |
| 17 | + |
| 18 | +``` |
| 19 | +knowledge/ |
| 20 | +├── docs/ |
| 21 | +│ ├── getting-started.md # Installation and first steps |
| 22 | +│ ├── tools.md # Available tools reference |
| 23 | +│ └── configuration.md # Advanced configuration guide |
| 24 | +└── README.md # This file |
| 25 | +``` |
| 26 | + |
| 27 | +## Adding Documentation |
| 28 | + |
| 29 | +1. Create a new `.md` file in the `docs/` folder |
| 30 | +2. Add YAML frontmatter with metadata: |
| 31 | + |
| 32 | +```markdown |
| 33 | +--- |
| 34 | +title: Your Document Title |
| 35 | +category: guide | reference | tutorial |
| 36 | +tags: [tag1, tag2, tag3] |
| 37 | +--- |
| 38 | + |
| 39 | +# Your Document Title |
| 40 | + |
| 41 | +Content here... |
| 42 | +``` |
| 43 | + |
| 44 | +3. Rebuild the CLI: `npm run build` |
| 45 | +4. The knowledge base will re-index on next launch (if empty) or you can force re-sync |
| 46 | + |
| 47 | +## Frontmatter Metadata |
| 48 | + |
| 49 | +The frontmatter is extracted and added to chunk metadata for filtering: |
| 50 | + |
| 51 | +- `title`: Document title |
| 52 | +- `category`: Document category (guide, reference, tutorial, etc.) |
| 53 | +- `tags`: Array of tags for categorization |
| 54 | +- Custom fields: Any additional YAML fields you add |
| 55 | + |
| 56 | +## Search Examples |
| 57 | + |
| 58 | +### Basic Search |
| 59 | + |
| 60 | +```typescript |
| 61 | +{ |
| 62 | + "tool": "knowledge_search", |
| 63 | + "parameters": { |
| 64 | + "query": "how to configure API keys" |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +### Filtered Search |
| 70 | + |
| 71 | +```typescript |
| 72 | +{ |
| 73 | + "tool": "knowledge_search", |
| 74 | + "parameters": { |
| 75 | + "query": "installation", |
| 76 | + "filter": { |
| 77 | + "category": "guide", |
| 78 | + "hasCode": true |
| 79 | + }, |
| 80 | + "limit": 3, |
| 81 | + "threshold": 0.8 |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Indexing Process |
| 87 | + |
| 88 | +1. **First Launch**: Knowledge base is created and all docs are indexed |
| 89 | +2. **Subsequent Launches**: Existing index is loaded from `~/.toolpack/knowledge/toolpack-cli.db` |
| 90 | +3. **Re-indexing**: Delete the DB file to force re-indexing, or set `reSync: true` in config |
| 91 | + |
| 92 | +## Performance |
| 93 | + |
| 94 | +- **Initial Index**: ~5-10 seconds for 3 docs (depends on Ollama performance) |
| 95 | +- **Query Time**: ~100-500ms per search |
| 96 | +- **Storage**: ~50KB per document (vectors + metadata) |
| 97 | + |
| 98 | +## Requirements |
| 99 | + |
| 100 | +- **Ollama**: Must be running locally with `nomic-embed-text` model pulled |
| 101 | +- **Model Pull**: `ollama pull nomic-embed-text` |
| 102 | + |
| 103 | +## Troubleshooting |
| 104 | + |
| 105 | +### Knowledge base not initializing |
| 106 | + |
| 107 | +**Symptom**: Warning message "Failed to initialize knowledge base" |
| 108 | + |
| 109 | +**Solutions**: |
| 110 | +1. Ensure Ollama is running: `ollama serve` |
| 111 | +2. Pull the embedding model: `ollama pull nomic-embed-text` |
| 112 | +3. Check Ollama is accessible at `http://localhost:11434` |
| 113 | + |
| 114 | +### Search returns no results |
| 115 | + |
| 116 | +**Possible causes**: |
| 117 | +1. Threshold too high - try lowering to 0.5-0.7 |
| 118 | +2. Query doesn't match document content semantically |
| 119 | +3. Knowledge base not indexed - check `~/.toolpack/knowledge/toolpack-cli.db` exists |
| 120 | + |
| 121 | +### Re-indexing |
| 122 | + |
| 123 | +To force re-indexing: |
| 124 | + |
| 125 | +```bash |
| 126 | +rm -rf ~/.toolpack/knowledge/toolpack-cli.db |
| 127 | +``` |
| 128 | + |
| 129 | +Or set `reSync: true` in the knowledge configuration. |
| 130 | + |
| 131 | +## Configuration |
| 132 | + |
| 133 | +The knowledge base is configured in `source/context/ToolpackContext.tsx`: |
| 134 | + |
| 135 | +```typescript |
| 136 | +knowledge = await Knowledge.create({ |
| 137 | + provider: new PersistentKnowledgeProvider({ |
| 138 | + namespace: 'toolpack-cli', |
| 139 | + reSync: false, |
| 140 | + }), |
| 141 | + sources: [new MarkdownSource(knowledgePath)], |
| 142 | + embedder: new OllamaEmbedder({ |
| 143 | + model: 'nomic-embed-text', |
| 144 | + }), |
| 145 | + description: 'Search Toolpack CLI documentation...', |
| 146 | +}); |
| 147 | +``` |
| 148 | + |
| 149 | +## Best Practices |
| 150 | + |
| 151 | +1. **Write semantic content**: Use natural language that matches how users ask questions |
| 152 | +2. **Include examples**: Code examples improve search relevance |
| 153 | +3. **Use headings**: Heading-based chunking preserves document structure |
| 154 | +4. **Add metadata**: Frontmatter enables filtered searches |
| 155 | +5. **Keep chunks focused**: Each section should cover one topic clearly |
| 156 | + |
| 157 | +## Future Enhancements |
| 158 | + |
| 159 | +- [ ] Support for multiple embedding models (OpenAI, etc.) |
| 160 | +- [ ] Hybrid search (keyword + semantic) |
| 161 | +- [ ] Watch mode for auto-reindexing on file changes |
| 162 | +- [ ] Custom chunking strategies |
| 163 | +- [ ] Multi-language support |
0 commit comments