From b2538222ffece8e38b6ebf8381c0d5ec9448f12c Mon Sep 17 00:00:00 2001 From: Minh Ha Date: Tue, 14 Jan 2025 08:11:32 -0500 Subject: [PATCH 1/2] provide fallback relationshipType in case it is not present --- .../experimental/graph_transformers/llm.ts | 48 +++++++++++-------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/libs/langchain-community/src/experimental/graph_transformers/llm.ts b/libs/langchain-community/src/experimental/graph_transformers/llm.ts index 53155ede9866..a2ca61f86598 100644 --- a/libs/langchain-community/src/experimental/graph_transformers/llm.ts +++ b/libs/langchain-community/src/experimental/graph_transformers/llm.ts @@ -201,25 +201,27 @@ function mapToBaseNode(node: any): Node { } // 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(), - properties: relationship.properties - ? convertPropertiesToRecord(relationship.properties) - : {}, - }); +function mapToBaseRelationship({ fallbackRelationshipType = "unknown" }: { fallbackRelationshipType: string }) { + return function (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 || "unknown").replace(" ", "_").toUpperCase(), + properties: relationship.properties + ? convertPropertiesToRecord(relationship.properties) + : {}, + }); + } } export interface LLMGraphTransformerProps { @@ -246,6 +248,8 @@ export class LLMGraphTransformer { relationshipProperties: string[]; + fallbackRelationshipType: string = "unknown"; + constructor({ llm, allowedNodes = [], @@ -254,6 +258,7 @@ export class LLMGraphTransformer { strictMode = true, nodeProperties = [], relationshipProperties = [], + fallbackRelationshipType = "unknown, }: LLMGraphTransformerProps) { if (typeof llm.withStructuredOutput !== "function") { throw new Error( @@ -266,6 +271,7 @@ export class LLMGraphTransformer { this.strictMode = strictMode; this.nodeProperties = nodeProperties; this.relationshipProperties = relationshipProperties; + this.fallbackRelationshipType = fallbackRelationshipType // Define chain const schema = createSchema( @@ -296,7 +302,9 @@ export class LLMGraphTransformer { let relationships: Relationship[] = []; if (rawSchema?.relationships) { - relationships = rawSchema.relationships.map(mapToBaseRelationship); + relationships = rawSchema.relationships.map( + mapToBaseRelationship({ fallbackRelationshipType: this.fallbackRelationshipType }) + ); } if ( From bb08334eb0216b38e91618bc09b850666cc54f3e Mon Sep 17 00:00:00 2001 From: Minh Ha Date: Tue, 14 Jan 2025 08:24:00 -0500 Subject: [PATCH 2/2] Update llm.ts --- .../src/experimental/graph_transformers/llm.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/langchain-community/src/experimental/graph_transformers/llm.ts b/libs/langchain-community/src/experimental/graph_transformers/llm.ts index a2ca61f86598..86240d04daf5 100644 --- a/libs/langchain-community/src/experimental/graph_transformers/llm.ts +++ b/libs/langchain-community/src/experimental/graph_transformers/llm.ts @@ -216,7 +216,7 @@ function mapToBaseRelationship({ fallbackRelationshipType = "unknown" }: { fallb ? toTitleCase(relationship.targetNodeType) : "", }), - type: (relationship.relationshipType || "unknown").replace(" ", "_").toUpperCase(), + type: (relationship.relationshipType || fallbackRelationshipType).replace(" ", "_").toUpperCase(), properties: relationship.properties ? convertPropertiesToRecord(relationship.properties) : {},