langchain 0.0.147 → 0.0.149
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/chat_models/googlevertexai/web.cjs +1 -0
- package/chat_models/googlevertexai/web.d.ts +1 -0
- package/chat_models/googlevertexai/web.js +1 -0
- package/chat_models/googlevertexai.cjs +1 -1
- package/chat_models/googlevertexai.d.ts +1 -1
- package/chat_models/googlevertexai.js +1 -1
- package/dist/chains/constitutional_ai/constitutional_principle.cjs +272 -1
- package/dist/chains/constitutional_ai/constitutional_principle.js +272 -1
- package/dist/chains/question_answering/load.cjs +12 -4
- package/dist/chains/question_answering/load.d.ts +2 -0
- package/dist/chains/question_answering/load.js +12 -4
- package/dist/chains/summarization/load.cjs +8 -4
- package/dist/chains/summarization/load.d.ts +2 -0
- package/dist/chains/summarization/load.js +8 -4
- package/dist/chat_models/{googlevertexai.cjs → googlevertexai/common.cjs} +14 -26
- package/dist/chat_models/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -22
- package/dist/chat_models/{googlevertexai.js → googlevertexai/common.js} +12 -24
- package/dist/chat_models/googlevertexai/index.cjs +36 -0
- package/dist/chat_models/googlevertexai/index.d.ts +21 -0
- package/dist/chat_models/googlevertexai/index.js +31 -0
- package/dist/chat_models/googlevertexai/web.cjs +33 -0
- package/dist/chat_models/googlevertexai/web.d.ts +19 -0
- package/dist/chat_models/googlevertexai/web.js +28 -0
- package/dist/document_loaders/web/notionapi.cjs +93 -70
- package/dist/document_loaders/web/notionapi.d.ts +33 -1
- package/dist/document_loaders/web/notionapi.js +89 -71
- package/dist/embeddings/googlevertexai.cjs +5 -1
- package/dist/embeddings/googlevertexai.d.ts +2 -1
- package/dist/embeddings/googlevertexai.js +5 -1
- package/dist/evaluation/agents/index.cjs +17 -0
- package/dist/evaluation/agents/index.d.ts +1 -0
- package/dist/evaluation/agents/index.js +1 -0
- package/dist/evaluation/agents/prompt.cjs +132 -0
- package/dist/evaluation/agents/prompt.d.ts +6 -0
- package/dist/evaluation/agents/prompt.js +129 -0
- package/dist/evaluation/agents/trajectory.cjs +189 -0
- package/dist/evaluation/agents/trajectory.d.ts +54 -0
- package/dist/evaluation/agents/trajectory.js +184 -0
- package/dist/evaluation/base.cjs +274 -0
- package/dist/evaluation/base.d.ts +232 -0
- package/dist/evaluation/base.js +263 -0
- package/dist/evaluation/comparison/index.cjs +17 -0
- package/dist/evaluation/comparison/index.d.ts +1 -0
- package/dist/evaluation/comparison/index.js +1 -0
- package/dist/evaluation/comparison/pairwise.cjs +244 -0
- package/dist/evaluation/comparison/pairwise.d.ts +50 -0
- package/dist/evaluation/comparison/pairwise.js +238 -0
- package/dist/evaluation/comparison/prompt.cjs +74 -0
- package/dist/evaluation/comparison/prompt.d.ts +21 -0
- package/dist/evaluation/comparison/prompt.js +71 -0
- package/dist/evaluation/criteria/criteria.cjs +259 -0
- package/dist/evaluation/criteria/criteria.d.ts +73 -0
- package/dist/evaluation/criteria/criteria.js +253 -0
- package/dist/evaluation/criteria/index.cjs +17 -0
- package/dist/evaluation/criteria/index.d.ts +1 -0
- package/dist/evaluation/criteria/index.js +1 -0
- package/dist/evaluation/criteria/prompt.cjs +36 -0
- package/dist/evaluation/criteria/prompt.d.ts +12 -0
- package/dist/evaluation/criteria/prompt.js +33 -0
- package/dist/evaluation/embedding_distance/base.cjs +163 -0
- package/dist/evaluation/embedding_distance/base.d.ts +78 -0
- package/dist/evaluation/embedding_distance/base.js +156 -0
- package/dist/evaluation/embedding_distance/index.cjs +17 -0
- package/dist/evaluation/embedding_distance/index.d.ts +1 -0
- package/dist/evaluation/embedding_distance/index.js +1 -0
- package/dist/evaluation/index.cjs +6 -0
- package/dist/evaluation/index.d.ts +6 -0
- package/dist/evaluation/index.js +6 -0
- package/dist/evaluation/loader.cjs +60 -0
- package/dist/evaluation/loader.d.ts +27 -0
- package/dist/evaluation/loader.js +56 -0
- package/dist/evaluation/types.cjs +2 -0
- package/dist/evaluation/types.d.ts +35 -0
- package/dist/evaluation/types.js +1 -0
- package/dist/experimental/multimodal_embeddings/googlevertexai.cjs +5 -1
- package/dist/experimental/multimodal_embeddings/googlevertexai.d.ts +2 -1
- package/dist/experimental/multimodal_embeddings/googlevertexai.js +5 -1
- package/dist/llms/bedrock.cjs +9 -1
- package/dist/llms/bedrock.d.ts +3 -0
- package/dist/llms/bedrock.js +9 -1
- package/dist/llms/{googlevertexai.js → googlevertexai/common.cjs} +21 -17
- package/dist/llms/{googlevertexai.d.ts → googlevertexai/common.d.ts} +13 -23
- package/dist/llms/{googlevertexai.cjs → googlevertexai/common.js} +17 -21
- package/dist/llms/googlevertexai/index.cjs +34 -0
- package/dist/llms/googlevertexai/index.d.ts +26 -0
- package/dist/llms/googlevertexai/index.js +30 -0
- package/dist/llms/googlevertexai/web.cjs +31 -0
- package/dist/llms/googlevertexai/web.d.ts +24 -0
- package/dist/llms/googlevertexai/web.js +27 -0
- package/dist/load/import_constants.cjs +2 -0
- package/dist/load/import_constants.js +2 -0
- package/dist/load/import_map.cjs +2 -1
- package/dist/load/import_map.d.ts +1 -0
- package/dist/load/import_map.js +1 -0
- package/dist/load/serializable.cjs +23 -4
- package/dist/load/serializable.js +23 -4
- package/dist/retrievers/multi_query.cjs +140 -0
- package/dist/retrievers/multi_query.d.ts +33 -0
- package/dist/retrievers/multi_query.js +136 -0
- package/dist/retrievers/self_query/base.cjs +1 -1
- package/dist/retrievers/self_query/base.js +2 -2
- package/dist/retrievers/self_query/functional.cjs +1 -1
- package/dist/retrievers/self_query/functional.js +2 -2
- package/dist/retrievers/self_query/utils.cjs +46 -6
- package/dist/retrievers/self_query/utils.d.ts +7 -0
- package/dist/retrievers/self_query/utils.js +44 -5
- package/dist/schema/runnable/base.cjs +910 -0
- package/dist/schema/runnable/base.d.ts +300 -0
- package/dist/schema/runnable/base.js +896 -0
- package/dist/schema/runnable/index.cjs +19 -926
- package/dist/schema/runnable/index.d.ts +4 -298
- package/dist/schema/runnable/index.js +3 -914
- package/dist/schema/runnable/passthrough.cjs +31 -0
- package/dist/schema/runnable/passthrough.d.ts +11 -0
- package/dist/schema/runnable/passthrough.js +27 -0
- package/dist/schema/runnable/router.cjs +74 -0
- package/dist/schema/runnable/router.d.ts +29 -0
- package/dist/schema/runnable/router.js +70 -0
- package/dist/types/googlevertexai-types.d.ts +11 -4
- package/dist/util/googlevertexai-connection.cjs +14 -15
- package/dist/util/googlevertexai-connection.d.ts +7 -7
- package/dist/util/googlevertexai-connection.js +14 -15
- package/dist/util/googlevertexai-webauth.cjs +56 -0
- package/dist/util/googlevertexai-webauth.d.ts +25 -0
- package/dist/util/googlevertexai-webauth.js +52 -0
- package/dist/vectorstores/googlevertexai.cjs +9 -8
- package/dist/vectorstores/googlevertexai.d.ts +8 -7
- package/dist/vectorstores/googlevertexai.js +9 -8
- package/dist/vectorstores/opensearch.cjs +4 -2
- package/dist/vectorstores/opensearch.d.ts +4 -1
- package/dist/vectorstores/opensearch.js +4 -2
- package/llms/googlevertexai/web.cjs +1 -0
- package/llms/googlevertexai/web.d.ts +1 -0
- package/llms/googlevertexai/web.js +1 -0
- package/llms/googlevertexai.cjs +1 -1
- package/llms/googlevertexai.d.ts +1 -1
- package/llms/googlevertexai.js +1 -1
- package/package.json +32 -3
- package/retrievers/multi_query.cjs +1 -0
- package/retrievers/multi_query.d.ts +1 -0
- package/retrievers/multi_query.js +1 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { LLMChain } from "../chains/llm_chain.js";
|
|
2
|
+
import { PromptTemplate } from "../prompts/prompt.js";
|
|
3
|
+
import { BaseOutputParser } from "../schema/output_parser.js";
|
|
4
|
+
import { BaseRetriever } from "../schema/retriever.js";
|
|
5
|
+
class LineListOutputParser extends BaseOutputParser {
|
|
6
|
+
constructor() {
|
|
7
|
+
super(...arguments);
|
|
8
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
9
|
+
enumerable: true,
|
|
10
|
+
configurable: true,
|
|
11
|
+
writable: true,
|
|
12
|
+
value: ["langchain", "retrievers", "multiquery"]
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
static lc_name() {
|
|
16
|
+
return "LineListOutputParser";
|
|
17
|
+
}
|
|
18
|
+
async parse(text) {
|
|
19
|
+
const startKeyIndex = text.indexOf("<questions>");
|
|
20
|
+
const endKeyIndex = text.indexOf("</questions>");
|
|
21
|
+
const questionsStartIndex = startKeyIndex === -1 ? 0 : startKeyIndex + "<questions>".length;
|
|
22
|
+
const questionsEndIndex = endKeyIndex === -1 ? text.length : endKeyIndex;
|
|
23
|
+
const lines = text
|
|
24
|
+
.slice(questionsStartIndex, questionsEndIndex)
|
|
25
|
+
.trim()
|
|
26
|
+
.split("\n")
|
|
27
|
+
.filter((line) => line.trim() !== "");
|
|
28
|
+
return { lines };
|
|
29
|
+
}
|
|
30
|
+
getFormatInstructions() {
|
|
31
|
+
throw new Error("Not implemented.");
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// Create template
|
|
35
|
+
const DEFAULT_QUERY_PROMPT = /* #__PURE__ */ new PromptTemplate({
|
|
36
|
+
inputVariables: ["question", "queryCount"],
|
|
37
|
+
template: `You are an AI language model assistant. Your task is
|
|
38
|
+
to generate {queryCount} different versions of the given user
|
|
39
|
+
question to retrieve relevant documents from a vector database.
|
|
40
|
+
By generating multiple perspectives on the user question,
|
|
41
|
+
your goal is to help the user overcome some of the limitations
|
|
42
|
+
of distance-based similarity search.
|
|
43
|
+
|
|
44
|
+
Provide these alternative questions separated by newlines between XML tags. For example:
|
|
45
|
+
|
|
46
|
+
<questions>
|
|
47
|
+
Question 1
|
|
48
|
+
Question 2
|
|
49
|
+
Question 3
|
|
50
|
+
</questions>
|
|
51
|
+
|
|
52
|
+
Original question: {question}`,
|
|
53
|
+
});
|
|
54
|
+
// Export class
|
|
55
|
+
export class MultiQueryRetriever extends BaseRetriever {
|
|
56
|
+
static lc_name() {
|
|
57
|
+
return "MultiQueryRetriever";
|
|
58
|
+
}
|
|
59
|
+
constructor(fields) {
|
|
60
|
+
super(fields);
|
|
61
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
configurable: true,
|
|
64
|
+
writable: true,
|
|
65
|
+
value: ["langchain", "retrievers", "multiquery"]
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(this, "retriever", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
configurable: true,
|
|
70
|
+
writable: true,
|
|
71
|
+
value: void 0
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(this, "llmChain", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
configurable: true,
|
|
76
|
+
writable: true,
|
|
77
|
+
value: void 0
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperty(this, "queryCount", {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
configurable: true,
|
|
82
|
+
writable: true,
|
|
83
|
+
value: 3
|
|
84
|
+
});
|
|
85
|
+
Object.defineProperty(this, "parserKey", {
|
|
86
|
+
enumerable: true,
|
|
87
|
+
configurable: true,
|
|
88
|
+
writable: true,
|
|
89
|
+
value: "lines"
|
|
90
|
+
});
|
|
91
|
+
this.retriever = fields.retriever;
|
|
92
|
+
this.llmChain = fields.llmChain;
|
|
93
|
+
this.queryCount = fields.queryCount ?? this.queryCount;
|
|
94
|
+
this.parserKey = fields.parserKey ?? this.parserKey;
|
|
95
|
+
}
|
|
96
|
+
static fromLLM(fields) {
|
|
97
|
+
const { retriever, llm, prompt = DEFAULT_QUERY_PROMPT, queryCount, parserKey, ...rest } = fields;
|
|
98
|
+
const outputParser = new LineListOutputParser();
|
|
99
|
+
const llmChain = new LLMChain({ llm, prompt, outputParser });
|
|
100
|
+
return new this({ retriever, llmChain, queryCount, parserKey, ...rest });
|
|
101
|
+
}
|
|
102
|
+
// Generate the different queries for each retrieval, using our llmChain
|
|
103
|
+
async _generateQueries(question, runManager) {
|
|
104
|
+
const response = await this.llmChain.call({ question, queryCount: this.queryCount }, runManager?.getChild());
|
|
105
|
+
const lines = response.text[this.parserKey] || [];
|
|
106
|
+
if (this.verbose) {
|
|
107
|
+
console.log(`Generated queries: ${lines}`);
|
|
108
|
+
}
|
|
109
|
+
return lines;
|
|
110
|
+
}
|
|
111
|
+
// Retrieve documents using the original retriever
|
|
112
|
+
async _retrieveDocuments(queries, runManager) {
|
|
113
|
+
const documents = [];
|
|
114
|
+
for (const query of queries) {
|
|
115
|
+
const docs = await this.retriever.getRelevantDocuments(query, runManager?.getChild());
|
|
116
|
+
documents.push(...docs);
|
|
117
|
+
}
|
|
118
|
+
return documents;
|
|
119
|
+
}
|
|
120
|
+
// Deduplicate the documents that were returned in multiple retrievals
|
|
121
|
+
_uniqueUnion(documents) {
|
|
122
|
+
const uniqueDocumentsDict = {};
|
|
123
|
+
for (const doc of documents) {
|
|
124
|
+
const key = `${doc.pageContent}:${JSON.stringify(Object.entries(doc.metadata).sort())}`;
|
|
125
|
+
uniqueDocumentsDict[key] = doc;
|
|
126
|
+
}
|
|
127
|
+
const uniqueDocuments = Object.values(uniqueDocumentsDict);
|
|
128
|
+
return uniqueDocuments;
|
|
129
|
+
}
|
|
130
|
+
async _getRelevantDocuments(question, runManager) {
|
|
131
|
+
const queries = await this._generateQueries(question, runManager);
|
|
132
|
+
const documents = await this._retrieveDocuments(queries, runManager);
|
|
133
|
+
const uniqueDocuments = this._uniqueUnion(documents);
|
|
134
|
+
return uniqueDocuments;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
@@ -84,7 +84,7 @@ class BasicTranslator extends BaseTranslator {
|
|
|
84
84
|
visitComparison(comparison) {
|
|
85
85
|
return {
|
|
86
86
|
[comparison.attribute]: {
|
|
87
|
-
[this.formatFunction(comparison.comparator)]: comparison.value,
|
|
87
|
+
[this.formatFunction(comparison.comparator)]: (0, utils_js_1.castValue)(comparison.value),
|
|
88
88
|
},
|
|
89
89
|
};
|
|
90
90
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Comparators, Operators, Visitor, } from "../../chains/query_constructor/ir.js";
|
|
2
|
-
import { isFilterEmpty } from "./utils.js";
|
|
2
|
+
import { isFilterEmpty, castValue } from "./utils.js";
|
|
3
3
|
/**
|
|
4
4
|
* Abstract class that provides a blueprint for creating specific
|
|
5
5
|
* translator classes. Defines two abstract methods: formatFunction and
|
|
@@ -80,7 +80,7 @@ export class BasicTranslator extends BaseTranslator {
|
|
|
80
80
|
visitComparison(comparison) {
|
|
81
81
|
return {
|
|
82
82
|
[comparison.attribute]: {
|
|
83
|
-
[this.formatFunction(comparison.comparator)]: comparison.value,
|
|
83
|
+
[this.formatFunction(comparison.comparator)]: castValue(comparison.value),
|
|
84
84
|
},
|
|
85
85
|
};
|
|
86
86
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Comparators, Operators, } from "../../chains/query_constructor/ir.js";
|
|
2
2
|
import { BaseTranslator } from "./base.js";
|
|
3
|
-
import { isFilterEmpty } from "./utils.js";
|
|
3
|
+
import { castValue, isFilterEmpty } from "./utils.js";
|
|
4
4
|
/**
|
|
5
5
|
* A class that extends `BaseTranslator` to translate structured queries
|
|
6
6
|
* into functional filters.
|
|
@@ -129,7 +129,7 @@ export class FunctionalTranslator extends BaseTranslator {
|
|
|
129
129
|
}
|
|
130
130
|
return false;
|
|
131
131
|
}
|
|
132
|
-
return comparatorFunction(documentValue, value);
|
|
132
|
+
return comparatorFunction(documentValue, castValue(value));
|
|
133
133
|
};
|
|
134
134
|
}
|
|
135
135
|
else {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isString = exports.isFloat = exports.isInt = exports.isFilterEmpty = exports.isObject = void 0;
|
|
3
|
+
exports.castValue = exports.isString = exports.isFloat = exports.isInt = exports.isFilterEmpty = exports.isObject = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Checks if the provided argument is an object and not an array.
|
|
6
6
|
*/
|
|
@@ -32,16 +32,32 @@ exports.isFilterEmpty = isFilterEmpty;
|
|
|
32
32
|
* Checks if the provided value is an integer.
|
|
33
33
|
*/
|
|
34
34
|
function isInt(value) {
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
if (typeof value === "number") {
|
|
36
|
+
return value % 1 === 0;
|
|
37
|
+
}
|
|
38
|
+
else if (typeof value === "string") {
|
|
39
|
+
const numberValue = parseInt(value, 10);
|
|
40
|
+
return (!Number.isNaN(numberValue) &&
|
|
41
|
+
numberValue % 1 === 0 &&
|
|
42
|
+
numberValue.toString() === value);
|
|
43
|
+
}
|
|
44
|
+
return false;
|
|
37
45
|
}
|
|
38
46
|
exports.isInt = isInt;
|
|
39
47
|
/**
|
|
40
48
|
* Checks if the provided value is a floating-point number.
|
|
41
49
|
*/
|
|
42
50
|
function isFloat(value) {
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
if (typeof value === "number") {
|
|
52
|
+
return value % 1 !== 0;
|
|
53
|
+
}
|
|
54
|
+
else if (typeof value === "string") {
|
|
55
|
+
const numberValue = parseFloat(value);
|
|
56
|
+
return (!Number.isNaN(numberValue) &&
|
|
57
|
+
numberValue % 1 !== 0 &&
|
|
58
|
+
numberValue.toString() === value);
|
|
59
|
+
}
|
|
60
|
+
return false;
|
|
45
61
|
}
|
|
46
62
|
exports.isFloat = isFloat;
|
|
47
63
|
/**
|
|
@@ -49,6 +65,30 @@ exports.isFloat = isFloat;
|
|
|
49
65
|
* number.
|
|
50
66
|
*/
|
|
51
67
|
function isString(value) {
|
|
52
|
-
return typeof value === "string" &&
|
|
68
|
+
return (typeof value === "string" &&
|
|
69
|
+
(Number.isNaN(parseFloat(value)) || parseFloat(value).toString() !== value));
|
|
53
70
|
}
|
|
54
71
|
exports.isString = isString;
|
|
72
|
+
/**
|
|
73
|
+
* Casts a value that might be string or number to actual string or number.
|
|
74
|
+
* Since LLM might return back an integer/float as a string, we need to cast
|
|
75
|
+
* it back to a number, as many vector databases can't handle number as string
|
|
76
|
+
* values as a comparator.
|
|
77
|
+
*/
|
|
78
|
+
function castValue(input) {
|
|
79
|
+
let value;
|
|
80
|
+
if (isString(input)) {
|
|
81
|
+
value = input;
|
|
82
|
+
}
|
|
83
|
+
else if (isInt(input)) {
|
|
84
|
+
value = parseInt(input, 10);
|
|
85
|
+
}
|
|
86
|
+
else if (isFloat(input)) {
|
|
87
|
+
value = parseFloat(input);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
throw new Error("Unsupported value type");
|
|
91
|
+
}
|
|
92
|
+
return value;
|
|
93
|
+
}
|
|
94
|
+
exports.castValue = castValue;
|
|
@@ -20,3 +20,10 @@ export declare function isFloat(value: unknown): boolean;
|
|
|
20
20
|
* number.
|
|
21
21
|
*/
|
|
22
22
|
export declare function isString(value: unknown): boolean;
|
|
23
|
+
/**
|
|
24
|
+
* Casts a value that might be string or number to actual string or number.
|
|
25
|
+
* Since LLM might return back an integer/float as a string, we need to cast
|
|
26
|
+
* it back to a number, as many vector databases can't handle number as string
|
|
27
|
+
* values as a comparator.
|
|
28
|
+
*/
|
|
29
|
+
export declare function castValue(input: unknown): string | number;
|
|
@@ -27,20 +27,59 @@ filter) {
|
|
|
27
27
|
* Checks if the provided value is an integer.
|
|
28
28
|
*/
|
|
29
29
|
export function isInt(value) {
|
|
30
|
-
|
|
31
|
-
|
|
30
|
+
if (typeof value === "number") {
|
|
31
|
+
return value % 1 === 0;
|
|
32
|
+
}
|
|
33
|
+
else if (typeof value === "string") {
|
|
34
|
+
const numberValue = parseInt(value, 10);
|
|
35
|
+
return (!Number.isNaN(numberValue) &&
|
|
36
|
+
numberValue % 1 === 0 &&
|
|
37
|
+
numberValue.toString() === value);
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
32
40
|
}
|
|
33
41
|
/**
|
|
34
42
|
* Checks if the provided value is a floating-point number.
|
|
35
43
|
*/
|
|
36
44
|
export function isFloat(value) {
|
|
37
|
-
|
|
38
|
-
|
|
45
|
+
if (typeof value === "number") {
|
|
46
|
+
return value % 1 !== 0;
|
|
47
|
+
}
|
|
48
|
+
else if (typeof value === "string") {
|
|
49
|
+
const numberValue = parseFloat(value);
|
|
50
|
+
return (!Number.isNaN(numberValue) &&
|
|
51
|
+
numberValue % 1 !== 0 &&
|
|
52
|
+
numberValue.toString() === value);
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
39
55
|
}
|
|
40
56
|
/**
|
|
41
57
|
* Checks if the provided value is a string that cannot be parsed into a
|
|
42
58
|
* number.
|
|
43
59
|
*/
|
|
44
60
|
export function isString(value) {
|
|
45
|
-
return typeof value === "string" &&
|
|
61
|
+
return (typeof value === "string" &&
|
|
62
|
+
(Number.isNaN(parseFloat(value)) || parseFloat(value).toString() !== value));
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Casts a value that might be string or number to actual string or number.
|
|
66
|
+
* Since LLM might return back an integer/float as a string, we need to cast
|
|
67
|
+
* it back to a number, as many vector databases can't handle number as string
|
|
68
|
+
* values as a comparator.
|
|
69
|
+
*/
|
|
70
|
+
export function castValue(input) {
|
|
71
|
+
let value;
|
|
72
|
+
if (isString(input)) {
|
|
73
|
+
value = input;
|
|
74
|
+
}
|
|
75
|
+
else if (isInt(input)) {
|
|
76
|
+
value = parseInt(input, 10);
|
|
77
|
+
}
|
|
78
|
+
else if (isFloat(input)) {
|
|
79
|
+
value = parseFloat(input);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
throw new Error("Unsupported value type");
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
46
85
|
}
|