langchain 0.0.196 → 0.0.197-rc.1
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 +21 -0
- package/dist/agents/executor.cjs +351 -1
- package/dist/agents/executor.d.ts +61 -3
- package/dist/agents/executor.js +350 -1
- package/dist/agents/openai/index.cjs +6 -2
- package/dist/agents/openai/index.js +6 -2
- package/dist/agents/toolkits/conversational_retrieval/token_buffer_memory.d.ts +1 -1
- package/dist/base_language/count_tokens.cjs +4 -4
- package/dist/base_language/count_tokens.d.ts +1 -1
- package/dist/base_language/count_tokens.js +1 -1
- package/dist/base_language/index.cjs +3 -3
- package/dist/base_language/index.d.ts +1 -1
- package/dist/base_language/index.js +1 -1
- package/dist/cache/base.cjs +1 -1
- package/dist/cache/base.d.ts +1 -1
- package/dist/cache/base.js +1 -1
- package/dist/cache/index.cjs +2 -2
- package/dist/cache/index.d.ts +1 -1
- package/dist/cache/index.js +1 -1
- package/dist/callbacks/base.cjs +1 -1
- package/dist/callbacks/base.d.ts +1 -1
- package/dist/callbacks/base.js +1 -1
- package/dist/callbacks/handlers/console.cjs +1 -1
- package/dist/callbacks/handlers/console.d.ts +1 -1
- package/dist/callbacks/handlers/console.js +1 -1
- package/dist/callbacks/handlers/initialize.cjs +1 -1
- package/dist/callbacks/handlers/initialize.d.ts +1 -1
- package/dist/callbacks/handlers/initialize.js +1 -1
- package/dist/callbacks/handlers/log_stream.cjs +1 -1
- package/dist/callbacks/handlers/log_stream.d.ts +1 -1
- package/dist/callbacks/handlers/log_stream.js +1 -1
- package/dist/callbacks/handlers/run_collector.cjs +1 -1
- package/dist/callbacks/handlers/run_collector.d.ts +1 -1
- package/dist/callbacks/handlers/run_collector.js +1 -1
- package/dist/callbacks/handlers/tracer.cjs +1 -1
- package/dist/callbacks/handlers/tracer.d.ts +1 -1
- package/dist/callbacks/handlers/tracer.js +1 -1
- package/dist/callbacks/handlers/tracer_langchain.cjs +1 -1
- package/dist/callbacks/handlers/tracer_langchain.d.ts +1 -1
- package/dist/callbacks/handlers/tracer_langchain.js +1 -1
- package/dist/callbacks/handlers/tracer_langchain_v1.cjs +1 -1
- package/dist/callbacks/handlers/tracer_langchain_v1.d.ts +1 -1
- package/dist/callbacks/handlers/tracer_langchain_v1.js +1 -1
- package/dist/callbacks/manager.cjs +1 -1
- package/dist/callbacks/manager.d.ts +1 -1
- package/dist/callbacks/manager.js +1 -1
- package/dist/callbacks/promises.cjs +1 -1
- package/dist/callbacks/promises.d.ts +1 -1
- package/dist/callbacks/promises.js +1 -1
- package/dist/chains/base.cjs +16 -0
- package/dist/chains/base.d.ts +2 -0
- package/dist/chains/base.js +16 -0
- package/dist/chains/openai_functions/structured_output.cjs +1 -1
- package/dist/chains/openai_functions/structured_output.d.ts +1 -1
- package/dist/chains/openai_functions/structured_output.js +1 -1
- package/dist/chains/openai_moderation.cjs +2 -2
- package/dist/chains/openai_moderation.d.ts +1 -1
- package/dist/chains/openai_moderation.js +1 -1
- package/dist/chat_models/anthropic.cjs +15 -348
- package/dist/chat_models/anthropic.d.ts +1 -156
- package/dist/chat_models/anthropic.js +1 -346
- package/dist/chat_models/base.cjs +1 -1
- package/dist/chat_models/base.d.ts +1 -1
- package/dist/chat_models/base.js +1 -1
- package/dist/chat_models/bedrock/web.cjs +21 -1
- package/dist/chat_models/bedrock/web.d.ts +1 -1
- package/dist/chat_models/bedrock/web.js +21 -1
- package/dist/chat_models/fireworks.d.ts +2 -2
- package/dist/chat_models/iflytek_xinghuo/common.d.ts +1 -1
- package/dist/chat_models/minimax.d.ts +2 -2
- package/dist/chat_models/openai.cjs +4 -698
- package/dist/chat_models/openai.d.ts +4 -137
- package/dist/chat_models/openai.js +2 -695
- package/dist/document.cjs +2 -2
- package/dist/document.d.ts +1 -1
- package/dist/document.js +1 -1
- package/dist/document_loaders/fs/openai_whisper_audio.cjs +2 -2
- package/dist/document_loaders/fs/openai_whisper_audio.d.ts +1 -1
- package/dist/document_loaders/fs/openai_whisper_audio.js +1 -1
- package/dist/document_loaders/web/azure_blob_storage_file.d.ts +1 -1
- package/dist/document_loaders/web/github.cjs +105 -0
- package/dist/document_loaders/web/github.d.ts +26 -0
- package/dist/document_loaders/web/github.js +105 -0
- package/dist/document_loaders/web/s3.d.ts +1 -1
- package/dist/embeddings/base.cjs +1 -1
- package/dist/embeddings/base.d.ts +1 -1
- package/dist/embeddings/base.js +1 -1
- package/dist/embeddings/cache_backed.cjs +1 -1
- package/dist/embeddings/cache_backed.js +1 -1
- package/dist/embeddings/openai.cjs +2 -240
- package/dist/embeddings/openai.d.ts +1 -82
- package/dist/embeddings/openai.js +1 -239
- package/dist/experimental/openai_assistant/index.cjs +3 -3
- package/dist/experimental/openai_assistant/index.d.ts +1 -1
- package/dist/experimental/openai_assistant/index.js +1 -1
- package/dist/experimental/openai_assistant/schema.d.ts +1 -1
- package/dist/experimental/openai_files/index.cjs +2 -2
- package/dist/experimental/openai_files/index.d.ts +1 -1
- package/dist/experimental/openai_files/index.js +1 -1
- package/dist/experimental/plan_and_execute/prompt.d.ts +1 -1
- package/dist/llms/base.cjs +1 -1
- package/dist/llms/base.d.ts +1 -1
- package/dist/llms/base.js +1 -1
- package/dist/llms/bedrock/web.cjs +21 -1
- package/dist/llms/bedrock/web.d.ts +1 -1
- package/dist/llms/bedrock/web.js +21 -1
- package/dist/llms/fireworks.d.ts +2 -2
- package/dist/llms/openai-chat.cjs +3 -445
- package/dist/llms/openai-chat.d.ts +4 -123
- package/dist/llms/openai-chat.js +2 -443
- package/dist/llms/openai.cjs +6 -530
- package/dist/llms/openai.d.ts +4 -123
- package/dist/llms/openai.js +2 -525
- package/dist/load/index.cjs +4 -2
- package/dist/load/index.d.ts +2 -2
- package/dist/load/index.js +4 -2
- package/dist/load/serializable.cjs +1 -1
- package/dist/load/serializable.d.ts +1 -1
- package/dist/load/serializable.js +1 -1
- package/dist/memory/base.cjs +2 -2
- package/dist/memory/base.d.ts +2 -2
- package/dist/memory/base.js +2 -2
- package/dist/output_parsers/list.cjs +4 -122
- package/dist/output_parsers/list.d.ts +1 -57
- package/dist/output_parsers/list.js +1 -119
- package/dist/output_parsers/openai_functions.cjs +1 -1
- package/dist/output_parsers/openai_functions.d.ts +1 -1
- package/dist/output_parsers/openai_functions.js +1 -1
- package/dist/output_parsers/structured.d.ts +1 -1
- package/dist/prompts/base.cjs +8 -8
- package/dist/prompts/base.d.ts +3 -3
- package/dist/prompts/base.js +3 -3
- package/dist/prompts/chat.cjs +13 -15
- package/dist/prompts/chat.d.ts +2 -1
- package/dist/prompts/chat.js +2 -1
- package/dist/prompts/few_shot.cjs +4 -15
- package/dist/prompts/few_shot.d.ts +1 -1
- package/dist/prompts/few_shot.js +1 -1
- package/dist/prompts/index.cjs +2 -2
- package/dist/prompts/index.d.ts +1 -1
- package/dist/prompts/index.js +1 -1
- package/dist/prompts/pipeline.cjs +3 -15
- package/dist/prompts/pipeline.d.ts +1 -1
- package/dist/prompts/pipeline.js +1 -1
- package/dist/prompts/prompt.cjs +3 -15
- package/dist/prompts/prompt.d.ts +1 -1
- package/dist/prompts/prompt.js +1 -1
- package/dist/prompts/selectors/LengthBasedExampleSelector.cjs +3 -15
- package/dist/prompts/selectors/LengthBasedExampleSelector.d.ts +1 -1
- package/dist/prompts/selectors/LengthBasedExampleSelector.js +1 -1
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.cjs +1 -1
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.d.ts +1 -1
- package/dist/prompts/selectors/SemanticSimilarityExampleSelector.js +1 -1
- package/dist/prompts/selectors/conditional.cjs +6 -15
- package/dist/prompts/selectors/conditional.d.ts +1 -1
- package/dist/prompts/selectors/conditional.js +1 -1
- package/dist/prompts/serde.cjs +0 -15
- package/dist/prompts/serde.d.ts +1 -1
- package/dist/prompts/serde.js +1 -1
- package/dist/prompts/template.cjs +9 -15
- package/dist/prompts/template.d.ts +1 -1
- package/dist/prompts/template.js +1 -1
- package/dist/runnables/index.cjs +1 -1
- package/dist/runnables/index.d.ts +1 -1
- package/dist/runnables/index.js +1 -1
- package/dist/schema/document.cjs +3 -3
- package/dist/schema/document.d.ts +1 -1
- package/dist/schema/document.js +1 -1
- package/dist/schema/index.cjs +12 -12
- package/dist/schema/index.d.ts +11 -11
- package/dist/schema/index.js +7 -7
- package/dist/schema/output_parser.cjs +1 -1
- package/dist/schema/output_parser.d.ts +1 -1
- package/dist/schema/output_parser.js +1 -1
- package/dist/schema/retriever.cjs +1 -1
- package/dist/schema/retriever.d.ts +1 -1
- package/dist/schema/retriever.js +1 -1
- package/dist/schema/runnable/base.cjs +1 -1
- package/dist/schema/runnable/base.d.ts +1 -1
- package/dist/schema/runnable/base.js +1 -1
- package/dist/schema/runnable/branch.cjs +1 -1
- package/dist/schema/runnable/branch.d.ts +1 -1
- package/dist/schema/runnable/branch.js +1 -1
- package/dist/schema/runnable/config.d.ts +1 -1
- package/dist/schema/runnable/index.cjs +1 -1
- package/dist/schema/runnable/index.d.ts +1 -1
- package/dist/schema/runnable/index.js +1 -1
- package/dist/schema/runnable/passthrough.cjs +1 -1
- package/dist/schema/runnable/passthrough.d.ts +1 -1
- package/dist/schema/runnable/passthrough.js +1 -1
- package/dist/schema/runnable/router.cjs +1 -1
- package/dist/schema/runnable/router.d.ts +1 -1
- package/dist/schema/runnable/router.js +1 -1
- package/dist/schema/storage.cjs +1 -1
- package/dist/schema/storage.d.ts +1 -1
- package/dist/schema/storage.js +1 -1
- package/dist/tools/base.cjs +4 -108
- package/dist/tools/base.d.ts +1 -72
- package/dist/tools/base.js +1 -105
- package/dist/tools/bingserpapi.d.ts +1 -1
- package/dist/tools/convert_to_openai.cjs +4 -38
- package/dist/tools/convert_to_openai.d.ts +1 -11
- package/dist/tools/convert_to_openai.js +1 -35
- package/dist/tools/searchapi.d.ts +1 -1
- package/dist/tools/serpapi.d.ts +1 -1
- package/dist/tools/serper.d.ts +1 -1
- package/dist/types/openai-types.d.ts +1 -133
- package/dist/util/async_caller.cjs +1 -1
- package/dist/util/async_caller.d.ts +1 -1
- package/dist/util/async_caller.js +1 -1
- package/dist/util/openai.cjs +2 -18
- package/dist/util/openai.d.ts +1 -1
- package/dist/util/openai.js +1 -17
- package/dist/util/openapi.d.ts +2 -2
- package/dist/util/prompt-layer.d.ts +1 -1
- package/dist/vectorstores/momento_vector_index.cjs +39 -0
- package/dist/vectorstores/momento_vector_index.d.ts +17 -1
- package/dist/vectorstores/momento_vector_index.js +40 -1
- package/dist/vectorstores/mongodb_atlas.cjs +22 -2
- package/dist/vectorstores/mongodb_atlas.d.ts +13 -0
- package/dist/vectorstores/mongodb_atlas.js +22 -2
- package/package.json +12 -10
- package/dist/util/openai-format-fndef.cjs +0 -81
- package/dist/util/openai-format-fndef.d.ts +0 -44
- package/dist/util/openai-format-fndef.js +0 -77
package/dist/tools/base.js
CHANGED
|
@@ -1,105 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { CallbackManager, parseCallbackConfigArg, } from "../callbacks/manager.js";
|
|
3
|
-
import { BaseLangChain } from "../base_language/index.js";
|
|
4
|
-
/**
|
|
5
|
-
* Custom error class used to handle exceptions related to tool input parsing.
|
|
6
|
-
* It extends the built-in `Error` class and adds an optional `output`
|
|
7
|
-
* property that can hold the output that caused the exception.
|
|
8
|
-
*/
|
|
9
|
-
export class ToolInputParsingException extends Error {
|
|
10
|
-
constructor(message, output) {
|
|
11
|
-
super(message);
|
|
12
|
-
Object.defineProperty(this, "output", {
|
|
13
|
-
enumerable: true,
|
|
14
|
-
configurable: true,
|
|
15
|
-
writable: true,
|
|
16
|
-
value: void 0
|
|
17
|
-
});
|
|
18
|
-
this.output = output;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* Base class for Tools that accept input of any shape defined by a Zod schema.
|
|
23
|
-
*/
|
|
24
|
-
export class StructuredTool extends BaseLangChain {
|
|
25
|
-
get lc_namespace() {
|
|
26
|
-
return ["langchain", "tools"];
|
|
27
|
-
}
|
|
28
|
-
constructor(fields) {
|
|
29
|
-
super(fields ?? {});
|
|
30
|
-
Object.defineProperty(this, "returnDirect", {
|
|
31
|
-
enumerable: true,
|
|
32
|
-
configurable: true,
|
|
33
|
-
writable: true,
|
|
34
|
-
value: false
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Invokes the tool with the provided input and configuration.
|
|
39
|
-
* @param input The input for the tool.
|
|
40
|
-
* @param config Optional configuration for the tool.
|
|
41
|
-
* @returns A Promise that resolves with a string.
|
|
42
|
-
*/
|
|
43
|
-
async invoke(input, config) {
|
|
44
|
-
return this.call(input, config);
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Calls the tool with the provided argument, configuration, and tags. It
|
|
48
|
-
* parses the input according to the schema, handles any errors, and
|
|
49
|
-
* manages callbacks.
|
|
50
|
-
* @param arg The input argument for the tool.
|
|
51
|
-
* @param configArg Optional configuration or callbacks for the tool.
|
|
52
|
-
* @param tags Optional tags for the tool.
|
|
53
|
-
* @returns A Promise that resolves with a string.
|
|
54
|
-
*/
|
|
55
|
-
async call(arg, configArg,
|
|
56
|
-
/** @deprecated */
|
|
57
|
-
tags) {
|
|
58
|
-
let parsed;
|
|
59
|
-
try {
|
|
60
|
-
parsed = await this.schema.parseAsync(arg);
|
|
61
|
-
}
|
|
62
|
-
catch (e) {
|
|
63
|
-
throw new ToolInputParsingException(`Received tool input did not match expected schema`, JSON.stringify(arg));
|
|
64
|
-
}
|
|
65
|
-
const config = parseCallbackConfigArg(configArg);
|
|
66
|
-
const callbackManager_ = await CallbackManager.configure(config.callbacks, this.callbacks, config.tags || tags, this.tags, config.metadata, this.metadata, { verbose: this.verbose });
|
|
67
|
-
const runManager = await callbackManager_?.handleToolStart(this.toJSON(), typeof parsed === "string" ? parsed : JSON.stringify(parsed), undefined, undefined, undefined, undefined, config.runName);
|
|
68
|
-
let result;
|
|
69
|
-
try {
|
|
70
|
-
result = await this._call(parsed, runManager);
|
|
71
|
-
}
|
|
72
|
-
catch (e) {
|
|
73
|
-
await runManager?.handleToolError(e);
|
|
74
|
-
throw e;
|
|
75
|
-
}
|
|
76
|
-
await runManager?.handleToolEnd(result);
|
|
77
|
-
return result;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* Base class for Tools that accept input as a string.
|
|
82
|
-
*/
|
|
83
|
-
export class Tool extends StructuredTool {
|
|
84
|
-
constructor(fields) {
|
|
85
|
-
super(fields);
|
|
86
|
-
Object.defineProperty(this, "schema", {
|
|
87
|
-
enumerable: true,
|
|
88
|
-
configurable: true,
|
|
89
|
-
writable: true,
|
|
90
|
-
value: z
|
|
91
|
-
.object({ input: z.string().optional() })
|
|
92
|
-
.transform((obj) => obj.input)
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Calls the tool with the provided argument and callbacks. It handles
|
|
97
|
-
* string inputs specifically.
|
|
98
|
-
* @param arg The input argument for the tool, which can be a string, undefined, or an input of the tool's schema.
|
|
99
|
-
* @param callbacks Optional callbacks for the tool.
|
|
100
|
-
* @returns A Promise that resolves with a string.
|
|
101
|
-
*/
|
|
102
|
-
call(arg, callbacks) {
|
|
103
|
-
return super.call(typeof arg === "string" || !arg ? { input: arg } : arg, callbacks);
|
|
104
|
-
}
|
|
105
|
-
}
|
|
1
|
+
export { ToolInputParsingException, StructuredTool, Tool, } from "@langchain/core/tools";
|
|
@@ -11,7 +11,7 @@ declare class BingSerpAPI extends Tool {
|
|
|
11
11
|
/**
|
|
12
12
|
* Not implemented. Will throw an error if called.
|
|
13
13
|
*/
|
|
14
|
-
toJSON(): import("langchain
|
|
14
|
+
toJSON(): import("@langchain/core/load/serializable").SerializedNotImplemented;
|
|
15
15
|
name: string;
|
|
16
16
|
description: string;
|
|
17
17
|
key: string;
|
|
@@ -1,41 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.formatToOpenAIAssistantTool = exports.formatToOpenAITool = exports.formatToOpenAIFunction = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
* function to convert the schema of the `StructuredTool` into a JSON
|
|
9
|
-
* schema, which is then used as the parameters for the OpenAI function.
|
|
10
|
-
*/
|
|
11
|
-
function formatToOpenAIFunction(tool) {
|
|
12
|
-
return {
|
|
13
|
-
name: tool.name,
|
|
14
|
-
description: tool.description,
|
|
15
|
-
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
|
|
16
|
-
};
|
|
17
|
-
}
|
|
18
|
-
exports.formatToOpenAIFunction = formatToOpenAIFunction;
|
|
19
|
-
function formatToOpenAITool(tool) {
|
|
20
|
-
const schema = (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema);
|
|
21
|
-
return {
|
|
22
|
-
type: "function",
|
|
23
|
-
function: {
|
|
24
|
-
name: tool.name,
|
|
25
|
-
description: tool.description,
|
|
26
|
-
parameters: schema,
|
|
27
|
-
},
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
exports.formatToOpenAITool = formatToOpenAITool;
|
|
31
|
-
function formatToOpenAIAssistantTool(tool) {
|
|
32
|
-
return {
|
|
33
|
-
type: "function",
|
|
34
|
-
function: {
|
|
35
|
-
name: tool.name,
|
|
36
|
-
description: tool.description,
|
|
37
|
-
parameters: (0, zod_to_json_schema_1.zodToJsonSchema)(tool.schema),
|
|
38
|
-
},
|
|
39
|
-
};
|
|
40
|
-
}
|
|
41
|
-
exports.formatToOpenAIAssistantTool = formatToOpenAIAssistantTool;
|
|
4
|
+
var openai_1 = require("@langchain/openai");
|
|
5
|
+
Object.defineProperty(exports, "formatToOpenAIFunction", { enumerable: true, get: function () { return openai_1.formatToOpenAIFunction; } });
|
|
6
|
+
Object.defineProperty(exports, "formatToOpenAITool", { enumerable: true, get: function () { return openai_1.formatToOpenAITool; } });
|
|
7
|
+
Object.defineProperty(exports, "formatToOpenAIAssistantTool", { enumerable: true, get: function () { return openai_1.formatToOpenAIAssistantTool; } });
|
|
@@ -1,11 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { StructuredTool } from "./base.js";
|
|
3
|
-
/**
|
|
4
|
-
* Formats a `StructuredTool` instance into a format that is compatible
|
|
5
|
-
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
|
|
6
|
-
* function to convert the schema of the `StructuredTool` into a JSON
|
|
7
|
-
* schema, which is then used as the parameters for the OpenAI function.
|
|
8
|
-
*/
|
|
9
|
-
export declare function formatToOpenAIFunction(tool: StructuredTool): OpenAIClient.Chat.ChatCompletionCreateParams.Function;
|
|
10
|
-
export declare function formatToOpenAITool(tool: StructuredTool): OpenAIClient.Chat.ChatCompletionTool;
|
|
11
|
-
export declare function formatToOpenAIAssistantTool(tool: StructuredTool): OpenAIClient.Beta.AssistantCreateParams.AssistantToolsFunction;
|
|
1
|
+
export { formatToOpenAIFunction, formatToOpenAITool, formatToOpenAIAssistantTool, } from "@langchain/openai";
|
|
@@ -1,35 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Formats a `StructuredTool` instance into a format that is compatible
|
|
4
|
-
* with OpenAI's ChatCompletionFunctions. It uses the `zodToJsonSchema`
|
|
5
|
-
* function to convert the schema of the `StructuredTool` into a JSON
|
|
6
|
-
* schema, which is then used as the parameters for the OpenAI function.
|
|
7
|
-
*/
|
|
8
|
-
export function formatToOpenAIFunction(tool) {
|
|
9
|
-
return {
|
|
10
|
-
name: tool.name,
|
|
11
|
-
description: tool.description,
|
|
12
|
-
parameters: zodToJsonSchema(tool.schema),
|
|
13
|
-
};
|
|
14
|
-
}
|
|
15
|
-
export function formatToOpenAITool(tool) {
|
|
16
|
-
const schema = zodToJsonSchema(tool.schema);
|
|
17
|
-
return {
|
|
18
|
-
type: "function",
|
|
19
|
-
function: {
|
|
20
|
-
name: tool.name,
|
|
21
|
-
description: tool.description,
|
|
22
|
-
parameters: schema,
|
|
23
|
-
},
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
export function formatToOpenAIAssistantTool(tool) {
|
|
27
|
-
return {
|
|
28
|
-
type: "function",
|
|
29
|
-
function: {
|
|
30
|
-
name: tool.name,
|
|
31
|
-
description: tool.description,
|
|
32
|
-
parameters: zodToJsonSchema(tool.schema),
|
|
33
|
-
},
|
|
34
|
-
};
|
|
35
|
-
}
|
|
1
|
+
export { formatToOpenAIFunction, formatToOpenAITool, formatToOpenAIAssistantTool, } from "@langchain/openai";
|
|
@@ -62,7 +62,7 @@ export declare class SearchApi extends Tool {
|
|
|
62
62
|
* and will throw an error if called.
|
|
63
63
|
* @returns Throws an error.
|
|
64
64
|
*/
|
|
65
|
-
toJSON(): import("langchain
|
|
65
|
+
toJSON(): import("@langchain/core/load/serializable").SerializedNotImplemented;
|
|
66
66
|
protected apiKey: string;
|
|
67
67
|
protected params: Partial<SearchApiParameters>;
|
|
68
68
|
constructor(apiKey?: string | undefined, params?: Partial<SearchApiParameters>);
|
package/dist/tools/serpapi.d.ts
CHANGED
|
@@ -281,7 +281,7 @@ type UrlParameters = Record<string, string | number | boolean | undefined | null
|
|
|
281
281
|
*/
|
|
282
282
|
export declare class SerpAPI extends Tool {
|
|
283
283
|
static lc_name(): string;
|
|
284
|
-
toJSON(): import("langchain
|
|
284
|
+
toJSON(): import("@langchain/core/load/serializable").SerializedNotImplemented;
|
|
285
285
|
protected key: string;
|
|
286
286
|
protected params: Partial<SerpAPIParameters>;
|
|
287
287
|
protected baseUrl: string;
|
package/dist/tools/serper.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare class Serper extends Tool {
|
|
|
21
21
|
* and will throw an error if called.
|
|
22
22
|
* @returns Throws an error.
|
|
23
23
|
*/
|
|
24
|
-
toJSON(): import("langchain
|
|
24
|
+
toJSON(): import("@langchain/core/load/serializable").SerializedNotImplemented;
|
|
25
25
|
protected key: string;
|
|
26
26
|
protected params: Partial<SerperParameters>;
|
|
27
27
|
constructor(apiKey?: string | undefined, params?: Partial<SerperParameters>);
|
|
@@ -1,135 +1,3 @@
|
|
|
1
|
-
import type { OpenAI as OpenAIClient } from "openai";
|
|
2
1
|
import { TiktokenModel } from "js-tiktoken/lite";
|
|
3
|
-
import { BaseLanguageModelCallOptions } from "../base_language/index.js";
|
|
4
2
|
export type { TiktokenModel };
|
|
5
|
-
export
|
|
6
|
-
/** Sampling temperature to use */
|
|
7
|
-
temperature: number;
|
|
8
|
-
/**
|
|
9
|
-
* Maximum number of tokens to generate in the completion. -1 returns as many
|
|
10
|
-
* tokens as possible given the prompt and the model's maximum context size.
|
|
11
|
-
*/
|
|
12
|
-
maxTokens?: number;
|
|
13
|
-
/** Total probability mass of tokens to consider at each step */
|
|
14
|
-
topP: number;
|
|
15
|
-
/** Penalizes repeated tokens according to frequency */
|
|
16
|
-
frequencyPenalty: number;
|
|
17
|
-
/** Penalizes repeated tokens */
|
|
18
|
-
presencePenalty: number;
|
|
19
|
-
/** Number of completions to generate for each prompt */
|
|
20
|
-
n: number;
|
|
21
|
-
/** Dictionary used to adjust the probability of specific tokens being generated */
|
|
22
|
-
logitBias?: Record<string, number>;
|
|
23
|
-
/** Unique string identifier representing your end-user, which can help OpenAI to monitor and detect abuse. */
|
|
24
|
-
user?: string;
|
|
25
|
-
/** Whether to stream the results or not. Enabling disables tokenUsage reporting */
|
|
26
|
-
streaming: boolean;
|
|
27
|
-
/** Model name to use */
|
|
28
|
-
modelName: string;
|
|
29
|
-
/** Holds any additional parameters that are valid to pass to {@link
|
|
30
|
-
* https://platform.openai.com/docs/api-reference/completions/create |
|
|
31
|
-
* `openai.createCompletion`} that are not explicitly specified on this class.
|
|
32
|
-
*/
|
|
33
|
-
modelKwargs?: Record<string, any>;
|
|
34
|
-
/** List of stop words to use when generating */
|
|
35
|
-
stop?: string[];
|
|
36
|
-
/**
|
|
37
|
-
* Timeout to use when making requests to OpenAI.
|
|
38
|
-
*/
|
|
39
|
-
timeout?: number;
|
|
40
|
-
/**
|
|
41
|
-
* API key to use when making requests to OpenAI. Defaults to the value of
|
|
42
|
-
* `OPENAI_API_KEY` environment variable.
|
|
43
|
-
*/
|
|
44
|
-
openAIApiKey?: string;
|
|
45
|
-
}
|
|
46
|
-
export type OpenAICoreRequestOptions<Req extends object = Record<string, unknown>> = {
|
|
47
|
-
path?: string;
|
|
48
|
-
query?: Req | undefined;
|
|
49
|
-
body?: Req | undefined;
|
|
50
|
-
headers?: Record<string, string | null | undefined> | undefined;
|
|
51
|
-
maxRetries?: number;
|
|
52
|
-
stream?: boolean | undefined;
|
|
53
|
-
timeout?: number;
|
|
54
|
-
httpAgent?: any;
|
|
55
|
-
signal?: AbortSignal | undefined | null;
|
|
56
|
-
idempotencyKey?: string;
|
|
57
|
-
};
|
|
58
|
-
export interface OpenAICallOptions extends BaseLanguageModelCallOptions {
|
|
59
|
-
/**
|
|
60
|
-
* Additional options to pass to the underlying axios request.
|
|
61
|
-
*/
|
|
62
|
-
options?: OpenAICoreRequestOptions;
|
|
63
|
-
}
|
|
64
|
-
/**
|
|
65
|
-
* Input to OpenAI class.
|
|
66
|
-
*/
|
|
67
|
-
export declare interface OpenAIInput extends OpenAIBaseInput {
|
|
68
|
-
/** Generates `bestOf` completions server side and returns the "best" */
|
|
69
|
-
bestOf?: number;
|
|
70
|
-
/** Batch size to use when passing multiple documents to generate */
|
|
71
|
-
batchSize: number;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* @deprecated Use "baseURL", "defaultHeaders", and "defaultParams" instead.
|
|
75
|
-
*/
|
|
76
|
-
export interface LegacyOpenAIInput {
|
|
77
|
-
/** @deprecated Use baseURL instead */
|
|
78
|
-
basePath?: string;
|
|
79
|
-
/** @deprecated Use defaultHeaders and defaultQuery instead */
|
|
80
|
-
baseOptions?: {
|
|
81
|
-
headers?: Record<string, string>;
|
|
82
|
-
params?: Record<string, string>;
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
export interface OpenAIChatInput extends OpenAIBaseInput {
|
|
86
|
-
/** ChatGPT messages to pass as a prefix to the prompt */
|
|
87
|
-
prefixMessages?: OpenAIClient.Chat.CreateChatCompletionRequestMessage[];
|
|
88
|
-
}
|
|
89
|
-
export declare interface AzureOpenAIInput {
|
|
90
|
-
/**
|
|
91
|
-
* API version to use when making requests to Azure OpenAI.
|
|
92
|
-
*/
|
|
93
|
-
azureOpenAIApiVersion?: string;
|
|
94
|
-
/**
|
|
95
|
-
* API key to use when making requests to Azure OpenAI.
|
|
96
|
-
*/
|
|
97
|
-
azureOpenAIApiKey?: string;
|
|
98
|
-
/**
|
|
99
|
-
* Azure OpenAI API instance name to use when making requests to Azure OpenAI.
|
|
100
|
-
* this is the name of the instance you created in the Azure portal.
|
|
101
|
-
* e.g. "my-openai-instance"
|
|
102
|
-
* this will be used in the endpoint URL: https://my-openai-instance.openai.azure.com/openai/deployments/{DeploymentName}/
|
|
103
|
-
*/
|
|
104
|
-
azureOpenAIApiInstanceName?: string;
|
|
105
|
-
/**
|
|
106
|
-
* Azure OpenAI API deployment name to use for completions when making requests to Azure OpenAI.
|
|
107
|
-
* This is the name of the deployment you created in the Azure portal.
|
|
108
|
-
* e.g. "my-openai-deployment"
|
|
109
|
-
* this will be used in the endpoint URL: https://{InstanceName}.openai.azure.com/openai/deployments/my-openai-deployment/
|
|
110
|
-
*/
|
|
111
|
-
azureOpenAIApiDeploymentName?: string;
|
|
112
|
-
/**
|
|
113
|
-
* Azure OpenAI API deployment name to use for embedding when making requests to Azure OpenAI.
|
|
114
|
-
* This is the name of the deployment you created in the Azure portal.
|
|
115
|
-
* This will fallback to azureOpenAIApiDeploymentName if not provided.
|
|
116
|
-
* e.g. "my-openai-deployment"
|
|
117
|
-
* this will be used in the endpoint URL: https://{InstanceName}.openai.azure.com/openai/deployments/my-openai-deployment/
|
|
118
|
-
*/
|
|
119
|
-
azureOpenAIApiEmbeddingsDeploymentName?: string;
|
|
120
|
-
/**
|
|
121
|
-
* Azure OpenAI API deployment name to use for completions when making requests to Azure OpenAI.
|
|
122
|
-
* Completions are only available for gpt-3.5-turbo and text-davinci-003 deployments.
|
|
123
|
-
* This is the name of the deployment you created in the Azure portal.
|
|
124
|
-
* This will fallback to azureOpenAIApiDeploymentName if not provided.
|
|
125
|
-
* e.g. "my-openai-deployment"
|
|
126
|
-
* this will be used in the endpoint URL: https://{InstanceName}.openai.azure.com/openai/deployments/my-openai-deployment/
|
|
127
|
-
*/
|
|
128
|
-
azureOpenAIApiCompletionsDeploymentName?: string;
|
|
129
|
-
/**
|
|
130
|
-
* Custom endpoint for Azure OpenAI API. This is useful in case you have a deployment in another region.
|
|
131
|
-
* e.g. setting this value to "https://westeurope.api.cognitive.microsoft.com/openai/deployments"
|
|
132
|
-
* will be result in the endpoint URL: https://westeurope.api.cognitive.microsoft.com/openai/deployments/{DeploymentName}/
|
|
133
|
-
*/
|
|
134
|
-
azureOpenAIBasePath?: string;
|
|
135
|
-
}
|
|
3
|
+
export type { OpenAIBaseInput, OpenAICoreRequestOptions, OpenAICallOptions, OpenAIInput, LegacyOpenAIInput, OpenAIChatInput, AzureOpenAIInput, } from "@langchain/openai";
|
|
@@ -14,4 +14,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
__exportStar(require("langchain
|
|
17
|
+
__exportStar(require("@langchain/core/utils/async_caller"), exports);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "langchain
|
|
1
|
+
export * from "@langchain/core/utils/async_caller";
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from "langchain
|
|
1
|
+
export * from "@langchain/core/utils/async_caller";
|
package/dist/util/openai.cjs
CHANGED
|
@@ -1,21 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.wrapOpenAIClientError = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function wrapOpenAIClientError(e) {
|
|
7
|
-
let error;
|
|
8
|
-
if (e.constructor.name === openai_1.APIConnectionTimeoutError.name) {
|
|
9
|
-
error = new Error(e.message);
|
|
10
|
-
error.name = "TimeoutError";
|
|
11
|
-
}
|
|
12
|
-
else if (e.constructor.name === openai_1.APIUserAbortError.name) {
|
|
13
|
-
error = new Error(e.message);
|
|
14
|
-
error.name = "AbortError";
|
|
15
|
-
}
|
|
16
|
-
else {
|
|
17
|
-
error = e;
|
|
18
|
-
}
|
|
19
|
-
return error;
|
|
20
|
-
}
|
|
21
|
-
exports.wrapOpenAIClientError = wrapOpenAIClientError;
|
|
4
|
+
var openai_1 = require("@langchain/openai");
|
|
5
|
+
Object.defineProperty(exports, "wrapOpenAIClientError", { enumerable: true, get: function () { return openai_1.wrapOpenAIClientError; } });
|
package/dist/util/openai.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export { wrapOpenAIClientError } from "@langchain/openai";
|
package/dist/util/openai.js
CHANGED
|
@@ -1,17 +1 @@
|
|
|
1
|
-
|
|
2
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
|
-
export function wrapOpenAIClientError(e) {
|
|
4
|
-
let error;
|
|
5
|
-
if (e.constructor.name === APIConnectionTimeoutError.name) {
|
|
6
|
-
error = new Error(e.message);
|
|
7
|
-
error.name = "TimeoutError";
|
|
8
|
-
}
|
|
9
|
-
else if (e.constructor.name === APIUserAbortError.name) {
|
|
10
|
-
error = new Error(e.message);
|
|
11
|
-
error.name = "AbortError";
|
|
12
|
-
}
|
|
13
|
-
else {
|
|
14
|
-
error = e;
|
|
15
|
-
}
|
|
16
|
-
return error;
|
|
17
|
-
}
|
|
1
|
+
export { wrapOpenAIClientError } from "@langchain/openai";
|
package/dist/util/openapi.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare class OpenAPISpec {
|
|
|
7
7
|
getParametersStrict(): Record<string, OpenAPIV3.ParameterObject | OpenAPIV3_1.ReferenceObject>;
|
|
8
8
|
getSchemasStrict(): Record<string, OpenAPIV3_1.SchemaObject>;
|
|
9
9
|
getRequestBodiesStrict(): Record<string, OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.RequestBodyObject>;
|
|
10
|
-
getPathStrict(path: string): Omit<OpenAPIV3.PathItemObject<{}>, "
|
|
10
|
+
getPathStrict(path: string): Omit<OpenAPIV3.PathItemObject<{}>, "servers" | "parameters"> & {
|
|
11
11
|
servers?: OpenAPIV3_1.ServerObject[] | undefined;
|
|
12
12
|
parameters?: (OpenAPIV3.ParameterObject | OpenAPIV3_1.ReferenceObject)[] | undefined;
|
|
13
13
|
} & {
|
|
@@ -59,7 +59,7 @@ export declare class OpenAPISpec {
|
|
|
59
59
|
deprecated?: boolean | undefined;
|
|
60
60
|
security?: OpenAPIV3.SecurityRequirementObject[] | undefined;
|
|
61
61
|
servers?: OpenAPIV3.ServerObject[] | undefined;
|
|
62
|
-
}, "callbacks" | "
|
|
62
|
+
}, "callbacks" | "servers" | "parameters" | "responses" | "requestBody"> & {
|
|
63
63
|
parameters?: (OpenAPIV3.ParameterObject | OpenAPIV3_1.ReferenceObject)[] | undefined;
|
|
64
64
|
requestBody?: OpenAPIV3_1.ReferenceObject | OpenAPIV3_1.RequestBodyObject | undefined;
|
|
65
65
|
responses?: OpenAPIV3_1.ResponsesObject | undefined;
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { OpenAIClient } from "@langchain/openai";
|
|
2
2
|
import { AsyncCaller } from "../util/async_caller.js";
|
|
3
3
|
export declare const promptLayerTrackRequest: (callerFunc: AsyncCaller, functionName: string, kwargs: OpenAIClient.CompletionCreateParams | OpenAIClient.Chat.CompletionCreateParams, plTags: string[] | undefined, requestResponse: any, startTime: number, endTime: number, apiKey: string | undefined) => Promise<any>;
|
|
@@ -30,6 +30,7 @@ const sdk_core_1 = require("@gomomento/sdk-core");
|
|
|
30
30
|
const uuid = __importStar(require("uuid"));
|
|
31
31
|
const document_js_1 = require("../document.cjs");
|
|
32
32
|
const base_js_1 = require("./base.cjs");
|
|
33
|
+
const math_js_1 = require("../util/math.cjs");
|
|
33
34
|
/**
|
|
34
35
|
* A vector store that uses the Momento Vector Index.
|
|
35
36
|
*
|
|
@@ -234,6 +235,44 @@ class MomentoVectorIndex extends base_js_1.VectorStore {
|
|
|
234
235
|
throw new Error(`Unknown response type: ${response.toString()}`);
|
|
235
236
|
}
|
|
236
237
|
}
|
|
238
|
+
/**
|
|
239
|
+
* Return documents selected using the maximal marginal relevance.
|
|
240
|
+
* Maximal marginal relevance optimizes for similarity to the query AND diversity
|
|
241
|
+
* among selected documents.
|
|
242
|
+
*
|
|
243
|
+
* @param {string} query - Text to look up documents similar to.
|
|
244
|
+
* @param {number} options.k - Number of documents to return.
|
|
245
|
+
* @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm.
|
|
246
|
+
* @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results,
|
|
247
|
+
* where 0 corresponds to maximum diversity and 1 to minimum diversity.
|
|
248
|
+
* @param {this["FilterType"]} options.filter - Optional filter
|
|
249
|
+
* @param _callbacks
|
|
250
|
+
*
|
|
251
|
+
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
|
|
252
|
+
*/
|
|
253
|
+
async maxMarginalRelevanceSearch(query, options) {
|
|
254
|
+
const queryEmbedding = await this.embeddings.embedQuery(query);
|
|
255
|
+
const response = await this.client.searchAndFetchVectors(this.indexName, queryEmbedding, { topK: options.fetchK ?? 20, metadataFields: sdk_core_1.ALL_VECTOR_METADATA });
|
|
256
|
+
if (response instanceof sdk_core_1.VectorSearchAndFetchVectors.Success) {
|
|
257
|
+
const hits = response.hits();
|
|
258
|
+
// Gather the embeddings of the search results
|
|
259
|
+
const embeddingList = hits.map((hit) => hit.vector);
|
|
260
|
+
// Gather the ids of the most relevant results when applying MMR
|
|
261
|
+
const mmrIndexes = (0, math_js_1.maximalMarginalRelevance)(queryEmbedding, embeddingList, options.lambda, options.k);
|
|
262
|
+
const finalResult = mmrIndexes.map((index) => {
|
|
263
|
+
const hit = hits[index];
|
|
264
|
+
const { [this.textField]: pageContent, ...metadata } = hit.metadata;
|
|
265
|
+
return new document_js_1.Document({ metadata, pageContent: pageContent });
|
|
266
|
+
});
|
|
267
|
+
return finalResult;
|
|
268
|
+
}
|
|
269
|
+
else if (response instanceof sdk_core_1.VectorSearchAndFetchVectors.Error) {
|
|
270
|
+
throw new Error(response.toString());
|
|
271
|
+
}
|
|
272
|
+
else {
|
|
273
|
+
throw new Error(`Unknown response type: ${response.toString()}`);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
237
276
|
/**
|
|
238
277
|
* Stores the documents in the index.
|
|
239
278
|
*
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { IVectorIndexClient } from "@gomomento/sdk-core";
|
|
2
2
|
import { Document } from "../document.js";
|
|
3
3
|
import { Embeddings } from "../embeddings/base.js";
|
|
4
|
-
import { VectorStore } from "./base.js";
|
|
4
|
+
import { MaxMarginalRelevanceSearchOptions, VectorStore } from "./base.js";
|
|
5
5
|
export interface DocumentProps {
|
|
6
6
|
ids: string[];
|
|
7
7
|
}
|
|
@@ -103,6 +103,22 @@ export declare class MomentoVectorIndex extends VectorStore {
|
|
|
103
103
|
* to the query vector.
|
|
104
104
|
*/
|
|
105
105
|
similaritySearchVectorWithScore(query: number[], k: number): Promise<[Document<Record<string, any>>, number][]>;
|
|
106
|
+
/**
|
|
107
|
+
* Return documents selected using the maximal marginal relevance.
|
|
108
|
+
* Maximal marginal relevance optimizes for similarity to the query AND diversity
|
|
109
|
+
* among selected documents.
|
|
110
|
+
*
|
|
111
|
+
* @param {string} query - Text to look up documents similar to.
|
|
112
|
+
* @param {number} options.k - Number of documents to return.
|
|
113
|
+
* @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm.
|
|
114
|
+
* @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results,
|
|
115
|
+
* where 0 corresponds to maximum diversity and 1 to minimum diversity.
|
|
116
|
+
* @param {this["FilterType"]} options.filter - Optional filter
|
|
117
|
+
* @param _callbacks
|
|
118
|
+
*
|
|
119
|
+
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
|
|
120
|
+
*/
|
|
121
|
+
maxMarginalRelevanceSearch(query: string, options: MaxMarginalRelevanceSearchOptions<this["FilterType"]>): Promise<Document[]>;
|
|
106
122
|
/**
|
|
107
123
|
* Stores the documents in the index.
|
|
108
124
|
*
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/* eslint-disable no-instanceof/no-instanceof */
|
|
2
2
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
import { ALL_VECTOR_METADATA, CreateVectorIndex, VectorUpsertItemBatch, VectorDeleteItemBatch, VectorSearch, } from "@gomomento/sdk-core";
|
|
3
|
+
import { ALL_VECTOR_METADATA, CreateVectorIndex, VectorUpsertItemBatch, VectorDeleteItemBatch, VectorSearch, VectorSearchAndFetchVectors, } from "@gomomento/sdk-core";
|
|
4
4
|
import * as uuid from "uuid";
|
|
5
5
|
import { Document } from "../document.js";
|
|
6
6
|
import { VectorStore } from "./base.js";
|
|
7
|
+
import { maximalMarginalRelevance } from "../util/math.js";
|
|
7
8
|
/**
|
|
8
9
|
* A vector store that uses the Momento Vector Index.
|
|
9
10
|
*
|
|
@@ -208,6 +209,44 @@ export class MomentoVectorIndex extends VectorStore {
|
|
|
208
209
|
throw new Error(`Unknown response type: ${response.toString()}`);
|
|
209
210
|
}
|
|
210
211
|
}
|
|
212
|
+
/**
|
|
213
|
+
* Return documents selected using the maximal marginal relevance.
|
|
214
|
+
* Maximal marginal relevance optimizes for similarity to the query AND diversity
|
|
215
|
+
* among selected documents.
|
|
216
|
+
*
|
|
217
|
+
* @param {string} query - Text to look up documents similar to.
|
|
218
|
+
* @param {number} options.k - Number of documents to return.
|
|
219
|
+
* @param {number} options.fetchK - Number of documents to fetch before passing to the MMR algorithm.
|
|
220
|
+
* @param {number} options.lambda - Number between 0 and 1 that determines the degree of diversity among the results,
|
|
221
|
+
* where 0 corresponds to maximum diversity and 1 to minimum diversity.
|
|
222
|
+
* @param {this["FilterType"]} options.filter - Optional filter
|
|
223
|
+
* @param _callbacks
|
|
224
|
+
*
|
|
225
|
+
* @returns {Promise<Document[]>} - List of documents selected by maximal marginal relevance.
|
|
226
|
+
*/
|
|
227
|
+
async maxMarginalRelevanceSearch(query, options) {
|
|
228
|
+
const queryEmbedding = await this.embeddings.embedQuery(query);
|
|
229
|
+
const response = await this.client.searchAndFetchVectors(this.indexName, queryEmbedding, { topK: options.fetchK ?? 20, metadataFields: ALL_VECTOR_METADATA });
|
|
230
|
+
if (response instanceof VectorSearchAndFetchVectors.Success) {
|
|
231
|
+
const hits = response.hits();
|
|
232
|
+
// Gather the embeddings of the search results
|
|
233
|
+
const embeddingList = hits.map((hit) => hit.vector);
|
|
234
|
+
// Gather the ids of the most relevant results when applying MMR
|
|
235
|
+
const mmrIndexes = maximalMarginalRelevance(queryEmbedding, embeddingList, options.lambda, options.k);
|
|
236
|
+
const finalResult = mmrIndexes.map((index) => {
|
|
237
|
+
const hit = hits[index];
|
|
238
|
+
const { [this.textField]: pageContent, ...metadata } = hit.metadata;
|
|
239
|
+
return new Document({ metadata, pageContent: pageContent });
|
|
240
|
+
});
|
|
241
|
+
return finalResult;
|
|
242
|
+
}
|
|
243
|
+
else if (response instanceof VectorSearchAndFetchVectors.Error) {
|
|
244
|
+
throw new Error(response.toString());
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
throw new Error(`Unknown response type: ${response.toString()}`);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
211
250
|
/**
|
|
212
251
|
* Stores the documents in the index.
|
|
213
252
|
*
|
|
@@ -99,7 +99,7 @@ class MongoDBAtlasVectorSearch extends base_js_1.VectorStore {
|
|
|
99
99
|
const pipeline = [
|
|
100
100
|
{
|
|
101
101
|
$vectorSearch: {
|
|
102
|
-
queryVector: query,
|
|
102
|
+
queryVector: MongoDBAtlasVectorSearch.fixArrayPrecision(query),
|
|
103
103
|
index: this.indexName,
|
|
104
104
|
path: this.embeddingKey,
|
|
105
105
|
limit: k,
|
|
@@ -148,7 +148,7 @@ class MongoDBAtlasVectorSearch extends base_js_1.VectorStore {
|
|
|
148
148
|
...filter,
|
|
149
149
|
includeEmbeddings: true,
|
|
150
150
|
};
|
|
151
|
-
const resultDocs = await this.similaritySearchVectorWithScore(queryEmbedding, fetchK, includeEmbeddingsFilter);
|
|
151
|
+
const resultDocs = await this.similaritySearchVectorWithScore(MongoDBAtlasVectorSearch.fixArrayPrecision(queryEmbedding), fetchK, includeEmbeddingsFilter);
|
|
152
152
|
const embeddingList = resultDocs.map((doc) => doc[0].metadata[this.embeddingKey]);
|
|
153
153
|
const mmrIndexes = (0, math_js_1.maximalMarginalRelevance)(queryEmbedding, embeddingList, lambda, k);
|
|
154
154
|
return mmrIndexes.map((idx) => {
|
|
@@ -196,5 +196,25 @@ class MongoDBAtlasVectorSearch extends base_js_1.VectorStore {
|
|
|
196
196
|
await instance.addDocuments(docs);
|
|
197
197
|
return instance;
|
|
198
198
|
}
|
|
199
|
+
/**
|
|
200
|
+
* Static method to fix the precision of the array that ensures that
|
|
201
|
+
* every number in this array is always float when casted to other types.
|
|
202
|
+
* This is needed since MongoDB Atlas Vector Search does not cast integer
|
|
203
|
+
* inside vector search to float automatically.
|
|
204
|
+
* This method shall introduce a hint of error but should be safe to use
|
|
205
|
+
* since introduced error is very small, only applies to integer numbers
|
|
206
|
+
* returned by embeddings, and most embeddings shall not have precision
|
|
207
|
+
* as high as 15 decimal places.
|
|
208
|
+
* @param array Array of number to be fixed.
|
|
209
|
+
* @returns
|
|
210
|
+
*/
|
|
211
|
+
static fixArrayPrecision(array) {
|
|
212
|
+
return array.map((value) => {
|
|
213
|
+
if (Number.isInteger(value)) {
|
|
214
|
+
return value + 0.000000000000001;
|
|
215
|
+
}
|
|
216
|
+
return value;
|
|
217
|
+
});
|
|
218
|
+
}
|
|
199
219
|
}
|
|
200
220
|
exports.MongoDBAtlasVectorSearch = MongoDBAtlasVectorSearch;
|