-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
structured.ts
116 lines (105 loc) Β· 3.43 KB
/
structured.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { ChatPromptValueInterface } from "../prompt_values.js";
import {
RunnableLike,
Runnable,
type RunnableBinding,
} from "../runnables/base.js";
import { RunnableConfig } from "../runnables/config.js";
import { InputValues } from "../utils/types/index.js";
import {
BaseMessagePromptTemplateLike,
ChatPromptTemplate,
ChatPromptTemplateInput,
} from "./chat.js";
function isWithStructuredOutput(
x: unknown
// eslint-disable-next-line @typescript-eslint/ban-types
): x is {
withStructuredOutput: (...arg: unknown[]) => Runnable;
} {
return (
typeof x === "object" &&
x != null &&
"withStructuredOutput" in x &&
typeof x.withStructuredOutput === "function"
);
}
function isRunnableBinding(x: unknown): x is RunnableBinding<unknown, unknown> {
return (
typeof x === "object" &&
x != null &&
"lc_id" in x &&
Array.isArray(x.lc_id) &&
x.lc_id.join("/") === "langchain_core/runnables/RunnableBinding"
);
}
/**
* Interface for the input of a ChatPromptTemplate.
*/
export interface StructuredPromptInput<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunInput extends InputValues = any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
PartialVariableName extends string = any
> extends ChatPromptTemplateInput<RunInput, PartialVariableName> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
schema: Record<string, any>;
}
export class StructuredPrompt<
// eslint-disable-next-line @typescript-eslint/no-explicit-any
RunInput extends InputValues = any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
PartialVariableName extends string = any
>
extends ChatPromptTemplate<RunInput, PartialVariableName>
implements StructuredPromptInput<RunInput, PartialVariableName>
{
// eslint-disable-next-line @typescript-eslint/no-explicit-any
schema: Record<string, any>;
lc_namespace = ["langchain_core", "prompts", "structured"];
get lc_aliases(): Record<string, string> {
return {
...super.lc_aliases,
schema: "schema_",
};
}
constructor(input: StructuredPromptInput<RunInput, PartialVariableName>) {
super(input);
this.schema = input.schema;
}
pipe<NewRunOutput>(
coerceable: RunnableLike<ChatPromptValueInterface, NewRunOutput>
): Runnable<RunInput, Exclude<NewRunOutput, Error>, RunnableConfig> {
if (isWithStructuredOutput(coerceable)) {
return super.pipe(coerceable.withStructuredOutput(this.schema));
}
if (
isRunnableBinding(coerceable) &&
isWithStructuredOutput(coerceable.bound)
) {
return super.pipe(
coerceable.bound
.withStructuredOutput(this.schema)
.bind(coerceable.kwargs ?? {})
.withConfig(coerceable.config)
);
}
throw new Error(
`Structured prompts need to be piped to a language model that supports the "withStructuredOutput()" method.`
);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
static fromMessagesAndSchema<RunInput extends InputValues = any>(
promptMessages: (
| ChatPromptTemplate<InputValues, string>
| BaseMessagePromptTemplateLike
)[],
schema: StructuredPromptInput["schema"]
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): ChatPromptTemplate<RunInput, any> {
return StructuredPrompt.fromMessages<
RunInput,
StructuredPromptInput<RunInput>
>(promptMessages, { schema });
}
}