-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathtransform.ts
72 lines (62 loc) · 2.11 KB
/
transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import {
CallbackManagerForChainRun,
Callbacks,
} from "@langchain/core/callbacks/manager";
import { ChainValues } from "@langchain/core/utils/types";
import { ChainInputs, BaseChain } from "./base.js";
/**
* Interface that extends the `ChainInputs` interface and defines the
* fields required for a transform chain. It includes the `transform`
* function, `inputVariables`, and `outputVariables` properties.
*
* @deprecated
* Switch to expression language: https://js.langchain.com/docs/expression_language/
* Will be removed in 0.2.0
*/
export interface TransformChainFields<
I extends ChainValues,
O extends ChainValues
> extends ChainInputs {
transform: (values: I, callbacks?: Callbacks) => O | Promise<O>;
inputVariables: (keyof I extends string ? keyof I : never)[];
outputVariables: (keyof O extends string ? keyof O : never)[];
}
/**
* Class that represents a transform chain. It extends the `BaseChain`
* class and implements the `TransformChainFields` interface. It provides
* a way to transform input values to output values using a specified
* transform function.
*
* @deprecated
* Switch to {@link https://js.langchain.com/docs/expression_language/ | expression language}.
* Will be removed in 0.2.0
*/
export class TransformChain<
I extends ChainValues,
O extends ChainValues
> extends BaseChain {
static lc_name() {
return "TransformChain";
}
transformFunc: (values: I, callbacks?: Callbacks) => O | Promise<O>;
inputVariables: (keyof I extends string ? keyof I : never)[];
outputVariables: (keyof O extends string ? keyof O : never)[];
_chainType() {
return "transform" as const;
}
get inputKeys() {
return this.inputVariables;
}
get outputKeys() {
return this.outputVariables;
}
constructor(fields: TransformChainFields<I, O>) {
super(fields);
this.transformFunc = fields.transform;
this.inputVariables = fields.inputVariables;
this.outputVariables = fields.outputVariables;
}
async _call(values: I, runManager?: CallbackManagerForChainRun): Promise<O> {
return this.transformFunc(values, runManager?.getChild("transform"));
}
}