@retrivora-ai/rag-engine 1.8.8 → 1.9.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/dist/{ILLMProvider-CbUtvWAW.d.mts → ILLMProvider-BOJFz3Na.d.mts} +0 -2
- package/dist/{ILLMProvider-CbUtvWAW.d.ts → ILLMProvider-BOJFz3Na.d.ts} +0 -2
- package/dist/handlers/index.d.mts +2 -2
- package/dist/handlers/index.d.ts +2 -2
- package/dist/handlers/index.js +52 -100
- package/dist/handlers/index.mjs +52 -100
- package/dist/{index-DgzcnqZs.d.ts → index-BwpcaziY.d.ts} +1 -1
- package/dist/{index-c_y_5qdw.d.mts → index-D3V9Et2M.d.mts} +1 -1
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -25
- package/dist/index.mjs +2 -24
- package/dist/server.d.mts +3 -3
- package/dist/server.d.ts +3 -3
- package/dist/server.js +55 -108
- package/dist/server.mjs +55 -108
- package/package.json +2 -2
- package/src/config/RagConfig.ts +0 -2
- package/src/index.ts +1 -1
- package/src/providers/vectordb/ChromaDBProvider.ts +0 -4
- package/src/providers/vectordb/MultiTablePostgresProvider.ts +5 -18
- package/src/providers/vectordb/PostgreSQLProvider.ts +3 -14
- package/src/providers/vectordb/QdrantProvider.ts +1 -4
- package/src/utils/UITransformer.ts +51 -99
package/dist/server.js
CHANGED
|
@@ -374,12 +374,10 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
374
374
|
embedding VECTOR(${this.dimensions})
|
|
375
375
|
)
|
|
376
376
|
`);
|
|
377
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
378
|
-
const indexOps = metric === "euclidean" ? "vector_l2_ops" : metric === "dotproduct" ? "vector_ip_ops" : "vector_cosine_ops";
|
|
379
377
|
await client.query(`
|
|
380
378
|
CREATE INDEX IF NOT EXISTS ${this.uploadTable}_embedding_idx
|
|
381
379
|
ON ${this.uploadTable}
|
|
382
|
-
USING hnsw (embedding
|
|
380
|
+
USING hnsw (embedding vector_cosine_ops)
|
|
383
381
|
`);
|
|
384
382
|
const res = await client.query(`
|
|
385
383
|
SELECT table_name
|
|
@@ -441,12 +439,10 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
441
439
|
)
|
|
442
440
|
`;
|
|
443
441
|
await client.query(createTableSql);
|
|
444
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
445
|
-
const indexOps = metric === "euclidean" ? "vector_l2_ops" : metric === "dotproduct" ? "vector_ip_ops" : "vector_cosine_ops";
|
|
446
442
|
await client.query(`
|
|
447
443
|
CREATE INDEX IF NOT EXISTS "${tableName}_embedding_idx"
|
|
448
444
|
ON "${tableName}"
|
|
449
|
-
USING hnsw (embedding
|
|
445
|
+
USING hnsw (embedding vector_cosine_ops)
|
|
450
446
|
`);
|
|
451
447
|
if (!this.tables.includes(tableName)) {
|
|
452
448
|
this.tables.push(tableName);
|
|
@@ -528,8 +524,6 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
528
524
|
try {
|
|
529
525
|
let sqlQuery = "";
|
|
530
526
|
let params = [];
|
|
531
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
532
|
-
const scoreExpr = metric === "euclidean" ? `1 / (1 + (embedding <-> $1::vector))` : metric === "dotproduct" ? `(embedding <#> $1::vector) * -1` : `1 - (embedding <=> $1::vector)`;
|
|
533
527
|
if (queryText) {
|
|
534
528
|
const hasEntityHints = entityHints.length > 0;
|
|
535
529
|
const exactNameScoreExpr = hasEntityHints ? `+ (
|
|
@@ -542,9 +536,9 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
542
536
|
) * 3.0` : "";
|
|
543
537
|
sqlQuery = `
|
|
544
538
|
SELECT *,
|
|
545
|
-
($
|
|
539
|
+
(1 - (embedding <=> $1::vector)) AS vector_score,
|
|
546
540
|
COALESCE(ts_rank(to_tsvector('english', (to_jsonb(t) - 'embedding')::text), to_tsquery('english', $2)), 0) AS keyword_score,
|
|
547
|
-
(($
|
|
541
|
+
((1 - (embedding <=> $1::vector)) + (COALESCE(ts_rank(to_tsvector('english', (to_jsonb(t) - 'embedding')::text), to_tsquery('english', $2)), 0) * 2.0) ${exactNameScoreExpr}) AS hybrid_score
|
|
548
542
|
FROM "${table}" t
|
|
549
543
|
ORDER BY hybrid_score DESC
|
|
550
544
|
LIMIT 50
|
|
@@ -553,7 +547,7 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
553
547
|
} else {
|
|
554
548
|
sqlQuery = `
|
|
555
549
|
SELECT *,
|
|
556
|
-
($
|
|
550
|
+
(1 - (embedding <=> $1::vector)) AS hybrid_score
|
|
557
551
|
FROM "${table}" t
|
|
558
552
|
ORDER BY hybrid_score DESC
|
|
559
553
|
LIMIT 50
|
|
@@ -1027,13 +1021,11 @@ var init_QdrantProvider = __esm({
|
|
|
1027
1021
|
if (import_axios4.default.isAxiosError(err) && ((_a = err.response) == null ? void 0 : _a.status) === 404) {
|
|
1028
1022
|
const opts = this.config.options;
|
|
1029
1023
|
const dimensionsForCreate = opts.dimensions || 1536;
|
|
1030
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
1031
|
-
const distance = metric === "euclidean" ? "Euclid" : metric === "dotproduct" ? "Dot" : "Cosine";
|
|
1032
1024
|
console.log(`[QdrantProvider] \u23F3 Creating collection "${this.indexName}" (dimensions: ${dimensionsForCreate})...`);
|
|
1033
1025
|
await this.http.put(`/collections/${this.indexName}`, {
|
|
1034
1026
|
vectors: {
|
|
1035
1027
|
size: dimensionsForCreate,
|
|
1036
|
-
distance
|
|
1028
|
+
distance: "Cosine"
|
|
1037
1029
|
}
|
|
1038
1030
|
});
|
|
1039
1031
|
console.log(`[QdrantProvider] \u2705 Created collection "${this.indexName}"`);
|
|
@@ -1211,12 +1203,9 @@ var init_ChromaDBProvider = __esm({
|
|
|
1211
1203
|
console.log(`[ChromaDBProvider] \u2705 Collection "${this.indexName}" found (id: ${this.collectionId})`);
|
|
1212
1204
|
} catch (err) {
|
|
1213
1205
|
if (import_axios5.default.isAxiosError(err) && ((_a = err.response) == null ? void 0 : _a.status) === 404) {
|
|
1214
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
1215
|
-
const space = metric === "euclidean" ? "l2" : metric === "dotproduct" ? "ip" : "cosine";
|
|
1216
1206
|
console.log(`[ChromaDBProvider] \u23F3 Collection "${this.indexName}" not found. Creating...`);
|
|
1217
1207
|
const { data } = await this.http.post("/api/v1/collections", {
|
|
1218
|
-
name: this.indexName
|
|
1219
|
-
metadata: { "hnsw:space": space }
|
|
1208
|
+
name: this.indexName
|
|
1220
1209
|
});
|
|
1221
1210
|
this.collectionId = data.id;
|
|
1222
1211
|
console.log(`[ChromaDBProvider] \u2705 Created collection "${this.indexName}" (id: ${this.collectionId})`);
|
|
@@ -4253,20 +4242,18 @@ var UITransformer = class {
|
|
|
4253
4242
|
}
|
|
4254
4243
|
const isStockRequest = this.isStockQuery(userQuery);
|
|
4255
4244
|
const filteredData = isStockRequest ? retrievedData.filter((item) => this.determineStockStatus(item)) : retrievedData;
|
|
4256
|
-
const
|
|
4257
|
-
const categories = this.detectCategories(filteredData, groupByField);
|
|
4245
|
+
const categories = this.detectCategories(filteredData);
|
|
4258
4246
|
const hasProducts = filteredData.some((item) => this.isProductData(item));
|
|
4259
4247
|
const isTimeSeries = filteredData.some((item) => this.isTimeSeriesData(item));
|
|
4260
4248
|
const isTrendQuery = this.isTrendQuery(userQuery);
|
|
4261
4249
|
if (isTrendQuery && isTimeSeries) {
|
|
4262
4250
|
return this.transformToLineChart(filteredData);
|
|
4263
4251
|
}
|
|
4264
|
-
|
|
4265
|
-
if (hasProducts && !explicitChartRequest && !this.shouldShowCategoryChart(userQuery)) {
|
|
4252
|
+
if (hasProducts && !this.shouldShowCategoryChart(userQuery, categories)) {
|
|
4266
4253
|
return this.transformToProductCarousel(filteredData, config, trainedSchema);
|
|
4267
4254
|
}
|
|
4268
|
-
if (
|
|
4269
|
-
return this.transformToPieChart(filteredData
|
|
4255
|
+
if (categories.length > 1 && this.shouldShowCategoryChart(userQuery, categories)) {
|
|
4256
|
+
return this.transformToPieChart(filteredData);
|
|
4270
4257
|
}
|
|
4271
4258
|
if (hasProducts) {
|
|
4272
4259
|
return this.transformToProductCarousel(filteredData, config, trainedSchema);
|
|
@@ -4291,12 +4278,11 @@ var UITransformer = class {
|
|
|
4291
4278
|
/**
|
|
4292
4279
|
* Transform data to pie chart format
|
|
4293
4280
|
*/
|
|
4294
|
-
static transformToPieChart(data
|
|
4295
|
-
|
|
4296
|
-
|
|
4297
|
-
const categoryData = this.aggregateByCategory(data, categories, groupByField);
|
|
4281
|
+
static transformToPieChart(data) {
|
|
4282
|
+
const categories = this.detectCategories(data);
|
|
4283
|
+
const categoryData = this.aggregateByCategory(data, categories);
|
|
4298
4284
|
const pieData = Object.entries(categoryData).map(([label, count]) => {
|
|
4299
|
-
const { inStockCount, outOfStockCount } = this.calculateStockCounts(label, data
|
|
4285
|
+
const { inStockCount, outOfStockCount } = this.calculateStockCounts(label, data);
|
|
4300
4286
|
return {
|
|
4301
4287
|
label,
|
|
4302
4288
|
value: count,
|
|
@@ -4306,7 +4292,7 @@ var UITransformer = class {
|
|
|
4306
4292
|
});
|
|
4307
4293
|
return {
|
|
4308
4294
|
type: "pie_chart",
|
|
4309
|
-
title:
|
|
4295
|
+
title: "Distribution by Category",
|
|
4310
4296
|
description: `Showing breakdown across ${categories.length} categories`,
|
|
4311
4297
|
data: pieData
|
|
4312
4298
|
};
|
|
@@ -4529,11 +4515,10 @@ var UITransformer = class {
|
|
|
4529
4515
|
});
|
|
4530
4516
|
return hasTimeKeyword || hasValidDateValue;
|
|
4531
4517
|
}
|
|
4532
|
-
static
|
|
4533
|
-
|
|
4534
|
-
|
|
4535
|
-
|
|
4536
|
-
static shouldShowCategoryChart(query) {
|
|
4518
|
+
static shouldShowCategoryChart(query, categories) {
|
|
4519
|
+
if (categories.length < 2) {
|
|
4520
|
+
return false;
|
|
4521
|
+
}
|
|
4537
4522
|
const normalized = query.toLowerCase();
|
|
4538
4523
|
const chartKeywords = [
|
|
4539
4524
|
"distribution",
|
|
@@ -4546,11 +4531,7 @@ var UITransformer = class {
|
|
|
4546
4531
|
"segmentation",
|
|
4547
4532
|
"split",
|
|
4548
4533
|
"category breakdown",
|
|
4549
|
-
"category distribution"
|
|
4550
|
-
"pie chart",
|
|
4551
|
-
"piechart",
|
|
4552
|
-
"bar chart",
|
|
4553
|
-
"barchart"
|
|
4534
|
+
"category distribution"
|
|
4554
4535
|
];
|
|
4555
4536
|
return chartKeywords.some((keyword) => normalized.includes(keyword));
|
|
4556
4537
|
}
|
|
@@ -4626,67 +4607,44 @@ var UITransformer = class {
|
|
|
4626
4607
|
return null;
|
|
4627
4608
|
}
|
|
4628
4609
|
/**
|
|
4629
|
-
* Helper:
|
|
4630
|
-
*/
|
|
4631
|
-
static determineGroupByField(query, data) {
|
|
4632
|
-
const normalized = query.toLowerCase();
|
|
4633
|
-
const match = normalized.match(/(?:by|across|per|based on)\s+([a-zA-Z]+)/i);
|
|
4634
|
-
let targetField = "category";
|
|
4635
|
-
if (match && match[1]) {
|
|
4636
|
-
let field = match[1].toLowerCase();
|
|
4637
|
-
if (field.endsWith("ies")) field = field.replace(/ies$/, "y");
|
|
4638
|
-
else if (field.endsWith("s") && field !== "status") field = field.slice(0, -1);
|
|
4639
|
-
const stopWords = ["the", "all", "different", "various", "some", "these", "those"];
|
|
4640
|
-
if (!stopWords.includes(field)) {
|
|
4641
|
-
targetField = field;
|
|
4642
|
-
}
|
|
4643
|
-
}
|
|
4644
|
-
const hasTargetField = data.some((d) => d.metadata && d.metadata[targetField] !== void 0);
|
|
4645
|
-
if (!hasTargetField && targetField === "category") {
|
|
4646
|
-
if (data.some((d) => d.metadata && d.metadata["type"] !== void 0)) return "type";
|
|
4647
|
-
if (data.some((d) => d.metadata && d.metadata["brand"] !== void 0)) return "brand";
|
|
4648
|
-
}
|
|
4649
|
-
return targetField;
|
|
4650
|
-
}
|
|
4651
|
-
/**
|
|
4652
|
-
* Helper: Detect grouping values dynamically
|
|
4610
|
+
* Helper: Detect categories in data
|
|
4653
4611
|
*/
|
|
4654
|
-
static detectCategories(data
|
|
4612
|
+
static detectCategories(data) {
|
|
4655
4613
|
const categories = /* @__PURE__ */ new Set();
|
|
4656
4614
|
data.forEach((item) => {
|
|
4657
4615
|
const meta = item.metadata || {};
|
|
4658
|
-
if (meta
|
|
4659
|
-
|
|
4660
|
-
|
|
4661
|
-
|
|
4662
|
-
|
|
4663
|
-
|
|
4664
|
-
|
|
4665
|
-
|
|
4666
|
-
|
|
4667
|
-
|
|
4668
|
-
|
|
4669
|
-
|
|
4616
|
+
if (meta.category) {
|
|
4617
|
+
categories.add(String(meta.category));
|
|
4618
|
+
}
|
|
4619
|
+
if (meta.type) {
|
|
4620
|
+
categories.add(String(meta.type));
|
|
4621
|
+
}
|
|
4622
|
+
if (meta.tag) {
|
|
4623
|
+
const tags = Array.isArray(meta.tag) ? meta.tag : [meta.tag];
|
|
4624
|
+
tags.forEach((t) => categories.add(String(t)));
|
|
4625
|
+
}
|
|
4626
|
+
const contentCategories = Array.from(new Set(
|
|
4627
|
+
Array.from(item.content.matchAll(/^\s*([^:\n]+):\s*(?:•|\*|-)*/gm)).map((match) => match[1].trim()).filter(Boolean)
|
|
4628
|
+
));
|
|
4629
|
+
contentCategories.forEach((category) => categories.add(category));
|
|
4630
|
+
const categoryMatch = item.content.match(/(?:Category|Type|Class):\s*([^\n]+)/i);
|
|
4631
|
+
if (categoryMatch) {
|
|
4632
|
+
categories.add(categoryMatch[1].trim());
|
|
4670
4633
|
}
|
|
4671
4634
|
});
|
|
4672
4635
|
return Array.from(categories);
|
|
4673
4636
|
}
|
|
4674
4637
|
/**
|
|
4675
|
-
* Helper: Aggregate data
|
|
4638
|
+
* Helper: Aggregate data by category
|
|
4676
4639
|
*/
|
|
4677
|
-
static aggregateByCategory(data, categories
|
|
4640
|
+
static aggregateByCategory(data, categories) {
|
|
4678
4641
|
const result = {};
|
|
4679
4642
|
categories.forEach((cat) => {
|
|
4680
4643
|
result[cat] = 0;
|
|
4681
4644
|
});
|
|
4682
4645
|
data.forEach((item) => {
|
|
4683
4646
|
const meta = item.metadata || {};
|
|
4684
|
-
|
|
4685
|
-
if (meta[groupByField] !== void 0) {
|
|
4686
|
-
itemCategory = String(meta[groupByField]);
|
|
4687
|
-
} else if (groupByField === "category" && meta.type) {
|
|
4688
|
-
itemCategory = String(meta.type);
|
|
4689
|
-
}
|
|
4647
|
+
const itemCategory = meta.category || meta.type || "Other";
|
|
4690
4648
|
if (Object.prototype.hasOwnProperty.call(result, itemCategory)) {
|
|
4691
4649
|
result[itemCategory]++;
|
|
4692
4650
|
} else {
|
|
@@ -4736,19 +4694,14 @@ var UITransformer = class {
|
|
|
4736
4694
|
});
|
|
4737
4695
|
}
|
|
4738
4696
|
/**
|
|
4739
|
-
* Helper: Calculate stock counts
|
|
4697
|
+
* Helper: Calculate stock counts by category
|
|
4740
4698
|
*/
|
|
4741
|
-
static calculateStockCounts(category, data
|
|
4699
|
+
static calculateStockCounts(category, data) {
|
|
4742
4700
|
let inStock = 0;
|
|
4743
4701
|
let outOfStock = 0;
|
|
4744
4702
|
data.forEach((d) => {
|
|
4745
4703
|
const meta = d.metadata || {};
|
|
4746
|
-
|
|
4747
|
-
if (meta[groupByField] !== void 0) {
|
|
4748
|
-
itemCategory = String(meta[groupByField]);
|
|
4749
|
-
} else if (groupByField === "category" && meta.type) {
|
|
4750
|
-
itemCategory = String(meta.type);
|
|
4751
|
-
}
|
|
4704
|
+
const itemCategory = meta.category || meta.type || "Other";
|
|
4752
4705
|
if (itemCategory === category) {
|
|
4753
4706
|
if (this.determineStockStatus(d)) {
|
|
4754
4707
|
inStock++;
|
|
@@ -4873,13 +4826,12 @@ DATA SHAPE per type:
|
|
|
4873
4826
|
- text: { "content": "<prose answer>" }
|
|
4874
4827
|
|
|
4875
4828
|
DECISION RULES (follow strictly):
|
|
4876
|
-
1.
|
|
4877
|
-
2.
|
|
4878
|
-
3.
|
|
4879
|
-
4.
|
|
4880
|
-
5.
|
|
4881
|
-
6.
|
|
4882
|
-
7. text \u2192 conversational or free-form answers where no chart adds value
|
|
4829
|
+
1. bar_chart \u2192 comparing quantities across categories (e.g. sales by region, price by product). Use this when there is only ONE value per category.
|
|
4830
|
+
2. line_chart \u2192 trends or changes over time (dates, months, years, sequential events)
|
|
4831
|
+
3. pie_chart \u2192 proportional breakdown or percentage distribution
|
|
4832
|
+
4. radar_chart \u2192 comparing 2 or more products across MULTIPLE attributes (e.g. comparing features, ratings, or characteristics of 2 specific products). If the user asks to "compare" products and the data contains multiple dimensions or ratings for them, you MUST use this.
|
|
4833
|
+
5. table \u2192 multi-field structured records where each item has \u2265 3 attributes
|
|
4834
|
+
6. text \u2192 conversational or free-form answers where no chart adds value
|
|
4883
4835
|
|
|
4884
4836
|
IMPORTANT:
|
|
4885
4837
|
- Aggregate numeric values from the raw data \u2014 do not pass raw object arrays as data.
|
|
@@ -6037,12 +5989,10 @@ var PostgreSQLProvider = class extends BaseVectorProvider {
|
|
|
6037
5989
|
embedding VECTOR(${this.dimensions})
|
|
6038
5990
|
)
|
|
6039
5991
|
`);
|
|
6040
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
6041
|
-
const indexOps = metric === "euclidean" ? "vector_l2_ops" : metric === "dotproduct" ? "vector_ip_ops" : "vector_cosine_ops";
|
|
6042
5992
|
await client.query(`
|
|
6043
5993
|
CREATE INDEX IF NOT EXISTS ${this.tableName}_embedding_idx
|
|
6044
5994
|
ON ${this.tableName}
|
|
6045
|
-
USING hnsw (embedding
|
|
5995
|
+
USING hnsw (embedding vector_cosine_ops)
|
|
6046
5996
|
`);
|
|
6047
5997
|
} finally {
|
|
6048
5998
|
client.release();
|
|
@@ -6114,14 +6064,11 @@ var PostgreSQLProvider = class extends BaseVectorProvider {
|
|
|
6114
6064
|
try {
|
|
6115
6065
|
const efSearch = this.config.options.efSearch || Math.max(topK * 10, 40);
|
|
6116
6066
|
await client.query(`SET LOCAL hnsw.ef_search = ${efSearch}`);
|
|
6117
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
6118
|
-
const queryOp = metric === "euclidean" ? "<->" : metric === "dotproduct" ? "<#>" : "<=>";
|
|
6119
|
-
const scoreExpr = metric === "euclidean" ? `1 / (1 + (embedding <-> $1::vector))` : metric === "dotproduct" ? `(embedding <#> $1::vector) * -1` : `1 - (embedding <=> $1::vector)`;
|
|
6120
6067
|
const result = await client.query(
|
|
6121
|
-
`SELECT id, content, metadata, $
|
|
6068
|
+
`SELECT id, content, metadata, 1 - (embedding <=> $1::vector) AS score
|
|
6122
6069
|
FROM ${this.tableName}
|
|
6123
6070
|
${whereClause}
|
|
6124
|
-
ORDER BY embedding
|
|
6071
|
+
ORDER BY embedding <=> $1::vector
|
|
6125
6072
|
LIMIT $2`,
|
|
6126
6073
|
params
|
|
6127
6074
|
);
|
package/dist/server.mjs
CHANGED
|
@@ -353,12 +353,10 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
353
353
|
embedding VECTOR(${this.dimensions})
|
|
354
354
|
)
|
|
355
355
|
`);
|
|
356
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
357
|
-
const indexOps = metric === "euclidean" ? "vector_l2_ops" : metric === "dotproduct" ? "vector_ip_ops" : "vector_cosine_ops";
|
|
358
356
|
await client.query(`
|
|
359
357
|
CREATE INDEX IF NOT EXISTS ${this.uploadTable}_embedding_idx
|
|
360
358
|
ON ${this.uploadTable}
|
|
361
|
-
USING hnsw (embedding
|
|
359
|
+
USING hnsw (embedding vector_cosine_ops)
|
|
362
360
|
`);
|
|
363
361
|
const res = await client.query(`
|
|
364
362
|
SELECT table_name
|
|
@@ -420,12 +418,10 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
420
418
|
)
|
|
421
419
|
`;
|
|
422
420
|
await client.query(createTableSql);
|
|
423
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
424
|
-
const indexOps = metric === "euclidean" ? "vector_l2_ops" : metric === "dotproduct" ? "vector_ip_ops" : "vector_cosine_ops";
|
|
425
421
|
await client.query(`
|
|
426
422
|
CREATE INDEX IF NOT EXISTS "${tableName}_embedding_idx"
|
|
427
423
|
ON "${tableName}"
|
|
428
|
-
USING hnsw (embedding
|
|
424
|
+
USING hnsw (embedding vector_cosine_ops)
|
|
429
425
|
`);
|
|
430
426
|
if (!this.tables.includes(tableName)) {
|
|
431
427
|
this.tables.push(tableName);
|
|
@@ -507,8 +503,6 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
507
503
|
try {
|
|
508
504
|
let sqlQuery = "";
|
|
509
505
|
let params = [];
|
|
510
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
511
|
-
const scoreExpr = metric === "euclidean" ? `1 / (1 + (embedding <-> $1::vector))` : metric === "dotproduct" ? `(embedding <#> $1::vector) * -1` : `1 - (embedding <=> $1::vector)`;
|
|
512
506
|
if (queryText) {
|
|
513
507
|
const hasEntityHints = entityHints.length > 0;
|
|
514
508
|
const exactNameScoreExpr = hasEntityHints ? `+ (
|
|
@@ -521,9 +515,9 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
521
515
|
) * 3.0` : "";
|
|
522
516
|
sqlQuery = `
|
|
523
517
|
SELECT *,
|
|
524
|
-
($
|
|
518
|
+
(1 - (embedding <=> $1::vector)) AS vector_score,
|
|
525
519
|
COALESCE(ts_rank(to_tsvector('english', (to_jsonb(t) - 'embedding')::text), to_tsquery('english', $2)), 0) AS keyword_score,
|
|
526
|
-
(($
|
|
520
|
+
((1 - (embedding <=> $1::vector)) + (COALESCE(ts_rank(to_tsvector('english', (to_jsonb(t) - 'embedding')::text), to_tsquery('english', $2)), 0) * 2.0) ${exactNameScoreExpr}) AS hybrid_score
|
|
527
521
|
FROM "${table}" t
|
|
528
522
|
ORDER BY hybrid_score DESC
|
|
529
523
|
LIMIT 50
|
|
@@ -532,7 +526,7 @@ var init_MultiTablePostgresProvider = __esm({
|
|
|
532
526
|
} else {
|
|
533
527
|
sqlQuery = `
|
|
534
528
|
SELECT *,
|
|
535
|
-
($
|
|
529
|
+
(1 - (embedding <=> $1::vector)) AS hybrid_score
|
|
536
530
|
FROM "${table}" t
|
|
537
531
|
ORDER BY hybrid_score DESC
|
|
538
532
|
LIMIT 50
|
|
@@ -1006,13 +1000,11 @@ var init_QdrantProvider = __esm({
|
|
|
1006
1000
|
if (axios4.isAxiosError(err) && ((_a = err.response) == null ? void 0 : _a.status) === 404) {
|
|
1007
1001
|
const opts = this.config.options;
|
|
1008
1002
|
const dimensionsForCreate = opts.dimensions || 1536;
|
|
1009
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
1010
|
-
const distance = metric === "euclidean" ? "Euclid" : metric === "dotproduct" ? "Dot" : "Cosine";
|
|
1011
1003
|
console.log(`[QdrantProvider] \u23F3 Creating collection "${this.indexName}" (dimensions: ${dimensionsForCreate})...`);
|
|
1012
1004
|
await this.http.put(`/collections/${this.indexName}`, {
|
|
1013
1005
|
vectors: {
|
|
1014
1006
|
size: dimensionsForCreate,
|
|
1015
|
-
distance
|
|
1007
|
+
distance: "Cosine"
|
|
1016
1008
|
}
|
|
1017
1009
|
});
|
|
1018
1010
|
console.log(`[QdrantProvider] \u2705 Created collection "${this.indexName}"`);
|
|
@@ -1190,12 +1182,9 @@ var init_ChromaDBProvider = __esm({
|
|
|
1190
1182
|
console.log(`[ChromaDBProvider] \u2705 Collection "${this.indexName}" found (id: ${this.collectionId})`);
|
|
1191
1183
|
} catch (err) {
|
|
1192
1184
|
if (axios5.isAxiosError(err) && ((_a = err.response) == null ? void 0 : _a.status) === 404) {
|
|
1193
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
1194
|
-
const space = metric === "euclidean" ? "l2" : metric === "dotproduct" ? "ip" : "cosine";
|
|
1195
1185
|
console.log(`[ChromaDBProvider] \u23F3 Collection "${this.indexName}" not found. Creating...`);
|
|
1196
1186
|
const { data } = await this.http.post("/api/v1/collections", {
|
|
1197
|
-
name: this.indexName
|
|
1198
|
-
metadata: { "hnsw:space": space }
|
|
1187
|
+
name: this.indexName
|
|
1199
1188
|
});
|
|
1200
1189
|
this.collectionId = data.id;
|
|
1201
1190
|
console.log(`[ChromaDBProvider] \u2705 Created collection "${this.indexName}" (id: ${this.collectionId})`);
|
|
@@ -4184,20 +4173,18 @@ var UITransformer = class {
|
|
|
4184
4173
|
}
|
|
4185
4174
|
const isStockRequest = this.isStockQuery(userQuery);
|
|
4186
4175
|
const filteredData = isStockRequest ? retrievedData.filter((item) => this.determineStockStatus(item)) : retrievedData;
|
|
4187
|
-
const
|
|
4188
|
-
const categories = this.detectCategories(filteredData, groupByField);
|
|
4176
|
+
const categories = this.detectCategories(filteredData);
|
|
4189
4177
|
const hasProducts = filteredData.some((item) => this.isProductData(item));
|
|
4190
4178
|
const isTimeSeries = filteredData.some((item) => this.isTimeSeriesData(item));
|
|
4191
4179
|
const isTrendQuery = this.isTrendQuery(userQuery);
|
|
4192
4180
|
if (isTrendQuery && isTimeSeries) {
|
|
4193
4181
|
return this.transformToLineChart(filteredData);
|
|
4194
4182
|
}
|
|
4195
|
-
|
|
4196
|
-
if (hasProducts && !explicitChartRequest && !this.shouldShowCategoryChart(userQuery)) {
|
|
4183
|
+
if (hasProducts && !this.shouldShowCategoryChart(userQuery, categories)) {
|
|
4197
4184
|
return this.transformToProductCarousel(filteredData, config, trainedSchema);
|
|
4198
4185
|
}
|
|
4199
|
-
if (
|
|
4200
|
-
return this.transformToPieChart(filteredData
|
|
4186
|
+
if (categories.length > 1 && this.shouldShowCategoryChart(userQuery, categories)) {
|
|
4187
|
+
return this.transformToPieChart(filteredData);
|
|
4201
4188
|
}
|
|
4202
4189
|
if (hasProducts) {
|
|
4203
4190
|
return this.transformToProductCarousel(filteredData, config, trainedSchema);
|
|
@@ -4222,12 +4209,11 @@ var UITransformer = class {
|
|
|
4222
4209
|
/**
|
|
4223
4210
|
* Transform data to pie chart format
|
|
4224
4211
|
*/
|
|
4225
|
-
static transformToPieChart(data
|
|
4226
|
-
|
|
4227
|
-
|
|
4228
|
-
const categoryData = this.aggregateByCategory(data, categories, groupByField);
|
|
4212
|
+
static transformToPieChart(data) {
|
|
4213
|
+
const categories = this.detectCategories(data);
|
|
4214
|
+
const categoryData = this.aggregateByCategory(data, categories);
|
|
4229
4215
|
const pieData = Object.entries(categoryData).map(([label, count]) => {
|
|
4230
|
-
const { inStockCount, outOfStockCount } = this.calculateStockCounts(label, data
|
|
4216
|
+
const { inStockCount, outOfStockCount } = this.calculateStockCounts(label, data);
|
|
4231
4217
|
return {
|
|
4232
4218
|
label,
|
|
4233
4219
|
value: count,
|
|
@@ -4237,7 +4223,7 @@ var UITransformer = class {
|
|
|
4237
4223
|
});
|
|
4238
4224
|
return {
|
|
4239
4225
|
type: "pie_chart",
|
|
4240
|
-
title:
|
|
4226
|
+
title: "Distribution by Category",
|
|
4241
4227
|
description: `Showing breakdown across ${categories.length} categories`,
|
|
4242
4228
|
data: pieData
|
|
4243
4229
|
};
|
|
@@ -4460,11 +4446,10 @@ var UITransformer = class {
|
|
|
4460
4446
|
});
|
|
4461
4447
|
return hasTimeKeyword || hasValidDateValue;
|
|
4462
4448
|
}
|
|
4463
|
-
static
|
|
4464
|
-
|
|
4465
|
-
|
|
4466
|
-
|
|
4467
|
-
static shouldShowCategoryChart(query) {
|
|
4449
|
+
static shouldShowCategoryChart(query, categories) {
|
|
4450
|
+
if (categories.length < 2) {
|
|
4451
|
+
return false;
|
|
4452
|
+
}
|
|
4468
4453
|
const normalized = query.toLowerCase();
|
|
4469
4454
|
const chartKeywords = [
|
|
4470
4455
|
"distribution",
|
|
@@ -4477,11 +4462,7 @@ var UITransformer = class {
|
|
|
4477
4462
|
"segmentation",
|
|
4478
4463
|
"split",
|
|
4479
4464
|
"category breakdown",
|
|
4480
|
-
"category distribution"
|
|
4481
|
-
"pie chart",
|
|
4482
|
-
"piechart",
|
|
4483
|
-
"bar chart",
|
|
4484
|
-
"barchart"
|
|
4465
|
+
"category distribution"
|
|
4485
4466
|
];
|
|
4486
4467
|
return chartKeywords.some((keyword) => normalized.includes(keyword));
|
|
4487
4468
|
}
|
|
@@ -4557,67 +4538,44 @@ var UITransformer = class {
|
|
|
4557
4538
|
return null;
|
|
4558
4539
|
}
|
|
4559
4540
|
/**
|
|
4560
|
-
* Helper:
|
|
4561
|
-
*/
|
|
4562
|
-
static determineGroupByField(query, data) {
|
|
4563
|
-
const normalized = query.toLowerCase();
|
|
4564
|
-
const match = normalized.match(/(?:by|across|per|based on)\s+([a-zA-Z]+)/i);
|
|
4565
|
-
let targetField = "category";
|
|
4566
|
-
if (match && match[1]) {
|
|
4567
|
-
let field = match[1].toLowerCase();
|
|
4568
|
-
if (field.endsWith("ies")) field = field.replace(/ies$/, "y");
|
|
4569
|
-
else if (field.endsWith("s") && field !== "status") field = field.slice(0, -1);
|
|
4570
|
-
const stopWords = ["the", "all", "different", "various", "some", "these", "those"];
|
|
4571
|
-
if (!stopWords.includes(field)) {
|
|
4572
|
-
targetField = field;
|
|
4573
|
-
}
|
|
4574
|
-
}
|
|
4575
|
-
const hasTargetField = data.some((d) => d.metadata && d.metadata[targetField] !== void 0);
|
|
4576
|
-
if (!hasTargetField && targetField === "category") {
|
|
4577
|
-
if (data.some((d) => d.metadata && d.metadata["type"] !== void 0)) return "type";
|
|
4578
|
-
if (data.some((d) => d.metadata && d.metadata["brand"] !== void 0)) return "brand";
|
|
4579
|
-
}
|
|
4580
|
-
return targetField;
|
|
4581
|
-
}
|
|
4582
|
-
/**
|
|
4583
|
-
* Helper: Detect grouping values dynamically
|
|
4541
|
+
* Helper: Detect categories in data
|
|
4584
4542
|
*/
|
|
4585
|
-
static detectCategories(data
|
|
4543
|
+
static detectCategories(data) {
|
|
4586
4544
|
const categories = /* @__PURE__ */ new Set();
|
|
4587
4545
|
data.forEach((item) => {
|
|
4588
4546
|
const meta = item.metadata || {};
|
|
4589
|
-
if (meta
|
|
4590
|
-
|
|
4591
|
-
|
|
4592
|
-
|
|
4593
|
-
|
|
4594
|
-
|
|
4595
|
-
|
|
4596
|
-
|
|
4597
|
-
|
|
4598
|
-
|
|
4599
|
-
|
|
4600
|
-
|
|
4547
|
+
if (meta.category) {
|
|
4548
|
+
categories.add(String(meta.category));
|
|
4549
|
+
}
|
|
4550
|
+
if (meta.type) {
|
|
4551
|
+
categories.add(String(meta.type));
|
|
4552
|
+
}
|
|
4553
|
+
if (meta.tag) {
|
|
4554
|
+
const tags = Array.isArray(meta.tag) ? meta.tag : [meta.tag];
|
|
4555
|
+
tags.forEach((t) => categories.add(String(t)));
|
|
4556
|
+
}
|
|
4557
|
+
const contentCategories = Array.from(new Set(
|
|
4558
|
+
Array.from(item.content.matchAll(/^\s*([^:\n]+):\s*(?:•|\*|-)*/gm)).map((match) => match[1].trim()).filter(Boolean)
|
|
4559
|
+
));
|
|
4560
|
+
contentCategories.forEach((category) => categories.add(category));
|
|
4561
|
+
const categoryMatch = item.content.match(/(?:Category|Type|Class):\s*([^\n]+)/i);
|
|
4562
|
+
if (categoryMatch) {
|
|
4563
|
+
categories.add(categoryMatch[1].trim());
|
|
4601
4564
|
}
|
|
4602
4565
|
});
|
|
4603
4566
|
return Array.from(categories);
|
|
4604
4567
|
}
|
|
4605
4568
|
/**
|
|
4606
|
-
* Helper: Aggregate data
|
|
4569
|
+
* Helper: Aggregate data by category
|
|
4607
4570
|
*/
|
|
4608
|
-
static aggregateByCategory(data, categories
|
|
4571
|
+
static aggregateByCategory(data, categories) {
|
|
4609
4572
|
const result = {};
|
|
4610
4573
|
categories.forEach((cat) => {
|
|
4611
4574
|
result[cat] = 0;
|
|
4612
4575
|
});
|
|
4613
4576
|
data.forEach((item) => {
|
|
4614
4577
|
const meta = item.metadata || {};
|
|
4615
|
-
|
|
4616
|
-
if (meta[groupByField] !== void 0) {
|
|
4617
|
-
itemCategory = String(meta[groupByField]);
|
|
4618
|
-
} else if (groupByField === "category" && meta.type) {
|
|
4619
|
-
itemCategory = String(meta.type);
|
|
4620
|
-
}
|
|
4578
|
+
const itemCategory = meta.category || meta.type || "Other";
|
|
4621
4579
|
if (Object.prototype.hasOwnProperty.call(result, itemCategory)) {
|
|
4622
4580
|
result[itemCategory]++;
|
|
4623
4581
|
} else {
|
|
@@ -4667,19 +4625,14 @@ var UITransformer = class {
|
|
|
4667
4625
|
});
|
|
4668
4626
|
}
|
|
4669
4627
|
/**
|
|
4670
|
-
* Helper: Calculate stock counts
|
|
4628
|
+
* Helper: Calculate stock counts by category
|
|
4671
4629
|
*/
|
|
4672
|
-
static calculateStockCounts(category, data
|
|
4630
|
+
static calculateStockCounts(category, data) {
|
|
4673
4631
|
let inStock = 0;
|
|
4674
4632
|
let outOfStock = 0;
|
|
4675
4633
|
data.forEach((d) => {
|
|
4676
4634
|
const meta = d.metadata || {};
|
|
4677
|
-
|
|
4678
|
-
if (meta[groupByField] !== void 0) {
|
|
4679
|
-
itemCategory = String(meta[groupByField]);
|
|
4680
|
-
} else if (groupByField === "category" && meta.type) {
|
|
4681
|
-
itemCategory = String(meta.type);
|
|
4682
|
-
}
|
|
4635
|
+
const itemCategory = meta.category || meta.type || "Other";
|
|
4683
4636
|
if (itemCategory === category) {
|
|
4684
4637
|
if (this.determineStockStatus(d)) {
|
|
4685
4638
|
inStock++;
|
|
@@ -4804,13 +4757,12 @@ DATA SHAPE per type:
|
|
|
4804
4757
|
- text: { "content": "<prose answer>" }
|
|
4805
4758
|
|
|
4806
4759
|
DECISION RULES (follow strictly):
|
|
4807
|
-
1.
|
|
4808
|
-
2.
|
|
4809
|
-
3.
|
|
4810
|
-
4.
|
|
4811
|
-
5.
|
|
4812
|
-
6.
|
|
4813
|
-
7. text \u2192 conversational or free-form answers where no chart adds value
|
|
4760
|
+
1. bar_chart \u2192 comparing quantities across categories (e.g. sales by region, price by product). Use this when there is only ONE value per category.
|
|
4761
|
+
2. line_chart \u2192 trends or changes over time (dates, months, years, sequential events)
|
|
4762
|
+
3. pie_chart \u2192 proportional breakdown or percentage distribution
|
|
4763
|
+
4. radar_chart \u2192 comparing 2 or more products across MULTIPLE attributes (e.g. comparing features, ratings, or characteristics of 2 specific products). If the user asks to "compare" products and the data contains multiple dimensions or ratings for them, you MUST use this.
|
|
4764
|
+
5. table \u2192 multi-field structured records where each item has \u2265 3 attributes
|
|
4765
|
+
6. text \u2192 conversational or free-form answers where no chart adds value
|
|
4814
4766
|
|
|
4815
4767
|
IMPORTANT:
|
|
4816
4768
|
- Aggregate numeric values from the raw data \u2014 do not pass raw object arrays as data.
|
|
@@ -5968,12 +5920,10 @@ var PostgreSQLProvider = class extends BaseVectorProvider {
|
|
|
5968
5920
|
embedding VECTOR(${this.dimensions})
|
|
5969
5921
|
)
|
|
5970
5922
|
`);
|
|
5971
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
5972
|
-
const indexOps = metric === "euclidean" ? "vector_l2_ops" : metric === "dotproduct" ? "vector_ip_ops" : "vector_cosine_ops";
|
|
5973
5923
|
await client.query(`
|
|
5974
5924
|
CREATE INDEX IF NOT EXISTS ${this.tableName}_embedding_idx
|
|
5975
5925
|
ON ${this.tableName}
|
|
5976
|
-
USING hnsw (embedding
|
|
5926
|
+
USING hnsw (embedding vector_cosine_ops)
|
|
5977
5927
|
`);
|
|
5978
5928
|
} finally {
|
|
5979
5929
|
client.release();
|
|
@@ -6045,14 +5995,11 @@ var PostgreSQLProvider = class extends BaseVectorProvider {
|
|
|
6045
5995
|
try {
|
|
6046
5996
|
const efSearch = this.config.options.efSearch || Math.max(topK * 10, 40);
|
|
6047
5997
|
await client.query(`SET LOCAL hnsw.ef_search = ${efSearch}`);
|
|
6048
|
-
const metric = this.config.distanceMetric || "cosine";
|
|
6049
|
-
const queryOp = metric === "euclidean" ? "<->" : metric === "dotproduct" ? "<#>" : "<=>";
|
|
6050
|
-
const scoreExpr = metric === "euclidean" ? `1 / (1 + (embedding <-> $1::vector))` : metric === "dotproduct" ? `(embedding <#> $1::vector) * -1` : `1 - (embedding <=> $1::vector)`;
|
|
6051
5998
|
const result = await client.query(
|
|
6052
|
-
`SELECT id, content, metadata, $
|
|
5999
|
+
`SELECT id, content, metadata, 1 - (embedding <=> $1::vector) AS score
|
|
6053
6000
|
FROM ${this.tableName}
|
|
6054
6001
|
${whereClause}
|
|
6055
|
-
ORDER BY embedding
|
|
6002
|
+
ORDER BY embedding <=> $1::vector
|
|
6056
6003
|
LIMIT $2`,
|
|
6057
6004
|
params
|
|
6058
6005
|
);
|