@langchain/community
Version:
Third-party integrations for LangChain.js
1 lines • 16.8 kB
Source Map (JSON)
{"version":3,"file":"llm.cjs","names":["z","Node","Relationship","GraphDocument"],"sources":["../../../src/experimental/graph_transformers/llm.ts"],"sourcesContent":["import { z } from \"zod/v3\";\nimport { toJsonSchema } from \"@langchain/core/utils/json_schema\";\nimport { BaseLanguageModel } from \"@langchain/core/language_models/base\";\nimport { ChatPromptTemplate } from \"@langchain/core/prompts\";\nimport { Document } from \"@langchain/core/documents\";\nimport { Node, Relationship, GraphDocument } from \"../../graphs/document.js\";\n\nexport const SYSTEM_PROMPT = `\n# Knowledge Graph Instructions for GPT-4\\n\n## 1. Overview\\n\nYou are a top-tier algorithm designed for extracting information in structured formats to build a knowledge graph.\\n\nTry 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\"\n- **Nodes** represent entities and concepts.\\n\"\n- The aim is to achieve simplicity and clarity in the knowledge graph, making it\\n\naccessible for a vast audience.\\n\n## 2. Labeling Nodes\\n\n- **Consistency**: Ensure you use available types for node labels.\\n\nEnsure you use basic or elementary types for node labels.\\n\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'\n- **Node IDs**: Never utilize integers as node IDs. Node IDs should be names or human-readable identifiers found in the text.\\n\n- **Relationships** represent connections between entities or concepts.\\n\nEnsure 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\n## 3. Coreference Resolution\\n\n- **Maintain Entity Consistency**: When extracting entities, it's vital to ensure consistency.\\n\nIf 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\nRemember, the knowledge graph should be coherent and easily understandable, so maintaining consistency in entity references is crucial.\\n\n## 4. Strict Compliance\\n\nAdhere to the rules strictly. Non-compliance will result in termination.\n`;\n\nconst DEFAULT_PROMPT = /* #__PURE__ */ ChatPromptTemplate.fromMessages([\n [\"system\", SYSTEM_PROMPT],\n [\n \"human\",\n \"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}\",\n ],\n]);\n\ninterface OptionalEnumFieldProps {\n enumValues?: string[];\n description: string;\n isRel?: boolean;\n fieldKwargs?: object;\n}\n\ninterface SchemaProperty {\n key: string;\n value: string;\n}\n\nfunction toTitleCase(str: string): string {\n return str\n .split(\" \")\n .map((w) => w[0].toUpperCase() + w.substring(1).toLowerCase())\n .join(\"\");\n}\n\nfunction createOptionalEnumType({\n enumValues = undefined,\n description = \"\",\n isRel = false,\n}: OptionalEnumFieldProps): z.ZodTypeAny {\n let schema;\n\n if (enumValues && enumValues.length) {\n schema = z\n .enum(enumValues as [string, ...string[]])\n .describe(\n `${description} Available options are: ${enumValues.join(\", \")}.`\n );\n } else {\n const nodeInfo =\n \"Ensure you use basic or elementary types for node labels.\\n\" +\n \"For example, when you identify an entity representing a person, \" +\n \"always label it as **'Person'**. Avoid using more specific terms \" +\n \"like 'Mathematician' or 'Scientist'\";\n const relInfo =\n \"Instead of using specific and momentary types such as \" +\n \"'BECAME_PROFESSOR', use more general and timeless relationship types like \" +\n \"'PROFESSOR'. However, do not sacrifice any accuracy for generality\";\n\n const additionalInfo = isRel ? relInfo : nodeInfo;\n\n schema = z.string().describe(description + additionalInfo);\n }\n\n return schema;\n}\n\nfunction createNodeSchema(allowedNodes: string[], nodeProperties: string[]) {\n const nodeSchema = z.object({\n id: z.string(),\n type: createOptionalEnumType({\n enumValues: allowedNodes,\n description: \"The type or label of the node.\",\n }),\n });\n\n return nodeProperties.length > 0\n ? nodeSchema.extend({\n properties: z\n .array(\n z.object({\n key: createOptionalEnumType({\n enumValues: nodeProperties,\n description: \"Property key.\",\n }),\n value: z.string().describe(\"Extracted value.\"),\n })\n )\n .describe(`List of node properties`),\n })\n : nodeSchema;\n}\n\nfunction createRelationshipSchema(\n allowedNodes: string[],\n allowedRelationships: string[],\n relationshipProperties: string[]\n) {\n const relationshipSchema = z.object({\n sourceNodeId: z.string(),\n sourceNodeType: createOptionalEnumType({\n enumValues: allowedNodes,\n description: \"The source node of the relationship.\",\n }),\n relationshipType: createOptionalEnumType({\n enumValues: allowedRelationships,\n description: \"The type of the relationship.\",\n isRel: true,\n }),\n targetNodeId: z.string(),\n targetNodeType: createOptionalEnumType({\n enumValues: allowedNodes,\n description: \"The target node of the relationship.\",\n }),\n });\n\n return relationshipProperties.length > 0\n ? relationshipSchema.extend({\n properties: z\n .array(\n z.object({\n key: createOptionalEnumType({\n enumValues: relationshipProperties,\n description: \"Property key.\",\n }),\n value: z.string().describe(\"Extracted value.\"),\n })\n )\n .describe(`List of relationship properties`),\n })\n : relationshipSchema;\n}\n\nfunction createSchema(\n allowedNodes: string[],\n allowedRelationships: string[],\n nodeProperties: string[],\n relationshipProperties: string[]\n) {\n const nodeSchema = createNodeSchema(allowedNodes, nodeProperties);\n const relationshipSchema = createRelationshipSchema(\n allowedNodes,\n allowedRelationships,\n relationshipProperties\n );\n\n const dynamicGraphSchema = z.object({\n nodes: z.array(nodeSchema).describe(\"List of nodes\"),\n relationships: z\n .array(relationshipSchema)\n .describe(\"List of relationships.\"),\n });\n\n return dynamicGraphSchema;\n}\n\nfunction convertPropertiesToRecord(\n properties: SchemaProperty[]\n): Record<string, string> {\n return properties.reduce((accumulator: Record<string, string>, prop) => {\n accumulator[prop.key] = prop.value;\n return accumulator;\n }, {});\n}\n\n// oxlint-disable-next-line typescript/no-explicit-any\nfunction mapToBaseNode(node: any): Node {\n return new Node({\n id: node.id,\n type: node.type ? toTitleCase(node.type) : \"\",\n properties: node.properties\n ? convertPropertiesToRecord(node.properties)\n : {},\n });\n}\n\nfunction mapToBaseRelationship({\n fallbackRelationshipType,\n}: {\n fallbackRelationshipType: string | null;\n}) {\n // oxlint-disable-next-line typescript/no-explicit-any\n return function (relationship: any): Relationship {\n return new Relationship({\n source: new Node({\n id: relationship.sourceNodeId,\n type: relationship.sourceNodeType\n ? toTitleCase(relationship.sourceNodeType)\n : \"\",\n }),\n target: new Node({\n id: relationship.targetNodeId,\n type: relationship.targetNodeType\n ? toTitleCase(relationship.targetNodeType)\n : \"\",\n }),\n type: (relationship.relationshipType || fallbackRelationshipType)\n .replace(\" \", \"_\")\n .toUpperCase(),\n properties: relationship.properties\n ? convertPropertiesToRecord(relationship.properties)\n : {},\n });\n };\n}\n\nexport interface LLMGraphTransformerProps {\n llm: BaseLanguageModel;\n allowedNodes?: string[];\n allowedRelationships?: string[];\n prompt?: ChatPromptTemplate;\n strictMode?: boolean;\n nodeProperties?: string[];\n relationshipProperties?: string[];\n\n /**\n * @description\n * The LLM may rarely create relationships without a type, causing extraction to fail.\n * Use this to provide a fallback relationship type in such case.\n */\n fallbackRelationshipType?: string | null;\n}\n\nexport class LLMGraphTransformer {\n // oxlint-disable-next-line typescript/no-explicit-any\n chain: any;\n\n allowedNodes: string[] = [];\n\n allowedRelationships: string[] = [];\n\n strictMode: boolean;\n\n nodeProperties: string[];\n\n relationshipProperties: string[];\n\n fallbackRelationshipType: string | null = null;\n\n constructor({\n llm,\n allowedNodes = [],\n allowedRelationships = [],\n prompt = DEFAULT_PROMPT,\n strictMode = true,\n nodeProperties = [],\n relationshipProperties = [],\n fallbackRelationshipType = null,\n }: LLMGraphTransformerProps) {\n if (typeof llm.withStructuredOutput !== \"function\") {\n throw new Error(\n \"The specified LLM does not support the 'withStructuredOutput'. Please ensure you are using an LLM that supports this feature.\"\n );\n }\n\n this.allowedNodes = allowedNodes;\n this.allowedRelationships = allowedRelationships;\n this.strictMode = strictMode;\n this.nodeProperties = nodeProperties;\n this.relationshipProperties = relationshipProperties;\n this.fallbackRelationshipType = fallbackRelationshipType;\n\n // Define chain\n const schema = createSchema(\n allowedNodes,\n allowedRelationships,\n nodeProperties,\n relationshipProperties\n );\n const structuredLLM = llm.withStructuredOutput(toJsonSchema(schema));\n this.chain = prompt.pipe(structuredLLM);\n }\n\n /**\n * Method that processes a single document, transforming it into a graph\n * document using an LLM based on the model's schema and constraints.\n * @param document The document to process.\n * @returns A promise that resolves to a graph document.\n */\n async processResponse(document: Document) {\n const text = document.pageContent;\n\n const rawSchema = await this.chain.invoke({ input: text });\n\n let nodes: Node[] = [];\n if (rawSchema?.nodes) {\n nodes = rawSchema.nodes.map(mapToBaseNode);\n }\n\n let relationships: Relationship[] = [];\n if (rawSchema?.relationships) {\n relationships = rawSchema.relationships.map(\n mapToBaseRelationship({\n fallbackRelationshipType: this.fallbackRelationshipType,\n })\n );\n }\n\n if (\n this.strictMode &&\n (this.allowedNodes.length > 0 || this.allowedRelationships.length > 0)\n ) {\n if (this.allowedNodes.length > 0) {\n const allowedNodesLowerCase = this.allowedNodes.map((node) =>\n node.toLowerCase()\n );\n\n // For nodes, compare lowercased types\n nodes = nodes.filter((node) =>\n allowedNodesLowerCase.includes(node.type.toLowerCase())\n );\n\n // For relationships, compare lowercased types for both source and target nodes\n relationships = relationships.filter(\n (rel) =>\n allowedNodesLowerCase.includes(rel.source.type.toLowerCase()) &&\n allowedNodesLowerCase.includes(rel.target.type.toLowerCase())\n );\n }\n\n if (this.allowedRelationships.length > 0) {\n // For relationships, compare lowercased types\n relationships = relationships.filter((rel) =>\n this.allowedRelationships\n .map((rel) => rel.toLowerCase())\n .includes(rel.type.toLowerCase())\n );\n }\n }\n\n return new GraphDocument({\n nodes,\n relationships,\n source: document,\n });\n }\n\n /**\n * Method that converts an array of documents into an array of graph\n * documents using the `processResponse` method.\n * @param documents The array of documents to convert.\n * @returns A promise that resolves to an array of graph documents.\n */\n async convertToGraphDocuments(\n documents: Document[]\n ): Promise<GraphDocument[]> {\n const results: GraphDocument[] = [];\n\n for (const document of documents) {\n const graphDocument = await this.processResponse(document);\n results.push(graphDocument);\n }\n\n return results;\n }\n}\n"],"mappings":";;;;;;;;;;;AAOA,MAAa,gBAAgB;;;;;;;;;;;;;;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