-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.ts
More file actions
167 lines (135 loc) · 5.2 KB
/
Copy pathcontext.ts
File metadata and controls
167 lines (135 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import * as path from 'path';
import * as fs from 'fs';
import { glob } from 'glob';
import {
ContextOptions,
QueryOptions,
QueryResult,
LoadedDoc,
} from './types';
import { Embedder } from './embedder';
type EmbedderInfo = { dimensions: number };
import { getLoader } from './loaders';
import { pathToId } from './utils/doc';
import { Store } from './storage/store';
import type { ZvecDoc } from './storage/zvec-store';
import {
safeJsonParse,
computeContentHash,
loadSampleText,
} from './utils';
import { expand } from './expander';
import { applyRerank } from './reranker';
export class Context {
private readonly options: ContextOptions;
private readonly embedder: Embedder;
readonly embedderInfo: EmbedderInfo;
private readonly store: Store;
private constructor(options: ContextOptions, embedder: Embedder, embedderInfo: EmbedderInfo) {
this.options = {
basePath: process.cwd(),
vectorsDir: '.context/vectors',
...options,
};
this.embedder = embedder;
this.embedderInfo = embedderInfo;
this.store = new Store(this.options.vectorsDir!, embedder, this.options);
}
static async create(options: ContextOptions): Promise<Context> {
const embedder = new Embedder();
await embedder.embed('probe');
const vectorsDir = options.vectorsDir ?? '.context/vectors';
if (!fs.existsSync(vectorsDir)) {
fs.mkdirSync(vectorsDir, { recursive: true });
}
return new Context(options, embedder, { dimensions: embedder.dimensions });
}
async load(library: string, pattern: string | string[]): Promise<void> {
const patterns = Array.isArray(pattern) ? pattern : [pattern];
const files = await glob(patterns, { absolute: true });
const sampleText = await loadSampleText(files);
await this.store.create(library, sampleText);
const docs: LoadedDoc[] = await Promise.all(
files.map(async (filePath) => {
const loader = getLoader(filePath);
if (!loader) throw new Error(`No loader for ${filePath}`);
const doc = await loader.load(filePath);
const relativePath = path.relative(this.options.basePath!, filePath);
return {
...doc,
id: pathToId(relativePath),
contentHash: computeContentHash(doc.content),
path: relativePath,
};
}),
);
const docIds = docs.map((d) => d.id);
const existing = await this.store.fetchDocs(library, docIds, ['contentHash']);
const docsToEmbed = docs.filter(
(doc) => !existing[doc.id] || existing[doc.id].fields.contentHash !== doc.contentHash,
);
if (this.options.onProgress) {
this.options.onProgress('load', { loaded: docsToEmbed.length, total: files.length });
}
if (docsToEmbed.length === 0) return;
const contents = docsToEmbed.map((doc) => doc.content);
const vectors = await this.embedder.embedBatch(contents);
if (this.options.onProgress) {
this.options.onProgress('embed', { loaded: vectors.length, total: docsToEmbed.length });
}
const zvecDocs: ZvecDoc[] = docsToEmbed.map((doc, index) => ({
id: doc.id,
vector: vectors[index],
fields: {
content: doc.content,
meta: doc.meta ? JSON.stringify(doc.meta) : '',
path: doc.path,
contentHash: doc.contentHash,
},
}));
await this.store.addDoc(library, zvecDocs);
if (this.options.onProgress) {
this.options.onProgress('insert', { loaded: zvecDocs.length, total: docsToEmbed.length });
}
}
async query(text: string, options: QueryOptions): Promise<QueryResult[]> {
const opt = { topK: 5, mode: 'hybrid' as const, rerank: { rerankFactor: 3, minCandidates: 10 }, ...options };
const { library, topK, mode, rerank } = opt;
// Expand the query with synonyms / cross-language bridging terms.
const expandedText = expand(text, this.options.queryExpansion);
const vector = await this.embedder.embed(expandedText);
const rerankConfig = typeof rerank === 'object' ? rerank : null;
const rerankEnabled = rerankConfig !== null;
const rerankFactor = rerankConfig?.rerankFactor ?? 3;
const minCandidates = rerankConfig?.minCandidates ?? 10;
const searchTopK = rerankEnabled ? Math.max(topK * rerankFactor, minCandidates) : topK;
const searchResults = await this.store.queryDoc(library, {
mode,
queryText: expandedText,
queryVector: vector,
topK: searchTopK,
filter: options.filter,
});
const queryResults: QueryResult[] = searchResults.map((result) => {
const content = String(result.fields?.content ?? '');
const metaStr = result.fields?.meta as string | undefined;
const meta = safeJsonParse(metaStr) as Record<string, unknown> | undefined;
return {
id: result.id,
content,
score: result.score,
scoreMode: mode === 'hybrid' ? ('hybrid' as const) : ('vector' as const),
meta,
path: result.fields?.path as string | undefined,
};
});
if (rerankEnabled) {
await applyRerank(this.options.rerankWeights, text, queryResults, topK);
}
queryResults.sort((a, b) => b.score - a.score);
return queryResults.slice(0, topK);
}
async close(): Promise<void> {
await this.store.closeAll();
}
}