Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(community): Provide fallback relationshipType in case it is not present in graph_transformer #7521

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 28 additions & 20 deletions libs/langchain-community/src/experimental/graph_transformers/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 || fallbackRelationshipType).replace(" ", "_").toUpperCase(),
properties: relationship.properties
? convertPropertiesToRecord(relationship.properties)
: {},
});
}
}

export interface LLMGraphTransformerProps {
Expand All @@ -246,6 +248,8 @@ export class LLMGraphTransformer {

relationshipProperties: string[];

fallbackRelationshipType: string = "unknown";

constructor({
llm,
allowedNodes = [],
Expand All @@ -254,6 +258,7 @@ export class LLMGraphTransformer {
strictMode = true,
nodeProperties = [],
relationshipProperties = [],
fallbackRelationshipType = "unknown,
Copy link
Collaborator

@jacoblee93 jacoblee93 Jan 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think you're missing a quote here!

And don't forget to run yarn format

}: LLMGraphTransformerProps) {
if (typeof llm.withStructuredOutput !== "function") {
throw new Error(
Expand All @@ -266,6 +271,7 @@ export class LLMGraphTransformer {
this.strictMode = strictMode;
this.nodeProperties = nodeProperties;
this.relationshipProperties = relationshipProperties;
this.fallbackRelationshipType = fallbackRelationshipType

// Define chain
const schema = createSchema(
Expand Down Expand Up @@ -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 (
Expand Down