@langchain/community
Advanced tools
Comparing version 0.0.44 to 0.0.45
@@ -35,9 +35,9 @@ import { ChatOpenAI } from "@langchain/openai"; | ||
nodes: [ | ||
new Node({ id: "Elon Musk", type: "PERSON" }), | ||
new Node({ id: "OpenAI", type: "ORGANIZATION" }), | ||
new Node({ id: "Elon Musk", type: "Person" }), | ||
new Node({ id: "OpenAI", type: "Organization" }), | ||
], | ||
relationships: [ | ||
new Relationship({ | ||
source: new Node({ id: "Elon Musk", type: "PERSON" }), | ||
target: new Node({ id: "OpenAI", type: "ORGANIZATION" }), | ||
source: new Node({ id: "Elon Musk", type: "Person" }), | ||
target: new Node({ id: "OpenAI", type: "Organization" }), | ||
type: "SUES", | ||
@@ -53,1 +53,35 @@ }), | ||
}); | ||
test("convertToGraphDocuments with allowed lowercased", async () => { | ||
const model = new ChatOpenAI({ | ||
temperature: 0, | ||
modelName: "gpt-4-turbo-preview", | ||
}); | ||
const llmGraphTransformer = new LLMGraphTransformer({ | ||
llm: model, | ||
allowedNodes: ["Person", "Organization"], | ||
allowedRelationships: ["SUES"], | ||
}); | ||
const result = await llmGraphTransformer.convertToGraphDocuments([ | ||
new Document({ pageContent: "Elon Musk is suing OpenAI" }), | ||
]); | ||
console.log(JSON.stringify(result)); | ||
expect(result).toEqual([ | ||
new GraphDocument({ | ||
nodes: [ | ||
new Node({ id: "Elon Musk", type: "Person" }), | ||
new Node({ id: "OpenAI", type: "Organization" }), | ||
], | ||
relationships: [ | ||
new Relationship({ | ||
source: new Node({ id: "Elon Musk", type: "Person" }), | ||
target: new Node({ id: "OpenAI", type: "Organization" }), | ||
type: "SUES", | ||
}), | ||
], | ||
source: new Document({ | ||
pageContent: "Elon Musk is suing OpenAI", | ||
metadata: {}, | ||
}), | ||
}), | ||
]); | ||
}); |
@@ -34,2 +34,8 @@ import { z } from "zod"; | ||
]); | ||
function toTitleCase(str) { | ||
return str | ||
.split(" ") | ||
.map((w) => w[0].toUpperCase() + w.substring(1).toLowerCase()) | ||
.join(""); | ||
} | ||
function createOptionalEnumType({ enumValues = undefined, description = "", isRel = false, }) { | ||
@@ -92,3 +98,3 @@ let schema; | ||
id: node.id, | ||
type: node.type.replace(" ", "_").toUpperCase(), | ||
type: toTitleCase(node.type), | ||
}); | ||
@@ -101,7 +107,7 @@ } | ||
id: relationship.sourceNodeId, | ||
type: relationship.sourceNodeType.replace(" ", "_").toUpperCase(), | ||
type: toTitleCase(relationship.sourceNodeType), | ||
}), | ||
target: new Node({ | ||
id: relationship.targetNodeId, | ||
type: relationship.targetNodeType.replace(" ", "_").toUpperCase(), | ||
type: toTitleCase(relationship.targetNodeType), | ||
}), | ||
@@ -169,8 +175,14 @@ type: relationship.relationshipType.replace(" ", "_").toUpperCase(), | ||
if (this.allowedNodes.length > 0) { | ||
nodes = nodes.filter((node) => this.allowedNodes.includes(node.type)); | ||
relationships = relationships.filter((rel) => this.allowedNodes.includes(rel.source.type) && | ||
this.allowedNodes.includes(rel.target.type)); | ||
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) { | ||
relationships = relationships.filter((rel) => this.allowedRelationships.includes(rel.type)); | ||
// For relationships, compare lowercased types | ||
relationships = relationships.filter((rel) => this.allowedRelationships | ||
.map((rel) => rel.toLowerCase()) | ||
.includes(rel.type.toLowerCase())); | ||
} | ||
@@ -177,0 +189,0 @@ } |
@@ -27,3 +27,3 @@ import { Serializable } from "@langchain/core/load/serializable"; | ||
} | ||
export declare class GraphDocument extends Document { | ||
export declare class GraphDocument extends Serializable { | ||
nodes: Node[]; | ||
@@ -30,0 +30,0 @@ relationships: Relationship[]; |
import { Serializable } from "@langchain/core/load/serializable"; | ||
import { Document } from "@langchain/core/documents"; | ||
export class Node extends Serializable { | ||
@@ -76,5 +75,9 @@ constructor({ id, type = "Node", properties = {}, }) { | ||
} | ||
export class GraphDocument extends Document { | ||
export class GraphDocument extends Serializable { | ||
constructor({ nodes, relationships, source, }) { | ||
super(source); | ||
super({ | ||
nodes, | ||
relationships, | ||
source, | ||
}); | ||
Object.defineProperty(this, "nodes", { | ||
@@ -102,3 +105,3 @@ enumerable: true, | ||
writable: true, | ||
value: ["langchain", "graph", "document_node"] | ||
value: ["langchain", "graph", "graph_document"] | ||
}); | ||
@@ -105,0 +108,0 @@ this.nodes = nodes; |
@@ -6,3 +6,3 @@ import neo4j from "neo4j-driver"; | ||
MERGE (d:Document {id:$document.metadata.id}) | ||
SET d.text = $document.page_content | ||
SET d.text = $document.pageContent | ||
SET d += $document.metadata | ||
@@ -9,0 +9,0 @@ WITH d |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
3725110
1442
90504
120