-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpinecone-client.ts
More file actions
605 lines (545 loc) · 19.4 KB
/
Copy pathpinecone-client.ts
File metadata and controls
605 lines (545 loc) · 19.4 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
/**
* Pinecone client for hybrid search retrieval.
*
* Optimized Pinecone query class that performs hybrid search (dense + sparse)
* with reranking. Designed for high performance with connection pooling and
* lazy initialization.
*/
import { Pinecone } from '@pinecone-database/pinecone';
import {
debug as logDebug,
error as logError,
info as logInfo,
warn as logWarn,
} from './logger.js';
import type {
PineconeClientConfig,
SearchResult,
PineconeHit,
QueryParams,
CountParams,
CountResult,
KeywordSearchParams,
MergedHit,
NamespaceHandle,
SearchableIndex,
PineconeMetadataValue,
} from './types.js';
import {
DEFAULT_INDEX_NAME,
DEFAULT_RERANK_MODEL,
DEFAULT_TOP_K,
MAX_TOP_K,
COUNT_TOP_K,
COUNT_FIELDS,
} from './constants.js';
/**
* Infers a human-readable metadata field type for namespace discovery.
* Distinguishes Pinecone-supported list type (string[]) from other arrays.
*/
function inferMetadataFieldType(value: unknown): string {
if (value === null || value === undefined) {
return 'unknown';
}
if (Array.isArray(value)) {
if (value.length === 0) return 'array';
if (value.every((item) => typeof item === 'string')) return 'string[]';
return 'array';
}
const t = typeof value;
if (t === 'string' || t === 'number' || t === 'boolean') return t;
return 'object';
}
export class PineconeClient {
private apiKey: string;
private indexName: string;
private rerankModel: string;
private defaultTopK: number;
// Lazy initialization
private pc: Pinecone | null = null;
private denseIndex: SearchableIndex | null = null;
private sparseIndex: SearchableIndex | null = null;
private initialized = false;
/** Create a client with the given config; env vars override index name, rerank model, and top-k. */
constructor(config: PineconeClientConfig) {
this.apiKey = config.apiKey;
this.indexName = config.indexName || process.env['PINECONE_INDEX_NAME'] || DEFAULT_INDEX_NAME;
this.rerankModel =
config.rerankModel || process.env['PINECONE_RERANK_MODEL'] || DEFAULT_RERANK_MODEL;
this.defaultTopK =
config.defaultTopK || parseInt(process.env['PINECONE_TOP_K'] || String(DEFAULT_TOP_K));
}
/** Returns the sparse index name (same as hybrid sparse: {indexName}-sparse). Used for keyword_search response. */
getSparseIndexName(): string {
return `${this.indexName}-sparse`;
}
/**
* Normalize and clamp topK from request (validates >= 1, caps at MAX_TOP_K).
*/
private clampTopK(requested: number | undefined): number {
if (requested !== undefined && !Number.isFinite(requested)) {
throw new Error('topK must be a finite number >= 1');
}
let topK = requested !== undefined ? requested : this.defaultTopK;
if (topK < 1) {
throw new Error('topK must be at least 1');
}
if (topK > MAX_TOP_K) {
topK = MAX_TOP_K;
}
return topK;
}
/**
* Ensure Pinecone client is initialized
*/
private ensureClient(): Pinecone {
if (!this.pc) {
if (!this.apiKey) {
throw new Error(
'Pinecone API key is required. Set PINECONE_API_KEY environment variable or pass apiKey parameter.'
);
}
this.pc = new Pinecone({ apiKey: this.apiKey });
logInfo('Pinecone client initialized');
}
return this.pc;
}
/**
* Ensure Pinecone indexes are initialized and return them
*/
private async ensureIndexes(): Promise<{
denseIndex: SearchableIndex;
sparseIndex: SearchableIndex;
}> {
if (this.initialized && this.denseIndex !== null && this.sparseIndex !== null) {
return { denseIndex: this.denseIndex, sparseIndex: this.sparseIndex };
}
const pc = this.ensureClient();
const denseName = this.indexName;
const sparseName = this.getSparseIndexName();
const dense = pc.index(denseName) as unknown as SearchableIndex;
const sparse = pc.index(sparseName) as unknown as SearchableIndex;
this.denseIndex = dense;
this.sparseIndex = sparse;
this.initialized = true;
logInfo(`Connected to indexes: ${denseName} and ${sparseName}`);
return { denseIndex: dense, sparseIndex: sparse };
}
/**
* List namespaces present on the sparse index (same index used for hybrid sparse and keyword_search).
* Use this to choose a namespace for sparse-only queries instead of the dense index list.
*/
async listNamespacesFromKeywordIndex(): Promise<
Array<{ namespace: string; recordCount: number }>
> {
try {
const { sparseIndex } = await this.ensureIndexes();
const stats = sparseIndex.describeIndexStats
? await sparseIndex.describeIndexStats()
: undefined;
const namespaces = stats?.namespaces ?? {};
return Object.entries(namespaces).map(([namespace, info]) => ({
namespace,
recordCount: info?.recordCount ?? 0,
}));
} catch (error) {
logError('Error listing namespaces from keyword index', error);
return [];
}
}
/**
* List all available namespaces with their metadata information
*
* Fetches namespaces from the index stats and samples records to discover
* available metadata fields and their types.
*/
async listNamespacesWithMetadata(): Promise<
Array<{
namespace: string;
recordCount: number;
metadata: Record<string, string>;
}>
> {
try {
const { denseIndex } = await this.ensureIndexes();
// Get index stats to find namespaces
const stats = denseIndex.describeIndexStats
? await denseIndex.describeIndexStats()
: undefined;
const namespaces = stats?.namespaces ? Object.keys(stats.namespaces) : [];
logInfo(`Found ${namespaces.length} namespace(s)`);
// Get metadata info for each namespace by sampling records
const namespacesInfo = await Promise.all(
namespaces.map(async (ns: string) => {
try {
const recordCount = stats?.namespaces?.[ns]?.recordCount || 0;
const metadataFields: Record<string, string> = {};
// Sample a few records to discover metadata fields
if (recordCount > 0 && denseIndex.namespace) {
try {
const nsObj: NamespaceHandle = denseIndex.namespace(ns);
const sampleQuery =
typeof nsObj.query === 'function'
? await nsObj.query({
topK: 5,
vector: Array(stats?.dimension ?? 1536).fill(0),
includeMetadata: true,
})
: { matches: undefined };
// Collect unique metadata fields and infer types (including string[])
if (sampleQuery?.matches) {
sampleQuery.matches.forEach((match: { metadata?: Record<string, unknown> }) => {
if (match.metadata) {
Object.entries(match.metadata).forEach(([key, value]) => {
const inferredType = inferMetadataFieldType(value);
if (!(key in metadataFields)) {
metadataFields[key] = inferredType;
} else if (
(metadataFields[key] === 'object' || metadataFields[key] === 'array') &&
inferredType === 'string[]'
) {
// Prefer array type over generic object when we see it in another sample
metadataFields[key] = inferredType;
}
});
}
});
}
} catch (queryError) {
logError(`Error sampling records for namespace ${ns}`, queryError);
}
}
return {
namespace: ns,
recordCount,
metadata: metadataFields,
};
} catch (error) {
logError(`Error processing namespace ${ns}`, error);
return {
namespace: ns,
recordCount: 0,
metadata: {},
};
}
})
);
return namespacesInfo;
} catch (error) {
logError('Error listing namespaces', error);
return [];
}
}
/**
* Search a Pinecone index using text query with optional metadata filtering.
* When options.fields is set, only those fields are requested (e.g. for count: no chunk_text).
*/
private async searchIndex(
index: SearchableIndex,
query: string,
topK: number,
namespace?: string,
metadataFilter?: Record<string, unknown>,
options?: { fields?: string[] }
): Promise<PineconeHit[]> {
// Build query payload in the same shape as Python implementation.
const queryPayload: Record<string, unknown> = {
top_k: topK,
inputs: { text: query },
};
// Include filter when explicitly provided (matches Python behavior).
if (metadataFilter !== undefined) {
queryPayload['filter'] = metadataFilter;
logDebug('Applying metadata filter', metadataFilter);
}
try {
// Preferred path: Pinecone search API.
if (typeof index.search === 'function') {
const searchOpts: {
namespace?: string;
query: Record<string, unknown>;
fields?: string[];
} = {
namespace,
query: queryPayload,
};
if (options?.fields?.length) {
searchOpts.fields = options.fields;
}
const result = await index.search(searchOpts);
return result?.result?.hits || [];
}
// Backward-compatible fallback for older API shapes.
const target = namespace && index.namespace ? index.namespace(namespace) : index;
const queryParams: { query: Record<string, unknown>; fields?: string[] } = {
query: {
topK,
inputs: { text: query },
},
};
if (metadataFilter !== undefined) {
queryParams.query['filter'] = metadataFilter;
}
if (options?.fields?.length) {
queryParams.fields = options.fields;
}
const result = target.searchRecords
? await target.searchRecords(queryParams)
: { result: { hits: [] as PineconeHit[] } };
return result?.result?.hits || [];
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
throw new Error(
`Pinecone search failed for namespace "${namespace ?? 'default'}": ${errorMessage}`
);
}
}
/**
* Merge and deduplicate results from dense and sparse searches
*
* Uses the higher score when duplicates are found.
*/
private mergeResults(denseHits: PineconeHit[], sparseHits: PineconeHit[]): MergedHit[] {
const deduped: Record<string, MergedHit> = {};
for (const hit of [...denseHits, ...sparseHits]) {
const hitId = hit._id || '';
const hitScore = hit._score || 0;
const existing = deduped[hitId];
if (existing !== undefined && (existing._score || 0) >= hitScore) {
continue;
}
const hitMetadata: Record<string, PineconeMetadataValue> = {};
let content = '';
for (const [key, value] of Object.entries(hit.fields || {})) {
if (key === 'chunk_text') {
content = typeof value === 'string' ? value : '';
} else {
hitMetadata[key] = value as PineconeMetadataValue;
}
}
deduped[hitId] = {
_id: hitId,
_score: hitScore,
chunk_text: content,
metadata: hitMetadata,
};
}
return Object.values(deduped).sort((a, b) => (b._score || 0) - (a._score || 0));
}
/**
* Rerank results using Pinecone's reranking model
*/
private async rerankResults(
query: string,
results: MergedHit[],
topN: number
): Promise<SearchResult[]> {
if (!results || results.length === 0) {
return [];
}
const pc = this.ensureClient();
try {
const rerankResult = await pc.inference.rerank({
model: this.rerankModel,
query,
// The Pinecone SDK types constrain document values to `Record<string, string>`,
// but the underlying HTTP API accepts any JSON value. We pass MergedHit objects
// (metadata may contain number/boolean/string[]) and only `chunk_text` — which is
// always a string — is accessed via rankFields. The double cast via `as unknown`
// is intentional: it bypasses the SDK's over-narrow type without stringifying
// metadata values that we need to read back from the returned documents.
documents: results as unknown as (string | Record<string, string>)[],
topN,
rankFields: ['chunk_text'],
returnDocuments: true,
parameters: { truncate: 'END' },
});
const reranked: SearchResult[] = [];
for (const item of rerankResult.data || []) {
const document = (item.document || {}) as MergedHit;
reranked.push({
id: document['_id'] || '',
content: document['chunk_text'] || '',
score: parseFloat(String(item.score || 0)),
metadata: document['metadata'] || {},
reranked: true,
});
}
return reranked;
} catch (error) {
logError('Error reranking results', error);
// Fall back to returning unreranked results
return results.slice(0, topN).map((result) => ({
id: result._id || '',
content: result.chunk_text || '',
score: result._score || 0,
metadata: result.metadata || {},
reranked: false,
}));
}
}
/**
* Query Pinecone indexes using hybrid search with optional reranking
*
* Performs parallel searches on dense and sparse indexes, merges results,
* and optionally reranks using the configured reranking model.
*/
async query(params: QueryParams): Promise<SearchResult[]> {
const {
query,
topK: requestedTopK,
namespace,
metadataFilter,
useReranking = true,
fields: requestedFields,
} = params;
// Validate inputs
if (!query || !query.trim()) {
throw new Error('Query cannot be empty');
}
const topK = this.clampTopK(requestedTopK);
// When reranking, Pinecone requires chunk_text in returned fields; add it if user specified fields without it
const searchFields =
requestedFields?.length && useReranking && !requestedFields.includes('chunk_text')
? [...requestedFields, 'chunk_text']
: requestedFields;
// Ensure indexes are ready
const { denseIndex, sparseIndex } = await this.ensureIndexes();
const searchOptions = searchFields?.length ? { fields: searchFields } : undefined;
// Perform hybrid search
const [denseResult, sparseResult] = await Promise.allSettled([
this.searchIndex(denseIndex, query, topK, namespace, metadataFilter, searchOptions),
this.searchIndex(sparseIndex, query, topK, namespace, metadataFilter, searchOptions),
]);
const denseHits = denseResult.status === 'fulfilled' ? denseResult.value : [];
const sparseHits = sparseResult.status === 'fulfilled' ? sparseResult.value : [];
if (denseResult.status === 'rejected') {
logError('Dense index search failed', denseResult.reason);
}
if (sparseResult.status === 'rejected') {
logError('Sparse index search failed', sparseResult.reason);
}
if (denseResult.status === 'rejected' && sparseResult.status === 'rejected') {
throw new Error('Hybrid search failed: both dense and sparse index searches failed.');
}
// Merge results
const mergedResults = this.mergeResults(denseHits, sparseHits);
// Optionally rerank
let documents: SearchResult[];
if (useReranking) {
documents = await this.rerankResults(query, mergedResults, topK);
} else {
documents = mergedResults.slice(0, topK).map((result) => ({
id: result._id || '',
content: result.chunk_text || '',
score: result._score || 0,
metadata: result.metadata || {},
reranked: false,
}));
}
logInfo(
`Retrieved ${documents.length} documents from hybrid search (dense: ${denseHits.length}, sparse: ${sparseHits.length})`
);
return documents;
}
/**
* Keyword (sparse-only) search against the dedicated sparse index.
* Performs lexical/keyword retrieval only—no dense index, no reranking.
* Use for exact or keyword-style queries on the configured sparse index.
*/
async keywordSearch(params: KeywordSearchParams): Promise<SearchResult[]> {
const {
query,
namespace,
topK: requestedTopK,
metadataFilter,
fields: requestedFields,
} = params;
if (!query || !query.trim()) {
throw new Error('Query cannot be empty');
}
const topK = this.clampTopK(requestedTopK);
const { sparseIndex } = await this.ensureIndexes();
const searchOptions = requestedFields?.length ? { fields: requestedFields } : undefined;
const hits = await this.searchIndex(
sparseIndex,
query.trim(),
topK,
namespace,
metadataFilter,
searchOptions
);
const documents: SearchResult[] = hits.map((hit) => {
const fields = hit.fields || {};
let content = '';
const metadata: Record<string, PineconeMetadataValue> = {};
for (const [key, value] of Object.entries(fields)) {
if (key === 'chunk_text') {
content = typeof value === 'string' ? value : '';
} else {
metadata[key] = value as PineconeMetadataValue;
}
}
return {
id: hit._id || '',
content,
score: hit._score || 0,
metadata,
reranked: false,
};
});
logInfo(
`Keyword search returned ${documents.length} results from ${this.getSparseIndexName()}`
);
return documents;
}
/**
* Return the number of unique documents matching the query and optional metadata filter.
* Uses semantic search only (dense index), requests minimal fields (document_number, url, doc_id)
* to avoid transferring chunk content, and deduplicates by document for a document-level count.
*/
async count(params: CountParams): Promise<CountResult> {
if (!params.query || !params.query.trim()) {
throw new Error('Query cannot be empty');
}
const { denseIndex } = await this.ensureIndexes();
const hits = await this.searchIndex(
denseIndex,
params.query,
COUNT_TOP_K,
params.namespace,
params.metadataFilter,
{ fields: [...COUNT_FIELDS] }
);
const docKeys = new Set<string>();
let idFallbackCount = 0;
for (const hit of hits) {
const fields = hit.fields || {};
const docNumber = fields['document_number'];
const url = fields['url'];
const docId = fields['doc_id'];
const docKey =
(typeof docNumber === 'string' ? docNumber : undefined) ??
(typeof url === 'string' ? url : undefined) ??
(typeof docId === 'string' ? docId : undefined);
if (docKey !== undefined) {
docKeys.add(docKey);
} else {
// Fall back to chunk ID — this yields a chunk count, not a document count
idFallbackCount++;
docKeys.add(hit._id ?? '');
}
}
if (idFallbackCount > 0) {
logWarn(
`count(): ${idFallbackCount} hit(s) in namespace "${params.namespace}" had none of the ` +
`identifier fields (${COUNT_FIELDS.join(', ')}); fell back to chunk ID — result may overcount documents`
);
}
const count = docKeys.size;
return {
count,
truncated: hits.length >= COUNT_TOP_K,
};
}
}