-
Notifications
You must be signed in to change notification settings - Fork 2k
/
image.ts
166 lines (152 loc) Β· 5.17 KB
/
image.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { MessageContent } from "../messages/index.js";
import { ImagePromptValue, ImageContent } from "../prompt_values.js";
import type { InputValues, PartialValues } from "../utils/types/index.js";
import {
BasePromptTemplate,
BasePromptTemplateInput,
TypedPromptInputValues,
} from "./base.js";
import { TemplateFormat, checkValidTemplate } from "./template.js";
/**
* Inputs to create a {@link ImagePromptTemplate}
* @augments BasePromptTemplateInput
*/
export interface ImagePromptTemplateInput<
// 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 BasePromptTemplateInput<RunInput, PartialVariableName> {
/**
* The prompt template
*/
template: Record<string, unknown>;
/**
* The format of the prompt template. Options are 'f-string'
*
* @defaultValue 'f-string'
*/
templateFormat?: TemplateFormat;
/**
* Whether or not to try validating the template on initialization
*
* @defaultValue `true`
*/
validateTemplate?: boolean;
}
/**
* An image prompt template for a multimodal model.
*/
export class ImagePromptTemplate<
// 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 BasePromptTemplate<RunInput, ImagePromptValue, PartialVariableName> {
static lc_name() {
return "ImagePromptTemplate";
}
lc_namespace = ["langchain_core", "prompts", "image"];
template: Record<string, unknown>;
templateFormat: TemplateFormat = "f-string";
validateTemplate = true;
constructor(input: ImagePromptTemplateInput<RunInput, PartialVariableName>) {
super(input);
this.template = input.template;
this.templateFormat = input.templateFormat ?? this.templateFormat;
this.validateTemplate = input.validateTemplate ?? this.validateTemplate;
if (this.validateTemplate) {
let totalInputVariables: string[] = this.inputVariables;
if (this.partialVariables) {
totalInputVariables = totalInputVariables.concat(
Object.keys(this.partialVariables)
);
}
checkValidTemplate(
[
{ type: "image_url", image_url: this.template },
] as unknown as MessageContent,
this.templateFormat,
totalInputVariables
);
}
}
_getPromptType(): "prompt" {
return "prompt";
}
/**
* Partially applies values to the prompt template.
* @param values The values to be partially applied to the prompt template.
* @returns A new instance of ImagePromptTemplate with the partially applied values.
*/
async partial<NewPartialVariableName extends string>(
values: PartialValues<NewPartialVariableName>
) {
const newInputVariables = this.inputVariables.filter(
(iv) => !(iv in values)
) as Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>[];
const newPartialVariables = {
...(this.partialVariables ?? {}),
...values,
} as PartialValues<PartialVariableName | NewPartialVariableName>;
const promptDict = {
...this,
inputVariables: newInputVariables,
partialVariables: newPartialVariables,
};
return new ImagePromptTemplate<
InputValues<
Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>
>
>(promptDict);
}
/**
* Formats the prompt template with the provided values.
* @param values The values to be used to format the prompt template.
* @returns A promise that resolves to a string which is the formatted prompt.
*/
async format<FormatOutput = ImageContent>(
values: TypedPromptInputValues<RunInput>
): Promise<FormatOutput> {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const formatted: Record<string, any> = {};
for (const [key, value] of Object.entries(this.template)) {
if (typeof value === "string") {
formatted[key] = value.replace(/{([^{}]*)}/g, (match, group) => {
const replacement = values[group];
return typeof replacement === "string" ||
typeof replacement === "number"
? String(replacement)
: match;
});
} else {
formatted[key] = value;
}
}
const url = values.url || formatted.url;
const detail = values.detail || formatted.detail;
if (!url) {
throw new Error("Must provide either an image URL.");
}
if (typeof url !== "string") {
throw new Error("url must be a string.");
}
const output: ImageContent = { url };
if (detail) {
output.detail = detail;
}
return output as FormatOutput;
}
/**
* Formats the prompt given the input values and returns a formatted
* prompt value.
* @param values The input values to format the prompt.
* @returns A Promise that resolves to a formatted prompt value.
*/
async formatPromptValue(
values: TypedPromptInputValues<RunInput>
): Promise<ImagePromptValue> {
const formattedPrompt = await this.format(values);
return new ImagePromptValue(formattedPrompt);
}
}