-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
openai_functions.ts
74 lines (67 loc) · 2.04 KB
/
openai_functions.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
import { z } from "zod";
import {
zodToJsonSchema,
type JsonSchema7ObjectType,
} from "zod-to-json-schema";
import {
Document,
MappingDocumentTransformer,
} from "@langchain/core/documents";
import { ChatOpenAI } from "@langchain/openai";
import { BaseChain } from "../chains/base.js";
import {
TaggingChainOptions,
createTaggingChain,
} from "../chains/openai_functions/index.js";
/**
* A transformer that tags metadata to a document using a tagging chain.
*/
export class MetadataTagger extends MappingDocumentTransformer {
static lc_name() {
return "MetadataTagger";
}
protected taggingChain: BaseChain;
constructor(fields: { taggingChain: BaseChain }) {
super();
this.taggingChain = fields.taggingChain;
if (this.taggingChain.inputKeys.length !== 1) {
throw new Error(
"Invalid input chain. The input chain must have exactly one input."
);
}
if (this.taggingChain.outputKeys.length !== 1) {
throw new Error(
"Invalid input chain. The input chain must have exactly one output."
);
}
}
async _transformDocument(document: Document): Promise<Document> {
const taggingChainResponse = await this.taggingChain.call({
[this.taggingChain.inputKeys[0]]: document.pageContent,
});
const extractedMetadata =
taggingChainResponse[this.taggingChain.outputKeys[0]];
return new Document({
pageContent: document.pageContent,
metadata: { ...extractedMetadata, ...document.metadata },
});
}
}
export function createMetadataTagger(
schema: JsonSchema7ObjectType,
options: TaggingChainOptions & { llm?: ChatOpenAI }
) {
const { llm = new ChatOpenAI({ modelName: "gpt-3.5-turbo-0613" }), ...rest } =
options;
const taggingChain = createTaggingChain(schema, llm, rest);
return new MetadataTagger({ taggingChain });
}
export function createMetadataTaggerFromZod(
schema: z.AnyZodObject,
options: TaggingChainOptions & { llm?: ChatOpenAI }
) {
return createMetadataTagger(
zodToJsonSchema(schema) as JsonSchema7ObjectType,
options
);
}