langchain 0.0.165 → 0.0.167
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/cache/momento.cjs +7 -7
- package/dist/cache/momento.d.ts +1 -1
- package/dist/cache/momento.js +1 -1
- package/dist/document_loaders/fs/unstructured.cjs +40 -0
- package/dist/document_loaders/fs/unstructured.d.ts +27 -0
- package/dist/document_loaders/fs/unstructured.js +40 -0
- package/dist/embeddings/bedrock.cjs +43 -22
- package/dist/embeddings/bedrock.d.ts +11 -4
- package/dist/embeddings/bedrock.js +43 -22
- package/dist/experimental/chat_models/anthropic_functions.cjs +34 -2
- package/dist/experimental/chat_models/anthropic_functions.d.ts +15 -5
- package/dist/experimental/chat_models/anthropic_functions.js +34 -2
- package/dist/llms/yandex.cjs +100 -0
- package/dist/llms/yandex.d.ts +40 -0
- package/dist/llms/yandex.js +96 -0
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/load/import_map.cjs +6 -2
- package/dist/load/import_map.d.ts +4 -0
- package/dist/load/import_map.js +4 -0
- package/dist/prompts/chat.cjs +19 -2
- package/dist/prompts/chat.d.ts +1 -0
- package/dist/prompts/chat.js +19 -2
- package/dist/retrievers/multi_vector.d.ts +3 -3
- package/dist/retrievers/parent_document.cjs +6 -16
- package/dist/retrievers/parent_document.d.ts +5 -12
- package/dist/retrievers/parent_document.js +6 -16
- package/dist/retrievers/self_query/base.cjs +4 -1
- package/dist/retrievers/self_query/base.d.ts +3 -2
- package/dist/retrievers/self_query/base.js +4 -1
- package/dist/retrievers/self_query/index.cjs +2 -2
- package/dist/retrievers/self_query/index.d.ts +2 -0
- package/dist/retrievers/self_query/index.js +2 -2
- package/dist/runnables/remote.cjs +225 -0
- package/dist/runnables/remote.d.ts +28 -0
- package/dist/runnables/remote.js +221 -0
- package/dist/schema/index.cjs +1 -1
- package/dist/schema/index.d.ts +1 -1
- package/dist/schema/index.js +1 -1
- package/dist/schema/runnable/base.cjs +4 -4
- package/dist/schema/runnable/base.d.ts +9 -7
- package/dist/schema/runnable/base.js +4 -4
- package/dist/schema/runnable/remote.cjs +225 -0
- package/dist/schema/runnable/remote.d.ts +28 -0
- package/dist/schema/runnable/remote.js +221 -0
- package/dist/schema/storage.d.ts +28 -1
- package/dist/storage/encoder_backed.cjs +14 -2
- package/dist/storage/encoder_backed.d.ts +2 -0
- package/dist/storage/encoder_backed.js +12 -1
- package/dist/storage/in_memory.cjs +1 -1
- package/dist/storage/in_memory.js +1 -1
- package/dist/storage/ioredis.cjs +4 -4
- package/dist/storage/ioredis.js +4 -4
- package/dist/storage/vercel_kv.cjs +146 -0
- package/dist/storage/vercel_kv.d.ts +46 -0
- package/dist/storage/vercel_kv.js +142 -0
- package/dist/stores/doc/in_memory.cjs +13 -0
- package/dist/stores/doc/in_memory.d.ts +6 -1
- package/dist/stores/doc/in_memory.js +13 -0
- package/dist/stores/message/momento.cjs +11 -11
- package/dist/stores/message/momento.d.ts +1 -1
- package/dist/stores/message/momento.js +1 -1
- package/dist/util/time.cjs +14 -0
- package/dist/util/time.d.ts +6 -0
- package/dist/util/time.js +10 -0
- package/dist/vectorstores/cassandra.cjs +4 -2
- package/dist/vectorstores/cassandra.js +4 -2
- package/dist/vectorstores/elasticsearch.cjs +3 -1
- package/dist/vectorstores/elasticsearch.js +3 -1
- package/dist/vectorstores/momento_vector_index.cjs +292 -0
- package/dist/vectorstores/momento_vector_index.d.ts +135 -0
- package/dist/vectorstores/momento_vector_index.js +265 -0
- package/dist/vectorstores/neo4j_vector.cjs +578 -0
- package/dist/vectorstores/neo4j_vector.d.ts +61 -0
- package/dist/vectorstores/neo4j_vector.js +548 -0
- package/dist/vectorstores/supabase.cjs +37 -8
- package/dist/vectorstores/supabase.d.ts +28 -1
- package/dist/vectorstores/supabase.js +37 -8
- package/llms/yandex.cjs +1 -0
- package/llms/yandex.d.ts +1 -0
- package/llms/yandex.js +1 -0
- package/package.json +73 -3
- package/runnables/remote.cjs +1 -0
- package/runnables/remote.d.ts +1 -0
- package/runnables/remote.js +1 -0
- package/storage/encoder_backed.cjs +1 -0
- package/storage/encoder_backed.d.ts +1 -0
- package/storage/encoder_backed.js +1 -0
- package/storage/vercel_kv.cjs +1 -0
- package/storage/vercel_kv.d.ts +1 -0
- package/storage/vercel_kv.js +1 -0
- package/util/time.cjs +1 -0
- package/util/time.d.ts +1 -0
- package/util/time.js +1 -0
- package/vectorstores/momento_vector_index.cjs +1 -0
- package/vectorstores/momento_vector_index.d.ts +1 -0
- package/vectorstores/momento_vector_index.js +1 -0
- package/vectorstores/neo4j_vector.cjs +1 -0
- package/vectorstores/neo4j_vector.d.ts +1 -0
- package/vectorstores/neo4j_vector.js +1 -0
package/dist/cache/momento.cjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MomentoCache = void 0;
|
|
4
4
|
/* eslint-disable no-instanceof/no-instanceof */
|
|
5
|
-
const
|
|
5
|
+
const sdk_core_1 = require("@gomomento/sdk-core");
|
|
6
6
|
const index_js_1 = require("../schema/index.cjs");
|
|
7
7
|
const base_js_1 = require("./base.cjs");
|
|
8
8
|
const momento_js_1 = require("../util/momento.cjs");
|
|
@@ -62,7 +62,7 @@ class MomentoCache extends index_js_1.BaseCache {
|
|
|
62
62
|
*/
|
|
63
63
|
validateTtlSeconds(ttlSeconds) {
|
|
64
64
|
if (ttlSeconds !== undefined && ttlSeconds <= 0) {
|
|
65
|
-
throw new
|
|
65
|
+
throw new sdk_core_1.InvalidArgumentError("ttlSeconds must be positive.");
|
|
66
66
|
}
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
@@ -74,7 +74,7 @@ class MomentoCache extends index_js_1.BaseCache {
|
|
|
74
74
|
async lookup(prompt, llmKey) {
|
|
75
75
|
const key = (0, base_js_1.getCacheKey)(prompt, llmKey);
|
|
76
76
|
const getResponse = await this.client.get(this.cacheName, key);
|
|
77
|
-
if (getResponse instanceof
|
|
77
|
+
if (getResponse instanceof sdk_core_1.CacheGet.Hit) {
|
|
78
78
|
const value = getResponse.valueString();
|
|
79
79
|
const parsedValue = JSON.parse(value);
|
|
80
80
|
if (!Array.isArray(parsedValue)) {
|
|
@@ -82,10 +82,10 @@ class MomentoCache extends index_js_1.BaseCache {
|
|
|
82
82
|
}
|
|
83
83
|
return JSON.parse(value).map(base_js_1.deserializeStoredGeneration);
|
|
84
84
|
}
|
|
85
|
-
else if (getResponse instanceof
|
|
85
|
+
else if (getResponse instanceof sdk_core_1.CacheGet.Miss) {
|
|
86
86
|
return null;
|
|
87
87
|
}
|
|
88
|
-
else if (getResponse instanceof
|
|
88
|
+
else if (getResponse instanceof sdk_core_1.CacheGet.Error) {
|
|
89
89
|
throw getResponse.innerException();
|
|
90
90
|
}
|
|
91
91
|
else {
|
|
@@ -104,10 +104,10 @@ class MomentoCache extends index_js_1.BaseCache {
|
|
|
104
104
|
async update(prompt, llmKey, value) {
|
|
105
105
|
const key = (0, base_js_1.getCacheKey)(prompt, llmKey);
|
|
106
106
|
const setResponse = await this.client.set(this.cacheName, key, JSON.stringify(value.map(base_js_1.serializeGeneration)), { ttl: this.ttlSeconds });
|
|
107
|
-
if (setResponse instanceof
|
|
107
|
+
if (setResponse instanceof sdk_core_1.CacheSet.Success) {
|
|
108
108
|
// pass
|
|
109
109
|
}
|
|
110
|
-
else if (setResponse instanceof
|
|
110
|
+
else if (setResponse instanceof sdk_core_1.CacheSet.Error) {
|
|
111
111
|
throw setResponse.innerException();
|
|
112
112
|
}
|
|
113
113
|
else {
|
package/dist/cache/momento.d.ts
CHANGED
package/dist/cache/momento.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/* eslint-disable no-instanceof/no-instanceof */
|
|
2
|
-
import { CacheGet, CacheSet, InvalidArgumentError, } from "@gomomento/sdk";
|
|
2
|
+
import { CacheGet, CacheSet, InvalidArgumentError, } from "@gomomento/sdk-core";
|
|
3
3
|
import { BaseCache } from "../schema/index.js";
|
|
4
4
|
import { deserializeStoredGeneration, getCacheKey, serializeGeneration, } from "./base.js";
|
|
5
5
|
import { ensureCacheExists } from "../util/momento.js";
|
|
@@ -92,6 +92,30 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
|
|
|
92
92
|
writable: true,
|
|
93
93
|
value: void 0
|
|
94
94
|
});
|
|
95
|
+
Object.defineProperty(this, "skipInferTableTypes", {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
configurable: true,
|
|
98
|
+
writable: true,
|
|
99
|
+
value: void 0
|
|
100
|
+
});
|
|
101
|
+
Object.defineProperty(this, "hiResModelName", {
|
|
102
|
+
enumerable: true,
|
|
103
|
+
configurable: true,
|
|
104
|
+
writable: true,
|
|
105
|
+
value: void 0
|
|
106
|
+
});
|
|
107
|
+
Object.defineProperty(this, "includePageBreaks", {
|
|
108
|
+
enumerable: true,
|
|
109
|
+
configurable: true,
|
|
110
|
+
writable: true,
|
|
111
|
+
value: void 0
|
|
112
|
+
});
|
|
113
|
+
Object.defineProperty(this, "chunkingStrategy", {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
configurable: true,
|
|
116
|
+
writable: true,
|
|
117
|
+
value: void 0
|
|
118
|
+
});
|
|
95
119
|
// Temporary shim to avoid breaking existing users
|
|
96
120
|
// Remove when API keys are enforced by Unstructured and existing code will break anyway
|
|
97
121
|
const isLegacySyntax = typeof optionsOrLegacyFilePath === "string";
|
|
@@ -110,6 +134,10 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
|
|
|
110
134
|
this.coordinates = options.coordinates;
|
|
111
135
|
this.pdfInferTableStructure = options.pdfInferTableStructure;
|
|
112
136
|
this.xmlKeepTags = options.xmlKeepTags;
|
|
137
|
+
this.skipInferTableTypes = options.skipInferTableTypes;
|
|
138
|
+
this.hiResModelName = options.hiResModelName;
|
|
139
|
+
this.includePageBreaks = options.includePageBreaks;
|
|
140
|
+
this.chunkingStrategy = options.chunkingStrategy;
|
|
113
141
|
}
|
|
114
142
|
}
|
|
115
143
|
async _partition() {
|
|
@@ -137,6 +165,18 @@ class UnstructuredLoader extends base_js_1.BaseDocumentLoader {
|
|
|
137
165
|
if (this.xmlKeepTags === true) {
|
|
138
166
|
formData.append("xml_keep_tags", "true");
|
|
139
167
|
}
|
|
168
|
+
if (this.skipInferTableTypes) {
|
|
169
|
+
formData.append("skip_infer_table_types", JSON.stringify(this.skipInferTableTypes));
|
|
170
|
+
}
|
|
171
|
+
if (this.hiResModelName) {
|
|
172
|
+
formData.append("hi_res_model_name", this.hiResModelName);
|
|
173
|
+
}
|
|
174
|
+
if (this.includePageBreaks) {
|
|
175
|
+
formData.append("include_page_breaks", "true");
|
|
176
|
+
}
|
|
177
|
+
if (this.chunkingStrategy) {
|
|
178
|
+
formData.append("chunking_strategy", this.chunkingStrategy);
|
|
179
|
+
}
|
|
140
180
|
const headers = {
|
|
141
181
|
"UNSTRUCTURED-API-KEY": this.apiKey ?? "",
|
|
142
182
|
};
|
|
@@ -21,6 +21,25 @@ type Element = {
|
|
|
21
21
|
* be one of "hi_res", "fast", "ocr_only", or "auto".
|
|
22
22
|
*/
|
|
23
23
|
export type UnstructuredLoaderStrategy = "hi_res" | "fast" | "ocr_only" | "auto";
|
|
24
|
+
/**
|
|
25
|
+
* Represents the available hi-res models for the UnstructuredLoader. It can
|
|
26
|
+
* be one of "chipper".
|
|
27
|
+
*/
|
|
28
|
+
export type HiResModelName = "chipper";
|
|
29
|
+
/**
|
|
30
|
+
* To enable or disable table extraction for file types other than PDF, set
|
|
31
|
+
* the skipInferTableTypes property in the UnstructuredLoaderOptions object.
|
|
32
|
+
* The skipInferTableTypes property is an array of file types for which table
|
|
33
|
+
* extraction is disabled. For example, to disable table extraction for .docx
|
|
34
|
+
* and .doc files, set the skipInferTableTypes property to ["docx", "doc"].
|
|
35
|
+
* You can also disable table extraction for all file types other than PDF by
|
|
36
|
+
* setting the skipInferTableTypes property to [].
|
|
37
|
+
*/
|
|
38
|
+
export type SkipInferTableTypes = "txt" | "text" | "pdf" | "docx" | "doc" | "jpg" | "jpeg" | "eml" | "html" | "htm" | "md" | "pptx" | "ppt" | "msg" | "rtf" | "xlsx" | "xls" | "odt" | "epub";
|
|
39
|
+
/**
|
|
40
|
+
* Set the chunking_strategy to chunk text into larger or smaller elements. Defaults to None with optional arg of by_title
|
|
41
|
+
*/
|
|
42
|
+
type ChunkingStrategy = "None" | "by_title";
|
|
24
43
|
/**
|
|
25
44
|
* Represents a string value with autocomplete suggestions. It is used for
|
|
26
45
|
* the `strategy` property in the UnstructuredLoaderOptions.
|
|
@@ -35,6 +54,10 @@ export type UnstructuredLoaderOptions = {
|
|
|
35
54
|
coordinates?: boolean;
|
|
36
55
|
pdfInferTableStructure?: boolean;
|
|
37
56
|
xmlKeepTags?: boolean;
|
|
57
|
+
skipInferTableTypes?: Array<StringWithAutocomplete<SkipInferTableTypes>>;
|
|
58
|
+
hiResModelName?: StringWithAutocomplete<HiResModelName>;
|
|
59
|
+
includePageBreaks?: boolean;
|
|
60
|
+
chunkingStrategy?: StringWithAutocomplete<ChunkingStrategy>;
|
|
38
61
|
};
|
|
39
62
|
type UnstructuredDirectoryLoaderOptions = UnstructuredLoaderOptions & {
|
|
40
63
|
recursive?: boolean;
|
|
@@ -58,6 +81,10 @@ export declare class UnstructuredLoader extends BaseDocumentLoader {
|
|
|
58
81
|
private coordinates?;
|
|
59
82
|
private pdfInferTableStructure?;
|
|
60
83
|
private xmlKeepTags?;
|
|
84
|
+
private skipInferTableTypes?;
|
|
85
|
+
private hiResModelName?;
|
|
86
|
+
private includePageBreaks?;
|
|
87
|
+
private chunkingStrategy?;
|
|
61
88
|
constructor(filePathOrLegacyApiUrl: string, optionsOrLegacyFilePath?: UnstructuredLoaderOptions | string);
|
|
62
89
|
_partition(): Promise<Element[]>;
|
|
63
90
|
load(): Promise<Document[]>;
|
|
@@ -88,6 +88,30 @@ export class UnstructuredLoader extends BaseDocumentLoader {
|
|
|
88
88
|
writable: true,
|
|
89
89
|
value: void 0
|
|
90
90
|
});
|
|
91
|
+
Object.defineProperty(this, "skipInferTableTypes", {
|
|
92
|
+
enumerable: true,
|
|
93
|
+
configurable: true,
|
|
94
|
+
writable: true,
|
|
95
|
+
value: void 0
|
|
96
|
+
});
|
|
97
|
+
Object.defineProperty(this, "hiResModelName", {
|
|
98
|
+
enumerable: true,
|
|
99
|
+
configurable: true,
|
|
100
|
+
writable: true,
|
|
101
|
+
value: void 0
|
|
102
|
+
});
|
|
103
|
+
Object.defineProperty(this, "includePageBreaks", {
|
|
104
|
+
enumerable: true,
|
|
105
|
+
configurable: true,
|
|
106
|
+
writable: true,
|
|
107
|
+
value: void 0
|
|
108
|
+
});
|
|
109
|
+
Object.defineProperty(this, "chunkingStrategy", {
|
|
110
|
+
enumerable: true,
|
|
111
|
+
configurable: true,
|
|
112
|
+
writable: true,
|
|
113
|
+
value: void 0
|
|
114
|
+
});
|
|
91
115
|
// Temporary shim to avoid breaking existing users
|
|
92
116
|
// Remove when API keys are enforced by Unstructured and existing code will break anyway
|
|
93
117
|
const isLegacySyntax = typeof optionsOrLegacyFilePath === "string";
|
|
@@ -106,6 +130,10 @@ export class UnstructuredLoader extends BaseDocumentLoader {
|
|
|
106
130
|
this.coordinates = options.coordinates;
|
|
107
131
|
this.pdfInferTableStructure = options.pdfInferTableStructure;
|
|
108
132
|
this.xmlKeepTags = options.xmlKeepTags;
|
|
133
|
+
this.skipInferTableTypes = options.skipInferTableTypes;
|
|
134
|
+
this.hiResModelName = options.hiResModelName;
|
|
135
|
+
this.includePageBreaks = options.includePageBreaks;
|
|
136
|
+
this.chunkingStrategy = options.chunkingStrategy;
|
|
109
137
|
}
|
|
110
138
|
}
|
|
111
139
|
async _partition() {
|
|
@@ -133,6 +161,18 @@ export class UnstructuredLoader extends BaseDocumentLoader {
|
|
|
133
161
|
if (this.xmlKeepTags === true) {
|
|
134
162
|
formData.append("xml_keep_tags", "true");
|
|
135
163
|
}
|
|
164
|
+
if (this.skipInferTableTypes) {
|
|
165
|
+
formData.append("skip_infer_table_types", JSON.stringify(this.skipInferTableTypes));
|
|
166
|
+
}
|
|
167
|
+
if (this.hiResModelName) {
|
|
168
|
+
formData.append("hi_res_model_name", this.hiResModelName);
|
|
169
|
+
}
|
|
170
|
+
if (this.includePageBreaks) {
|
|
171
|
+
formData.append("include_page_breaks", "true");
|
|
172
|
+
}
|
|
173
|
+
if (this.chunkingStrategy) {
|
|
174
|
+
formData.append("chunking_strategy", this.chunkingStrategy);
|
|
175
|
+
}
|
|
136
176
|
const headers = {
|
|
137
177
|
"UNSTRUCTURED-API-KEY": this.apiKey ?? "",
|
|
138
178
|
};
|
|
@@ -22,6 +22,12 @@ class BedrockEmbeddings extends base_js_1.Embeddings {
|
|
|
22
22
|
writable: true,
|
|
23
23
|
value: void 0
|
|
24
24
|
});
|
|
25
|
+
Object.defineProperty(this, "batchSize", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: 512
|
|
30
|
+
});
|
|
25
31
|
this.model = fields?.model ?? "amazon.titan-embed-text-v1";
|
|
26
32
|
this.client =
|
|
27
33
|
fields?.client ??
|
|
@@ -30,24 +36,40 @@ class BedrockEmbeddings extends base_js_1.Embeddings {
|
|
|
30
36
|
credentials: fields?.credentials,
|
|
31
37
|
});
|
|
32
38
|
}
|
|
39
|
+
/**
|
|
40
|
+
* Protected method to make a request to the Bedrock API to generate
|
|
41
|
+
* embeddings. Handles the retry logic and returns the response from the
|
|
42
|
+
* API.
|
|
43
|
+
* @param request Request to send to the Bedrock API.
|
|
44
|
+
* @returns Promise that resolves to the response from the API.
|
|
45
|
+
*/
|
|
33
46
|
async _embedText(text) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
47
|
+
return this.caller.call(async () => {
|
|
48
|
+
try {
|
|
49
|
+
// replace newlines, which can negatively affect performance.
|
|
50
|
+
const cleanedText = text.replace(/\n/g, " ");
|
|
51
|
+
const res = await this.client.send(new client_bedrock_runtime_1.InvokeModelCommand({
|
|
52
|
+
modelId: this.model,
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
inputText: cleanedText,
|
|
55
|
+
}),
|
|
56
|
+
contentType: "application/json",
|
|
57
|
+
accept: "application/json",
|
|
58
|
+
}));
|
|
59
|
+
const body = new TextDecoder().decode(res.body);
|
|
60
|
+
return JSON.parse(body).embedding;
|
|
61
|
+
}
|
|
62
|
+
catch (e) {
|
|
63
|
+
console.error({
|
|
64
|
+
error: e,
|
|
65
|
+
});
|
|
66
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
67
|
+
if (e instanceof Error) {
|
|
68
|
+
throw new Error(`An error occurred while embedding documents with Bedrock: ${e.message}`);
|
|
69
|
+
}
|
|
70
|
+
throw new Error("An error occurred while embedding documents with Bedrock");
|
|
71
|
+
}
|
|
72
|
+
});
|
|
51
73
|
}
|
|
52
74
|
/**
|
|
53
75
|
* Method that takes a document as input and returns a promise that
|
|
@@ -60,13 +82,12 @@ class BedrockEmbeddings extends base_js_1.Embeddings {
|
|
|
60
82
|
return this.caller.callWithOptions({}, this._embedText.bind(this), document);
|
|
61
83
|
}
|
|
62
84
|
/**
|
|
63
|
-
* Method
|
|
64
|
-
*
|
|
65
|
-
*
|
|
66
|
-
* @param documents Array of documents for which to generate embeddings.
|
|
85
|
+
* Method to generate embeddings for an array of texts. Calls _embedText
|
|
86
|
+
* method which batches and handles retry logic when calling the AWS Bedrock API.
|
|
87
|
+
* @param documents Array of texts for which to generate embeddings.
|
|
67
88
|
* @returns Promise that resolves to a 2D array of embeddings for each input document.
|
|
68
89
|
*/
|
|
69
|
-
embedDocuments(documents) {
|
|
90
|
+
async embedDocuments(documents) {
|
|
70
91
|
return Promise.all(documents.map((document) => this._embedText(document)));
|
|
71
92
|
}
|
|
72
93
|
}
|
|
@@ -26,7 +26,15 @@ export interface BedrockEmbeddingsParams extends EmbeddingsParams {
|
|
|
26
26
|
export declare class BedrockEmbeddings extends Embeddings implements BedrockEmbeddingsParams {
|
|
27
27
|
model: string;
|
|
28
28
|
client: BedrockRuntimeClient;
|
|
29
|
+
batchSize: number;
|
|
29
30
|
constructor(fields?: BedrockEmbeddingsParams);
|
|
31
|
+
/**
|
|
32
|
+
* Protected method to make a request to the Bedrock API to generate
|
|
33
|
+
* embeddings. Handles the retry logic and returns the response from the
|
|
34
|
+
* API.
|
|
35
|
+
* @param request Request to send to the Bedrock API.
|
|
36
|
+
* @returns Promise that resolves to the response from the API.
|
|
37
|
+
*/
|
|
30
38
|
protected _embedText(text: string): Promise<number[]>;
|
|
31
39
|
/**
|
|
32
40
|
* Method that takes a document as input and returns a promise that
|
|
@@ -37,10 +45,9 @@ export declare class BedrockEmbeddings extends Embeddings implements BedrockEmbe
|
|
|
37
45
|
*/
|
|
38
46
|
embedQuery(document: string): Promise<number[]>;
|
|
39
47
|
/**
|
|
40
|
-
* Method
|
|
41
|
-
*
|
|
42
|
-
*
|
|
43
|
-
* @param documents Array of documents for which to generate embeddings.
|
|
48
|
+
* Method to generate embeddings for an array of texts. Calls _embedText
|
|
49
|
+
* method which batches and handles retry logic when calling the AWS Bedrock API.
|
|
50
|
+
* @param documents Array of texts for which to generate embeddings.
|
|
44
51
|
* @returns Promise that resolves to a 2D array of embeddings for each input document.
|
|
45
52
|
*/
|
|
46
53
|
embedDocuments(documents: string[]): Promise<number[][]>;
|
|
@@ -19,6 +19,12 @@ export class BedrockEmbeddings extends Embeddings {
|
|
|
19
19
|
writable: true,
|
|
20
20
|
value: void 0
|
|
21
21
|
});
|
|
22
|
+
Object.defineProperty(this, "batchSize", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: 512
|
|
27
|
+
});
|
|
22
28
|
this.model = fields?.model ?? "amazon.titan-embed-text-v1";
|
|
23
29
|
this.client =
|
|
24
30
|
fields?.client ??
|
|
@@ -27,24 +33,40 @@ export class BedrockEmbeddings extends Embeddings {
|
|
|
27
33
|
credentials: fields?.credentials,
|
|
28
34
|
});
|
|
29
35
|
}
|
|
36
|
+
/**
|
|
37
|
+
* Protected method to make a request to the Bedrock API to generate
|
|
38
|
+
* embeddings. Handles the retry logic and returns the response from the
|
|
39
|
+
* API.
|
|
40
|
+
* @param request Request to send to the Bedrock API.
|
|
41
|
+
* @returns Promise that resolves to the response from the API.
|
|
42
|
+
*/
|
|
30
43
|
async _embedText(text) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
return this.caller.call(async () => {
|
|
45
|
+
try {
|
|
46
|
+
// replace newlines, which can negatively affect performance.
|
|
47
|
+
const cleanedText = text.replace(/\n/g, " ");
|
|
48
|
+
const res = await this.client.send(new InvokeModelCommand({
|
|
49
|
+
modelId: this.model,
|
|
50
|
+
body: JSON.stringify({
|
|
51
|
+
inputText: cleanedText,
|
|
52
|
+
}),
|
|
53
|
+
contentType: "application/json",
|
|
54
|
+
accept: "application/json",
|
|
55
|
+
}));
|
|
56
|
+
const body = new TextDecoder().decode(res.body);
|
|
57
|
+
return JSON.parse(body).embedding;
|
|
58
|
+
}
|
|
59
|
+
catch (e) {
|
|
60
|
+
console.error({
|
|
61
|
+
error: e,
|
|
62
|
+
});
|
|
63
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
64
|
+
if (e instanceof Error) {
|
|
65
|
+
throw new Error(`An error occurred while embedding documents with Bedrock: ${e.message}`);
|
|
66
|
+
}
|
|
67
|
+
throw new Error("An error occurred while embedding documents with Bedrock");
|
|
68
|
+
}
|
|
69
|
+
});
|
|
48
70
|
}
|
|
49
71
|
/**
|
|
50
72
|
* Method that takes a document as input and returns a promise that
|
|
@@ -57,13 +79,12 @@ export class BedrockEmbeddings extends Embeddings {
|
|
|
57
79
|
return this.caller.callWithOptions({}, this._embedText.bind(this), document);
|
|
58
80
|
}
|
|
59
81
|
/**
|
|
60
|
-
* Method
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* @param documents Array of documents for which to generate embeddings.
|
|
82
|
+
* Method to generate embeddings for an array of texts. Calls _embedText
|
|
83
|
+
* method which batches and handles retry logic when calling the AWS Bedrock API.
|
|
84
|
+
* @param documents Array of texts for which to generate embeddings.
|
|
64
85
|
* @returns Promise that resolves to a 2D array of embeddings for each input document.
|
|
65
86
|
*/
|
|
66
|
-
embedDocuments(documents) {
|
|
87
|
+
async embedDocuments(documents) {
|
|
67
88
|
return Promise.all(documents.map((document) => this._embedText(document)));
|
|
68
89
|
}
|
|
69
90
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AnthropicFunctions = void 0;
|
|
4
4
|
const fast_xml_parser_1 = require("fast-xml-parser");
|
|
5
|
+
const base_js_1 = require("../../chat_models/base.cjs");
|
|
5
6
|
const index_js_1 = require("../../schema/index.cjs");
|
|
6
7
|
const anthropic_js_1 = require("../../chat_models/anthropic.cjs");
|
|
7
8
|
const prompt_js_1 = require("../../prompts/prompt.cjs");
|
|
@@ -26,12 +27,43 @@ for the weather in SF you would respond:
|
|
|
26
27
|
|
|
27
28
|
<tool>search</tool><tool_input><query>weather in SF</query></tool_input>
|
|
28
29
|
<observation>64 degrees</observation>`);
|
|
29
|
-
class AnthropicFunctions extends
|
|
30
|
+
class AnthropicFunctions extends base_js_1.BaseChatModel {
|
|
30
31
|
static lc_name() {
|
|
31
32
|
return "AnthropicFunctions";
|
|
32
33
|
}
|
|
33
34
|
constructor(fields) {
|
|
34
35
|
super(fields ?? {});
|
|
36
|
+
Object.defineProperty(this, "llm", {
|
|
37
|
+
enumerable: true,
|
|
38
|
+
configurable: true,
|
|
39
|
+
writable: true,
|
|
40
|
+
value: void 0
|
|
41
|
+
});
|
|
42
|
+
Object.defineProperty(this, "stopSequences", {
|
|
43
|
+
enumerable: true,
|
|
44
|
+
configurable: true,
|
|
45
|
+
writable: true,
|
|
46
|
+
value: void 0
|
|
47
|
+
});
|
|
48
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
49
|
+
enumerable: true,
|
|
50
|
+
configurable: true,
|
|
51
|
+
writable: true,
|
|
52
|
+
value: ["langchain", "experimental", "chat_models"]
|
|
53
|
+
});
|
|
54
|
+
this.llm = fields?.llm ?? new anthropic_js_1.ChatAnthropic(fields);
|
|
55
|
+
this.stopSequences =
|
|
56
|
+
fields?.stopSequences ?? this.llm.stopSequences;
|
|
57
|
+
}
|
|
58
|
+
invocationParams() {
|
|
59
|
+
return this.llm.invocationParams();
|
|
60
|
+
}
|
|
61
|
+
/** @ignore */
|
|
62
|
+
_identifyingParams() {
|
|
63
|
+
return this.llm._identifyingParams();
|
|
64
|
+
}
|
|
65
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
66
|
+
yield* this.llm._streamResponseChunks(messages, options, runManager);
|
|
35
67
|
}
|
|
36
68
|
async _generate(messages, options, runManager) {
|
|
37
69
|
let promptMessages = messages;
|
|
@@ -78,7 +110,7 @@ class AnthropicFunctions extends anthropic_js_1.ChatAnthropic {
|
|
|
78
110
|
else if (options.function_call !== undefined) {
|
|
79
111
|
throw new Error(`If "function_call" is provided, "functions" must also be.`);
|
|
80
112
|
}
|
|
81
|
-
const chatResult = await
|
|
113
|
+
const chatResult = await this.llm._generate(promptMessages, options, runManager);
|
|
82
114
|
const chatGenerationContent = chatResult.generations[0].message.content;
|
|
83
115
|
if (forced) {
|
|
84
116
|
const parser = new fast_xml_parser_1.XMLParser();
|
|
@@ -1,15 +1,25 @@
|
|
|
1
|
-
import { BaseChatModelParams } from "../../chat_models/base.js";
|
|
1
|
+
import { BaseChatModel, BaseChatModelParams } from "../../chat_models/base.js";
|
|
2
2
|
import { CallbackManagerForLLMRun } from "../../callbacks/manager.js";
|
|
3
|
-
import { BaseMessage, ChatResult } from "../../schema/index.js";
|
|
4
|
-
import {
|
|
3
|
+
import { BaseMessage, ChatGenerationChunk, ChatResult } from "../../schema/index.js";
|
|
4
|
+
import { type AnthropicInput } from "../../chat_models/anthropic.js";
|
|
5
5
|
import { BaseFunctionCallOptions } from "../../base_language/index.js";
|
|
6
6
|
import { StructuredTool } from "../../tools/base.js";
|
|
7
7
|
export interface ChatAnthropicFunctionsCallOptions extends BaseFunctionCallOptions {
|
|
8
8
|
tools?: StructuredTool[];
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export type AnthropicFunctionsInput = Partial<AnthropicInput> & BaseChatModelParams & {
|
|
11
|
+
llm?: BaseChatModel;
|
|
12
|
+
};
|
|
13
|
+
export declare class AnthropicFunctions extends BaseChatModel<ChatAnthropicFunctionsCallOptions> {
|
|
14
|
+
llm: BaseChatModel;
|
|
15
|
+
stopSequences?: string[];
|
|
16
|
+
lc_namespace: string[];
|
|
11
17
|
static lc_name(): string;
|
|
12
|
-
constructor(fields?:
|
|
18
|
+
constructor(fields?: AnthropicFunctionsInput);
|
|
19
|
+
invocationParams(): any;
|
|
20
|
+
/** @ignore */
|
|
21
|
+
_identifyingParams(): Record<string, any>;
|
|
22
|
+
_streamResponseChunks(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
13
23
|
_generate(messages: BaseMessage[], options: this["ParsedCallOptions"], runManager?: CallbackManagerForLLMRun | undefined): Promise<ChatResult>;
|
|
14
24
|
_llmType(): string;
|
|
15
25
|
/** @ignore */
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { XMLParser } from "fast-xml-parser";
|
|
2
|
+
import { BaseChatModel } from "../../chat_models/base.js";
|
|
2
3
|
import { AIMessage, SystemMessage, } from "../../schema/index.js";
|
|
3
4
|
import { ChatAnthropic, DEFAULT_STOP_SEQUENCES, } from "../../chat_models/anthropic.js";
|
|
4
5
|
import { PromptTemplate } from "../../prompts/prompt.js";
|
|
@@ -23,12 +24,43 @@ for the weather in SF you would respond:
|
|
|
23
24
|
|
|
24
25
|
<tool>search</tool><tool_input><query>weather in SF</query></tool_input>
|
|
25
26
|
<observation>64 degrees</observation>`);
|
|
26
|
-
export class AnthropicFunctions extends
|
|
27
|
+
export class AnthropicFunctions extends BaseChatModel {
|
|
27
28
|
static lc_name() {
|
|
28
29
|
return "AnthropicFunctions";
|
|
29
30
|
}
|
|
30
31
|
constructor(fields) {
|
|
31
32
|
super(fields ?? {});
|
|
33
|
+
Object.defineProperty(this, "llm", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: void 0
|
|
38
|
+
});
|
|
39
|
+
Object.defineProperty(this, "stopSequences", {
|
|
40
|
+
enumerable: true,
|
|
41
|
+
configurable: true,
|
|
42
|
+
writable: true,
|
|
43
|
+
value: void 0
|
|
44
|
+
});
|
|
45
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
46
|
+
enumerable: true,
|
|
47
|
+
configurable: true,
|
|
48
|
+
writable: true,
|
|
49
|
+
value: ["langchain", "experimental", "chat_models"]
|
|
50
|
+
});
|
|
51
|
+
this.llm = fields?.llm ?? new ChatAnthropic(fields);
|
|
52
|
+
this.stopSequences =
|
|
53
|
+
fields?.stopSequences ?? this.llm.stopSequences;
|
|
54
|
+
}
|
|
55
|
+
invocationParams() {
|
|
56
|
+
return this.llm.invocationParams();
|
|
57
|
+
}
|
|
58
|
+
/** @ignore */
|
|
59
|
+
_identifyingParams() {
|
|
60
|
+
return this.llm._identifyingParams();
|
|
61
|
+
}
|
|
62
|
+
async *_streamResponseChunks(messages, options, runManager) {
|
|
63
|
+
yield* this.llm._streamResponseChunks(messages, options, runManager);
|
|
32
64
|
}
|
|
33
65
|
async _generate(messages, options, runManager) {
|
|
34
66
|
let promptMessages = messages;
|
|
@@ -75,7 +107,7 @@ export class AnthropicFunctions extends ChatAnthropic {
|
|
|
75
107
|
else if (options.function_call !== undefined) {
|
|
76
108
|
throw new Error(`If "function_call" is provided, "functions" must also be.`);
|
|
77
109
|
}
|
|
78
|
-
const chatResult = await
|
|
110
|
+
const chatResult = await this.llm._generate(promptMessages, options, runManager);
|
|
79
111
|
const chatGenerationContent = chatResult.generations[0].message.content;
|
|
80
112
|
if (forced) {
|
|
81
113
|
const parser = new XMLParser();
|