-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
llm.ts
274 lines (241 loc) Β· 9.29 KB
/
llm.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import { z } from "zod";
import { zodToJsonSchema } from "zod-to-json-schema";
import { BaseLanguageModel } from "@langchain/core/language_models/base";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { Document } from "@langchain/core/documents";
import {
Node,
Relationship,
GraphDocument,
} from "../../graphs/graph_document.js";
export const SYSTEM_PROMPT = `
# Knowledge Graph Instructions for GPT-4\n
## 1. Overview\n
You are a top-tier algorithm designed for extracting information in structured formats to build a knowledge graph.\n
Try to capture as much information from the text as possible without sacrifing accuracy. Do not add any information that is not explicitly mentioned in the text\n"
- **Nodes** represent entities and concepts.\n"
- The aim is to achieve simplicity and clarity in the knowledge graph, making it\n
accessible for a vast audience.\n
## 2. Labeling Nodes\n
- **Consistency**: Ensure you use available types for node labels.\n
Ensure you use basic or elementary types for node labels.\n
- For example, when you identify an entity representing a person, always label it as **'person'**. Avoid using more specific terms like 'mathematician' or 'scientist'
- **Node IDs**: Never utilize integers as node IDs. Node IDs should be names or human-readable identifiers found in the text.\n
- **Relationships** represent connections between entities or concepts.\n
Ensure consistency and generality in relationship types when constructing knowledge graphs. Instead of using specific and momentary types such as 'BECAME_PROFESSOR', use more general and timeless relationship types like 'PROFESSOR'. Make sure to use general and timeless relationship types!\n
## 3. Coreference Resolution\n
- **Maintain Entity Consistency**: When extracting entities, it's vital to ensure consistency.\n
If an entity, such as "John Doe", is mentioned multiple times in the text but is referred to by different names or pronouns (e.g., "Joe", "he"), always use the most complete identifier for that entity throughout the knowledge graph. In this example, use "John Doe" as the entity ID.\n
Remember, the knowledge graph should be coherent and easily understandable, so maintaining consistency in entity references is crucial.\n
## 4. Strict Compliance\n
Adhere to the rules strictly. Non-compliance will result in termination.
`;
const DEFAULT_PROMPT = /* #__PURE__ */ ChatPromptTemplate.fromMessages([
["system", SYSTEM_PROMPT],
[
"human",
"Tip: Make sure to answer in the correct format and do not include any explanations. Use the given format to extract information from the following input: {input}",
],
]);
interface OptionalEnumFieldProps {
enumValues?: string[];
description: string;
isRel?: boolean;
fieldKwargs?: object;
}
function toTitleCase(str: string): string {
return str
.split(" ")
.map((w) => w[0].toUpperCase() + w.substring(1).toLowerCase())
.join("");
}
function createOptionalEnumType({
enumValues = undefined,
description = "",
isRel = false,
}: OptionalEnumFieldProps): z.ZodTypeAny {
let schema;
if (enumValues && enumValues.length) {
schema = z
.enum(enumValues as [string, ...string[]])
.describe(
`${description} Available options are: ${enumValues.join(", ")}.`
);
} else {
const nodeInfo =
"Ensure you use basic or elementary types for node labels.\n" +
"For example, when you identify an entity representing a person, " +
"always label it as **'Person'**. Avoid using more specific terms " +
"like 'Mathematician' or 'Scientist'";
const relInfo =
"Instead of using specific and momentary types such as " +
"'BECAME_PROFESSOR', use more general and timeless relationship types like " +
"'PROFESSOR'. However, do not sacrifice any accuracy for generality";
const additionalInfo = isRel ? relInfo : nodeInfo;
schema = z.string().describe(description + additionalInfo);
}
return schema;
}
function createSchema(allowedNodes: string[], allowedRelationships: string[]) {
const dynamicGraphSchema = z.object({
nodes: z
.array(
z.object({
id: z.string(),
type: createOptionalEnumType({
enumValues: allowedNodes,
description: "The type or label of the node.",
}),
})
)
.describe("List of nodes"),
relationships: z
.array(
z.object({
sourceNodeId: z.string(),
sourceNodeType: createOptionalEnumType({
enumValues: allowedNodes,
description: "The source node of the relationship.",
}),
relationshipType: createOptionalEnumType({
enumValues: allowedRelationships,
description: "The type of the relationship.",
isRel: true,
}),
targetNodeId: z.string(),
targetNodeType: createOptionalEnumType({
enumValues: allowedNodes,
description: "The target node of the relationship.",
}),
})
)
.describe("List of relationships."),
});
return dynamicGraphSchema;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function mapToBaseNode(node: any): Node {
return new Node({
id: node.id,
type: node.type ? toTitleCase(node.type) : "",
});
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function mapToBaseRelationship(relationship: any): Relationship {
return new Relationship({
source: new Node({
id: relationship.sourceNodeId,
type: relationship.sourceNodeType
? toTitleCase(relationship.sourceNodeType)
: "",
}),
target: new Node({
id: relationship.targetNodeId,
type: relationship.targetNodeType
? toTitleCase(relationship.targetNodeType)
: "",
}),
type: relationship.relationshipType.replace(" ", "_").toUpperCase(),
});
}
export interface LLMGraphTransformerProps {
llm: BaseLanguageModel;
allowedNodes?: string[];
allowedRelationships?: string[];
prompt?: ChatPromptTemplate;
strictMode?: boolean;
}
export class LLMGraphTransformer {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
chain: any;
allowedNodes: string[] = [];
allowedRelationships: string[] = [];
strictMode: boolean;
constructor({
llm,
allowedNodes = [],
allowedRelationships = [],
prompt = DEFAULT_PROMPT,
strictMode = true,
}: LLMGraphTransformerProps) {
if (typeof llm.withStructuredOutput !== "function") {
throw new Error(
"The specified LLM does not support the 'withStructuredOutput'. Please ensure you are using an LLM that supports this feature."
);
}
this.allowedNodes = allowedNodes;
this.allowedRelationships = allowedRelationships;
this.strictMode = strictMode;
// Define chain
const schema = createSchema(allowedNodes, allowedRelationships);
const structuredLLM = llm.withStructuredOutput(zodToJsonSchema(schema));
this.chain = prompt.pipe(structuredLLM);
}
/**
* Method that processes a single document, transforming it into a graph
* document using an LLM based on the model's schema and constraints.
* @param document The document to process.
* @returns A promise that resolves to a graph document.
*/
async processResponse(document: Document) {
const text = document.pageContent;
const rawSchema = await this.chain.invoke({ input: text });
let nodes: Node[] = [];
if (rawSchema?.nodes) {
nodes = rawSchema.nodes.map(mapToBaseNode);
}
let relationships: Relationship[] = [];
if (rawSchema?.relationships) {
relationships = rawSchema.relationships.map(mapToBaseRelationship);
}
if (
this.strictMode &&
(this.allowedNodes.length > 0 || this.allowedRelationships.length > 0)
) {
if (this.allowedNodes.length > 0) {
const allowedNodesLowerCase = this.allowedNodes.map((node) =>
node.toLowerCase()
);
// For nodes, compare lowercased types
nodes = nodes.filter((node) =>
allowedNodesLowerCase.includes(node.type.toLowerCase())
);
// For relationships, compare lowercased types for both source and target nodes
relationships = relationships.filter(
(rel) =>
allowedNodesLowerCase.includes(rel.source.type.toLowerCase()) &&
allowedNodesLowerCase.includes(rel.target.type.toLowerCase())
);
}
if (this.allowedRelationships.length > 0) {
// For relationships, compare lowercased types
relationships = relationships.filter((rel) =>
this.allowedRelationships
.map((rel) => rel.toLowerCase())
.includes(rel.type.toLowerCase())
);
}
}
return new GraphDocument({
nodes,
relationships,
source: document,
});
}
/**
* Method that converts an array of documents into an array of graph
* documents using the `processResponse` method.
* @param documents The array of documents to convert.
* @returns A promise that resolves to an array of graph documents.
*/
async convertToGraphDocuments(
documents: Document[]
): Promise<GraphDocument[]> {
const results: GraphDocument[] = [];
for (const document of documents) {
const graphDocument = await this.processResponse(document);
results.push(graphDocument);
}
return results;
}
}