|
| 1 | +# @antv/context |
| 2 | + |
| 3 | +A local context retrieval library that enables semantic search over your documentation. It loads documents (Markdown, JSON, Text), vectorizes them using [Transformers.js](https://huggingface.co/transformers.js), and stores vectors locally in `.zvec` files for fast semantic querying. |
| 4 | + |
| 5 | +> [!TIP] |
| 6 | +> Base it, We provide an official context HTTP server simlar with context7, used to provide AI code generation context services in MCP, Skill, and CLI, for free! |
| 7 | +
|
| 8 | + |
| 9 | +## Features |
| 10 | + |
| 11 | +- 📄 **Multi-format Support**: Supports Markdown, JSON, Text, and other file formats |
| 12 | +- 📚 **Multi-library Support**: Manage documents by library |
| 13 | +- ⚡ **Auto-indexing**: Automatic vectorization on load |
| 14 | +- 🔍 **Semantic Retrieval**: Retrieve relevant documents based on vector similarity (file-level) |
| 15 | + |
| 16 | + |
| 17 | +## Quick Start |
| 18 | + |
| 19 | +```bash |
| 20 | +npm install @antv/context |
| 21 | +``` |
| 22 | + |
| 23 | +```typescript |
| 24 | +import { Context } from '@antv/context'; |
| 25 | + |
| 26 | +const ctx = await Context.create({ vectorsDir: './vectors' }); |
| 27 | + |
| 28 | +// Load documents into a specific library with automatic vectorization |
| 29 | +await ctx.load('g2', './g2-docs/**/*.md'); |
| 30 | +await ctx.load('f2', './f2-docs/**/*.json'); |
| 31 | + |
| 32 | +// Query |
| 33 | +const results = await ctx.query('How to configure a line chart', { library: 'g2', topK: 5 }); |
| 34 | +// => [{ content: '...', score: 0.92, id: 'g2-docs/line.md' }, ...] |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +## API |
| 39 | + |
| 40 | +### `Context.create(options)` |
| 41 | + |
| 42 | +| Parameter | Type | Description | |
| 43 | +|-----------|------|-------------| |
| 44 | +| `vectorsDir` | `string` | Directory to store vector files | |
| 45 | +| `model` | `string` | Transformers model name, default `sentence-transformers/all-MiniLM-L6-v2` | |
| 46 | + |
| 47 | +### `ctx.load(library, glob)` |
| 48 | + |
| 49 | +Load files into a specified library with automatic vectorization. Document ID defaults to the file path. |
| 50 | + |
| 51 | +```typescript |
| 52 | +await ctx.load('g2', './docs/**/*.md'); |
| 53 | +await ctx.load('g2', ['./docs/**/*.md', './docs/**/*.json']); |
| 54 | +``` |
| 55 | + |
| 56 | +### `ctx.query(text, options)` |
| 57 | + |
| 58 | +Vector similarity retrieval. |
| 59 | + |
| 60 | +| Parameter | Type | Description | |
| 61 | +|-----------|------|-------------| |
| 62 | +| `library` | `string` | Required, library to query | |
| 63 | +| `topK` | `number` | Number of results to return, default 5 | |
| 64 | + |
| 65 | +```typescript |
| 66 | +const results = await ctx.query('How to configure a line chart', { library: 'g2', topK 5 }); |
| 67 | +// => [{ id: 'g2-docs/line.md', content: '...', score: 0.92 }, ...] |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +## Architecture |
| 72 | + |
| 73 | +``` |
| 74 | ++------------------------------------------------------------------------+ |
| 75 | +| @antv/context | |
| 76 | ++------------------------------------------------------------------------+ |
| 77 | +
|
| 78 | + LOAD PHASE QUERY PHASE |
| 79 | + ---------- ---------- |
| 80 | +
|
| 81 | + +----------+ +----------+ +----------+ +----------+ |
| 82 | + | markdown | | json | | text | | Query | |
| 83 | + +----+-----+ +----+-----+ +----+-----+ +----+-----+ |
| 84 | + | | | | |
| 85 | + +--------------+--------------+ | |
| 86 | + v v |
| 87 | + +-----------------+ +-----------------+ |
| 88 | + | FileLoader | | Transformers | |
| 89 | + +--------+--------+ +--------+--------+ |
| 90 | + | | |
| 91 | + +--------v--------+ | |
| 92 | + | Transformers | | |
| 93 | + +--------+--------+ | |
| 94 | + | | |
| 95 | + +--------v--------+ | |
| 96 | + | .zvec |<---------------Query-------+ |
| 97 | + +-----------------+ |
| 98 | +
|
| 99 | ++------------------------------------------------------------------------+ |
| 100 | +``` |
| 101 | + |
| 102 | + |
| 103 | +## License |
| 104 | + |
| 105 | +MIT |
0 commit comments