@snap-agent/rag-ecommerce 0.1.0
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/LICENSE +22 -0
- package/README.md +279 -0
- package/dist/index.d.mts +246 -0
- package/dist/index.d.ts +246 -0
- package/dist/index.js +1036 -0
- package/dist/index.mjs +999 -0
- package/package.json +57 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { RAGPlugin, RAGContext, RAGDocument, IngestOptions, IngestResult, BulkOperation, BulkResult } from '@snap-agent/core';
|
|
2
|
+
|
|
3
|
+
interface URLSource {
|
|
4
|
+
url: string;
|
|
5
|
+
type: 'json' | 'csv' | 'xml' | 'api';
|
|
6
|
+
auth?: {
|
|
7
|
+
type: 'bearer' | 'basic' | 'api-key' | 'custom';
|
|
8
|
+
token?: string;
|
|
9
|
+
username?: string;
|
|
10
|
+
password?: string;
|
|
11
|
+
header?: string;
|
|
12
|
+
key?: string;
|
|
13
|
+
headers?: Record<string, string>;
|
|
14
|
+
};
|
|
15
|
+
transform?: {
|
|
16
|
+
documentPath?: string;
|
|
17
|
+
fieldMapping?: {
|
|
18
|
+
id?: string;
|
|
19
|
+
content?: string;
|
|
20
|
+
[key: string]: string | undefined;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
headers?: Record<string, string>;
|
|
24
|
+
timeout?: number;
|
|
25
|
+
metadata?: Record<string, any>;
|
|
26
|
+
}
|
|
27
|
+
interface URLIngestResult extends IngestResult {
|
|
28
|
+
sourceUrl: string;
|
|
29
|
+
fetchedAt: Date;
|
|
30
|
+
documentsFetched: number;
|
|
31
|
+
}
|
|
32
|
+
interface EcommerceRAGConfig {
|
|
33
|
+
mongoUri: string;
|
|
34
|
+
dbName?: string;
|
|
35
|
+
collection?: string;
|
|
36
|
+
openaiApiKey: string;
|
|
37
|
+
voyageApiKey: string;
|
|
38
|
+
embeddingModel?: string;
|
|
39
|
+
tenantId: string;
|
|
40
|
+
attributeList?: string[];
|
|
41
|
+
enableAttributeExtraction?: boolean;
|
|
42
|
+
numCandidates?: number;
|
|
43
|
+
limit?: number;
|
|
44
|
+
vectorIndexName?: string;
|
|
45
|
+
rescoringWeights?: {
|
|
46
|
+
color?: number;
|
|
47
|
+
size?: number;
|
|
48
|
+
material?: number;
|
|
49
|
+
category?: number;
|
|
50
|
+
brand?: number;
|
|
51
|
+
popularity?: number;
|
|
52
|
+
ctr?: number;
|
|
53
|
+
sales?: number;
|
|
54
|
+
};
|
|
55
|
+
enableReranking?: boolean;
|
|
56
|
+
rerankTopK?: number;
|
|
57
|
+
contextProductCount?: number;
|
|
58
|
+
language?: 'es' | 'en';
|
|
59
|
+
includeOutOfStock?: boolean;
|
|
60
|
+
cache?: {
|
|
61
|
+
embeddings?: {
|
|
62
|
+
enabled?: boolean;
|
|
63
|
+
ttl?: number;
|
|
64
|
+
maxSize?: number;
|
|
65
|
+
};
|
|
66
|
+
attributes?: {
|
|
67
|
+
enabled?: boolean;
|
|
68
|
+
ttl?: number;
|
|
69
|
+
maxSize?: number;
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
priority?: number;
|
|
73
|
+
}
|
|
74
|
+
interface ProductDoc {
|
|
75
|
+
_id?: any;
|
|
76
|
+
tenantId: string;
|
|
77
|
+
agentId?: string;
|
|
78
|
+
sku: string;
|
|
79
|
+
title: string;
|
|
80
|
+
description?: string;
|
|
81
|
+
embedding: number[];
|
|
82
|
+
attributes: {
|
|
83
|
+
category?: string;
|
|
84
|
+
brand?: string;
|
|
85
|
+
color?: string;
|
|
86
|
+
material?: string;
|
|
87
|
+
size?: string[];
|
|
88
|
+
gender?: 'M' | 'F' | 'Unisex';
|
|
89
|
+
season?: string;
|
|
90
|
+
price?: number;
|
|
91
|
+
[key: string]: any;
|
|
92
|
+
};
|
|
93
|
+
inStock?: boolean;
|
|
94
|
+
metrics?: {
|
|
95
|
+
popularity?: number;
|
|
96
|
+
ctr?: number;
|
|
97
|
+
sales?: number;
|
|
98
|
+
};
|
|
99
|
+
vectorSearchScore?: number;
|
|
100
|
+
}
|
|
101
|
+
interface QueryAttrs {
|
|
102
|
+
category?: string;
|
|
103
|
+
color?: string;
|
|
104
|
+
gender?: string;
|
|
105
|
+
brand?: string;
|
|
106
|
+
material?: string;
|
|
107
|
+
size?: string;
|
|
108
|
+
season?: string;
|
|
109
|
+
priceMin?: number;
|
|
110
|
+
priceMax?: number;
|
|
111
|
+
[key: string]: any;
|
|
112
|
+
}
|
|
113
|
+
declare class EcommerceRAGPlugin implements RAGPlugin {
|
|
114
|
+
name: string;
|
|
115
|
+
type: "rag";
|
|
116
|
+
priority: number;
|
|
117
|
+
private config;
|
|
118
|
+
private client;
|
|
119
|
+
private db;
|
|
120
|
+
private openai;
|
|
121
|
+
private embeddingCache;
|
|
122
|
+
private attributeCache;
|
|
123
|
+
private cacheStats;
|
|
124
|
+
private cleanupInterval?;
|
|
125
|
+
constructor(config: EcommerceRAGConfig);
|
|
126
|
+
private ensureConnection;
|
|
127
|
+
/**
|
|
128
|
+
* Main retrieval method - called by the SDK
|
|
129
|
+
*/
|
|
130
|
+
retrieveContext(message: string, options: {
|
|
131
|
+
agentId: string;
|
|
132
|
+
threadId?: string;
|
|
133
|
+
filters?: Record<string, any>;
|
|
134
|
+
metadata?: Record<string, any>;
|
|
135
|
+
}): Promise<RAGContext>;
|
|
136
|
+
/**
|
|
137
|
+
* Format context for LLM
|
|
138
|
+
*/
|
|
139
|
+
formatContext(context: RAGContext): string;
|
|
140
|
+
/**
|
|
141
|
+
* Embed text using Voyage with caching
|
|
142
|
+
*/
|
|
143
|
+
private embedText;
|
|
144
|
+
/**
|
|
145
|
+
* Extract attributes from user message using OpenAI with caching
|
|
146
|
+
*/
|
|
147
|
+
private extractAttributes;
|
|
148
|
+
/**
|
|
149
|
+
* MongoDB Atlas Vector Search
|
|
150
|
+
*/
|
|
151
|
+
private vectorSearch;
|
|
152
|
+
/**
|
|
153
|
+
* Soft rescore based on attributes and metrics
|
|
154
|
+
*/
|
|
155
|
+
private softRescore;
|
|
156
|
+
/**
|
|
157
|
+
* Optional Voyage reranking
|
|
158
|
+
*/
|
|
159
|
+
private rerank;
|
|
160
|
+
/**
|
|
161
|
+
* Build context string for LLM
|
|
162
|
+
*/
|
|
163
|
+
private buildContextString;
|
|
164
|
+
/**
|
|
165
|
+
* Start periodic cache cleanup (remove expired entries)
|
|
166
|
+
*/
|
|
167
|
+
private startCacheCleanup;
|
|
168
|
+
/**
|
|
169
|
+
* Clean up expired cache entries
|
|
170
|
+
*/
|
|
171
|
+
private cleanupExpiredCache;
|
|
172
|
+
/**
|
|
173
|
+
* Get cache statistics
|
|
174
|
+
*/
|
|
175
|
+
getCacheStats(): {
|
|
176
|
+
embeddings: {
|
|
177
|
+
size: number;
|
|
178
|
+
maxSize: number;
|
|
179
|
+
hits: number;
|
|
180
|
+
misses: number;
|
|
181
|
+
hitRate: string;
|
|
182
|
+
};
|
|
183
|
+
attributes: {
|
|
184
|
+
size: number;
|
|
185
|
+
maxSize: number;
|
|
186
|
+
hits: number;
|
|
187
|
+
misses: number;
|
|
188
|
+
hitRate: string;
|
|
189
|
+
};
|
|
190
|
+
};
|
|
191
|
+
/**
|
|
192
|
+
* Clear all caches
|
|
193
|
+
*/
|
|
194
|
+
clearCache(): void;
|
|
195
|
+
/**
|
|
196
|
+
* Get MongoDB collection
|
|
197
|
+
*/
|
|
198
|
+
private getCollection;
|
|
199
|
+
/**
|
|
200
|
+
* Generate embedding for a single text
|
|
201
|
+
*/
|
|
202
|
+
private generateEmbedding;
|
|
203
|
+
/**
|
|
204
|
+
* Ingest products into the RAG system
|
|
205
|
+
* Converts RAGDocuments to ProductDocs and indexes them with embeddings
|
|
206
|
+
*/
|
|
207
|
+
ingest(documents: RAGDocument[], options?: IngestOptions): Promise<IngestResult>;
|
|
208
|
+
/**
|
|
209
|
+
* Update a single product
|
|
210
|
+
*/
|
|
211
|
+
update(id: string, document: Partial<RAGDocument>, options?: IngestOptions): Promise<void>;
|
|
212
|
+
/**
|
|
213
|
+
* Delete product(s) by SKU
|
|
214
|
+
*/
|
|
215
|
+
delete(ids: string | string[], options?: IngestOptions): Promise<number>;
|
|
216
|
+
/**
|
|
217
|
+
* Bulk operations for efficient batch processing
|
|
218
|
+
*/
|
|
219
|
+
bulk(operations: BulkOperation[], options?: IngestOptions): Promise<BulkResult>;
|
|
220
|
+
/**
|
|
221
|
+
* Generate embeddings for a batch of texts
|
|
222
|
+
*/
|
|
223
|
+
private generateEmbeddingsBatch;
|
|
224
|
+
/**
|
|
225
|
+
* Ingest documents from URL source (CSV, JSON, XML, API)
|
|
226
|
+
*/
|
|
227
|
+
ingestFromUrl(source: URLSource, options?: IngestOptions): Promise<URLIngestResult>;
|
|
228
|
+
/**
|
|
229
|
+
* Handle webhook payload for real-time updates
|
|
230
|
+
*/
|
|
231
|
+
handleWebhook(payload: any, source: string, options?: IngestOptions): Promise<IngestResult>;
|
|
232
|
+
private buildAuthHeaders;
|
|
233
|
+
private transformJsonToDocuments;
|
|
234
|
+
private transformCsvToDocuments;
|
|
235
|
+
private transformXmlToDocuments;
|
|
236
|
+
private extractByPath;
|
|
237
|
+
private extractField;
|
|
238
|
+
private parseShopifyWebhook;
|
|
239
|
+
private parseWooCommerceWebhook;
|
|
240
|
+
/**
|
|
241
|
+
* Cleanup resources and close connections
|
|
242
|
+
*/
|
|
243
|
+
disconnect(): Promise<void>;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
export { type EcommerceRAGConfig, EcommerceRAGPlugin, type ProductDoc, type QueryAttrs, type URLIngestResult, type URLSource };
|