@langchain/core
Advanced tools
Comparing version
@@ -11,2 +11,3 @@ import type { BaseCallbackConfig } from "../callbacks/manager.js"; | ||
import { TemplateFormat } from "./template.js"; | ||
import { DictPromptTemplate } from "./dict.js"; | ||
/** | ||
@@ -120,3 +121,3 @@ * Abstract class that serves as a base for creating message prompt | ||
additionalOptions: _StringImageMessagePromptTemplateOptions; | ||
prompt: BaseStringPromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | Array<BaseStringPromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | ImagePromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | MessageStringPromptTemplateFields<InputValues<Extract<keyof RunInput, string>>>>; | ||
prompt: BaseStringPromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | Array<BaseStringPromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | ImagePromptTemplate<InputValues<Extract<keyof RunInput, string>>, string> | MessageStringPromptTemplateFields<InputValues<Extract<keyof RunInput, string>>> | DictPromptTemplate<InputValues<Extract<keyof RunInput, string>>>>; | ||
protected messageClass?: MessageClass; | ||
@@ -130,3 +131,3 @@ static _messageClass(): MessageClass; | ||
getRoleFromMessageClass(name: string): "human" | "ai" | "system" | "chat"; | ||
static fromTemplate(template: string | Array<string | _TextTemplateParam | _ImageTemplateParam>, additionalOptions?: _StringImageMessagePromptTemplateOptions): _StringImageMessagePromptTemplate<any, BaseMessage[]>; | ||
static fromTemplate(template: string | Array<string | _TextTemplateParam | _ImageTemplateParam | Record<string, unknown>>, additionalOptions?: _StringImageMessagePromptTemplateOptions): _StringImageMessagePromptTemplate<any, BaseMessage[]>; | ||
format(input: TypedPromptInputValues<RunInput>): Promise<BaseMessage>; | ||
@@ -133,0 +134,0 @@ formatMessages(values: RunInput): Promise<RunOutput>; |
@@ -12,2 +12,3 @@ // Default generic "any" values are for backwards compatibility. | ||
import { addLangChainErrorFields } from "../errors/index.js"; | ||
import { DictPromptTemplate } from "./dict.js"; | ||
/** | ||
@@ -184,2 +185,21 @@ * Abstract class that serves as a base for creating message prompt | ||
} | ||
function isTextTemplateParam(param) { | ||
if (param === null || typeof param !== "object" || Array.isArray(param)) { | ||
return false; | ||
} | ||
return (Object.keys(param).length === 1 && | ||
"text" in param && | ||
typeof param.text === "string"); | ||
} | ||
function isImageTemplateParam(param) { | ||
if (param === null || typeof param !== "object" || Array.isArray(param)) { | ||
return false; | ||
} | ||
return ("image_url" in param && | ||
(typeof param.image_url === "string" || | ||
(typeof param.image_url === "object" && | ||
param.image_url !== null && | ||
"url" in param.image_url && | ||
typeof param.image_url.url === "string"))); | ||
} | ||
class _StringImageMessagePromptTemplate extends BaseMessagePromptTemplate { | ||
@@ -296,9 +316,12 @@ static _messageClass() { | ||
for (const item of template) { | ||
if (typeof item === "string" || | ||
(typeof item === "object" && "text" in item)) { | ||
// handle string cases | ||
if (typeof item === "string") { | ||
prompt.push(PromptTemplate.fromTemplate(item, additionalOptions)); | ||
} | ||
else if (item === null) { | ||
// pass | ||
} | ||
else if (isTextTemplateParam(item)) { | ||
let text = ""; | ||
if (typeof item === "string") { | ||
text = item; | ||
} | ||
else if (typeof item.text === "string") { | ||
if (typeof item.text === "string") { | ||
text = item.text ?? ""; | ||
@@ -308,9 +331,7 @@ } | ||
...additionalOptions, | ||
...(typeof item !== "string" | ||
? { additionalContentFields: item } | ||
: {}), | ||
additionalContentFields: item, | ||
}; | ||
prompt.push(PromptTemplate.fromTemplate(text, options)); | ||
} | ||
else if (typeof item === "object" && "image_url" in item) { | ||
else if (isImageTemplateParam(item)) { | ||
let imgTemplate = item.image_url ?? ""; | ||
@@ -371,2 +392,8 @@ let imgTemplateObject; | ||
} | ||
else if (typeof item === "object") { | ||
prompt.push(new DictPromptTemplate({ | ||
template: item, | ||
templateFormat: additionalOptions?.templateFormat, | ||
})); | ||
} | ||
} | ||
@@ -423,3 +450,16 @@ return new this({ prompt, additionalOptions }); | ||
}); | ||
// eslint-disable-next-line no-instanceof/no-instanceof | ||
} | ||
else if (prompt instanceof DictPromptTemplate) { | ||
const formatted = await prompt.format(inputs); | ||
let additionalContentFields; | ||
if ("additionalContentFields" in prompt) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
additionalContentFields = prompt.additionalContentFields; | ||
} | ||
content.push({ | ||
...additionalContentFields, | ||
...formatted, | ||
}); | ||
} | ||
} | ||
@@ -426,0 +466,0 @@ return this.createMessage(content); |
@@ -11,1 +11,2 @@ export * from "./base.js"; | ||
export * from "./structured.js"; | ||
export * from "./dict.js"; |
@@ -11,1 +11,2 @@ export * from "./base.js"; | ||
export * from "./structured.js"; | ||
export * from "./dict.js"; |
{ | ||
"name": "@langchain/core", | ||
"version": "0.3.45", | ||
"version": "0.3.46", | ||
"description": "Core LangChain.js abstractions and schemas", | ||
@@ -5,0 +5,0 @@ "type": "module", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2263724
0.52%669
0.45%56667
0.54%