langchain 0.0.127 → 0.0.128
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/agents/toolkits/conversational_retrieval/tool.cjs +8 -2
- package/dist/agents/toolkits/conversational_retrieval/tool.d.ts +9 -2
- package/dist/agents/toolkits/conversational_retrieval/tool.js +9 -3
- package/dist/chains/query_constructor/ir.d.ts +5 -4
- package/dist/chains/query_constructor/prompt.cjs +3 -1
- package/dist/chains/query_constructor/prompt.d.ts +2 -2
- package/dist/chains/query_constructor/prompt.js +3 -1
- package/dist/chains/question_answering/refine_prompts.cjs +2 -2
- package/dist/chains/question_answering/refine_prompts.d.ts +1 -1
- package/dist/chains/question_answering/refine_prompts.js +2 -2
- package/dist/chains/sql_db/index.cjs +2 -1
- package/dist/chains/sql_db/index.d.ts +1 -1
- package/dist/chains/sql_db/index.js +1 -1
- package/dist/chains/sql_db/sql_db_prompt.cjs +21 -1
- package/dist/chains/sql_db/sql_db_prompt.d.ts +6 -0
- package/dist/chains/sql_db/sql_db_prompt.js +20 -0
- package/dist/chat_models/openai.cjs +8 -0
- package/dist/chat_models/openai.d.ts +1 -0
- package/dist/chat_models/openai.js +8 -0
- package/dist/document_loaders/web/github.cjs +104 -32
- package/dist/document_loaders/web/github.d.ts +46 -3
- package/dist/document_loaders/web/github.js +104 -32
- package/dist/document_transformers/mozilla_readability.cjs +30 -0
- package/dist/document_transformers/mozilla_readability.d.ts +8 -0
- package/dist/document_transformers/mozilla_readability.js +26 -0
- package/dist/llms/base.cjs +3 -1
- package/dist/llms/base.js +3 -1
- package/dist/llms/bedrock.cjs +152 -0
- package/dist/llms/bedrock.d.ts +54 -0
- package/dist/llms/bedrock.js +148 -0
- package/dist/llms/openai-chat.cjs +8 -0
- package/dist/llms/openai-chat.d.ts +1 -0
- package/dist/llms/openai-chat.js +8 -0
- package/dist/llms/openai.cjs +8 -0
- package/dist/llms/openai.d.ts +1 -0
- package/dist/llms/openai.js +8 -0
- package/dist/load/import_constants.cjs +2 -0
- package/dist/load/import_constants.js +2 -0
- package/dist/retrievers/self_query/base.cjs +31 -0
- package/dist/retrievers/self_query/base.d.ts +5 -2
- package/dist/retrievers/self_query/base.js +31 -0
- package/dist/retrievers/self_query/chroma.d.ts +2 -1
- package/dist/retrievers/self_query/functional.cjs +28 -1
- package/dist/retrievers/self_query/functional.d.ts +3 -0
- package/dist/retrievers/self_query/functional.js +28 -1
- package/dist/retrievers/self_query/index.cjs +18 -4
- package/dist/retrievers/self_query/index.d.ts +14 -10
- package/dist/retrievers/self_query/index.js +18 -4
- package/dist/retrievers/self_query/pinecone.d.ts +2 -1
- package/dist/retrievers/self_query/supabase.cjs +30 -0
- package/dist/retrievers/self_query/supabase.d.ts +3 -5
- package/dist/retrievers/self_query/supabase.js +30 -0
- package/dist/retrievers/self_query/utils.cjs +21 -0
- package/dist/retrievers/self_query/utils.d.ts +2 -0
- package/dist/retrievers/self_query/utils.js +16 -0
- package/dist/retrievers/self_query/weaviate.cjs +67 -16
- package/dist/retrievers/self_query/weaviate.d.ts +6 -3
- package/dist/retrievers/self_query/weaviate.js +67 -16
- package/dist/types/openai-types.d.ts +2 -0
- package/dist/util/event-source-parse.cjs +22 -6
- package/dist/util/event-source-parse.js +22 -6
- package/dist/util/ollama.cjs +16 -2
- package/dist/util/ollama.js +16 -2
- package/dist/util/sql_utils.cjs +38 -3
- package/dist/util/sql_utils.js +39 -4
- package/dist/vectorstores/chroma.cjs +5 -6
- package/dist/vectorstores/chroma.js +5 -6
- package/dist/vectorstores/faiss.cjs +41 -1
- package/dist/vectorstores/faiss.d.ts +6 -0
- package/dist/vectorstores/faiss.js +41 -1
- package/dist/vectorstores/supabase.cjs +10 -4
- package/dist/vectorstores/supabase.d.ts +4 -2
- package/dist/vectorstores/supabase.js +10 -4
- package/document_transformers/mozilla_readability.cjs +1 -0
- package/document_transformers/mozilla_readability.d.ts +1 -0
- package/document_transformers/mozilla_readability.js +1 -0
- package/llms/bedrock.cjs +1 -0
- package/llms/bedrock.d.ts +1 -0
- package/llms/bedrock.js +1 -0
- package/package.json +46 -3
|
@@ -3,6 +3,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.WeaviateTranslator = void 0;
|
|
4
4
|
const ir_js_1 = require("../../chains/query_constructor/ir.cjs");
|
|
5
5
|
const base_js_1 = require("./base.cjs");
|
|
6
|
+
const utils_js_1 = require("./utils.cjs");
|
|
7
|
+
function isInt(value) {
|
|
8
|
+
const numberValue = parseFloat(value);
|
|
9
|
+
return !Number.isNaN(numberValue) && numberValue % 1 === 0;
|
|
10
|
+
}
|
|
11
|
+
function isFloat(value) {
|
|
12
|
+
const numberValue = parseFloat(value);
|
|
13
|
+
return !Number.isNaN(numberValue) && numberValue % 1 !== 0;
|
|
14
|
+
}
|
|
15
|
+
function isString(value) {
|
|
16
|
+
return typeof value === "string" && Number.isNaN(parseFloat(value));
|
|
17
|
+
}
|
|
6
18
|
class WeaviateTranslator extends base_js_1.BaseTranslator {
|
|
7
19
|
constructor() {
|
|
8
20
|
super(...arguments);
|
|
@@ -62,28 +74,26 @@ class WeaviateTranslator extends base_js_1.BaseTranslator {
|
|
|
62
74
|
};
|
|
63
75
|
}
|
|
64
76
|
visitComparison(comparison) {
|
|
65
|
-
if (
|
|
77
|
+
if (isString(comparison.value)) {
|
|
66
78
|
return {
|
|
67
79
|
path: [comparison.attribute],
|
|
68
80
|
operator: this.formatFunction(comparison.comparator),
|
|
69
81
|
valueText: comparison.value,
|
|
70
82
|
};
|
|
71
83
|
}
|
|
72
|
-
if (
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
};
|
|
86
|
-
}
|
|
84
|
+
if (isInt(comparison.value)) {
|
|
85
|
+
return {
|
|
86
|
+
path: [comparison.attribute],
|
|
87
|
+
operator: this.formatFunction(comparison.comparator),
|
|
88
|
+
valueInt: parseInt(comparison.value, 10),
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
if (isFloat(comparison.value)) {
|
|
92
|
+
return {
|
|
93
|
+
path: [comparison.attribute],
|
|
94
|
+
operator: this.formatFunction(comparison.comparator),
|
|
95
|
+
valueNumber: parseFloat(comparison.value),
|
|
96
|
+
};
|
|
87
97
|
}
|
|
88
98
|
throw new Error("Value type is not supported");
|
|
89
99
|
}
|
|
@@ -96,5 +106,46 @@ class WeaviateTranslator extends base_js_1.BaseTranslator {
|
|
|
96
106
|
}
|
|
97
107
|
return nextArg;
|
|
98
108
|
}
|
|
109
|
+
mergeFilters(defaultFilter, generatedFilter, mergeType = "and") {
|
|
110
|
+
if ((0, utils_js_1.isFilterEmpty)(defaultFilter?.where) &&
|
|
111
|
+
(0, utils_js_1.isFilterEmpty)(generatedFilter?.where)) {
|
|
112
|
+
return undefined;
|
|
113
|
+
}
|
|
114
|
+
if ((0, utils_js_1.isFilterEmpty)(defaultFilter?.where) || mergeType === "replace") {
|
|
115
|
+
if ((0, utils_js_1.isFilterEmpty)(generatedFilter?.where)) {
|
|
116
|
+
return undefined;
|
|
117
|
+
}
|
|
118
|
+
return generatedFilter;
|
|
119
|
+
}
|
|
120
|
+
if ((0, utils_js_1.isFilterEmpty)(generatedFilter?.where)) {
|
|
121
|
+
if (mergeType === "and") {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
return defaultFilter;
|
|
125
|
+
}
|
|
126
|
+
const merged = {
|
|
127
|
+
operator: "And",
|
|
128
|
+
operands: [
|
|
129
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
130
|
+
defaultFilter.where,
|
|
131
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
132
|
+
generatedFilter.where,
|
|
133
|
+
],
|
|
134
|
+
};
|
|
135
|
+
if (mergeType === "and") {
|
|
136
|
+
return {
|
|
137
|
+
where: merged,
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
else if (mergeType === "or") {
|
|
141
|
+
merged.operator = "Or";
|
|
142
|
+
return {
|
|
143
|
+
where: merged,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
throw new Error("Unknown merge type");
|
|
148
|
+
}
|
|
149
|
+
}
|
|
99
150
|
}
|
|
100
151
|
exports.WeaviateTranslator = WeaviateTranslator;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Comparator, Comparison, Operation, Operator, StructuredQuery } from "../../chains/query_constructor/ir.js";
|
|
2
|
+
import { WeaviateFilter, WeaviateStore } from "../../vectorstores/weaviate.js";
|
|
2
3
|
import { BaseTranslator } from "./base.js";
|
|
3
4
|
type WeaviateOperatorValues = {
|
|
4
5
|
valueText: string;
|
|
@@ -24,17 +25,19 @@ export type WeaviateComparisonResult = {
|
|
|
24
25
|
operator: string;
|
|
25
26
|
} & ExclusiveOperatorValue;
|
|
26
27
|
export type WeaviateStructuredQueryResult = {
|
|
27
|
-
filter?:
|
|
28
|
+
filter?: {
|
|
29
|
+
where?: WeaviateComparisonResult | WeaviateOperationResult;
|
|
30
|
+
};
|
|
28
31
|
};
|
|
29
|
-
export declare class WeaviateTranslator extends BaseTranslator {
|
|
32
|
+
export declare class WeaviateTranslator<T extends WeaviateStore> extends BaseTranslator<T> {
|
|
30
33
|
VisitOperationOutput: WeaviateOperationResult;
|
|
31
34
|
VisitComparisonOutput: WeaviateComparisonResult;
|
|
32
|
-
VisitStructuredQueryOutput: WeaviateStructuredQueryResult;
|
|
33
35
|
allowedOperators: Operator[];
|
|
34
36
|
allowedComparators: Comparator[];
|
|
35
37
|
formatFunction(func: Operator | Comparator): string;
|
|
36
38
|
visitOperation(operation: Operation): this["VisitOperationOutput"];
|
|
37
39
|
visitComparison(comparison: Comparison): this["VisitComparisonOutput"];
|
|
38
40
|
visitStructuredQuery(query: StructuredQuery): this["VisitStructuredQueryOutput"];
|
|
41
|
+
mergeFilters(defaultFilter: WeaviateFilter | undefined, generatedFilter: WeaviateFilter | undefined, mergeType?: string): WeaviateFilter | undefined;
|
|
39
42
|
}
|
|
40
43
|
export {};
|
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
import { Comparators, Operators, } from "../../chains/query_constructor/ir.js";
|
|
2
2
|
import { BaseTranslator } from "./base.js";
|
|
3
|
+
import { isFilterEmpty } from "./utils.js";
|
|
4
|
+
function isInt(value) {
|
|
5
|
+
const numberValue = parseFloat(value);
|
|
6
|
+
return !Number.isNaN(numberValue) && numberValue % 1 === 0;
|
|
7
|
+
}
|
|
8
|
+
function isFloat(value) {
|
|
9
|
+
const numberValue = parseFloat(value);
|
|
10
|
+
return !Number.isNaN(numberValue) && numberValue % 1 !== 0;
|
|
11
|
+
}
|
|
12
|
+
function isString(value) {
|
|
13
|
+
return typeof value === "string" && Number.isNaN(parseFloat(value));
|
|
14
|
+
}
|
|
3
15
|
export class WeaviateTranslator extends BaseTranslator {
|
|
4
16
|
constructor() {
|
|
5
17
|
super(...arguments);
|
|
@@ -59,28 +71,26 @@ export class WeaviateTranslator extends BaseTranslator {
|
|
|
59
71
|
};
|
|
60
72
|
}
|
|
61
73
|
visitComparison(comparison) {
|
|
62
|
-
if (
|
|
74
|
+
if (isString(comparison.value)) {
|
|
63
75
|
return {
|
|
64
76
|
path: [comparison.attribute],
|
|
65
77
|
operator: this.formatFunction(comparison.comparator),
|
|
66
78
|
valueText: comparison.value,
|
|
67
79
|
};
|
|
68
80
|
}
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
};
|
|
83
|
-
}
|
|
81
|
+
if (isInt(comparison.value)) {
|
|
82
|
+
return {
|
|
83
|
+
path: [comparison.attribute],
|
|
84
|
+
operator: this.formatFunction(comparison.comparator),
|
|
85
|
+
valueInt: parseInt(comparison.value, 10),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
if (isFloat(comparison.value)) {
|
|
89
|
+
return {
|
|
90
|
+
path: [comparison.attribute],
|
|
91
|
+
operator: this.formatFunction(comparison.comparator),
|
|
92
|
+
valueNumber: parseFloat(comparison.value),
|
|
93
|
+
};
|
|
84
94
|
}
|
|
85
95
|
throw new Error("Value type is not supported");
|
|
86
96
|
}
|
|
@@ -93,4 +103,45 @@ export class WeaviateTranslator extends BaseTranslator {
|
|
|
93
103
|
}
|
|
94
104
|
return nextArg;
|
|
95
105
|
}
|
|
106
|
+
mergeFilters(defaultFilter, generatedFilter, mergeType = "and") {
|
|
107
|
+
if (isFilterEmpty(defaultFilter?.where) &&
|
|
108
|
+
isFilterEmpty(generatedFilter?.where)) {
|
|
109
|
+
return undefined;
|
|
110
|
+
}
|
|
111
|
+
if (isFilterEmpty(defaultFilter?.where) || mergeType === "replace") {
|
|
112
|
+
if (isFilterEmpty(generatedFilter?.where)) {
|
|
113
|
+
return undefined;
|
|
114
|
+
}
|
|
115
|
+
return generatedFilter;
|
|
116
|
+
}
|
|
117
|
+
if (isFilterEmpty(generatedFilter?.where)) {
|
|
118
|
+
if (mergeType === "and") {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
return defaultFilter;
|
|
122
|
+
}
|
|
123
|
+
const merged = {
|
|
124
|
+
operator: "And",
|
|
125
|
+
operands: [
|
|
126
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
127
|
+
defaultFilter.where,
|
|
128
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
129
|
+
generatedFilter.where,
|
|
130
|
+
],
|
|
131
|
+
};
|
|
132
|
+
if (mergeType === "and") {
|
|
133
|
+
return {
|
|
134
|
+
where: merged,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
else if (mergeType === "or") {
|
|
138
|
+
merged.operator = "Or";
|
|
139
|
+
return {
|
|
140
|
+
where: merged,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
throw new Error("Unknown merge type");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
96
147
|
}
|
|
@@ -19,6 +19,8 @@ export declare interface OpenAIBaseInput {
|
|
|
19
19
|
n: number;
|
|
20
20
|
/** Dictionary used to adjust the probability of specific tokens being generated */
|
|
21
21
|
logitBias?: Record<string, number>;
|
|
22
|
+
/** Unique string identifier representing your end-user, which can help OpenAI to monitor and detect abuse. */
|
|
23
|
+
user?: string;
|
|
22
24
|
/** Whether to stream the results or not. Enabling disables tokenUsage reporting */
|
|
23
25
|
streaming: boolean;
|
|
24
26
|
/** Model name to use */
|
|
@@ -1,13 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable prefer-template */
|
|
3
|
-
/* eslint-disable default-case */
|
|
4
|
-
/* eslint-disable no-plusplus */
|
|
5
|
-
// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts
|
|
6
|
-
// due to a packaging issue in the original.
|
|
7
|
-
// MIT License
|
|
8
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
3
|
exports.getMessages = exports.getLines = exports.getBytes = exports.EventStreamContentType = void 0;
|
|
10
4
|
exports.EventStreamContentType = "text/event-stream";
|
|
5
|
+
function isNodeJSReadable(x) {
|
|
6
|
+
return x != null && typeof x === "object" && "on" in x;
|
|
7
|
+
}
|
|
11
8
|
/**
|
|
12
9
|
* Converts a ReadableStream into a callback pattern.
|
|
13
10
|
* @param stream The input ReadableStream.
|
|
@@ -15,6 +12,25 @@ exports.EventStreamContentType = "text/event-stream";
|
|
|
15
12
|
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
|
|
16
13
|
*/
|
|
17
14
|
async function getBytes(stream, onChunk) {
|
|
15
|
+
// stream is a Node.js Readable / PassThrough stream
|
|
16
|
+
// this can happen if node-fetch is polyfilled
|
|
17
|
+
if (isNodeJSReadable(stream)) {
|
|
18
|
+
return new Promise((resolve) => {
|
|
19
|
+
stream.on("readable", () => {
|
|
20
|
+
let chunk;
|
|
21
|
+
// eslint-disable-next-line no-constant-condition
|
|
22
|
+
while (true) {
|
|
23
|
+
chunk = stream.read();
|
|
24
|
+
if (chunk == null) {
|
|
25
|
+
onChunk(new Uint8Array(), true);
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
onChunk(chunk);
|
|
29
|
+
}
|
|
30
|
+
resolve();
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
}
|
|
18
34
|
const reader = stream.getReader();
|
|
19
35
|
// CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
|
|
20
36
|
// This change is essential to ensure that we capture every last piece of information from streams,
|
|
@@ -1,10 +1,7 @@
|
|
|
1
|
-
/* eslint-disable prefer-template */
|
|
2
|
-
/* eslint-disable default-case */
|
|
3
|
-
/* eslint-disable no-plusplus */
|
|
4
|
-
// Adapted from https://github.com/gfortaine/fetch-event-source/blob/main/src/parse.ts
|
|
5
|
-
// due to a packaging issue in the original.
|
|
6
|
-
// MIT License
|
|
7
1
|
export const EventStreamContentType = "text/event-stream";
|
|
2
|
+
function isNodeJSReadable(x) {
|
|
3
|
+
return x != null && typeof x === "object" && "on" in x;
|
|
4
|
+
}
|
|
8
5
|
/**
|
|
9
6
|
* Converts a ReadableStream into a callback pattern.
|
|
10
7
|
* @param stream The input ReadableStream.
|
|
@@ -12,6 +9,25 @@ export const EventStreamContentType = "text/event-stream";
|
|
|
12
9
|
* @returns {Promise<void>} A promise that will be resolved when the stream closes.
|
|
13
10
|
*/
|
|
14
11
|
export async function getBytes(stream, onChunk) {
|
|
12
|
+
// stream is a Node.js Readable / PassThrough stream
|
|
13
|
+
// this can happen if node-fetch is polyfilled
|
|
14
|
+
if (isNodeJSReadable(stream)) {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
stream.on("readable", () => {
|
|
17
|
+
let chunk;
|
|
18
|
+
// eslint-disable-next-line no-constant-condition
|
|
19
|
+
while (true) {
|
|
20
|
+
chunk = stream.read();
|
|
21
|
+
if (chunk == null) {
|
|
22
|
+
onChunk(new Uint8Array(), true);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
onChunk(chunk);
|
|
26
|
+
}
|
|
27
|
+
resolve();
|
|
28
|
+
});
|
|
29
|
+
});
|
|
30
|
+
}
|
|
15
31
|
const reader = stream.getReader();
|
|
16
32
|
// CHANGED: Introduced a "flush" mechanism to process potential pending messages when the stream ends.
|
|
17
33
|
// This change is essential to ensure that we capture every last piece of information from streams,
|
package/dist/util/ollama.cjs
CHANGED
|
@@ -3,7 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createOllamaStream = void 0;
|
|
4
4
|
const stream_js_1 = require("./stream.cjs");
|
|
5
5
|
async function* createOllamaStream(baseUrl, params, options) {
|
|
6
|
-
|
|
6
|
+
let formattedBaseUrl = baseUrl;
|
|
7
|
+
if (formattedBaseUrl.startsWith("http://localhost:")) {
|
|
8
|
+
// Node 18 has issues with resolving "localhost"
|
|
9
|
+
// See https://github.com/node-fetch/node-fetch/issues/1624
|
|
10
|
+
formattedBaseUrl = formattedBaseUrl.replace("http://localhost:", "http://127.0.0.1:");
|
|
11
|
+
}
|
|
12
|
+
const response = await fetch(`${formattedBaseUrl}/api/generate`, {
|
|
7
13
|
method: "POST",
|
|
8
14
|
body: JSON.stringify(params),
|
|
9
15
|
headers: {
|
|
@@ -25,7 +31,15 @@ async function* createOllamaStream(baseUrl, params, options) {
|
|
|
25
31
|
const decoder = new TextDecoder();
|
|
26
32
|
for await (const chunk of stream) {
|
|
27
33
|
try {
|
|
28
|
-
|
|
34
|
+
if (chunk !== undefined) {
|
|
35
|
+
const lines = decoder
|
|
36
|
+
.decode(chunk)
|
|
37
|
+
.split("\n")
|
|
38
|
+
.filter((v) => v.length);
|
|
39
|
+
for (const line of lines) {
|
|
40
|
+
yield JSON.parse(line);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
29
43
|
}
|
|
30
44
|
catch (e) {
|
|
31
45
|
console.warn(`Received a non-JSON parseable chunk: ${decoder.decode(chunk)}`);
|
package/dist/util/ollama.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { IterableReadableStream } from "./stream.js";
|
|
2
2
|
export async function* createOllamaStream(baseUrl, params, options) {
|
|
3
|
-
|
|
3
|
+
let formattedBaseUrl = baseUrl;
|
|
4
|
+
if (formattedBaseUrl.startsWith("http://localhost:")) {
|
|
5
|
+
// Node 18 has issues with resolving "localhost"
|
|
6
|
+
// See https://github.com/node-fetch/node-fetch/issues/1624
|
|
7
|
+
formattedBaseUrl = formattedBaseUrl.replace("http://localhost:", "http://127.0.0.1:");
|
|
8
|
+
}
|
|
9
|
+
const response = await fetch(`${formattedBaseUrl}/api/generate`, {
|
|
4
10
|
method: "POST",
|
|
5
11
|
body: JSON.stringify(params),
|
|
6
12
|
headers: {
|
|
@@ -22,7 +28,15 @@ export async function* createOllamaStream(baseUrl, params, options) {
|
|
|
22
28
|
const decoder = new TextDecoder();
|
|
23
29
|
for await (const chunk of stream) {
|
|
24
30
|
try {
|
|
25
|
-
|
|
31
|
+
if (chunk !== undefined) {
|
|
32
|
+
const lines = decoder
|
|
33
|
+
.decode(chunk)
|
|
34
|
+
.split("\n")
|
|
35
|
+
.filter((v) => v.length);
|
|
36
|
+
for (const line of lines) {
|
|
37
|
+
yield JSON.parse(line);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
26
40
|
}
|
|
27
41
|
catch (e) {
|
|
28
42
|
console.warn(`Received a non-JSON parseable chunk: ${decoder.decode(chunk)}`);
|
package/dist/util/sql_utils.cjs
CHANGED
|
@@ -107,6 +107,25 @@ const getTableAndColumnsName = async (appDataSource) => {
|
|
|
107
107
|
const rep = await appDataSource.query(sql);
|
|
108
108
|
return formatToSqlTable(rep);
|
|
109
109
|
}
|
|
110
|
+
if (appDataSource.options.type === "sap") {
|
|
111
|
+
const schema = appDataSource.options?.schema ?? "public";
|
|
112
|
+
sql = `SELECT
|
|
113
|
+
TABLE_NAME,
|
|
114
|
+
COLUMN_NAME,
|
|
115
|
+
DATA_TYPE_NAME AS data_type,
|
|
116
|
+
CASE WHEN IS_NULLABLE='TRUE' THEN 'YES' ELSE 'NO' END AS is_nullable
|
|
117
|
+
FROM TABLE_COLUMNS
|
|
118
|
+
WHERE SCHEMA_NAME='${schema}'`;
|
|
119
|
+
const rep = await appDataSource.query(sql);
|
|
120
|
+
const repLowerCase = [];
|
|
121
|
+
rep.forEach((_rep) => repLowerCase.push({
|
|
122
|
+
table_name: _rep.TABLE_NAME,
|
|
123
|
+
column_name: _rep.COLUMN_NAME,
|
|
124
|
+
data_type: _rep.DATA_TYPE,
|
|
125
|
+
is_nullable: _rep.IS_NULLABLE,
|
|
126
|
+
}));
|
|
127
|
+
return formatToSqlTable(repLowerCase);
|
|
128
|
+
}
|
|
110
129
|
throw new Error("Database type not implemented yet");
|
|
111
130
|
};
|
|
112
131
|
exports.getTableAndColumnsName = getTableAndColumnsName;
|
|
@@ -127,9 +146,16 @@ const generateTableInfoFromTables = async (tables, appDataSource, nbSampleRow) =
|
|
|
127
146
|
let globalString = "";
|
|
128
147
|
for (const currentTable of tables) {
|
|
129
148
|
// Add the creation of the table in SQL
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
149
|
+
let schema = null;
|
|
150
|
+
if (appDataSource.options.type === "postgres") {
|
|
151
|
+
schema = appDataSource.options?.schema ?? "public";
|
|
152
|
+
}
|
|
153
|
+
else if (appDataSource.options.type === "sap") {
|
|
154
|
+
schema =
|
|
155
|
+
appDataSource.options?.schema ??
|
|
156
|
+
appDataSource.options?.username ??
|
|
157
|
+
"public";
|
|
158
|
+
}
|
|
133
159
|
let sqlCreateTableQuery = schema
|
|
134
160
|
? `CREATE TABLE "${schema}"."${currentTable.tableName}" (\n`
|
|
135
161
|
: `CREATE TABLE ${currentTable.tableName} (\n`;
|
|
@@ -152,6 +178,12 @@ const generateTableInfoFromTables = async (tables, appDataSource, nbSampleRow) =
|
|
|
152
178
|
else if (appDataSource.options.type === "mssql") {
|
|
153
179
|
sqlSelectInfoQuery = `SELECT TOP ${nbSampleRow} * FROM [${currentTable.tableName}];\n`;
|
|
154
180
|
}
|
|
181
|
+
else if (appDataSource.options.type === "sap") {
|
|
182
|
+
const schema = appDataSource.options?.schema ??
|
|
183
|
+
appDataSource.options?.username ??
|
|
184
|
+
"public";
|
|
185
|
+
sqlSelectInfoQuery = `SELECT * FROM "${schema}"."${currentTable.tableName}" LIMIT ${nbSampleRow};\n`;
|
|
186
|
+
}
|
|
155
187
|
else {
|
|
156
188
|
sqlSelectInfoQuery = `SELECT * FROM "${currentTable.tableName}" LIMIT ${nbSampleRow};\n`;
|
|
157
189
|
}
|
|
@@ -188,6 +220,9 @@ const getPromptTemplateFromDataSource = (appDataSource) => {
|
|
|
188
220
|
if (appDataSource.options.type === "mssql") {
|
|
189
221
|
return sql_db_prompt_js_1.SQL_MSSQL_PROMPT;
|
|
190
222
|
}
|
|
223
|
+
if (appDataSource.options.type === "sap") {
|
|
224
|
+
return sql_db_prompt_js_1.SQL_SAP_HANA_PROMPT;
|
|
225
|
+
}
|
|
191
226
|
return sql_db_prompt_js_1.DEFAULT_SQL_DATABASE_PROMPT;
|
|
192
227
|
};
|
|
193
228
|
exports.getPromptTemplateFromDataSource = getPromptTemplateFromDataSource;
|
package/dist/util/sql_utils.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { DEFAULT_SQL_DATABASE_PROMPT, SQL_MSSQL_PROMPT, SQL_MYSQL_PROMPT, SQL_POSTGRES_PROMPT, SQL_SQLITE_PROMPT, } from "../chains/sql_db/sql_db_prompt.js";
|
|
1
|
+
import { DEFAULT_SQL_DATABASE_PROMPT, SQL_SAP_HANA_PROMPT, SQL_MSSQL_PROMPT, SQL_MYSQL_PROMPT, SQL_POSTGRES_PROMPT, SQL_SQLITE_PROMPT, } from "../chains/sql_db/sql_db_prompt.js";
|
|
2
2
|
export const verifyListTablesExistInDatabase = (tablesFromDatabase, listTables, errorPrefixMsg) => {
|
|
3
3
|
const onlyTableNames = tablesFromDatabase.map((table) => table.tableName);
|
|
4
4
|
if (listTables.length > 0) {
|
|
@@ -101,6 +101,25 @@ export const getTableAndColumnsName = async (appDataSource) => {
|
|
|
101
101
|
const rep = await appDataSource.query(sql);
|
|
102
102
|
return formatToSqlTable(rep);
|
|
103
103
|
}
|
|
104
|
+
if (appDataSource.options.type === "sap") {
|
|
105
|
+
const schema = appDataSource.options?.schema ?? "public";
|
|
106
|
+
sql = `SELECT
|
|
107
|
+
TABLE_NAME,
|
|
108
|
+
COLUMN_NAME,
|
|
109
|
+
DATA_TYPE_NAME AS data_type,
|
|
110
|
+
CASE WHEN IS_NULLABLE='TRUE' THEN 'YES' ELSE 'NO' END AS is_nullable
|
|
111
|
+
FROM TABLE_COLUMNS
|
|
112
|
+
WHERE SCHEMA_NAME='${schema}'`;
|
|
113
|
+
const rep = await appDataSource.query(sql);
|
|
114
|
+
const repLowerCase = [];
|
|
115
|
+
rep.forEach((_rep) => repLowerCase.push({
|
|
116
|
+
table_name: _rep.TABLE_NAME,
|
|
117
|
+
column_name: _rep.COLUMN_NAME,
|
|
118
|
+
data_type: _rep.DATA_TYPE,
|
|
119
|
+
is_nullable: _rep.IS_NULLABLE,
|
|
120
|
+
}));
|
|
121
|
+
return formatToSqlTable(repLowerCase);
|
|
122
|
+
}
|
|
104
123
|
throw new Error("Database type not implemented yet");
|
|
105
124
|
};
|
|
106
125
|
const formatSqlResponseToSimpleTableString = (rawResult) => {
|
|
@@ -120,9 +139,16 @@ export const generateTableInfoFromTables = async (tables, appDataSource, nbSampl
|
|
|
120
139
|
let globalString = "";
|
|
121
140
|
for (const currentTable of tables) {
|
|
122
141
|
// Add the creation of the table in SQL
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
142
|
+
let schema = null;
|
|
143
|
+
if (appDataSource.options.type === "postgres") {
|
|
144
|
+
schema = appDataSource.options?.schema ?? "public";
|
|
145
|
+
}
|
|
146
|
+
else if (appDataSource.options.type === "sap") {
|
|
147
|
+
schema =
|
|
148
|
+
appDataSource.options?.schema ??
|
|
149
|
+
appDataSource.options?.username ??
|
|
150
|
+
"public";
|
|
151
|
+
}
|
|
126
152
|
let sqlCreateTableQuery = schema
|
|
127
153
|
? `CREATE TABLE "${schema}"."${currentTable.tableName}" (\n`
|
|
128
154
|
: `CREATE TABLE ${currentTable.tableName} (\n`;
|
|
@@ -145,6 +171,12 @@ export const generateTableInfoFromTables = async (tables, appDataSource, nbSampl
|
|
|
145
171
|
else if (appDataSource.options.type === "mssql") {
|
|
146
172
|
sqlSelectInfoQuery = `SELECT TOP ${nbSampleRow} * FROM [${currentTable.tableName}];\n`;
|
|
147
173
|
}
|
|
174
|
+
else if (appDataSource.options.type === "sap") {
|
|
175
|
+
const schema = appDataSource.options?.schema ??
|
|
176
|
+
appDataSource.options?.username ??
|
|
177
|
+
"public";
|
|
178
|
+
sqlSelectInfoQuery = `SELECT * FROM "${schema}"."${currentTable.tableName}" LIMIT ${nbSampleRow};\n`;
|
|
179
|
+
}
|
|
148
180
|
else {
|
|
149
181
|
sqlSelectInfoQuery = `SELECT * FROM "${currentTable.tableName}" LIMIT ${nbSampleRow};\n`;
|
|
150
182
|
}
|
|
@@ -180,5 +212,8 @@ export const getPromptTemplateFromDataSource = (appDataSource) => {
|
|
|
180
212
|
if (appDataSource.options.type === "mssql") {
|
|
181
213
|
return SQL_MSSQL_PROMPT;
|
|
182
214
|
}
|
|
215
|
+
if (appDataSource.options.type === "sap") {
|
|
216
|
+
return SQL_SAP_HANA_PROMPT;
|
|
217
|
+
}
|
|
183
218
|
return DEFAULT_SQL_DATABASE_PROMPT;
|
|
184
219
|
};
|
|
@@ -180,15 +180,14 @@ class Chroma extends base_js_1.VectorStore {
|
|
|
180
180
|
const [firstMetadatas] = metadatas;
|
|
181
181
|
const results = [];
|
|
182
182
|
for (let i = 0; i < firstIds.length; i += 1) {
|
|
183
|
-
let metadata = {};
|
|
184
|
-
|
|
185
|
-
if (storedMetadata && storedMetadata.locFrom && storedMetadata.locTo) {
|
|
183
|
+
let metadata = firstMetadatas?.[i] ?? {};
|
|
184
|
+
if (metadata.locFrom && metadata.locTo) {
|
|
186
185
|
metadata = {
|
|
187
|
-
...
|
|
186
|
+
...metadata,
|
|
188
187
|
loc: {
|
|
189
188
|
lines: {
|
|
190
|
-
from:
|
|
191
|
-
to:
|
|
189
|
+
from: metadata.locFrom,
|
|
190
|
+
to: metadata.locTo,
|
|
192
191
|
},
|
|
193
192
|
},
|
|
194
193
|
};
|
|
@@ -154,15 +154,14 @@ export class Chroma extends VectorStore {
|
|
|
154
154
|
const [firstMetadatas] = metadatas;
|
|
155
155
|
const results = [];
|
|
156
156
|
for (let i = 0; i < firstIds.length; i += 1) {
|
|
157
|
-
let metadata = {};
|
|
158
|
-
|
|
159
|
-
if (storedMetadata && storedMetadata.locFrom && storedMetadata.locTo) {
|
|
157
|
+
let metadata = firstMetadatas?.[i] ?? {};
|
|
158
|
+
if (metadata.locFrom && metadata.locTo) {
|
|
160
159
|
metadata = {
|
|
161
|
-
...
|
|
160
|
+
...metadata,
|
|
162
161
|
loc: {
|
|
163
162
|
lines: {
|
|
164
|
-
from:
|
|
165
|
-
to:
|
|
163
|
+
from: metadata.locFrom,
|
|
164
|
+
to: metadata.locTo,
|
|
166
165
|
},
|
|
167
166
|
},
|
|
168
167
|
};
|
|
@@ -32,6 +32,12 @@ class FaissStore extends base_js_1.SaveableVectorStore {
|
|
|
32
32
|
_vectorstoreType() {
|
|
33
33
|
return "faiss";
|
|
34
34
|
}
|
|
35
|
+
getMapping() {
|
|
36
|
+
return this._mapping;
|
|
37
|
+
}
|
|
38
|
+
getDocstore() {
|
|
39
|
+
return this.docstore;
|
|
40
|
+
}
|
|
35
41
|
constructor(embeddings, args) {
|
|
36
42
|
super(embeddings, args);
|
|
37
43
|
Object.defineProperty(this, "_index", {
|
|
@@ -70,7 +76,7 @@ class FaissStore extends base_js_1.SaveableVectorStore {
|
|
|
70
76
|
}
|
|
71
77
|
get index() {
|
|
72
78
|
if (!this._index) {
|
|
73
|
-
throw new Error("Vector store not initialised yet. Try calling `fromTexts` or `
|
|
79
|
+
throw new Error("Vector store not initialised yet. Try calling `fromTexts`, `fromDocuments` or `fromIndex` first.");
|
|
74
80
|
}
|
|
75
81
|
return this._index;
|
|
76
82
|
}
|
|
@@ -134,6 +140,32 @@ class FaissStore extends base_js_1.SaveableVectorStore {
|
|
|
134
140
|
])),
|
|
135
141
|
]);
|
|
136
142
|
}
|
|
143
|
+
async mergeFrom(targetIndex) {
|
|
144
|
+
const targetIndexDimensions = targetIndex.index.getDimension();
|
|
145
|
+
if (!this._index) {
|
|
146
|
+
const { IndexFlatL2 } = await FaissStore.importFaiss();
|
|
147
|
+
this._index = new IndexFlatL2(targetIndexDimensions);
|
|
148
|
+
}
|
|
149
|
+
const d = this.index.getDimension();
|
|
150
|
+
if (targetIndexDimensions !== d) {
|
|
151
|
+
throw new Error("Cannot merge indexes with different dimensions.");
|
|
152
|
+
}
|
|
153
|
+
const targetMapping = targetIndex.getMapping();
|
|
154
|
+
const targetDocstore = targetIndex.getDocstore();
|
|
155
|
+
const targetSize = targetIndex.index.ntotal();
|
|
156
|
+
const documentIds = [];
|
|
157
|
+
const currentDocstoreSize = this.index.ntotal();
|
|
158
|
+
for (let i = 0; i < targetSize; i += 1) {
|
|
159
|
+
const targetId = targetMapping[i];
|
|
160
|
+
documentIds.push(targetId);
|
|
161
|
+
const targetDocument = targetDocstore.search(targetId);
|
|
162
|
+
const id = currentDocstoreSize + i;
|
|
163
|
+
this._mapping[id] = targetId;
|
|
164
|
+
this.docstore.add({ [targetId]: targetDocument });
|
|
165
|
+
}
|
|
166
|
+
this.index.mergeFrom(targetIndex.index);
|
|
167
|
+
return documentIds;
|
|
168
|
+
}
|
|
137
169
|
static async load(directory, embeddings) {
|
|
138
170
|
const fs = await import("node:fs/promises");
|
|
139
171
|
const path = await import("node:path");
|
|
@@ -231,6 +263,14 @@ class FaissStore extends base_js_1.SaveableVectorStore {
|
|
|
231
263
|
await instance.addDocuments(docs);
|
|
232
264
|
return instance;
|
|
233
265
|
}
|
|
266
|
+
static async fromIndex(targetIndex, embeddings, dbConfig) {
|
|
267
|
+
const args = {
|
|
268
|
+
docstore: dbConfig?.docstore,
|
|
269
|
+
};
|
|
270
|
+
const instance = new this(embeddings, args);
|
|
271
|
+
await instance.mergeFrom(targetIndex);
|
|
272
|
+
return instance;
|
|
273
|
+
}
|
|
234
274
|
static async importFaiss() {
|
|
235
275
|
try {
|
|
236
276
|
const { default: { IndexFlatL2 }, } = await import("faiss-node");
|