@mastra/pinecone 0.0.0-a2a-20250421213654
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +1426 -0
- package/LICENSE.md +7 -0
- package/README.md +72 -0
- package/dist/_tsup-dts-rollup.d.cts +60 -0
- package/dist/_tsup-dts-rollup.d.ts +60 -0
- package/dist/index.cjs +214 -0
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +212 -0
- package/eslint.config.js +6 -0
- package/package.json +42 -0
- package/src/index.ts +1 -0
- package/src/vector/filter.test.ts +457 -0
- package/src/vector/filter.ts +102 -0
- package/src/vector/index.test.ts +1548 -0
- package/src/vector/index.ts +213 -0
- package/tsconfig.json +5 -0
- package/vitest.config.ts +11 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { MastraVector } from '@mastra/core/vector';
|
|
2
|
+
import type {
|
|
3
|
+
QueryResult,
|
|
4
|
+
IndexStats,
|
|
5
|
+
CreateIndexParams,
|
|
6
|
+
UpsertVectorParams,
|
|
7
|
+
QueryVectorParams,
|
|
8
|
+
ParamsToArgs,
|
|
9
|
+
QueryVectorArgs,
|
|
10
|
+
UpsertVectorArgs,
|
|
11
|
+
} from '@mastra/core/vector';
|
|
12
|
+
import type { VectorFilter } from '@mastra/core/vector/filter';
|
|
13
|
+
import { Pinecone } from '@pinecone-database/pinecone';
|
|
14
|
+
import type {
|
|
15
|
+
IndexStatsDescription,
|
|
16
|
+
QueryOptions,
|
|
17
|
+
RecordSparseValues,
|
|
18
|
+
UpdateOptions,
|
|
19
|
+
} from '@pinecone-database/pinecone';
|
|
20
|
+
|
|
21
|
+
import { PineconeFilterTranslator } from './filter';
|
|
22
|
+
|
|
23
|
+
interface PineconeIndexStats extends IndexStats {
|
|
24
|
+
namespaces?: IndexStatsDescription['namespaces'];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
interface PineconeQueryVectorParams extends QueryVectorParams {
|
|
28
|
+
namespace?: string;
|
|
29
|
+
sparseVector?: RecordSparseValues;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
type PineconeQueryVectorArgs = [...QueryVectorArgs, string?, RecordSparseValues?];
|
|
33
|
+
|
|
34
|
+
interface PineconeUpsertVectorParams extends UpsertVectorParams {
|
|
35
|
+
namespace?: string;
|
|
36
|
+
sparseVectors?: RecordSparseValues[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
type PineconeUpsertVectorArgs = [...UpsertVectorArgs, string?, RecordSparseValues[]?];
|
|
40
|
+
|
|
41
|
+
export class PineconeVector extends MastraVector {
|
|
42
|
+
private client: Pinecone;
|
|
43
|
+
|
|
44
|
+
constructor(apiKey: string, environment?: string) {
|
|
45
|
+
super();
|
|
46
|
+
|
|
47
|
+
const opts: { apiKey: string; controllerHostUrl?: string } = { apiKey };
|
|
48
|
+
|
|
49
|
+
if (environment) {
|
|
50
|
+
opts['controllerHostUrl'] = environment;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const baseClient = new Pinecone(opts);
|
|
54
|
+
const telemetry = this.__getTelemetry();
|
|
55
|
+
this.client =
|
|
56
|
+
telemetry?.traceClass(baseClient, {
|
|
57
|
+
spanNamePrefix: 'pinecone-vector',
|
|
58
|
+
attributes: {
|
|
59
|
+
'vector.type': 'pinecone',
|
|
60
|
+
},
|
|
61
|
+
}) ?? baseClient;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
async createIndex(...args: ParamsToArgs<CreateIndexParams>): Promise<void> {
|
|
65
|
+
const params = this.normalizeArgs<CreateIndexParams>('createIndex', args);
|
|
66
|
+
|
|
67
|
+
const { indexName, dimension, metric = 'cosine' } = params;
|
|
68
|
+
|
|
69
|
+
if (!Number.isInteger(dimension) || dimension <= 0) {
|
|
70
|
+
throw new Error('Dimension must be a positive integer');
|
|
71
|
+
}
|
|
72
|
+
await this.client.createIndex({
|
|
73
|
+
name: indexName,
|
|
74
|
+
dimension: dimension,
|
|
75
|
+
metric: metric,
|
|
76
|
+
spec: {
|
|
77
|
+
serverless: {
|
|
78
|
+
cloud: 'aws',
|
|
79
|
+
region: 'us-east-1',
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async upsert(...args: ParamsToArgs<PineconeUpsertVectorParams> | PineconeUpsertVectorArgs): Promise<string[]> {
|
|
86
|
+
const params = this.normalizeArgs<PineconeUpsertVectorParams, PineconeUpsertVectorArgs>('upsert', args, [
|
|
87
|
+
'namespace',
|
|
88
|
+
'sparseVectors',
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
const { indexName, vectors, metadata, ids, namespace, sparseVectors } = params;
|
|
92
|
+
|
|
93
|
+
const index = this.client.Index(indexName).namespace(namespace || '');
|
|
94
|
+
|
|
95
|
+
// Generate IDs if not provided
|
|
96
|
+
const vectorIds = ids || vectors.map(() => crypto.randomUUID());
|
|
97
|
+
|
|
98
|
+
const records = vectors.map((vector, i) => ({
|
|
99
|
+
id: vectorIds[i]!,
|
|
100
|
+
values: vector,
|
|
101
|
+
...(sparseVectors?.[i] && { sparseValues: sparseVectors?.[i] }),
|
|
102
|
+
metadata: metadata?.[i] || {},
|
|
103
|
+
}));
|
|
104
|
+
|
|
105
|
+
// Pinecone has a limit of 100 vectors per upsert request
|
|
106
|
+
const batchSize = 100;
|
|
107
|
+
for (let i = 0; i < records.length; i += batchSize) {
|
|
108
|
+
const batch = records.slice(i, i + batchSize);
|
|
109
|
+
await index.upsert(batch);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return vectorIds;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
transformFilter(filter?: VectorFilter) {
|
|
116
|
+
const translator = new PineconeFilterTranslator();
|
|
117
|
+
return translator.translate(filter);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async query(...args: ParamsToArgs<PineconeQueryVectorParams> | PineconeQueryVectorArgs): Promise<QueryResult[]> {
|
|
121
|
+
const params = this.normalizeArgs<PineconeQueryVectorParams, PineconeQueryVectorArgs>('query', args, [
|
|
122
|
+
'namespace',
|
|
123
|
+
'sparseVector',
|
|
124
|
+
]);
|
|
125
|
+
|
|
126
|
+
const { indexName, queryVector, topK = 10, filter, includeVector = false, namespace, sparseVector } = params;
|
|
127
|
+
|
|
128
|
+
const index = this.client.Index(indexName).namespace(namespace || '');
|
|
129
|
+
|
|
130
|
+
const translatedFilter = this.transformFilter(filter) ?? undefined;
|
|
131
|
+
|
|
132
|
+
const queryParams: QueryOptions = {
|
|
133
|
+
vector: queryVector,
|
|
134
|
+
topK,
|
|
135
|
+
includeMetadata: true,
|
|
136
|
+
includeValues: includeVector,
|
|
137
|
+
filter: translatedFilter,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
// If sparse vector is provided, use hybrid search
|
|
141
|
+
if (sparseVector) {
|
|
142
|
+
queryParams.sparseVector = sparseVector;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
const results = await index.query(queryParams);
|
|
146
|
+
|
|
147
|
+
return results.matches.map(match => ({
|
|
148
|
+
id: match.id,
|
|
149
|
+
score: match.score || 0,
|
|
150
|
+
metadata: match.metadata as Record<string, any>,
|
|
151
|
+
...(includeVector && { vector: match.values || [] }),
|
|
152
|
+
}));
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
async listIndexes(): Promise<string[]> {
|
|
156
|
+
const indexesResult = await this.client.listIndexes();
|
|
157
|
+
return indexesResult?.indexes?.map(index => index.name) || [];
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async describeIndex(indexName: string): Promise<PineconeIndexStats> {
|
|
161
|
+
const index = this.client.Index(indexName);
|
|
162
|
+
const stats = await index.describeIndexStats();
|
|
163
|
+
const description = await this.client.describeIndex(indexName);
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
dimension: description.dimension,
|
|
167
|
+
count: stats.totalRecordCount || 0,
|
|
168
|
+
metric: description.metric as 'cosine' | 'euclidean' | 'dotproduct',
|
|
169
|
+
namespaces: stats.namespaces,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async deleteIndex(indexName: string): Promise<void> {
|
|
174
|
+
try {
|
|
175
|
+
await this.client.deleteIndex(indexName);
|
|
176
|
+
} catch (error: any) {
|
|
177
|
+
throw new Error(`Failed to delete Pinecone index: ${error.message}`);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async updateIndexById(
|
|
182
|
+
indexName: string,
|
|
183
|
+
id: string,
|
|
184
|
+
update: {
|
|
185
|
+
vector?: number[];
|
|
186
|
+
metadata?: Record<string, any>;
|
|
187
|
+
},
|
|
188
|
+
namespace?: string,
|
|
189
|
+
): Promise<void> {
|
|
190
|
+
if (!update.vector && !update.metadata) {
|
|
191
|
+
throw new Error('No updates provided');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
const index = this.client.Index(indexName).namespace(namespace || '');
|
|
195
|
+
|
|
196
|
+
const updateObj: UpdateOptions = { id };
|
|
197
|
+
|
|
198
|
+
if (update.vector) {
|
|
199
|
+
updateObj.values = update.vector;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (update.metadata) {
|
|
203
|
+
updateObj.metadata = update.metadata;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
await index.update(updateObj);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
async deleteIndexById(indexName: string, id: string, namespace?: string): Promise<void> {
|
|
210
|
+
const index = this.client.Index(indexName).namespace(namespace || '');
|
|
211
|
+
await index.deleteOne(id);
|
|
212
|
+
}
|
|
213
|
+
}
|
package/tsconfig.json
ADDED