-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
prompt_template.ts
43 lines (42 loc) · 1.4 KB
/
prompt_template.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
import { Document } from "@langchain/core/documents";
import { BasePromptTemplate } from "@langchain/core/prompts";
/**
* Formats a document using a given prompt template.
*
* @async
* @param {Document} document - The document to format.
* @param {BasePromptTemplate} prompt - The prompt template to use for formatting.
* @returns {Promise<string>} A Promise that resolves to the formatted document as a string.
* @throws {Error} If the document is missing required metadata variables specified in the prompt template.
*/
export const formatDocument = async (
document: Document,
prompt: BasePromptTemplate
): Promise<string> => {
const baseInfo = {
pageContent: document.pageContent,
...document.metadata,
};
const variables = new Set(prompt.inputVariables);
const requiredMetadata = new Set(
prompt.inputVariables
.map((v) => (v !== "pageContent" ? v : null))
.filter((v) => v !== null)
);
const missingMetadata = [];
for (const variable of variables) {
if (!(variable in baseInfo) && variable !== "pageContent") {
missingMetadata.push(variable);
}
}
if (missingMetadata.length) {
throw new Error(
`Document prompt requires documents to have metadata variables: ${JSON.stringify(
requiredMetadata
)}. Received document with missing metadata: ${JSON.stringify(
missingMetadata
)}`
);
}
return prompt.format(baseInfo);
};