langchain 0.0.160 → 0.0.162
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/README.md +3 -2
- package/dist/callbacks/base.d.ts +10 -10
- package/dist/callbacks/handlers/log_stream.cjs +23 -11
- package/dist/callbacks/handlers/log_stream.d.ts +3 -2
- package/dist/callbacks/handlers/log_stream.js +23 -11
- package/dist/callbacks/manager.cjs +13 -12
- package/dist/callbacks/manager.d.ts +9 -5
- package/dist/callbacks/manager.js +13 -12
- package/dist/chains/base.cjs +1 -1
- package/dist/chains/base.js +1 -1
- package/dist/chat_models/base.cjs +2 -2
- package/dist/chat_models/base.js +2 -2
- package/dist/embeddings/bedrock.cjs +73 -0
- package/dist/embeddings/bedrock.d.ts +47 -0
- package/dist/embeddings/bedrock.js +69 -0
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.cjs +297 -0
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.d.ts +183 -0
- package/dist/experimental/hubs/makersuite/googlemakersuitehub.js +291 -0
- package/dist/llms/base.cjs +2 -2
- package/dist/llms/base.js +2 -2
- package/dist/load/import_constants.cjs +2 -0
- package/dist/load/import_constants.js +2 -0
- package/dist/load/import_map.cjs +4 -1
- package/dist/load/import_map.d.ts +3 -0
- package/dist/load/import_map.js +3 -0
- package/dist/prompts/chat.cjs +4 -3
- package/dist/prompts/chat.js +2 -1
- package/dist/retrievers/chaindesk.cjs +71 -0
- package/dist/retrievers/chaindesk.d.ts +18 -0
- package/dist/retrievers/chaindesk.js +67 -0
- package/dist/retrievers/contextual_compression.cjs +2 -2
- package/dist/retrievers/contextual_compression.js +2 -2
- package/dist/retrievers/databerry.cjs +1 -0
- package/dist/retrievers/databerry.d.ts +1 -0
- package/dist/retrievers/databerry.js +1 -0
- package/dist/retrievers/document_compressors/embeddings_filter.cjs +78 -0
- package/dist/retrievers/document_compressors/embeddings_filter.d.ts +39 -0
- package/dist/retrievers/document_compressors/embeddings_filter.js +74 -0
- package/dist/retrievers/document_compressors/index.cjs +33 -1
- package/dist/retrievers/document_compressors/index.d.ts +14 -1
- package/dist/retrievers/document_compressors/index.js +31 -0
- package/dist/retrievers/document_compressors/test/document_compressor.int.test.cjs +28 -0
- package/dist/retrievers/document_compressors/test/document_compressor.int.test.d.ts +1 -0
- package/dist/retrievers/document_compressors/test/document_compressor.int.test.js +26 -0
- package/dist/retrievers/tavily_search_api.cjs +144 -0
- package/dist/retrievers/tavily_search_api.d.ts +38 -0
- package/dist/retrievers/tavily_search_api.js +140 -0
- package/dist/schema/retriever.cjs +1 -1
- package/dist/schema/retriever.js +1 -1
- package/dist/schema/runnable/base.cjs +29 -10
- package/dist/schema/runnable/base.d.ts +5 -1
- package/dist/schema/runnable/base.js +29 -10
- package/dist/stores/message/dynamodb.cjs +9 -6
- package/dist/stores/message/dynamodb.d.ts +3 -2
- package/dist/stores/message/dynamodb.js +9 -6
- package/dist/text_splitter.cjs +22 -8
- package/dist/text_splitter.d.ts +1 -0
- package/dist/text_splitter.js +22 -8
- package/dist/tools/base.cjs +1 -1
- package/dist/tools/base.js +1 -1
- package/dist/types/googlevertexai-types.d.ts +7 -5
- package/dist/util/googlevertexai-connection.cjs +35 -27
- package/dist/util/googlevertexai-connection.d.ts +12 -8
- package/dist/util/googlevertexai-connection.js +33 -26
- package/dist/util/googlevertexai-webauth.d.ts +2 -2
- package/dist/vectorstores/googlevertexai.d.ts +4 -4
- package/embeddings/bedrock.cjs +1 -0
- package/embeddings/bedrock.d.ts +1 -0
- package/embeddings/bedrock.js +1 -0
- package/experimental/hubs/makersuite/googlemakersuitehub.cjs +1 -0
- package/experimental/hubs/makersuite/googlemakersuitehub.d.ts +1 -0
- package/experimental/hubs/makersuite/googlemakersuitehub.js +1 -0
- package/package.json +46 -1
- package/retrievers/chaindesk.cjs +1 -0
- package/retrievers/chaindesk.d.ts +1 -0
- package/retrievers/chaindesk.js +1 -0
- package/retrievers/document_compressors/embeddings_filter.cjs +1 -0
- package/retrievers/document_compressors/embeddings_filter.d.ts +1 -0
- package/retrievers/document_compressors/embeddings_filter.js +1 -0
- package/retrievers/tavily_search_api.cjs +1 -0
- package/retrievers/tavily_search_api.d.ts +1 -0
- package/retrievers/tavily_search_api.js +1 -0
package/README.md
CHANGED
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
|
|
8
8
|
Looking for the Python version? Check out [LangChain](https://github.com/hwchase17/langchain).
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
10
|
+
To help you ship LangChain apps to production faster, check out [LangSmith](https://smith.langchain.com).
|
|
11
|
+
[LangSmith](https://smith.langchain.com) is a unified developer platform for building, testing, and monitoring LLM applications.
|
|
12
|
+
Fill out [this form](https://airtable.com/appwQzlErAS2qiP0L/shrGtGaVBVAz7NcV2) to get off the waitlist or speak with our sales team
|
|
12
13
|
|
|
13
14
|
## Quick Install
|
|
14
15
|
|
package/dist/callbacks/base.d.ts
CHANGED
|
@@ -35,7 +35,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
35
35
|
* Called at the start of an LLM or Chat Model run, with the prompt(s)
|
|
36
36
|
* and the run ID.
|
|
37
37
|
*/
|
|
38
|
-
handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown
|
|
38
|
+
handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
39
39
|
Promise<any> | any;
|
|
40
40
|
/**
|
|
41
41
|
* Called when an LLM/ChatModel in `streaming` mode produces a new token
|
|
@@ -63,13 +63,13 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
63
63
|
* Called at the start of a Chat Model run, with the prompt(s)
|
|
64
64
|
* and the run ID.
|
|
65
65
|
*/
|
|
66
|
-
handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown
|
|
66
|
+
handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string, extraParams?: Record<string, unknown>, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
67
67
|
Promise<any> | any;
|
|
68
68
|
/**
|
|
69
69
|
* Called at the start of a Chain run, with the chain name and inputs
|
|
70
70
|
* and the run ID.
|
|
71
71
|
*/
|
|
72
|
-
handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, runType?: string, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
73
|
Promise<any> | any;
|
|
74
74
|
/**
|
|
75
75
|
* Called if a Chain run encounters an error
|
|
@@ -89,7 +89,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
89
89
|
* Called at the start of a Tool run, with the tool name and input
|
|
90
90
|
* and the run ID.
|
|
91
91
|
*/
|
|
92
|
-
handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown
|
|
92
|
+
handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
93
93
|
Promise<any> | any;
|
|
94
94
|
/**
|
|
95
95
|
* Called if a Tool run encounters an error
|
|
@@ -112,7 +112,7 @@ declare abstract class BaseCallbackHandlerMethodsClass {
|
|
|
112
112
|
* with the final output and the run ID.
|
|
113
113
|
*/
|
|
114
114
|
handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string, tags?: string[]): Promise<void> | void;
|
|
115
|
-
handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown
|
|
115
|
+
handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string, tags?: string[], metadata?: Record<string, unknown>, name?: string): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
116
116
|
Promise<any> | any;
|
|
117
117
|
handleRetrieverEnd?(documents: Document[], runId: string, parentRunId?: string, tags?: string[]): // eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
118
118
|
Promise<any> | any;
|
|
@@ -199,7 +199,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
199
199
|
* Called at the start of an LLM or Chat Model run, with the prompt(s)
|
|
200
200
|
* and the run ID.
|
|
201
201
|
*/
|
|
202
|
-
handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
|
|
202
|
+
handleLLMStart?(llm: Serialized, prompts: string[], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
|
|
203
203
|
/**
|
|
204
204
|
* Called when an LLM/ChatModel in `streaming` mode produces a new token
|
|
205
205
|
*/
|
|
@@ -216,12 +216,12 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
216
216
|
* Called at the start of a Chat Model run, with the prompt(s)
|
|
217
217
|
* and the run ID.
|
|
218
218
|
*/
|
|
219
|
-
handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
|
|
219
|
+
handleChatModelStart?(llm: Serialized, messages: BaseMessage[][], runId: string, parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
|
|
220
220
|
/**
|
|
221
221
|
* Called at the start of a Chain run, with the chain name and inputs
|
|
222
222
|
* and the run ID.
|
|
223
223
|
*/
|
|
224
|
-
handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runType?: string | undefined): any;
|
|
224
|
+
handleChainStart?(chain: Serialized, inputs: ChainValues, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, runType?: string | undefined, name?: string | undefined): any;
|
|
225
225
|
/**
|
|
226
226
|
* Called if a Chain run encounters an error
|
|
227
227
|
*/
|
|
@@ -238,7 +238,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
238
238
|
* Called at the start of a Tool run, with the tool name and input
|
|
239
239
|
* and the run ID.
|
|
240
240
|
*/
|
|
241
|
-
handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
|
|
241
|
+
handleToolStart?(tool: Serialized, input: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
|
|
242
242
|
/**
|
|
243
243
|
* Called if a Tool run encounters an error
|
|
244
244
|
*/
|
|
@@ -258,7 +258,7 @@ export declare abstract class BaseCallbackHandler extends BaseCallbackHandlerMet
|
|
|
258
258
|
* with the final output and the run ID.
|
|
259
259
|
*/
|
|
260
260
|
handleAgentEnd?(action: AgentFinish, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): void | Promise<void>;
|
|
261
|
-
handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined): any;
|
|
261
|
+
handleRetrieverStart?(retriever: Serialized, query: string, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined, metadata?: Record<string, unknown> | undefined, name?: string | undefined): any;
|
|
262
262
|
handleRetrieverEnd?(documents: Document<Record<string, any>>[], runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
|
|
263
263
|
handleRetrieverError?(err: any, runId: string, parentRunId?: string | undefined, tags?: string[] | undefined): any;
|
|
264
264
|
};
|
|
@@ -101,7 +101,13 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
101
101
|
writable: true,
|
|
102
102
|
value: void 0
|
|
103
103
|
});
|
|
104
|
-
Object.defineProperty(this, "
|
|
104
|
+
Object.defineProperty(this, "keyMapByRunId", {
|
|
105
|
+
enumerable: true,
|
|
106
|
+
configurable: true,
|
|
107
|
+
writable: true,
|
|
108
|
+
value: {}
|
|
109
|
+
});
|
|
110
|
+
Object.defineProperty(this, "counterMapByRunName", {
|
|
105
111
|
enumerable: true,
|
|
106
112
|
configurable: true,
|
|
107
113
|
writable: true,
|
|
@@ -191,7 +197,7 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
191
197
|
id: run.id,
|
|
192
198
|
streamed_output: [],
|
|
193
199
|
final_output: undefined,
|
|
194
|
-
logs:
|
|
200
|
+
logs: {},
|
|
195
201
|
},
|
|
196
202
|
},
|
|
197
203
|
],
|
|
@@ -200,7 +206,13 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
200
206
|
if (!this._includeRun(run)) {
|
|
201
207
|
return;
|
|
202
208
|
}
|
|
203
|
-
this.
|
|
209
|
+
if (this.counterMapByRunName[run.name] === undefined) {
|
|
210
|
+
this.counterMapByRunName[run.name] = 0;
|
|
211
|
+
}
|
|
212
|
+
this.counterMapByRunName[run.name] += 1;
|
|
213
|
+
const count = this.counterMapByRunName[run.name];
|
|
214
|
+
this.keyMapByRunId[run.id] =
|
|
215
|
+
count === 1 ? run.name : `${run.name}:${count}`;
|
|
204
216
|
const logEntry = {
|
|
205
217
|
id: run.id,
|
|
206
218
|
name: run.name,
|
|
@@ -216,7 +228,7 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
216
228
|
ops: [
|
|
217
229
|
{
|
|
218
230
|
op: "add",
|
|
219
|
-
path: `/logs/${this.
|
|
231
|
+
path: `/logs/${this.keyMapByRunId[run.id]}`,
|
|
220
232
|
value: logEntry,
|
|
221
233
|
},
|
|
222
234
|
],
|
|
@@ -224,21 +236,21 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
224
236
|
}
|
|
225
237
|
async onRunUpdate(run) {
|
|
226
238
|
try {
|
|
227
|
-
const
|
|
228
|
-
if (
|
|
239
|
+
const runName = this.keyMapByRunId[run.id];
|
|
240
|
+
if (runName === undefined) {
|
|
229
241
|
return;
|
|
230
242
|
}
|
|
231
243
|
const ops = [
|
|
232
244
|
{
|
|
233
245
|
op: "add",
|
|
234
|
-
path: `/logs/${
|
|
246
|
+
path: `/logs/${runName}/final_output`,
|
|
235
247
|
value: run.outputs,
|
|
236
248
|
},
|
|
237
249
|
];
|
|
238
250
|
if (run.end_time !== undefined) {
|
|
239
251
|
ops.push({
|
|
240
252
|
op: "add",
|
|
241
|
-
path: `/logs/${
|
|
253
|
+
path: `/logs/${runName}/end_time`,
|
|
242
254
|
value: new Date(run.end_time).toISOString(),
|
|
243
255
|
});
|
|
244
256
|
}
|
|
@@ -264,15 +276,15 @@ class LogStreamCallbackHandler extends tracer_js_1.BaseTracer {
|
|
|
264
276
|
}
|
|
265
277
|
}
|
|
266
278
|
async onLLMNewToken(run, token) {
|
|
267
|
-
const
|
|
268
|
-
if (
|
|
279
|
+
const runName = this.keyMapByRunId[run.id];
|
|
280
|
+
if (runName === undefined) {
|
|
269
281
|
return;
|
|
270
282
|
}
|
|
271
283
|
const patch = new RunLogPatch({
|
|
272
284
|
ops: [
|
|
273
285
|
{
|
|
274
286
|
op: "add",
|
|
275
|
-
path: `/logs/${
|
|
287
|
+
path: `/logs/${runName}/streamed_output_str/-`,
|
|
276
288
|
value: token,
|
|
277
289
|
},
|
|
278
290
|
],
|
|
@@ -37,7 +37,7 @@ export type RunState = {
|
|
|
37
37
|
* List of sub-runs contained in this run, if any, in the order they were started.
|
|
38
38
|
* If filters were supplied, this list will contain only the runs that matched the filters.
|
|
39
39
|
*/
|
|
40
|
-
logs: LogEntry
|
|
40
|
+
logs: Record<string, LogEntry>;
|
|
41
41
|
};
|
|
42
42
|
/**
|
|
43
43
|
* List of jsonpatch JSONPatchOperations, which describe how to create the run state
|
|
@@ -84,7 +84,8 @@ export declare class LogStreamCallbackHandler extends BaseTracer {
|
|
|
84
84
|
protected excludeNames?: string[];
|
|
85
85
|
protected excludeTypes?: string[];
|
|
86
86
|
protected excludeTags?: string[];
|
|
87
|
-
private
|
|
87
|
+
private keyMapByRunId;
|
|
88
|
+
private counterMapByRunName;
|
|
88
89
|
protected transformStream: TransformStream;
|
|
89
90
|
writer: WritableStreamDefaultWriter;
|
|
90
91
|
receiveStream: IterableReadableStream<RunLogPatch>;
|
|
@@ -96,7 +96,13 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
96
96
|
writable: true,
|
|
97
97
|
value: void 0
|
|
98
98
|
});
|
|
99
|
-
Object.defineProperty(this, "
|
|
99
|
+
Object.defineProperty(this, "keyMapByRunId", {
|
|
100
|
+
enumerable: true,
|
|
101
|
+
configurable: true,
|
|
102
|
+
writable: true,
|
|
103
|
+
value: {}
|
|
104
|
+
});
|
|
105
|
+
Object.defineProperty(this, "counterMapByRunName", {
|
|
100
106
|
enumerable: true,
|
|
101
107
|
configurable: true,
|
|
102
108
|
writable: true,
|
|
@@ -186,7 +192,7 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
186
192
|
id: run.id,
|
|
187
193
|
streamed_output: [],
|
|
188
194
|
final_output: undefined,
|
|
189
|
-
logs:
|
|
195
|
+
logs: {},
|
|
190
196
|
},
|
|
191
197
|
},
|
|
192
198
|
],
|
|
@@ -195,7 +201,13 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
195
201
|
if (!this._includeRun(run)) {
|
|
196
202
|
return;
|
|
197
203
|
}
|
|
198
|
-
this.
|
|
204
|
+
if (this.counterMapByRunName[run.name] === undefined) {
|
|
205
|
+
this.counterMapByRunName[run.name] = 0;
|
|
206
|
+
}
|
|
207
|
+
this.counterMapByRunName[run.name] += 1;
|
|
208
|
+
const count = this.counterMapByRunName[run.name];
|
|
209
|
+
this.keyMapByRunId[run.id] =
|
|
210
|
+
count === 1 ? run.name : `${run.name}:${count}`;
|
|
199
211
|
const logEntry = {
|
|
200
212
|
id: run.id,
|
|
201
213
|
name: run.name,
|
|
@@ -211,7 +223,7 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
211
223
|
ops: [
|
|
212
224
|
{
|
|
213
225
|
op: "add",
|
|
214
|
-
path: `/logs/${this.
|
|
226
|
+
path: `/logs/${this.keyMapByRunId[run.id]}`,
|
|
215
227
|
value: logEntry,
|
|
216
228
|
},
|
|
217
229
|
],
|
|
@@ -219,21 +231,21 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
219
231
|
}
|
|
220
232
|
async onRunUpdate(run) {
|
|
221
233
|
try {
|
|
222
|
-
const
|
|
223
|
-
if (
|
|
234
|
+
const runName = this.keyMapByRunId[run.id];
|
|
235
|
+
if (runName === undefined) {
|
|
224
236
|
return;
|
|
225
237
|
}
|
|
226
238
|
const ops = [
|
|
227
239
|
{
|
|
228
240
|
op: "add",
|
|
229
|
-
path: `/logs/${
|
|
241
|
+
path: `/logs/${runName}/final_output`,
|
|
230
242
|
value: run.outputs,
|
|
231
243
|
},
|
|
232
244
|
];
|
|
233
245
|
if (run.end_time !== undefined) {
|
|
234
246
|
ops.push({
|
|
235
247
|
op: "add",
|
|
236
|
-
path: `/logs/${
|
|
248
|
+
path: `/logs/${runName}/end_time`,
|
|
237
249
|
value: new Date(run.end_time).toISOString(),
|
|
238
250
|
});
|
|
239
251
|
}
|
|
@@ -259,15 +271,15 @@ export class LogStreamCallbackHandler extends BaseTracer {
|
|
|
259
271
|
}
|
|
260
272
|
}
|
|
261
273
|
async onLLMNewToken(run, token) {
|
|
262
|
-
const
|
|
263
|
-
if (
|
|
274
|
+
const runName = this.keyMapByRunId[run.id];
|
|
275
|
+
if (runName === undefined) {
|
|
264
276
|
return;
|
|
265
277
|
}
|
|
266
278
|
const patch = new RunLogPatch({
|
|
267
279
|
ops: [
|
|
268
280
|
{
|
|
269
281
|
op: "add",
|
|
270
|
-
path: `/logs/${
|
|
282
|
+
path: `/logs/${runName}/streamed_output_str/-`,
|
|
271
283
|
value: token,
|
|
272
284
|
},
|
|
273
285
|
],
|
|
@@ -330,13 +330,13 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
330
330
|
this.inheritableHandlers = [];
|
|
331
331
|
this._parentRunId = parentRunId;
|
|
332
332
|
}
|
|
333
|
-
async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined) {
|
|
333
|
+
async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
334
334
|
return Promise.all(prompts.map(async (prompt) => {
|
|
335
335
|
const runId = (0, uuid_1.v4)();
|
|
336
336
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
337
337
|
if (!handler.ignoreLLM) {
|
|
338
338
|
try {
|
|
339
|
-
await handler.handleLLMStart?.(llm, [prompt], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
339
|
+
await handler.handleLLMStart?.(llm, [prompt], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
340
340
|
}
|
|
341
341
|
catch (err) {
|
|
342
342
|
console.error(`Error in handler ${handler.constructor.name}, handleLLMStart: ${err}`);
|
|
@@ -346,17 +346,18 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
346
346
|
return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
347
347
|
}));
|
|
348
348
|
}
|
|
349
|
-
async handleChatModelStart(llm, messages, _runId = undefined, _parentRunId = undefined, extraParams = undefined) {
|
|
349
|
+
async handleChatModelStart(llm, messages, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
350
350
|
return Promise.all(messages.map(async (messageGroup) => {
|
|
351
351
|
const runId = (0, uuid_1.v4)();
|
|
352
352
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
353
353
|
if (!handler.ignoreLLM) {
|
|
354
354
|
try {
|
|
355
|
-
if (handler.handleChatModelStart)
|
|
356
|
-
await handler.handleChatModelStart?.(llm, [messageGroup], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
355
|
+
if (handler.handleChatModelStart) {
|
|
356
|
+
await handler.handleChatModelStart?.(llm, [messageGroup], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
357
|
+
}
|
|
357
358
|
else if (handler.handleLLMStart) {
|
|
358
359
|
const messageString = (0, base_js_2.getBufferString)(messageGroup);
|
|
359
|
-
await handler.handleLLMStart?.(llm, [messageString], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
360
|
+
await handler.handleLLMStart?.(llm, [messageString], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
360
361
|
}
|
|
361
362
|
}
|
|
362
363
|
catch (err) {
|
|
@@ -367,11 +368,11 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
367
368
|
return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
368
369
|
}));
|
|
369
370
|
}
|
|
370
|
-
async handleChainStart(chain, inputs, runId = (0, uuid_1.v4)(), runType = undefined) {
|
|
371
|
+
async handleChainStart(chain, inputs, runId = (0, uuid_1.v4)(), runType = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
371
372
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
372
373
|
if (!handler.ignoreChain) {
|
|
373
374
|
try {
|
|
374
|
-
await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType);
|
|
375
|
+
await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType, runName);
|
|
375
376
|
}
|
|
376
377
|
catch (err) {
|
|
377
378
|
console.error(`Error in handler ${handler.constructor.name}, handleChainStart: ${err}`);
|
|
@@ -380,11 +381,11 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
380
381
|
}, handler.awaitHandlers)));
|
|
381
382
|
return new CallbackManagerForChainRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
382
383
|
}
|
|
383
|
-
async handleToolStart(tool, input, runId = (0, uuid_1.v4)()) {
|
|
384
|
+
async handleToolStart(tool, input, runId = (0, uuid_1.v4)(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
384
385
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
385
386
|
if (!handler.ignoreAgent) {
|
|
386
387
|
try {
|
|
387
|
-
await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata);
|
|
388
|
+
await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata, runName);
|
|
388
389
|
}
|
|
389
390
|
catch (err) {
|
|
390
391
|
console.error(`Error in handler ${handler.constructor.name}, handleToolStart: ${err}`);
|
|
@@ -393,11 +394,11 @@ class CallbackManager extends BaseCallbackManager {
|
|
|
393
394
|
}, handler.awaitHandlers)));
|
|
394
395
|
return new CallbackManagerForToolRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
395
396
|
}
|
|
396
|
-
async handleRetrieverStart(retriever, query, runId = (0, uuid_1.v4)(), _parentRunId = undefined) {
|
|
397
|
+
async handleRetrieverStart(retriever, query, runId = (0, uuid_1.v4)(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
397
398
|
await Promise.all(this.handlers.map((handler) => (0, promises_js_1.consumeCallback)(async () => {
|
|
398
399
|
if (!handler.ignoreRetriever) {
|
|
399
400
|
try {
|
|
400
|
-
await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata);
|
|
401
|
+
await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata, runName);
|
|
401
402
|
}
|
|
402
403
|
catch (err) {
|
|
403
404
|
console.error(`Error in handler ${handler.constructor.name}, handleRetrieverStart: ${err}`);
|
|
@@ -12,6 +12,10 @@ export interface CallbackManagerOptions {
|
|
|
12
12
|
}
|
|
13
13
|
export type Callbacks = CallbackManager | (BaseCallbackHandler | CallbackHandlerMethods)[];
|
|
14
14
|
export interface BaseCallbackConfig {
|
|
15
|
+
/**
|
|
16
|
+
* Name for the tracer run for this call. Defaults to the name of the class.
|
|
17
|
+
*/
|
|
18
|
+
runName?: string;
|
|
15
19
|
/**
|
|
16
20
|
* Tags for this call and any sub-calls (eg. a Chain calling an LLM).
|
|
17
21
|
* You can use these to filter calls.
|
|
@@ -92,11 +96,11 @@ export declare class CallbackManager extends BaseCallbackManager implements Base
|
|
|
92
96
|
name: string;
|
|
93
97
|
private readonly _parentRunId?;
|
|
94
98
|
constructor(parentRunId?: string);
|
|
95
|
-
handleLLMStart(llm: Serialized, prompts: string[], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined): Promise<CallbackManagerForLLMRun[]>;
|
|
96
|
-
handleChatModelStart(llm: Serialized, messages: BaseMessage[][], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined): Promise<CallbackManagerForLLMRun[]>;
|
|
97
|
-
handleChainStart(chain: Serialized, inputs: ChainValues, runId?: string, runType?: string | undefined): Promise<CallbackManagerForChainRun>;
|
|
98
|
-
handleToolStart(tool: Serialized, input: string, runId?: string): Promise<CallbackManagerForToolRun>;
|
|
99
|
-
handleRetrieverStart(retriever: Serialized, query: string, runId?: string, _parentRunId?: string | undefined): Promise<CallbackManagerForRetrieverRun>;
|
|
99
|
+
handleLLMStart(llm: Serialized, prompts: string[], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForLLMRun[]>;
|
|
100
|
+
handleChatModelStart(llm: Serialized, messages: BaseMessage[][], _runId?: string | undefined, _parentRunId?: string | undefined, extraParams?: Record<string, unknown> | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForLLMRun[]>;
|
|
101
|
+
handleChainStart(chain: Serialized, inputs: ChainValues, runId?: string, runType?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForChainRun>;
|
|
102
|
+
handleToolStart(tool: Serialized, input: string, runId?: string, _parentRunId?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForToolRun>;
|
|
103
|
+
handleRetrieverStart(retriever: Serialized, query: string, runId?: string, _parentRunId?: string | undefined, _tags?: string[] | undefined, _metadata?: Record<string, unknown> | undefined, runName?: string | undefined): Promise<CallbackManagerForRetrieverRun>;
|
|
100
104
|
addHandler(handler: BaseCallbackHandler, inherit?: boolean): void;
|
|
101
105
|
removeHandler(handler: BaseCallbackHandler): void;
|
|
102
106
|
setHandlers(handlers: BaseCallbackHandler[], inherit?: boolean): void;
|
|
@@ -321,13 +321,13 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
321
321
|
this.inheritableHandlers = [];
|
|
322
322
|
this._parentRunId = parentRunId;
|
|
323
323
|
}
|
|
324
|
-
async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined) {
|
|
324
|
+
async handleLLMStart(llm, prompts, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
325
325
|
return Promise.all(prompts.map(async (prompt) => {
|
|
326
326
|
const runId = uuidv4();
|
|
327
327
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
328
328
|
if (!handler.ignoreLLM) {
|
|
329
329
|
try {
|
|
330
|
-
await handler.handleLLMStart?.(llm, [prompt], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
330
|
+
await handler.handleLLMStart?.(llm, [prompt], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
331
331
|
}
|
|
332
332
|
catch (err) {
|
|
333
333
|
console.error(`Error in handler ${handler.constructor.name}, handleLLMStart: ${err}`);
|
|
@@ -337,17 +337,18 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
337
337
|
return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
338
338
|
}));
|
|
339
339
|
}
|
|
340
|
-
async handleChatModelStart(llm, messages, _runId = undefined, _parentRunId = undefined, extraParams = undefined) {
|
|
340
|
+
async handleChatModelStart(llm, messages, _runId = undefined, _parentRunId = undefined, extraParams = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
341
341
|
return Promise.all(messages.map(async (messageGroup) => {
|
|
342
342
|
const runId = uuidv4();
|
|
343
343
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
344
344
|
if (!handler.ignoreLLM) {
|
|
345
345
|
try {
|
|
346
|
-
if (handler.handleChatModelStart)
|
|
347
|
-
await handler.handleChatModelStart?.(llm, [messageGroup], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
346
|
+
if (handler.handleChatModelStart) {
|
|
347
|
+
await handler.handleChatModelStart?.(llm, [messageGroup], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
348
|
+
}
|
|
348
349
|
else if (handler.handleLLMStart) {
|
|
349
350
|
const messageString = getBufferString(messageGroup);
|
|
350
|
-
await handler.handleLLMStart?.(llm, [messageString], runId, this._parentRunId, extraParams, this.tags, this.metadata);
|
|
351
|
+
await handler.handleLLMStart?.(llm, [messageString], runId, this._parentRunId, extraParams, this.tags, this.metadata, runName);
|
|
351
352
|
}
|
|
352
353
|
}
|
|
353
354
|
catch (err) {
|
|
@@ -358,11 +359,11 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
358
359
|
return new CallbackManagerForLLMRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
359
360
|
}));
|
|
360
361
|
}
|
|
361
|
-
async handleChainStart(chain, inputs, runId = uuidv4(), runType = undefined) {
|
|
362
|
+
async handleChainStart(chain, inputs, runId = uuidv4(), runType = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
362
363
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
363
364
|
if (!handler.ignoreChain) {
|
|
364
365
|
try {
|
|
365
|
-
await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType);
|
|
366
|
+
await handler.handleChainStart?.(chain, inputs, runId, this._parentRunId, this.tags, this.metadata, runType, runName);
|
|
366
367
|
}
|
|
367
368
|
catch (err) {
|
|
368
369
|
console.error(`Error in handler ${handler.constructor.name}, handleChainStart: ${err}`);
|
|
@@ -371,11 +372,11 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
371
372
|
}, handler.awaitHandlers)));
|
|
372
373
|
return new CallbackManagerForChainRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
373
374
|
}
|
|
374
|
-
async handleToolStart(tool, input, runId = uuidv4()) {
|
|
375
|
+
async handleToolStart(tool, input, runId = uuidv4(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
375
376
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
376
377
|
if (!handler.ignoreAgent) {
|
|
377
378
|
try {
|
|
378
|
-
await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata);
|
|
379
|
+
await handler.handleToolStart?.(tool, input, runId, this._parentRunId, this.tags, this.metadata, runName);
|
|
379
380
|
}
|
|
380
381
|
catch (err) {
|
|
381
382
|
console.error(`Error in handler ${handler.constructor.name}, handleToolStart: ${err}`);
|
|
@@ -384,11 +385,11 @@ export class CallbackManager extends BaseCallbackManager {
|
|
|
384
385
|
}, handler.awaitHandlers)));
|
|
385
386
|
return new CallbackManagerForToolRun(runId, this.handlers, this.inheritableHandlers, this.tags, this.inheritableTags, this.metadata, this.inheritableMetadata, this._parentRunId);
|
|
386
387
|
}
|
|
387
|
-
async handleRetrieverStart(retriever, query, runId = uuidv4(), _parentRunId = undefined) {
|
|
388
|
+
async handleRetrieverStart(retriever, query, runId = uuidv4(), _parentRunId = undefined, _tags = undefined, _metadata = undefined, runName = undefined) {
|
|
388
389
|
await Promise.all(this.handlers.map((handler) => consumeCallback(async () => {
|
|
389
390
|
if (!handler.ignoreRetriever) {
|
|
390
391
|
try {
|
|
391
|
-
await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata);
|
|
392
|
+
await handler.handleRetrieverStart?.(retriever, query, runId, this._parentRunId, this.tags, this.metadata, runName);
|
|
392
393
|
}
|
|
393
394
|
catch (err) {
|
|
394
395
|
console.error(`Error in handler ${handler.constructor.name}, handleRetrieverStart: ${err}`);
|
package/dist/chains/base.cjs
CHANGED
|
@@ -98,7 +98,7 @@ class BaseChain extends index_js_2.BaseLangChain {
|
|
|
98
98
|
const fullValues = await this._formatValues(values);
|
|
99
99
|
const parsedConfig = (0, manager_js_1.parseCallbackConfigArg)(config);
|
|
100
100
|
const callbackManager_ = await manager_js_1.CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags || tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
101
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), fullValues);
|
|
101
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), fullValues, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
102
102
|
let outputValues;
|
|
103
103
|
try {
|
|
104
104
|
outputValues = await (values.signal
|
package/dist/chains/base.js
CHANGED
|
@@ -95,7 +95,7 @@ export class BaseChain extends BaseLangChain {
|
|
|
95
95
|
const fullValues = await this._formatValues(values);
|
|
96
96
|
const parsedConfig = parseCallbackConfigArg(config);
|
|
97
97
|
const callbackManager_ = await CallbackManager.configure(parsedConfig.callbacks, this.callbacks, parsedConfig.tags || tags, this.tags, parsedConfig.metadata, this.metadata, { verbose: this.verbose });
|
|
98
|
-
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), fullValues);
|
|
98
|
+
const runManager = await callbackManager_?.handleChainStart(this.toJSON(), fullValues, undefined, undefined, undefined, undefined, parsedConfig.runName);
|
|
99
99
|
let outputValues;
|
|
100
100
|
try {
|
|
101
101
|
outputValues = await (values.signal
|
|
@@ -71,7 +71,7 @@ class BaseChatModel extends index_js_2.BaseLanguageModel {
|
|
|
71
71
|
options: callOptions,
|
|
72
72
|
invocation_params: this?.invocationParams(callOptions),
|
|
73
73
|
};
|
|
74
|
-
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra);
|
|
74
|
+
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra, undefined, undefined, runnableConfig.runName);
|
|
75
75
|
let generationChunk;
|
|
76
76
|
try {
|
|
77
77
|
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
|
|
@@ -103,7 +103,7 @@ class BaseChatModel extends index_js_2.BaseLanguageModel {
|
|
|
103
103
|
options: parsedOptions,
|
|
104
104
|
invocation_params: this?.invocationParams(parsedOptions),
|
|
105
105
|
};
|
|
106
|
-
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra);
|
|
106
|
+
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra, undefined, undefined, handledOptions.runName);
|
|
107
107
|
// generate results
|
|
108
108
|
const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...parsedOptions, promptIndex: i }, runManagers?.[i])));
|
|
109
109
|
// handle results
|
package/dist/chat_models/base.js
CHANGED
|
@@ -67,7 +67,7 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
67
67
|
options: callOptions,
|
|
68
68
|
invocation_params: this?.invocationParams(callOptions),
|
|
69
69
|
};
|
|
70
|
-
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra);
|
|
70
|
+
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), [messages], undefined, undefined, extra, undefined, undefined, runnableConfig.runName);
|
|
71
71
|
let generationChunk;
|
|
72
72
|
try {
|
|
73
73
|
for await (const chunk of this._streamResponseChunks(messages, callOptions, runManagers?.[0])) {
|
|
@@ -99,7 +99,7 @@ export class BaseChatModel extends BaseLanguageModel {
|
|
|
99
99
|
options: parsedOptions,
|
|
100
100
|
invocation_params: this?.invocationParams(parsedOptions),
|
|
101
101
|
};
|
|
102
|
-
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra);
|
|
102
|
+
const runManagers = await callbackManager_?.handleChatModelStart(this.toJSON(), baseMessages, undefined, undefined, extra, undefined, undefined, handledOptions.runName);
|
|
103
103
|
// generate results
|
|
104
104
|
const results = await Promise.allSettled(baseMessages.map((messageList, i) => this._generate(messageList, { ...parsedOptions, promptIndex: i }, runManagers?.[i])));
|
|
105
105
|
// handle results
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BedrockEmbeddings = void 0;
|
|
4
|
+
const client_bedrock_runtime_1 = require("@aws-sdk/client-bedrock-runtime");
|
|
5
|
+
const base_js_1 = require("./base.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Class that extends the Embeddings class and provides methods for
|
|
8
|
+
* generating embeddings using the Bedrock API.
|
|
9
|
+
*/
|
|
10
|
+
class BedrockEmbeddings extends base_js_1.Embeddings {
|
|
11
|
+
constructor(fields) {
|
|
12
|
+
super(fields ?? {});
|
|
13
|
+
Object.defineProperty(this, "model", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: void 0
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(this, "client", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: void 0
|
|
24
|
+
});
|
|
25
|
+
this.model = fields?.model ?? "amazon.titan-embed-text-v1";
|
|
26
|
+
this.client =
|
|
27
|
+
fields?.client ??
|
|
28
|
+
new client_bedrock_runtime_1.BedrockRuntimeClient({
|
|
29
|
+
region: fields?.region,
|
|
30
|
+
credentials: fields?.credentials,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async _embedText(text) {
|
|
34
|
+
// replace newlines, which can negatively affect performance.
|
|
35
|
+
const cleanedText = text.replace(/\n/g, " ");
|
|
36
|
+
const res = await this.client.send(new client_bedrock_runtime_1.InvokeModelCommand({
|
|
37
|
+
modelId: this.model,
|
|
38
|
+
body: JSON.stringify({
|
|
39
|
+
inputText: cleanedText,
|
|
40
|
+
}),
|
|
41
|
+
contentType: "application/json",
|
|
42
|
+
accept: "application/json",
|
|
43
|
+
}));
|
|
44
|
+
try {
|
|
45
|
+
const body = new TextDecoder().decode(res.body);
|
|
46
|
+
return JSON.parse(body).embedding;
|
|
47
|
+
}
|
|
48
|
+
catch (e) {
|
|
49
|
+
throw new Error("An invalid response was returned by Bedrock.");
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Method that takes a document as input and returns a promise that
|
|
54
|
+
* resolves to an embedding for the document. It calls the _embedText
|
|
55
|
+
* method with the document as the input.
|
|
56
|
+
* @param document Document for which to generate an embedding.
|
|
57
|
+
* @returns Promise that resolves to an embedding for the input document.
|
|
58
|
+
*/
|
|
59
|
+
embedQuery(document) {
|
|
60
|
+
return this.caller.callWithOptions({}, this._embedText.bind(this), document);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Method that takes an array of documents as input and returns a promise
|
|
64
|
+
* that resolves to a 2D array of embeddings for each document. It calls
|
|
65
|
+
* the _embedText method for each document in the array.
|
|
66
|
+
* @param documents Array of documents for which to generate embeddings.
|
|
67
|
+
* @returns Promise that resolves to a 2D array of embeddings for each input document.
|
|
68
|
+
*/
|
|
69
|
+
embedDocuments(documents) {
|
|
70
|
+
return Promise.all(documents.map((document) => this._embedText(document)));
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.BedrockEmbeddings = BedrockEmbeddings;
|