langchain 0.0.151 → 0.0.153
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/cache/cloudflare_kv.cjs +1 -0
- package/cache/cloudflare_kv.d.ts +1 -0
- package/cache/cloudflare_kv.js +1 -0
- package/chat_models/fireworks.cjs +1 -0
- package/chat_models/fireworks.d.ts +1 -0
- package/chat_models/fireworks.js +1 -0
- package/dist/agents/chat/index.cjs +1 -1
- package/dist/agents/chat/index.js +1 -1
- package/dist/agents/chat_convo/index.cjs +1 -1
- package/dist/agents/chat_convo/index.js +1 -1
- package/dist/agents/executor.cjs +9 -2
- package/dist/agents/executor.js +9 -2
- package/dist/agents/openai/index.cjs +1 -1
- package/dist/agents/openai/index.js +1 -1
- package/dist/agents/structured_chat/index.cjs +1 -1
- package/dist/agents/structured_chat/index.js +1 -1
- package/dist/agents/xml/index.cjs +1 -1
- package/dist/agents/xml/index.js +1 -1
- package/dist/base_language/count_tokens.cjs +2 -1
- package/dist/base_language/count_tokens.js +2 -1
- package/dist/base_language/index.cjs +5 -3
- package/dist/base_language/index.d.ts +1 -1
- package/dist/base_language/index.js +4 -3
- package/dist/cache/cloudflare_kv.cjs +61 -0
- package/dist/cache/cloudflare_kv.d.ts +29 -0
- package/dist/cache/cloudflare_kv.js +57 -0
- package/dist/chains/openai_functions/openapi.cjs +1 -1
- package/dist/chains/openai_functions/openapi.js +1 -1
- package/dist/chains/openai_functions/structured_output.d.ts +2 -2
- package/dist/chains/question_answering/map_reduce_prompts.cjs +2 -3
- package/dist/chains/question_answering/map_reduce_prompts.js +2 -3
- package/dist/chains/question_answering/refine_prompts.cjs +2 -2
- package/dist/chains/question_answering/refine_prompts.js +2 -2
- package/dist/chains/question_answering/stuff_prompts.cjs +1 -2
- package/dist/chains/question_answering/stuff_prompts.js +1 -2
- package/dist/chat_models/base.d.ts +1 -1
- package/dist/chat_models/fireworks.cjs +81 -0
- package/dist/chat_models/fireworks.d.ts +33 -0
- package/dist/chat_models/fireworks.js +77 -0
- package/dist/chat_models/ollama.cjs +25 -12
- package/dist/chat_models/ollama.d.ts +2 -3
- package/dist/chat_models/ollama.js +25 -12
- package/dist/chat_models/openai.d.ts +2 -2
- package/dist/document_loaders/web/pdf.cjs +87 -0
- package/dist/document_loaders/web/pdf.d.ts +17 -0
- package/dist/document_loaders/web/pdf.js +83 -0
- package/dist/evaluation/agents/prompt.cjs +2 -3
- package/dist/evaluation/agents/prompt.js +2 -3
- package/dist/experimental/plan_and_execute/prompt.cjs +1 -1
- package/dist/experimental/plan_and_execute/prompt.js +1 -1
- package/dist/llms/fireworks.cjs +92 -0
- package/dist/llms/fireworks.d.ts +33 -0
- package/dist/llms/fireworks.js +88 -0
- package/dist/llms/llama_cpp.cjs +10 -4
- package/dist/llms/llama_cpp.d.ts +2 -1
- package/dist/llms/llama_cpp.js +10 -4
- package/dist/llms/ollama.cjs +29 -14
- package/dist/llms/ollama.d.ts +3 -4
- package/dist/llms/ollama.js +29 -14
- package/dist/llms/openai-chat.cjs +1 -5
- package/dist/llms/openai-chat.d.ts +1 -1
- package/dist/llms/openai-chat.js +1 -5
- package/dist/llms/openai.cjs +3 -4
- package/dist/llms/openai.d.ts +2 -2
- package/dist/llms/openai.js +3 -4
- package/dist/load/import_constants.cjs +3 -0
- package/dist/load/import_constants.js +3 -0
- package/dist/load/import_map.cjs +4 -2
- package/dist/load/import_map.d.ts +2 -0
- package/dist/load/import_map.js +2 -0
- package/dist/prompts/chat.cjs +12 -1
- package/dist/prompts/chat.d.ts +8 -0
- package/dist/prompts/chat.js +12 -1
- package/dist/schema/output_parser.cjs +38 -6
- package/dist/schema/output_parser.d.ts +20 -5
- package/dist/schema/output_parser.js +38 -6
- package/dist/schema/runnable/base.cjs +65 -10
- package/dist/schema/runnable/base.d.ts +17 -3
- package/dist/schema/runnable/base.js +65 -10
- package/dist/stores/message/cloudflare_d1.cjs +134 -0
- package/dist/stores/message/cloudflare_d1.d.ts +49 -0
- package/dist/stores/message/cloudflare_d1.js +130 -0
- package/dist/types/openai-types.d.ts +2 -0
- package/dist/util/ollama.cjs +2 -2
- package/dist/util/ollama.d.ts +6 -0
- package/dist/util/ollama.js +2 -2
- package/document_loaders/web/pdf.cjs +1 -0
- package/document_loaders/web/pdf.d.ts +1 -0
- package/document_loaders/web/pdf.js +1 -0
- package/llms/fireworks.cjs +1 -0
- package/llms/fireworks.d.ts +1 -0
- package/llms/fireworks.js +1 -0
- package/package.json +46 -1
- package/stores/message/cloudflare_d1.cjs +1 -0
- package/stores/message/cloudflare_d1.d.ts +1 -0
- package/stores/message/cloudflare_d1.js +1 -0
|
@@ -118,11 +118,26 @@ export declare class BytesOutputParser extends BaseTransformOutputParser<Uint8Ar
|
|
|
118
118
|
getFormatInstructions(): string;
|
|
119
119
|
}
|
|
120
120
|
/**
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
121
|
+
* Exception that output parsers should raise to signify a parsing error.
|
|
122
|
+
*
|
|
123
|
+
* This exists to differentiate parsing errors from other code or execution errors
|
|
124
|
+
* that also may arise inside the output parser. OutputParserExceptions will be
|
|
125
|
+
* available to catch and handle in ways to fix the parsing error, while other
|
|
126
|
+
* errors will be raised.
|
|
127
|
+
*
|
|
128
|
+
* @param message - The error that's being re-raised or an error message.
|
|
129
|
+
* @param llmOutput - String model output which is error-ing.
|
|
130
|
+
* @param observation - String explanation of error which can be passed to a
|
|
131
|
+
* model to try and remediate the issue.
|
|
132
|
+
* @param sendToLLM - Whether to send the observation and llm_output back to an Agent
|
|
133
|
+
* after an OutputParserException has been raised. This gives the underlying
|
|
134
|
+
* model driving the agent the context that the previous output was improperly
|
|
135
|
+
* structured, in the hopes that it will update the output to the correct
|
|
136
|
+
* format.
|
|
124
137
|
*/
|
|
125
138
|
export declare class OutputParserException extends Error {
|
|
126
|
-
|
|
127
|
-
|
|
139
|
+
llmOutput?: string;
|
|
140
|
+
observation?: string;
|
|
141
|
+
sendToLLM: boolean;
|
|
142
|
+
constructor(message: string, llmOutput?: string, observation?: string, sendToLLM?: boolean);
|
|
128
143
|
}
|
|
@@ -154,19 +154,51 @@ export class BytesOutputParser extends BaseTransformOutputParser {
|
|
|
154
154
|
}
|
|
155
155
|
}
|
|
156
156
|
/**
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
*
|
|
157
|
+
* Exception that output parsers should raise to signify a parsing error.
|
|
158
|
+
*
|
|
159
|
+
* This exists to differentiate parsing errors from other code or execution errors
|
|
160
|
+
* that also may arise inside the output parser. OutputParserExceptions will be
|
|
161
|
+
* available to catch and handle in ways to fix the parsing error, while other
|
|
162
|
+
* errors will be raised.
|
|
163
|
+
*
|
|
164
|
+
* @param message - The error that's being re-raised or an error message.
|
|
165
|
+
* @param llmOutput - String model output which is error-ing.
|
|
166
|
+
* @param observation - String explanation of error which can be passed to a
|
|
167
|
+
* model to try and remediate the issue.
|
|
168
|
+
* @param sendToLLM - Whether to send the observation and llm_output back to an Agent
|
|
169
|
+
* after an OutputParserException has been raised. This gives the underlying
|
|
170
|
+
* model driving the agent the context that the previous output was improperly
|
|
171
|
+
* structured, in the hopes that it will update the output to the correct
|
|
172
|
+
* format.
|
|
160
173
|
*/
|
|
161
174
|
export class OutputParserException extends Error {
|
|
162
|
-
constructor(message,
|
|
175
|
+
constructor(message, llmOutput, observation, sendToLLM = false) {
|
|
163
176
|
super(message);
|
|
164
|
-
Object.defineProperty(this, "
|
|
177
|
+
Object.defineProperty(this, "llmOutput", {
|
|
165
178
|
enumerable: true,
|
|
166
179
|
configurable: true,
|
|
167
180
|
writable: true,
|
|
168
181
|
value: void 0
|
|
169
182
|
});
|
|
170
|
-
this
|
|
183
|
+
Object.defineProperty(this, "observation", {
|
|
184
|
+
enumerable: true,
|
|
185
|
+
configurable: true,
|
|
186
|
+
writable: true,
|
|
187
|
+
value: void 0
|
|
188
|
+
});
|
|
189
|
+
Object.defineProperty(this, "sendToLLM", {
|
|
190
|
+
enumerable: true,
|
|
191
|
+
configurable: true,
|
|
192
|
+
writable: true,
|
|
193
|
+
value: void 0
|
|
194
|
+
});
|
|
195
|
+
this.llmOutput = llmOutput;
|
|
196
|
+
this.observation = observation;
|
|
197
|
+
this.sendToLLM = sendToLLM;
|
|
198
|
+
if (sendToLLM) {
|
|
199
|
+
if (observation === undefined || llmOutput === undefined) {
|
|
200
|
+
throw new Error("Arguments 'observation' & 'llmOutput' are required if 'sendToLlm' is true");
|
|
201
|
+
}
|
|
202
|
+
}
|
|
171
203
|
}
|
|
172
204
|
}
|
|
@@ -37,7 +37,7 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
37
37
|
*/
|
|
38
38
|
bind(kwargs) {
|
|
39
39
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
40
|
-
return new RunnableBinding({ bound: this, kwargs });
|
|
40
|
+
return new RunnableBinding({ bound: this, kwargs, config: {} });
|
|
41
41
|
}
|
|
42
42
|
/**
|
|
43
43
|
* Return a new Runnable that maps a list of inputs to a list of outputs,
|
|
@@ -48,19 +48,33 @@ class Runnable extends serializable_js_1.Serializable {
|
|
|
48
48
|
return new RunnableEach({ bound: this });
|
|
49
49
|
}
|
|
50
50
|
/**
|
|
51
|
-
*
|
|
51
|
+
* Add retry logic to an existing runnable.
|
|
52
52
|
* @param kwargs
|
|
53
|
-
* @returns A new
|
|
53
|
+
* @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
|
|
54
54
|
*/
|
|
55
55
|
withRetry(fields) {
|
|
56
56
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
57
57
|
return new RunnableRetry({
|
|
58
58
|
bound: this,
|
|
59
59
|
kwargs: {},
|
|
60
|
+
config: {},
|
|
60
61
|
maxAttemptNumber: fields?.stopAfterAttempt,
|
|
61
62
|
...fields,
|
|
62
63
|
});
|
|
63
64
|
}
|
|
65
|
+
/**
|
|
66
|
+
* Bind config to a Runnable, returning a new Runnable.
|
|
67
|
+
* @param config New configuration parameters to attach to the new runnable.
|
|
68
|
+
* @returns A new RunnableBinding with a config matching what's passed.
|
|
69
|
+
*/
|
|
70
|
+
withConfig(config) {
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
72
|
+
return new RunnableBinding({
|
|
73
|
+
bound: this,
|
|
74
|
+
config,
|
|
75
|
+
kwargs: {},
|
|
76
|
+
});
|
|
77
|
+
}
|
|
64
78
|
/**
|
|
65
79
|
* Create a new runnable from the current one that will try invoking
|
|
66
80
|
* other passed fallback runnables if the initial invocation fails.
|
|
@@ -312,6 +326,12 @@ class RunnableBinding extends Runnable {
|
|
|
312
326
|
writable: true,
|
|
313
327
|
value: void 0
|
|
314
328
|
});
|
|
329
|
+
Object.defineProperty(this, "config", {
|
|
330
|
+
enumerable: true,
|
|
331
|
+
configurable: true,
|
|
332
|
+
writable: true,
|
|
333
|
+
value: void 0
|
|
334
|
+
});
|
|
315
335
|
Object.defineProperty(this, "kwargs", {
|
|
316
336
|
enumerable: true,
|
|
317
337
|
configurable: true,
|
|
@@ -320,35 +340,70 @@ class RunnableBinding extends Runnable {
|
|
|
320
340
|
});
|
|
321
341
|
this.bound = fields.bound;
|
|
322
342
|
this.kwargs = fields.kwargs;
|
|
343
|
+
this.config = fields.config;
|
|
344
|
+
}
|
|
345
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
346
|
+
_mergeConfig(options) {
|
|
347
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
348
|
+
const copy = { ...this.config };
|
|
349
|
+
if (options) {
|
|
350
|
+
for (const key of Object.keys(options)) {
|
|
351
|
+
if (key === "metadata") {
|
|
352
|
+
copy[key] = { ...copy[key], ...options[key] };
|
|
353
|
+
}
|
|
354
|
+
else if (key === "tags") {
|
|
355
|
+
copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
copy[key] = options[key] ?? copy[key];
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
return copy;
|
|
323
363
|
}
|
|
324
364
|
bind(kwargs) {
|
|
325
|
-
return
|
|
365
|
+
return this.constructor({
|
|
326
366
|
bound: this.bound,
|
|
327
367
|
kwargs: { ...this.kwargs, ...kwargs },
|
|
368
|
+
config: this.config,
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
withConfig(config) {
|
|
372
|
+
return this.constructor({
|
|
373
|
+
bound: this.bound,
|
|
374
|
+
kwargs: this.kwargs,
|
|
375
|
+
config: { ...this.config, ...config },
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
withRetry(fields) {
|
|
379
|
+
return this.constructor({
|
|
380
|
+
bound: this.bound.withRetry(fields),
|
|
381
|
+
kwargs: this.kwargs,
|
|
382
|
+
config: this.config,
|
|
328
383
|
});
|
|
329
384
|
}
|
|
330
385
|
async invoke(input, options) {
|
|
331
|
-
return this.bound.invoke(input, { ...options, ...this.kwargs });
|
|
386
|
+
return this.bound.invoke(input, this._mergeConfig({ ...options, ...this.kwargs }));
|
|
332
387
|
}
|
|
333
388
|
async batch(inputs, options, batchOptions) {
|
|
334
389
|
const mergedOptions = Array.isArray(options)
|
|
335
|
-
? options.map((individualOption) => ({
|
|
390
|
+
? options.map((individualOption) => this._mergeConfig({
|
|
336
391
|
...individualOption,
|
|
337
392
|
...this.kwargs,
|
|
338
393
|
}))
|
|
339
|
-
: { ...options, ...this.kwargs };
|
|
394
|
+
: this._mergeConfig({ ...options, ...this.kwargs });
|
|
340
395
|
return this.bound.batch(inputs, mergedOptions, batchOptions);
|
|
341
396
|
}
|
|
342
397
|
async *_streamIterator(input, options) {
|
|
343
|
-
yield* this.bound._streamIterator(input, { ...options, ...this.kwargs });
|
|
398
|
+
yield* this.bound._streamIterator(input, this._mergeConfig({ ...options, ...this.kwargs }));
|
|
344
399
|
}
|
|
345
400
|
async stream(input, options) {
|
|
346
|
-
return this.bound.stream(input, { ...options, ...this.kwargs });
|
|
401
|
+
return this.bound.stream(input, this._mergeConfig({ ...options, ...this.kwargs }));
|
|
347
402
|
}
|
|
348
403
|
async *transform(
|
|
349
404
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
350
405
|
generator, options) {
|
|
351
|
-
yield* this.bound.transform(generator, options);
|
|
406
|
+
yield* this.bound.transform(generator, this._mergeConfig({ ...options, ...this.kwargs }));
|
|
352
407
|
}
|
|
353
408
|
static isRunnableBinding(
|
|
354
409
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -33,14 +33,20 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
33
33
|
*/
|
|
34
34
|
map(): Runnable<RunInput[], RunOutput[], CallOptions>;
|
|
35
35
|
/**
|
|
36
|
-
*
|
|
36
|
+
* Add retry logic to an existing runnable.
|
|
37
37
|
* @param kwargs
|
|
38
|
-
* @returns A new
|
|
38
|
+
* @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
|
|
39
39
|
*/
|
|
40
40
|
withRetry(fields?: {
|
|
41
41
|
stopAfterAttempt?: number;
|
|
42
42
|
onFailedAttempt?: RunnableRetryFailedAttemptHandler;
|
|
43
43
|
}): RunnableRetry<RunInput, RunOutput, CallOptions>;
|
|
44
|
+
/**
|
|
45
|
+
* Bind config to a Runnable, returning a new Runnable.
|
|
46
|
+
* @param config New configuration parameters to attach to the new runnable.
|
|
47
|
+
* @returns A new RunnableBinding with a config matching what's passed.
|
|
48
|
+
*/
|
|
49
|
+
withConfig(config: RunnableConfig): RunnableBinding<RunInput, RunOutput, CallOptions>;
|
|
44
50
|
/**
|
|
45
51
|
* Create a new runnable from the current one that will try invoking
|
|
46
52
|
* other passed fallback runnables if the initial invocation fails.
|
|
@@ -122,18 +128,26 @@ export declare abstract class Runnable<RunInput = any, RunOutput = any, CallOpti
|
|
|
122
128
|
export type RunnableBindingArgs<RunInput, RunOutput, CallOptions extends RunnableConfig> = {
|
|
123
129
|
bound: Runnable<RunInput, RunOutput, CallOptions>;
|
|
124
130
|
kwargs: Partial<CallOptions>;
|
|
131
|
+
config: RunnableConfig;
|
|
125
132
|
};
|
|
126
133
|
/**
|
|
127
134
|
* A runnable that delegates calls to another runnable with a set of kwargs.
|
|
128
135
|
*/
|
|
129
|
-
export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends
|
|
136
|
+
export declare class RunnableBinding<RunInput, RunOutput, CallOptions extends RunnableConfig> extends Runnable<RunInput, RunOutput, CallOptions> {
|
|
130
137
|
static lc_name(): string;
|
|
131
138
|
lc_namespace: string[];
|
|
132
139
|
lc_serializable: boolean;
|
|
133
140
|
bound: Runnable<RunInput, RunOutput, CallOptions>;
|
|
141
|
+
config: RunnableConfig;
|
|
134
142
|
protected kwargs: Partial<CallOptions>;
|
|
135
143
|
constructor(fields: RunnableBindingArgs<RunInput, RunOutput, CallOptions>);
|
|
144
|
+
_mergeConfig(options?: Record<string, any>): Partial<CallOptions>;
|
|
136
145
|
bind(kwargs: Partial<CallOptions>): RunnableBinding<RunInput, RunOutput, CallOptions>;
|
|
146
|
+
withConfig(config: RunnableConfig): RunnableBinding<RunInput, RunOutput, CallOptions>;
|
|
147
|
+
withRetry(fields?: {
|
|
148
|
+
stopAfterAttempt?: number;
|
|
149
|
+
onFailedAttempt?: RunnableRetryFailedAttemptHandler;
|
|
150
|
+
}): RunnableRetry<RunInput, RunOutput, CallOptions>;
|
|
137
151
|
invoke(input: RunInput, options?: Partial<CallOptions>): Promise<RunOutput>;
|
|
138
152
|
batch(inputs: RunInput[], options?: Partial<CallOptions> | Partial<CallOptions>[], batchOptions?: RunnableBatchOptions & {
|
|
139
153
|
returnExceptions?: false;
|
|
@@ -31,7 +31,7 @@ export class Runnable extends Serializable {
|
|
|
31
31
|
*/
|
|
32
32
|
bind(kwargs) {
|
|
33
33
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
34
|
-
return new RunnableBinding({ bound: this, kwargs });
|
|
34
|
+
return new RunnableBinding({ bound: this, kwargs, config: {} });
|
|
35
35
|
}
|
|
36
36
|
/**
|
|
37
37
|
* Return a new Runnable that maps a list of inputs to a list of outputs,
|
|
@@ -42,19 +42,33 @@ export class Runnable extends Serializable {
|
|
|
42
42
|
return new RunnableEach({ bound: this });
|
|
43
43
|
}
|
|
44
44
|
/**
|
|
45
|
-
*
|
|
45
|
+
* Add retry logic to an existing runnable.
|
|
46
46
|
* @param kwargs
|
|
47
|
-
* @returns A new
|
|
47
|
+
* @returns A new RunnableRetry that, when invoked, will retry according to the parameters.
|
|
48
48
|
*/
|
|
49
49
|
withRetry(fields) {
|
|
50
50
|
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
51
51
|
return new RunnableRetry({
|
|
52
52
|
bound: this,
|
|
53
53
|
kwargs: {},
|
|
54
|
+
config: {},
|
|
54
55
|
maxAttemptNumber: fields?.stopAfterAttempt,
|
|
55
56
|
...fields,
|
|
56
57
|
});
|
|
57
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* Bind config to a Runnable, returning a new Runnable.
|
|
61
|
+
* @param config New configuration parameters to attach to the new runnable.
|
|
62
|
+
* @returns A new RunnableBinding with a config matching what's passed.
|
|
63
|
+
*/
|
|
64
|
+
withConfig(config) {
|
|
65
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
66
|
+
return new RunnableBinding({
|
|
67
|
+
bound: this,
|
|
68
|
+
config,
|
|
69
|
+
kwargs: {},
|
|
70
|
+
});
|
|
71
|
+
}
|
|
58
72
|
/**
|
|
59
73
|
* Create a new runnable from the current one that will try invoking
|
|
60
74
|
* other passed fallback runnables if the initial invocation fails.
|
|
@@ -305,6 +319,12 @@ export class RunnableBinding extends Runnable {
|
|
|
305
319
|
writable: true,
|
|
306
320
|
value: void 0
|
|
307
321
|
});
|
|
322
|
+
Object.defineProperty(this, "config", {
|
|
323
|
+
enumerable: true,
|
|
324
|
+
configurable: true,
|
|
325
|
+
writable: true,
|
|
326
|
+
value: void 0
|
|
327
|
+
});
|
|
308
328
|
Object.defineProperty(this, "kwargs", {
|
|
309
329
|
enumerable: true,
|
|
310
330
|
configurable: true,
|
|
@@ -313,35 +333,70 @@ export class RunnableBinding extends Runnable {
|
|
|
313
333
|
});
|
|
314
334
|
this.bound = fields.bound;
|
|
315
335
|
this.kwargs = fields.kwargs;
|
|
336
|
+
this.config = fields.config;
|
|
337
|
+
}
|
|
338
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
339
|
+
_mergeConfig(options) {
|
|
340
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
341
|
+
const copy = { ...this.config };
|
|
342
|
+
if (options) {
|
|
343
|
+
for (const key of Object.keys(options)) {
|
|
344
|
+
if (key === "metadata") {
|
|
345
|
+
copy[key] = { ...copy[key], ...options[key] };
|
|
346
|
+
}
|
|
347
|
+
else if (key === "tags") {
|
|
348
|
+
copy[key] = (copy[key] ?? []).concat(options[key] ?? []);
|
|
349
|
+
}
|
|
350
|
+
else {
|
|
351
|
+
copy[key] = options[key] ?? copy[key];
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
return copy;
|
|
316
356
|
}
|
|
317
357
|
bind(kwargs) {
|
|
318
|
-
return
|
|
358
|
+
return this.constructor({
|
|
319
359
|
bound: this.bound,
|
|
320
360
|
kwargs: { ...this.kwargs, ...kwargs },
|
|
361
|
+
config: this.config,
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
withConfig(config) {
|
|
365
|
+
return this.constructor({
|
|
366
|
+
bound: this.bound,
|
|
367
|
+
kwargs: this.kwargs,
|
|
368
|
+
config: { ...this.config, ...config },
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
withRetry(fields) {
|
|
372
|
+
return this.constructor({
|
|
373
|
+
bound: this.bound.withRetry(fields),
|
|
374
|
+
kwargs: this.kwargs,
|
|
375
|
+
config: this.config,
|
|
321
376
|
});
|
|
322
377
|
}
|
|
323
378
|
async invoke(input, options) {
|
|
324
|
-
return this.bound.invoke(input, { ...options, ...this.kwargs });
|
|
379
|
+
return this.bound.invoke(input, this._mergeConfig({ ...options, ...this.kwargs }));
|
|
325
380
|
}
|
|
326
381
|
async batch(inputs, options, batchOptions) {
|
|
327
382
|
const mergedOptions = Array.isArray(options)
|
|
328
|
-
? options.map((individualOption) => ({
|
|
383
|
+
? options.map((individualOption) => this._mergeConfig({
|
|
329
384
|
...individualOption,
|
|
330
385
|
...this.kwargs,
|
|
331
386
|
}))
|
|
332
|
-
: { ...options, ...this.kwargs };
|
|
387
|
+
: this._mergeConfig({ ...options, ...this.kwargs });
|
|
333
388
|
return this.bound.batch(inputs, mergedOptions, batchOptions);
|
|
334
389
|
}
|
|
335
390
|
async *_streamIterator(input, options) {
|
|
336
|
-
yield* this.bound._streamIterator(input, { ...options, ...this.kwargs });
|
|
391
|
+
yield* this.bound._streamIterator(input, this._mergeConfig({ ...options, ...this.kwargs }));
|
|
337
392
|
}
|
|
338
393
|
async stream(input, options) {
|
|
339
|
-
return this.bound.stream(input, { ...options, ...this.kwargs });
|
|
394
|
+
return this.bound.stream(input, this._mergeConfig({ ...options, ...this.kwargs }));
|
|
340
395
|
}
|
|
341
396
|
async *transform(
|
|
342
397
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
343
398
|
generator, options) {
|
|
344
|
-
yield* this.bound.transform(generator, options);
|
|
399
|
+
yield* this.bound.transform(generator, this._mergeConfig({ ...options, ...this.kwargs }));
|
|
345
400
|
}
|
|
346
401
|
static isRunnableBinding(
|
|
347
402
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CloudflareD1MessageHistory = void 0;
|
|
4
|
+
const uuid_1 = require("uuid");
|
|
5
|
+
const index_js_1 = require("../../schema/index.cjs");
|
|
6
|
+
const utils_js_1 = require("./utils.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Class for storing and retrieving chat message history from a
|
|
9
|
+
* Cloudflare D1 database. Extends the BaseListChatMessageHistory class.
|
|
10
|
+
*/
|
|
11
|
+
class CloudflareD1MessageHistory extends index_js_1.BaseListChatMessageHistory {
|
|
12
|
+
constructor(fields) {
|
|
13
|
+
super(fields);
|
|
14
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
configurable: true,
|
|
17
|
+
writable: true,
|
|
18
|
+
value: ["langchain", "stores", "message", "cloudflare_d1"]
|
|
19
|
+
});
|
|
20
|
+
Object.defineProperty(this, "database", {
|
|
21
|
+
enumerable: true,
|
|
22
|
+
configurable: true,
|
|
23
|
+
writable: true,
|
|
24
|
+
value: void 0
|
|
25
|
+
});
|
|
26
|
+
Object.defineProperty(this, "tableName", {
|
|
27
|
+
enumerable: true,
|
|
28
|
+
configurable: true,
|
|
29
|
+
writable: true,
|
|
30
|
+
value: void 0
|
|
31
|
+
});
|
|
32
|
+
Object.defineProperty(this, "sessionId", {
|
|
33
|
+
enumerable: true,
|
|
34
|
+
configurable: true,
|
|
35
|
+
writable: true,
|
|
36
|
+
value: void 0
|
|
37
|
+
});
|
|
38
|
+
Object.defineProperty(this, "tableInitialized", {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
configurable: true,
|
|
41
|
+
writable: true,
|
|
42
|
+
value: void 0
|
|
43
|
+
});
|
|
44
|
+
const { sessionId, database, tableName } = fields;
|
|
45
|
+
if (database) {
|
|
46
|
+
this.database = database;
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
throw new Error("Either a client or config must be provided to CloudflareD1MessageHistory");
|
|
50
|
+
}
|
|
51
|
+
this.tableName = tableName || "langchain_chat_histories";
|
|
52
|
+
this.tableInitialized = false;
|
|
53
|
+
this.sessionId = sessionId;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Private method to ensure that the necessary table exists in the
|
|
57
|
+
* Cloudflare D1 database before performing any operations. If the table
|
|
58
|
+
* does not exist, it is created.
|
|
59
|
+
* @returns Promise that resolves to void.
|
|
60
|
+
*/
|
|
61
|
+
async ensureTable() {
|
|
62
|
+
if (this.tableInitialized) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
const query = `CREATE TABLE IF NOT EXISTS ${this.tableName} (id TEXT PRIMARY KEY, session_id TEXT, type TEXT, content TEXT, role TEXT, name TEXT, additional_kwargs TEXT);`;
|
|
66
|
+
await this.database.prepare(query).bind().all();
|
|
67
|
+
const idIndexQuery = `CREATE INDEX IF NOT EXISTS id_index ON ${this.tableName} (id);`;
|
|
68
|
+
await this.database.prepare(idIndexQuery).bind().all();
|
|
69
|
+
const sessionIdIndexQuery = `CREATE INDEX IF NOT EXISTS session_id_index ON ${this.tableName} (session_id);`;
|
|
70
|
+
await this.database.prepare(sessionIdIndexQuery).bind().all();
|
|
71
|
+
this.tableInitialized = true;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Method to retrieve all messages from the Cloudflare D1 database for the
|
|
75
|
+
* current session.
|
|
76
|
+
* @returns Promise that resolves to an array of BaseMessage objects.
|
|
77
|
+
*/
|
|
78
|
+
async getMessages() {
|
|
79
|
+
await this.ensureTable();
|
|
80
|
+
const query = `SELECT * FROM ${this.tableName} WHERE session_id = ?`;
|
|
81
|
+
const rawStoredMessages = await this.database
|
|
82
|
+
.prepare(query)
|
|
83
|
+
.bind(this.sessionId)
|
|
84
|
+
.all();
|
|
85
|
+
const storedMessagesObject = rawStoredMessages.results;
|
|
86
|
+
const orderedMessages = storedMessagesObject.map((message) => {
|
|
87
|
+
const data = {
|
|
88
|
+
content: message.content,
|
|
89
|
+
additional_kwargs: JSON.parse(message.additional_kwargs),
|
|
90
|
+
};
|
|
91
|
+
if (message.role) {
|
|
92
|
+
data.role = message.role;
|
|
93
|
+
}
|
|
94
|
+
if (message.name) {
|
|
95
|
+
data.name = message.name;
|
|
96
|
+
}
|
|
97
|
+
return {
|
|
98
|
+
type: message.type,
|
|
99
|
+
data,
|
|
100
|
+
};
|
|
101
|
+
});
|
|
102
|
+
return (0, utils_js_1.mapStoredMessagesToChatMessages)(orderedMessages);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Method to add a new message to the Cloudflare D1 database for the current
|
|
106
|
+
* session.
|
|
107
|
+
* @param message The BaseMessage object to be added to the database.
|
|
108
|
+
* @returns Promise that resolves to void.
|
|
109
|
+
*/
|
|
110
|
+
async addMessage(message) {
|
|
111
|
+
await this.ensureTable();
|
|
112
|
+
const messageToAdd = (0, utils_js_1.mapChatMessagesToStoredMessages)([message]);
|
|
113
|
+
const query = `INSERT INTO ${this.tableName} (id, session_id, type, content, role, name, additional_kwargs) VALUES(?, ?, ?, ?, ?, ?, ?)`;
|
|
114
|
+
const id = (0, uuid_1.v4)();
|
|
115
|
+
await this.database
|
|
116
|
+
.prepare(query)
|
|
117
|
+
.bind(id, this.sessionId, messageToAdd[0].type || null, messageToAdd[0].data.content || null, messageToAdd[0].data.role || null, messageToAdd[0].data.name || null, JSON.stringify(messageToAdd[0].data.additional_kwargs))
|
|
118
|
+
.all();
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Method to delete all messages from the Cloudflare D1 database for the
|
|
122
|
+
* current session.
|
|
123
|
+
* @returns Promise that resolves to void.
|
|
124
|
+
*/
|
|
125
|
+
async clear() {
|
|
126
|
+
await this.ensureTable();
|
|
127
|
+
const query = `DELETE FROM ? WHERE session_id = ? `;
|
|
128
|
+
await this.database
|
|
129
|
+
.prepare(query)
|
|
130
|
+
.bind(this.tableName, this.sessionId)
|
|
131
|
+
.all();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.CloudflareD1MessageHistory = CloudflareD1MessageHistory;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { D1Database } from "@cloudflare/workers-types";
|
|
2
|
+
import { BaseMessage, BaseListChatMessageHistory } from "../../schema/index.js";
|
|
3
|
+
/**
|
|
4
|
+
* Type definition for the input parameters required when instantiating a
|
|
5
|
+
* CloudflareD1MessageHistory object.
|
|
6
|
+
*/
|
|
7
|
+
export type CloudflareD1MessageHistoryInput = {
|
|
8
|
+
tableName?: string;
|
|
9
|
+
sessionId: string;
|
|
10
|
+
database?: D1Database;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Class for storing and retrieving chat message history from a
|
|
14
|
+
* Cloudflare D1 database. Extends the BaseListChatMessageHistory class.
|
|
15
|
+
*/
|
|
16
|
+
export declare class CloudflareD1MessageHistory extends BaseListChatMessageHistory {
|
|
17
|
+
lc_namespace: string[];
|
|
18
|
+
database: D1Database;
|
|
19
|
+
private tableName;
|
|
20
|
+
private sessionId;
|
|
21
|
+
private tableInitialized;
|
|
22
|
+
constructor(fields: CloudflareD1MessageHistoryInput);
|
|
23
|
+
/**
|
|
24
|
+
* Private method to ensure that the necessary table exists in the
|
|
25
|
+
* Cloudflare D1 database before performing any operations. If the table
|
|
26
|
+
* does not exist, it is created.
|
|
27
|
+
* @returns Promise that resolves to void.
|
|
28
|
+
*/
|
|
29
|
+
private ensureTable;
|
|
30
|
+
/**
|
|
31
|
+
* Method to retrieve all messages from the Cloudflare D1 database for the
|
|
32
|
+
* current session.
|
|
33
|
+
* @returns Promise that resolves to an array of BaseMessage objects.
|
|
34
|
+
*/
|
|
35
|
+
getMessages(): Promise<BaseMessage[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Method to add a new message to the Cloudflare D1 database for the current
|
|
38
|
+
* session.
|
|
39
|
+
* @param message The BaseMessage object to be added to the database.
|
|
40
|
+
* @returns Promise that resolves to void.
|
|
41
|
+
*/
|
|
42
|
+
addMessage(message: BaseMessage): Promise<void>;
|
|
43
|
+
/**
|
|
44
|
+
* Method to delete all messages from the Cloudflare D1 database for the
|
|
45
|
+
* current session.
|
|
46
|
+
* @returns Promise that resolves to void.
|
|
47
|
+
*/
|
|
48
|
+
clear(): Promise<void>;
|
|
49
|
+
}
|