@langchain/core 0.1.20 → 0.1.21
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/agents.d.cts +1 -0
- package/caches.d.cts +1 -0
- package/callbacks/base.d.cts +1 -0
- package/callbacks/manager.d.cts +1 -0
- package/callbacks/promises.d.cts +1 -0
- package/chat_history.d.cts +1 -0
- package/dist/load/import_type.cjs +3 -0
- package/dist/load/import_type.d.ts +4 -0
- package/dist/load/import_type.js +2 -0
- package/dist/messages/index.cjs +3 -0
- package/dist/messages/index.d.ts +5 -2
- package/dist/messages/index.js +3 -0
- package/dist/prompt_values.cjs +62 -1
- package/dist/prompt_values.d.ts +25 -0
- package/dist/prompt_values.js +60 -0
- package/dist/prompts/chat.cjs +235 -27
- package/dist/prompts/chat.d.ts +35 -10
- package/dist/prompts/chat.js +235 -27
- package/dist/prompts/image.cjs +115 -0
- package/dist/prompts/image.d.ts +56 -0
- package/dist/prompts/image.js +111 -0
- package/dist/prompts/prompt.d.ts +3 -2
- package/dist/prompts/serde.d.ts +2 -1
- package/dist/prompts/template.cjs +22 -1
- package/dist/prompts/template.d.ts +2 -1
- package/dist/prompts/template.js +22 -1
- package/dist/runnables/base.cjs +4 -3
- package/dist/runnables/base.js +4 -3
- package/dist/utils/stream.cjs +3 -0
- package/dist/utils/stream.js +3 -0
- package/documents.d.cts +1 -0
- package/embeddings.d.cts +1 -0
- package/example_selectors.d.cts +1 -0
- package/language_models/base.d.cts +1 -0
- package/language_models/chat_models.d.cts +1 -0
- package/language_models/llms.d.cts +1 -0
- package/load/serializable.d.cts +1 -0
- package/load.d.cts +1 -0
- package/memory.d.cts +1 -0
- package/messages.d.cts +1 -0
- package/output_parsers.d.cts +1 -0
- package/outputs.d.cts +1 -0
- package/package.json +267 -47
- package/prompt_values.d.cts +1 -0
- package/prompts.d.cts +1 -0
- package/retrievers.d.cts +1 -0
- package/runnables.d.cts +1 -0
- package/stores.d.cts +1 -0
- package/tools.d.cts +1 -0
- package/tracers/base.d.cts +1 -0
- package/tracers/console.d.cts +1 -0
- package/tracers/initialize.d.cts +1 -0
- package/tracers/log_stream.d.cts +1 -0
- package/tracers/run_collector.d.cts +1 -0
- package/tracers/tracer_langchain.d.cts +1 -0
- package/tracers/tracer_langchain_v1.d.cts +1 -0
- package/utils/async_caller.d.cts +1 -0
- package/utils/chunk_array.d.cts +1 -0
- package/utils/env.d.cts +1 -0
- package/utils/function_calling.d.cts +1 -0
- package/utils/hash.d.cts +1 -0
- package/utils/json_patch.d.cts +1 -0
- package/utils/json_schema.d.cts +1 -0
- package/utils/math.d.cts +1 -0
- package/utils/stream.d.cts +1 -0
- package/utils/testing.d.cts +1 -0
- package/utils/tiktoken.d.cts +1 -0
- package/utils/types.d.cts +1 -0
- package/vectorstores.d.cts +1 -0
package/dist/prompts/chat.js
CHANGED
|
@@ -3,8 +3,11 @@
|
|
|
3
3
|
import { AIMessage, HumanMessage, SystemMessage, BaseMessage, ChatMessage, coerceMessageLikeToMessage, isBaseMessage, } from "../messages/index.js";
|
|
4
4
|
import { ChatPromptValue, } from "../prompt_values.js";
|
|
5
5
|
import { Runnable } from "../runnables/base.js";
|
|
6
|
+
import { BaseStringPromptTemplate } from "./string.js";
|
|
6
7
|
import { BasePromptTemplate, } from "./base.js";
|
|
7
8
|
import { PromptTemplate } from "./prompt.js";
|
|
9
|
+
import { ImagePromptTemplate } from "./image.js";
|
|
10
|
+
import { parseFString } from "./template.js";
|
|
8
11
|
/**
|
|
9
12
|
* Abstract class that serves as a base for creating message prompt
|
|
10
13
|
* templates. It defines how to format messages for different roles in a
|
|
@@ -168,6 +171,210 @@ export class ChatMessagePromptTemplate extends BaseMessageStringPromptTemplate {
|
|
|
168
171
|
return new this(PromptTemplate.fromTemplate(template), role);
|
|
169
172
|
}
|
|
170
173
|
}
|
|
174
|
+
class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate {
|
|
175
|
+
static _messageClass() {
|
|
176
|
+
throw new Error("Can not invoke _messageClass from inside _StringImageMessagePromptTemplate");
|
|
177
|
+
}
|
|
178
|
+
constructor(
|
|
179
|
+
/** @TODO When we come up with a better way to type prompt templates, fix this */
|
|
180
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
181
|
+
fields, additionalOptions) {
|
|
182
|
+
if (!("prompt" in fields)) {
|
|
183
|
+
// eslint-disable-next-line no-param-reassign
|
|
184
|
+
fields = { prompt: fields };
|
|
185
|
+
}
|
|
186
|
+
super(fields);
|
|
187
|
+
Object.defineProperty(this, "lc_namespace", {
|
|
188
|
+
enumerable: true,
|
|
189
|
+
configurable: true,
|
|
190
|
+
writable: true,
|
|
191
|
+
value: ["langchain_core", "prompts", "chat"]
|
|
192
|
+
});
|
|
193
|
+
Object.defineProperty(this, "lc_serializable", {
|
|
194
|
+
enumerable: true,
|
|
195
|
+
configurable: true,
|
|
196
|
+
writable: true,
|
|
197
|
+
value: true
|
|
198
|
+
});
|
|
199
|
+
Object.defineProperty(this, "inputVariables", {
|
|
200
|
+
enumerable: true,
|
|
201
|
+
configurable: true,
|
|
202
|
+
writable: true,
|
|
203
|
+
value: []
|
|
204
|
+
});
|
|
205
|
+
Object.defineProperty(this, "additionalOptions", {
|
|
206
|
+
enumerable: true,
|
|
207
|
+
configurable: true,
|
|
208
|
+
writable: true,
|
|
209
|
+
value: {}
|
|
210
|
+
});
|
|
211
|
+
Object.defineProperty(this, "prompt", {
|
|
212
|
+
enumerable: true,
|
|
213
|
+
configurable: true,
|
|
214
|
+
writable: true,
|
|
215
|
+
value: void 0
|
|
216
|
+
});
|
|
217
|
+
Object.defineProperty(this, "messageClass", {
|
|
218
|
+
enumerable: true,
|
|
219
|
+
configurable: true,
|
|
220
|
+
writable: true,
|
|
221
|
+
value: void 0
|
|
222
|
+
});
|
|
223
|
+
// ChatMessage contains role field, others don't.
|
|
224
|
+
// Because of this, we have a separate class property for ChatMessage.
|
|
225
|
+
Object.defineProperty(this, "chatMessageClass", {
|
|
226
|
+
enumerable: true,
|
|
227
|
+
configurable: true,
|
|
228
|
+
writable: true,
|
|
229
|
+
value: void 0
|
|
230
|
+
});
|
|
231
|
+
this.prompt = fields.prompt;
|
|
232
|
+
if (Array.isArray(this.prompt)) {
|
|
233
|
+
let inputVariables = [];
|
|
234
|
+
this.prompt.forEach((prompt) => {
|
|
235
|
+
if ("inputVariables" in prompt) {
|
|
236
|
+
inputVariables = inputVariables.concat(prompt.inputVariables);
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
this.inputVariables = inputVariables;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
this.inputVariables = this.prompt.inputVariables;
|
|
243
|
+
}
|
|
244
|
+
this.additionalOptions = additionalOptions ?? this.additionalOptions;
|
|
245
|
+
}
|
|
246
|
+
createMessage(content) {
|
|
247
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
248
|
+
const constructor = this.constructor;
|
|
249
|
+
if (constructor._messageClass()) {
|
|
250
|
+
const MsgClass = constructor._messageClass();
|
|
251
|
+
return new MsgClass({ content });
|
|
252
|
+
}
|
|
253
|
+
else if (constructor.chatMessageClass) {
|
|
254
|
+
const MsgClass = constructor.chatMessageClass();
|
|
255
|
+
// Assuming ChatMessage constructor also takes a content argument
|
|
256
|
+
return new MsgClass({
|
|
257
|
+
content,
|
|
258
|
+
role: this.getRoleFromMessageClass(MsgClass.lc_name()),
|
|
259
|
+
});
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
throw new Error("No message class defined");
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
getRoleFromMessageClass(name) {
|
|
266
|
+
switch (name) {
|
|
267
|
+
case "HumanMessage":
|
|
268
|
+
return "human";
|
|
269
|
+
case "AIMessage":
|
|
270
|
+
return "ai";
|
|
271
|
+
case "SystemMessage":
|
|
272
|
+
return "system";
|
|
273
|
+
case "ChatMessage":
|
|
274
|
+
return "chat";
|
|
275
|
+
default:
|
|
276
|
+
throw new Error("Invalid message class name");
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
static fromTemplate(template, additionalOptions) {
|
|
280
|
+
if (typeof template === "string") {
|
|
281
|
+
return new this(PromptTemplate.fromTemplate(template));
|
|
282
|
+
}
|
|
283
|
+
const prompt = [];
|
|
284
|
+
for (const item of template) {
|
|
285
|
+
if (typeof item === "string" ||
|
|
286
|
+
(typeof item === "object" && "text" in item)) {
|
|
287
|
+
let text = "";
|
|
288
|
+
if (typeof item === "string") {
|
|
289
|
+
text = item;
|
|
290
|
+
}
|
|
291
|
+
else if (typeof item.text === "string") {
|
|
292
|
+
text = item.text ?? "";
|
|
293
|
+
}
|
|
294
|
+
prompt.push(PromptTemplate.fromTemplate(text));
|
|
295
|
+
}
|
|
296
|
+
else if (typeof item === "object" && "image_url" in item) {
|
|
297
|
+
let imgTemplate = item.image_url ?? "";
|
|
298
|
+
let imgTemplateObject;
|
|
299
|
+
let inputVariables = [];
|
|
300
|
+
if (typeof imgTemplate === "string") {
|
|
301
|
+
const parsedTemplate = parseFString(imgTemplate);
|
|
302
|
+
const variables = parsedTemplate.flatMap((item) => item.type === "variable" ? [item.name] : []);
|
|
303
|
+
if (variables) {
|
|
304
|
+
if (variables.length > 1) {
|
|
305
|
+
throw new Error(`Only one format variable allowed per image template.\nGot: ${variables}\nFrom: ${imgTemplate}`);
|
|
306
|
+
}
|
|
307
|
+
inputVariables = [variables[0]];
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
inputVariables = [];
|
|
311
|
+
}
|
|
312
|
+
imgTemplate = { url: imgTemplate };
|
|
313
|
+
imgTemplateObject = new ImagePromptTemplate({
|
|
314
|
+
template: imgTemplate,
|
|
315
|
+
inputVariables,
|
|
316
|
+
});
|
|
317
|
+
}
|
|
318
|
+
else if (typeof imgTemplate === "object") {
|
|
319
|
+
if ("url" in imgTemplate) {
|
|
320
|
+
const parsedTemplate = parseFString(imgTemplate.url);
|
|
321
|
+
inputVariables = parsedTemplate.flatMap((item) => item.type === "variable" ? [item.name] : []);
|
|
322
|
+
}
|
|
323
|
+
else {
|
|
324
|
+
inputVariables = [];
|
|
325
|
+
}
|
|
326
|
+
imgTemplateObject = new ImagePromptTemplate({
|
|
327
|
+
template: imgTemplate,
|
|
328
|
+
inputVariables,
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
else {
|
|
332
|
+
throw new Error("Invalid image template");
|
|
333
|
+
}
|
|
334
|
+
prompt.push(imgTemplateObject);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
return new this({ prompt, additionalOptions });
|
|
338
|
+
}
|
|
339
|
+
async format(input) {
|
|
340
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
341
|
+
if (this.prompt instanceof BaseStringPromptTemplate) {
|
|
342
|
+
const text = await this.prompt.format(input);
|
|
343
|
+
return this.createMessage(text);
|
|
344
|
+
}
|
|
345
|
+
else {
|
|
346
|
+
const content = [];
|
|
347
|
+
for (const prompt of this.prompt) {
|
|
348
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
349
|
+
let inputs = {};
|
|
350
|
+
if (!("inputVariables" in prompt)) {
|
|
351
|
+
throw new Error(`Prompt ${prompt} does not have inputVariables defined.`);
|
|
352
|
+
}
|
|
353
|
+
for (const item of prompt.inputVariables) {
|
|
354
|
+
if (!inputs) {
|
|
355
|
+
inputs = { [item]: input[item] };
|
|
356
|
+
}
|
|
357
|
+
inputs = { ...inputs, [item]: input[item] };
|
|
358
|
+
}
|
|
359
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
360
|
+
if (prompt instanceof BaseStringPromptTemplate) {
|
|
361
|
+
const formatted = await prompt.format(inputs);
|
|
362
|
+
content.push({ type: "text", text: formatted });
|
|
363
|
+
/** @TODO replace this */
|
|
364
|
+
// eslint-disable-next-line no-instanceof/no-instanceof
|
|
365
|
+
}
|
|
366
|
+
else if (prompt instanceof ImagePromptTemplate) {
|
|
367
|
+
const formatted = await prompt.format(inputs);
|
|
368
|
+
content.push({ type: "image_url", image_url: formatted });
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
return this.createMessage(content);
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
async formatMessages(values) {
|
|
375
|
+
return [await this.format(values)];
|
|
376
|
+
}
|
|
377
|
+
}
|
|
171
378
|
/**
|
|
172
379
|
* Class that represents a human message prompt template. It extends the
|
|
173
380
|
* BaseMessageStringPromptTemplate.
|
|
@@ -182,31 +389,25 @@ export class ChatMessagePromptTemplate extends BaseMessageStringPromptTemplate {
|
|
|
182
389
|
* });
|
|
183
390
|
* ```
|
|
184
391
|
*/
|
|
185
|
-
export class HumanMessagePromptTemplate extends
|
|
392
|
+
export class HumanMessagePromptTemplate extends _StringImageMessagePromptTemplate {
|
|
393
|
+
static _messageClass() {
|
|
394
|
+
return HumanMessage;
|
|
395
|
+
}
|
|
186
396
|
static lc_name() {
|
|
187
397
|
return "HumanMessagePromptTemplate";
|
|
188
398
|
}
|
|
189
|
-
async format(values) {
|
|
190
|
-
return new HumanMessage(await this.prompt.format(values));
|
|
191
|
-
}
|
|
192
|
-
static fromTemplate(template) {
|
|
193
|
-
return new this(PromptTemplate.fromTemplate(template));
|
|
194
|
-
}
|
|
195
399
|
}
|
|
196
400
|
/**
|
|
197
401
|
* Class that represents an AI message prompt template. It extends the
|
|
198
402
|
* BaseMessageStringPromptTemplate.
|
|
199
403
|
*/
|
|
200
|
-
export class AIMessagePromptTemplate extends
|
|
404
|
+
export class AIMessagePromptTemplate extends _StringImageMessagePromptTemplate {
|
|
405
|
+
static _messageClass() {
|
|
406
|
+
return AIMessage;
|
|
407
|
+
}
|
|
201
408
|
static lc_name() {
|
|
202
409
|
return "AIMessagePromptTemplate";
|
|
203
410
|
}
|
|
204
|
-
async format(values) {
|
|
205
|
-
return new AIMessage(await this.prompt.format(values));
|
|
206
|
-
}
|
|
207
|
-
static fromTemplate(template) {
|
|
208
|
-
return new this(PromptTemplate.fromTemplate(template));
|
|
209
|
-
}
|
|
210
411
|
}
|
|
211
412
|
/**
|
|
212
413
|
* Class that represents a system message prompt template. It extends the
|
|
@@ -222,16 +423,13 @@ export class AIMessagePromptTemplate extends BaseMessageStringPromptTemplate {
|
|
|
222
423
|
* });
|
|
223
424
|
* ```
|
|
224
425
|
*/
|
|
225
|
-
export class SystemMessagePromptTemplate extends
|
|
426
|
+
export class SystemMessagePromptTemplate extends _StringImageMessagePromptTemplate {
|
|
427
|
+
static _messageClass() {
|
|
428
|
+
return SystemMessage;
|
|
429
|
+
}
|
|
226
430
|
static lc_name() {
|
|
227
431
|
return "SystemMessagePromptTemplate";
|
|
228
432
|
}
|
|
229
|
-
async format(values) {
|
|
230
|
-
return new SystemMessage(await this.prompt.format(values));
|
|
231
|
-
}
|
|
232
|
-
static fromTemplate(template) {
|
|
233
|
-
return new this(PromptTemplate.fromTemplate(template));
|
|
234
|
-
}
|
|
235
433
|
}
|
|
236
434
|
function _isBaseMessagePromptTemplate(baseMessagePromptTemplateLike) {
|
|
237
435
|
return (typeof baseMessagePromptTemplateLike
|
|
@@ -339,16 +537,26 @@ export class ChatPromptTemplate extends BaseChatPromptTemplate {
|
|
|
339
537
|
return message;
|
|
340
538
|
}
|
|
341
539
|
const formattedMessageContent = await Promise.all(message.content.map(async (item) => {
|
|
342
|
-
if (item.type !== "image_url"
|
|
343
|
-
typeof item.image_url === "string" ||
|
|
344
|
-
!item.image_url?.url) {
|
|
540
|
+
if (item.type !== "image_url") {
|
|
345
541
|
return item;
|
|
346
542
|
}
|
|
347
|
-
|
|
543
|
+
let imageUrl = "";
|
|
544
|
+
if (typeof item.image_url === "string") {
|
|
545
|
+
imageUrl = item.image_url;
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
imageUrl = item.image_url.url;
|
|
549
|
+
}
|
|
348
550
|
const promptTemplatePlaceholder = PromptTemplate.fromTemplate(imageUrl);
|
|
349
551
|
const formattedUrl = await promptTemplatePlaceholder.format(inputValues);
|
|
350
|
-
|
|
351
|
-
|
|
552
|
+
if (typeof item.image_url !== "string" && "url" in item.image_url) {
|
|
553
|
+
// eslint-disable-next-line no-param-reassign
|
|
554
|
+
item.image_url.url = formattedUrl;
|
|
555
|
+
}
|
|
556
|
+
else {
|
|
557
|
+
// eslint-disable-next-line no-param-reassign
|
|
558
|
+
item.image_url = formattedUrl;
|
|
559
|
+
}
|
|
352
560
|
return item;
|
|
353
561
|
}));
|
|
354
562
|
// eslint-disable-next-line no-param-reassign
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ImagePromptTemplate = void 0;
|
|
4
|
+
const prompt_values_js_1 = require("../prompt_values.cjs");
|
|
5
|
+
const base_js_1 = require("./base.cjs");
|
|
6
|
+
const template_js_1 = require("./template.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* An image prompt template for a multimodal model.
|
|
9
|
+
*/
|
|
10
|
+
class ImagePromptTemplate extends base_js_1.BasePromptTemplate {
|
|
11
|
+
static lc_name() {
|
|
12
|
+
return "ImagePromptTemplate";
|
|
13
|
+
}
|
|
14
|
+
constructor(input) {
|
|
15
|
+
super(input);
|
|
16
|
+
Object.defineProperty(this, "template", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
configurable: true,
|
|
19
|
+
writable: true,
|
|
20
|
+
value: void 0
|
|
21
|
+
});
|
|
22
|
+
Object.defineProperty(this, "templateFormat", {
|
|
23
|
+
enumerable: true,
|
|
24
|
+
configurable: true,
|
|
25
|
+
writable: true,
|
|
26
|
+
value: "f-string"
|
|
27
|
+
});
|
|
28
|
+
Object.defineProperty(this, "validateTemplate", {
|
|
29
|
+
enumerable: true,
|
|
30
|
+
configurable: true,
|
|
31
|
+
writable: true,
|
|
32
|
+
value: true
|
|
33
|
+
});
|
|
34
|
+
this.template = input.template;
|
|
35
|
+
this.templateFormat = input.templateFormat ?? this.templateFormat;
|
|
36
|
+
this.validateTemplate = input.validateTemplate ?? this.validateTemplate;
|
|
37
|
+
if (this.validateTemplate) {
|
|
38
|
+
let totalInputVariables = this.inputVariables;
|
|
39
|
+
if (this.partialVariables) {
|
|
40
|
+
totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
|
|
41
|
+
}
|
|
42
|
+
(0, template_js_1.checkValidTemplate)([
|
|
43
|
+
{ type: "image_url", image_url: this.template },
|
|
44
|
+
], this.templateFormat, totalInputVariables);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
_getPromptType() {
|
|
48
|
+
return "prompt";
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Partially applies values to the prompt template.
|
|
52
|
+
* @param values The values to be partially applied to the prompt template.
|
|
53
|
+
* @returns A new instance of ImagePromptTemplate with the partially applied values.
|
|
54
|
+
*/
|
|
55
|
+
async partial(values) {
|
|
56
|
+
const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
|
|
57
|
+
const newPartialVariables = {
|
|
58
|
+
...(this.partialVariables ?? {}),
|
|
59
|
+
...values,
|
|
60
|
+
};
|
|
61
|
+
const promptDict = {
|
|
62
|
+
...this,
|
|
63
|
+
inputVariables: newInputVariables,
|
|
64
|
+
partialVariables: newPartialVariables,
|
|
65
|
+
};
|
|
66
|
+
return new ImagePromptTemplate(promptDict);
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Formats the prompt template with the provided values.
|
|
70
|
+
* @param values The values to be used to format the prompt template.
|
|
71
|
+
* @returns A promise that resolves to a string which is the formatted prompt.
|
|
72
|
+
*/
|
|
73
|
+
async format(values) {
|
|
74
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
75
|
+
const formatted = {};
|
|
76
|
+
for (const [key, value] of Object.entries(this.template)) {
|
|
77
|
+
if (typeof value === "string") {
|
|
78
|
+
formatted[key] = value.replace(/{([^{}]*)}/g, (match, group) => {
|
|
79
|
+
const replacement = values[group];
|
|
80
|
+
return typeof replacement === "string" ||
|
|
81
|
+
typeof replacement === "number"
|
|
82
|
+
? String(replacement)
|
|
83
|
+
: match;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
formatted[key] = value;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
const url = values.url || formatted.url;
|
|
91
|
+
const detail = values.detail || formatted.detail;
|
|
92
|
+
if (!url) {
|
|
93
|
+
throw new Error("Must provide either an image URL.");
|
|
94
|
+
}
|
|
95
|
+
if (typeof url !== "string") {
|
|
96
|
+
throw new Error("url must be a string.");
|
|
97
|
+
}
|
|
98
|
+
const output = { url };
|
|
99
|
+
if (detail) {
|
|
100
|
+
output.detail = detail;
|
|
101
|
+
}
|
|
102
|
+
return output;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Formats the prompt given the input values and returns a formatted
|
|
106
|
+
* prompt value.
|
|
107
|
+
* @param values The input values to format the prompt.
|
|
108
|
+
* @returns A Promise that resolves to a formatted prompt value.
|
|
109
|
+
*/
|
|
110
|
+
async formatPromptValue(values) {
|
|
111
|
+
const formattedPrompt = await this.format(values);
|
|
112
|
+
return new prompt_values_js_1.ImagePromptValue(formattedPrompt);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
exports.ImagePromptTemplate = ImagePromptTemplate;
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { ImagePromptValue, ImageContent } from "../prompt_values.js";
|
|
2
|
+
import type { InputValues, PartialValues } from "../utils/types.js";
|
|
3
|
+
import { BasePromptTemplate, BasePromptTemplateInput, TypedPromptInputValues } from "./base.js";
|
|
4
|
+
import { TemplateFormat } from "./template.js";
|
|
5
|
+
/**
|
|
6
|
+
* Inputs to create a {@link ImagePromptTemplate}
|
|
7
|
+
* @augments BasePromptTemplateInput
|
|
8
|
+
*/
|
|
9
|
+
export interface ImagePromptTemplateInput<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BasePromptTemplateInput<RunInput, PartialVariableName> {
|
|
10
|
+
/**
|
|
11
|
+
* The prompt template
|
|
12
|
+
*/
|
|
13
|
+
template: Record<string, unknown>;
|
|
14
|
+
/**
|
|
15
|
+
* The format of the prompt template. Options are 'f-string'
|
|
16
|
+
*
|
|
17
|
+
* @defaultValue 'f-string'
|
|
18
|
+
*/
|
|
19
|
+
templateFormat?: TemplateFormat;
|
|
20
|
+
/**
|
|
21
|
+
* Whether or not to try validating the template on initialization
|
|
22
|
+
*
|
|
23
|
+
* @defaultValue `true`
|
|
24
|
+
*/
|
|
25
|
+
validateTemplate?: boolean;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* An image prompt template for a multimodal model.
|
|
29
|
+
*/
|
|
30
|
+
export declare class ImagePromptTemplate<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BasePromptTemplate<RunInput, ImagePromptValue, PartialVariableName> {
|
|
31
|
+
static lc_name(): string;
|
|
32
|
+
template: Record<string, unknown>;
|
|
33
|
+
templateFormat: TemplateFormat;
|
|
34
|
+
validateTemplate: boolean;
|
|
35
|
+
constructor(input: ImagePromptTemplateInput<RunInput, PartialVariableName>);
|
|
36
|
+
_getPromptType(): "prompt";
|
|
37
|
+
/**
|
|
38
|
+
* Partially applies values to the prompt template.
|
|
39
|
+
* @param values The values to be partially applied to the prompt template.
|
|
40
|
+
* @returns A new instance of ImagePromptTemplate with the partially applied values.
|
|
41
|
+
*/
|
|
42
|
+
partial<NewPartialVariableName extends string>(values: PartialValues<NewPartialVariableName>): Promise<ImagePromptTemplate<InputValues<Exclude<Extract<keyof RunInput, string>, NewPartialVariableName>>, any>>;
|
|
43
|
+
/**
|
|
44
|
+
* Formats the prompt template with the provided values.
|
|
45
|
+
* @param values The values to be used to format the prompt template.
|
|
46
|
+
* @returns A promise that resolves to a string which is the formatted prompt.
|
|
47
|
+
*/
|
|
48
|
+
format<FormatOutput = ImageContent>(values: TypedPromptInputValues<RunInput>): Promise<FormatOutput>;
|
|
49
|
+
/**
|
|
50
|
+
* Formats the prompt given the input values and returns a formatted
|
|
51
|
+
* prompt value.
|
|
52
|
+
* @param values The input values to format the prompt.
|
|
53
|
+
* @returns A Promise that resolves to a formatted prompt value.
|
|
54
|
+
*/
|
|
55
|
+
formatPromptValue(values: TypedPromptInputValues<RunInput>): Promise<ImagePromptValue>;
|
|
56
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { ImagePromptValue } from "../prompt_values.js";
|
|
2
|
+
import { BasePromptTemplate, } from "./base.js";
|
|
3
|
+
import { checkValidTemplate } from "./template.js";
|
|
4
|
+
/**
|
|
5
|
+
* An image prompt template for a multimodal model.
|
|
6
|
+
*/
|
|
7
|
+
export class ImagePromptTemplate extends BasePromptTemplate {
|
|
8
|
+
static lc_name() {
|
|
9
|
+
return "ImagePromptTemplate";
|
|
10
|
+
}
|
|
11
|
+
constructor(input) {
|
|
12
|
+
super(input);
|
|
13
|
+
Object.defineProperty(this, "template", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: void 0
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(this, "templateFormat", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: "f-string"
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "validateTemplate", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: true
|
|
30
|
+
});
|
|
31
|
+
this.template = input.template;
|
|
32
|
+
this.templateFormat = input.templateFormat ?? this.templateFormat;
|
|
33
|
+
this.validateTemplate = input.validateTemplate ?? this.validateTemplate;
|
|
34
|
+
if (this.validateTemplate) {
|
|
35
|
+
let totalInputVariables = this.inputVariables;
|
|
36
|
+
if (this.partialVariables) {
|
|
37
|
+
totalInputVariables = totalInputVariables.concat(Object.keys(this.partialVariables));
|
|
38
|
+
}
|
|
39
|
+
checkValidTemplate([
|
|
40
|
+
{ type: "image_url", image_url: this.template },
|
|
41
|
+
], this.templateFormat, totalInputVariables);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
_getPromptType() {
|
|
45
|
+
return "prompt";
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Partially applies values to the prompt template.
|
|
49
|
+
* @param values The values to be partially applied to the prompt template.
|
|
50
|
+
* @returns A new instance of ImagePromptTemplate with the partially applied values.
|
|
51
|
+
*/
|
|
52
|
+
async partial(values) {
|
|
53
|
+
const newInputVariables = this.inputVariables.filter((iv) => !(iv in values));
|
|
54
|
+
const newPartialVariables = {
|
|
55
|
+
...(this.partialVariables ?? {}),
|
|
56
|
+
...values,
|
|
57
|
+
};
|
|
58
|
+
const promptDict = {
|
|
59
|
+
...this,
|
|
60
|
+
inputVariables: newInputVariables,
|
|
61
|
+
partialVariables: newPartialVariables,
|
|
62
|
+
};
|
|
63
|
+
return new ImagePromptTemplate(promptDict);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Formats the prompt template with the provided values.
|
|
67
|
+
* @param values The values to be used to format the prompt template.
|
|
68
|
+
* @returns A promise that resolves to a string which is the formatted prompt.
|
|
69
|
+
*/
|
|
70
|
+
async format(values) {
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
const formatted = {};
|
|
73
|
+
for (const [key, value] of Object.entries(this.template)) {
|
|
74
|
+
if (typeof value === "string") {
|
|
75
|
+
formatted[key] = value.replace(/{([^{}]*)}/g, (match, group) => {
|
|
76
|
+
const replacement = values[group];
|
|
77
|
+
return typeof replacement === "string" ||
|
|
78
|
+
typeof replacement === "number"
|
|
79
|
+
? String(replacement)
|
|
80
|
+
: match;
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
formatted[key] = value;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
const url = values.url || formatted.url;
|
|
88
|
+
const detail = values.detail || formatted.detail;
|
|
89
|
+
if (!url) {
|
|
90
|
+
throw new Error("Must provide either an image URL.");
|
|
91
|
+
}
|
|
92
|
+
if (typeof url !== "string") {
|
|
93
|
+
throw new Error("url must be a string.");
|
|
94
|
+
}
|
|
95
|
+
const output = { url };
|
|
96
|
+
if (detail) {
|
|
97
|
+
output.detail = detail;
|
|
98
|
+
}
|
|
99
|
+
return output;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Formats the prompt given the input values and returns a formatted
|
|
103
|
+
* prompt value.
|
|
104
|
+
* @param values The input values to format the prompt.
|
|
105
|
+
* @returns A Promise that resolves to a formatted prompt value.
|
|
106
|
+
*/
|
|
107
|
+
async formatPromptValue(values) {
|
|
108
|
+
const formattedPrompt = await this.format(values);
|
|
109
|
+
return new ImagePromptValue(formattedPrompt);
|
|
110
|
+
}
|
|
111
|
+
}
|
package/dist/prompts/prompt.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import type { BasePromptTemplateInput, TypedPromptInputValues } from "./base.js"
|
|
|
3
3
|
import { type TemplateFormat } from "./template.js";
|
|
4
4
|
import type { SerializedPromptTemplate } from "./serde.js";
|
|
5
5
|
import type { InputValues, PartialValues } from "../utils/types.js";
|
|
6
|
+
import { MessageContent } from "../messages/index.js";
|
|
6
7
|
/**
|
|
7
8
|
* Inputs to create a {@link PromptTemplate}
|
|
8
9
|
* @augments BasePromptTemplateInput
|
|
@@ -11,7 +12,7 @@ export interface PromptTemplateInput<RunInput extends InputValues = any, Partial
|
|
|
11
12
|
/**
|
|
12
13
|
* The prompt template
|
|
13
14
|
*/
|
|
14
|
-
template:
|
|
15
|
+
template: MessageContent;
|
|
15
16
|
/**
|
|
16
17
|
* The format of the prompt template. Options are 'f-string'
|
|
17
18
|
*
|
|
@@ -52,7 +53,7 @@ export type ParamsFromFString<T extends string> = {
|
|
|
52
53
|
*/
|
|
53
54
|
export declare class PromptTemplate<RunInput extends InputValues = any, PartialVariableName extends string = any> extends BaseStringPromptTemplate<RunInput, PartialVariableName> implements PromptTemplateInput<RunInput, PartialVariableName> {
|
|
54
55
|
static lc_name(): string;
|
|
55
|
-
template:
|
|
56
|
+
template: MessageContent;
|
|
56
57
|
templateFormat: TemplateFormat;
|
|
57
58
|
validateTemplate: boolean;
|
|
58
59
|
constructor(input: PromptTemplateInput<RunInput, PartialVariableName>);
|
package/dist/prompts/serde.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MessageContent } from "../messages/index.js";
|
|
1
2
|
import type { TemplateFormat } from "./template.js";
|
|
2
3
|
/**
|
|
3
4
|
* Represents a serialized version of a prompt template. This type is used
|
|
@@ -12,7 +13,7 @@ export type SerializedPromptTemplate = {
|
|
|
12
13
|
_type?: "prompt";
|
|
13
14
|
input_variables: string[];
|
|
14
15
|
template_format?: TemplateFormat;
|
|
15
|
-
template?:
|
|
16
|
+
template?: MessageContent;
|
|
16
17
|
};
|
|
17
18
|
/**
|
|
18
19
|
* Represents a serialized version of a few-shot template. This type
|
|
@@ -81,7 +81,28 @@ const checkValidTemplate = (template, templateFormat, inputVariables) => {
|
|
|
81
81
|
acc[v] = "foo";
|
|
82
82
|
return acc;
|
|
83
83
|
}, {});
|
|
84
|
-
(
|
|
84
|
+
if (Array.isArray(template)) {
|
|
85
|
+
template.forEach((message) => {
|
|
86
|
+
if (message.type === "text") {
|
|
87
|
+
(0, exports.renderTemplate)(message.text, templateFormat, dummyInputs);
|
|
88
|
+
}
|
|
89
|
+
else if (message.type === "image_url") {
|
|
90
|
+
if (typeof message.image_url === "string") {
|
|
91
|
+
(0, exports.renderTemplate)(message.image_url, templateFormat, dummyInputs);
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
const imageUrl = message.image_url.url;
|
|
95
|
+
(0, exports.renderTemplate)(imageUrl, templateFormat, dummyInputs);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
else {
|
|
99
|
+
throw new Error(`Invalid message template received. ${JSON.stringify(message, null, 2)}`);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
(0, exports.renderTemplate)(template, templateFormat, dummyInputs);
|
|
105
|
+
}
|
|
85
106
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
86
107
|
}
|
|
87
108
|
catch (e) {
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MessageContent } from "../messages/index.js";
|
|
1
2
|
import type { InputValues } from "../utils/types.js";
|
|
2
3
|
/**
|
|
3
4
|
* Type that specifies the format of a template. Only
|
|
@@ -32,5 +33,5 @@ export declare const DEFAULT_FORMATTER_MAPPING: Record<TemplateFormat, Interpola
|
|
|
32
33
|
export declare const DEFAULT_PARSER_MAPPING: Record<TemplateFormat, Parser>;
|
|
33
34
|
export declare const renderTemplate: (template: string, templateFormat: TemplateFormat, inputValues: InputValues) => string;
|
|
34
35
|
export declare const parseTemplate: (template: string, templateFormat: TemplateFormat) => ParsedFStringNode[];
|
|
35
|
-
export declare const checkValidTemplate: (template:
|
|
36
|
+
export declare const checkValidTemplate: (template: MessageContent, templateFormat: TemplateFormat, inputVariables: string[]) => void;
|
|
36
37
|
export {};
|