Skip to content

Commit

Permalink
resolve conflicts and update missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
Naseem77 committed Dec 24, 2024
1 parent 08a70d9 commit 9aad38e
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FalkorDBGraph } from "@langchain/community/graphs/falkordb_graph";
import { OpenAI } from "@langchain/llms/openai";
import { FalkorDBGraph } from "@langchain/community/graphs/falkordb";
import { OpenAI } from "@langchain/openai";
import { GraphCypherQAChain } from "langchain/chains/graph_qa/cypher";

/**
Expand All @@ -18,6 +18,8 @@ await graph.query(
"-[:ACTED_IN]->(:Movie {title: 'Pulp Fiction'})"
);

await graph.refreshSchema();

const chain = GraphCypherQAChain.fromLLM({
llm: model,
graph,
Expand Down
4 changes: 2 additions & 2 deletions libs/langchain-community/langchain.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ export const config = {
// graphs
"graphs/neo4j_graph": "graphs/neo4j_graph",
"graphs/memgraph_graph": "graphs/memgraph_graph",
"graphs/memgraph_graph": "graphs/falkordb_graph",
"graphs/falkordb": "graphs/falkordb",
// document_compressors
"document_compressors/ibm": "document_compressors/ibm",
// document transformers
Expand Down Expand Up @@ -454,7 +454,7 @@ export const config = {
"cache/upstash_redis",
"graphs/neo4j_graph",
"graphs/memgraph_graph",
"graphs/falkordb_graph",
"graphs/falkordb",
// document_compressors
"document_compressors/ibm",
// document_transformers
Expand Down
19 changes: 19 additions & 0 deletions libs/langchain-community/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@langchain/openai": ">=0.2.0 <0.4.0",
"binary-extensions": "^2.2.0",
"expr-eval": "^2.0.2",
"falkordb": "^6.2.5",
"flat": "^5.0.2",
"js-yaml": "^4.1.0",
"langchain": ">=0.2.3 <0.3.0 || >=0.3.4 <0.4.0",
Expand Down Expand Up @@ -189,6 +190,7 @@
"mongodb": "^5.2.0",
"mysql2": "^3.9.8",
"neo4j-driver": "^5.17.0",
"falkordb": "^6.2.5",
"node-llama-cpp": "3.1.1",
"notion-to-md": "^3.1.0",
"officeparser": "^4.0.4",
Expand Down Expand Up @@ -321,6 +323,7 @@
"mongodb": ">=5.2.0",
"mysql2": "^3.9.8",
"neo4j-driver": "*",
"falkordb": "*",
"notion-to-md": "^3.1.0",
"officeparser": "^4.0.4",
"openai": "*",
Expand Down Expand Up @@ -638,6 +641,9 @@
"neo4j-driver": {
"optional": true
},
"falkordb": {
"optional": true
},
"notion-to-md": {
"optional": true
},
Expand Down Expand Up @@ -2323,6 +2329,15 @@
"import": "./graphs/memgraph_graph.js",
"require": "./graphs/memgraph_graph.cjs"
},
"./graphs/falkordb": {
"types": {
"import": "./graphs/falkordb.d.ts",
"require": "./graphs/falkordb.d.cts",
"default": "./graphs/falkordb.d.ts"
},
"import": "./graphs/falkordb.js",
"require": "./graphs/falkordb.cjs"
},
"./document_compressors/ibm": {
"types": {
"import": "./document_compressors/ibm.d.ts",
Expand Down Expand Up @@ -3862,6 +3877,10 @@
"graphs/memgraph_graph.js",
"graphs/memgraph_graph.d.ts",
"graphs/memgraph_graph.d.cts",
"graphs/falkordb.cjs",
"graphs/falkordb.js",
"graphs/falkordb.d.ts",
"graphs/falkordb.d.cts",
"document_compressors/ibm.cjs",
"document_compressors/ibm.js",
"document_compressors/ibm.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { createClient } from "redis";
import { Graph } from "redisgraph.js";

import { FalkorDB, Graph } from "falkordb";
// eslint-disable-next-line @typescript-eslint/no-explicit-any

interface FalkorDBGraphConfig {
Expand All @@ -16,7 +14,7 @@ interface StructuredSchema {
}

export class FalkorDBGraph {
private driver;
private driver: FalkorDB;
private graph: Graph;
private schema = "";
private structuredSchema: StructuredSchema = {
Expand All @@ -26,24 +24,29 @@ export class FalkorDBGraph {
};
private enhancedSchema: boolean;

constructor({ url, graph = "falkordb", enhancedSchema = false }: FalkorDBGraphConfig) {
constructor({ enhancedSchema = false }: FalkorDBGraphConfig) {
try {
this.driver = createClient({ url });
this.graph = new Graph(graph); // Initialize the Graph instance
this.enhancedSchema = enhancedSchema;
} catch (error) {
throw new Error(
"Could not create a FalkorDB driver instance. Please check the connection details."
);
console.error("Error in FalkorDBGraph constructor:", error);
throw new Error("Failed to initialize FalkorDBGraph.");
}
}

static async initialize(config: FalkorDBGraphConfig): Promise<FalkorDBGraph> {
const graph = new FalkorDBGraph(config);
const driver = await FalkorDB.connect({
socket: {
host: new URL(config.url).hostname,
port: parseInt(new URL(config.url).port),
},
});
graph.driver = driver;
await graph.verifyConnectivity();
await graph.refreshSchema();

return graph;
}


getSchema(): string {
return this.schema;
Expand All @@ -53,25 +56,18 @@ export class FalkorDBGraph {
return this.structuredSchema;
}

async query(query: string): Promise<any[]> {
const resultSet = await this.graph.query(query); // Run the query
const rows = [];

// Iterate through the ResultSet
while (resultSet.hasNext()) {
const record = resultSet.next(); // Get the next record
const keys = record.keys(); // Get column names
const values = record.values(); // Get values
const obj = Object.fromEntries(keys.map((key, i) => [key, values[i]])); // Map keys to values
rows.push(obj); // Add the object to rows
}

return rows;
async selectGraph(graphName: string): Promise<void> {
this.graph = await this.driver.selectGraph(graphName);
}

async query(query: string): Promise<any> {
return await this.graph.query(query);
}

async verifyConnectivity(): Promise<void> {
await this.driver.connect(); // Ensure the Redis client is connected
await this.driver.info()
}


async refreshSchema(): Promise<void> {
const nodePropertiesQuery = `
Expand All @@ -98,27 +94,31 @@ export class FalkorDBGraph {
RETURN DISTINCT {start: src_label, type: type(r), end: dst_label} AS output
`;

const nodeProperties = await this.query(nodePropertiesQuery);
const relationshipsProperties = await this.query(relPropertiesQuery);
const relationships = await this.query(relQuery);

this.structuredSchema = {
nodeProps: Object.fromEntries(
nodeProperties.map((el: { output: { label: string; properties: string[] } }) => [el.output.label, el.output.properties])
),
relProps: Object.fromEntries(
relationshipsProperties.map((el: { output: { type: string; properties: string[] } }) => [el.output.type, el.output.properties])
),
relationships: relationships.map((el: { output: { start: string; type: string; end: string } }) => el.output),
};

if (this.enhancedSchema) {
this.enhanceSchemaDetails();
}
const nodePropertiesResult = await this.query(nodePropertiesQuery);
const relationshipsPropertiesResult = await this.query(relPropertiesQuery);
const relationshipsResult = await this.query(relQuery);

const nodeProperties = nodePropertiesResult.data || [];
const relationshipsProperties = relationshipsPropertiesResult.data || [];
const relationships = relationshipsResult.data || [];

this.structuredSchema = {
nodeProps: Object.fromEntries(
nodeProperties.map((el: { output: { label: string; properties: string[] } }) => [el.output.label, el.output.properties])
),
relProps: Object.fromEntries(
relationshipsProperties.map((el: { output: { type: string; properties: string[] } }) => [el.output.type, el.output.properties])
),
relationships: relationships.map((el: { output: { start: string; type: string; end: string } }) => el.output),
};

this.schema = this.formatSchema();
if (this.enhancedSchema) {
await this.enhanceSchemaDetails();
}

this.schema = this.formatSchema();
}

private async enhanceSchemaDetails(): Promise<void> {
console.log("Enhanced schema details not yet implemented for FalkorDB.");
}
Expand Down Expand Up @@ -149,6 +149,6 @@ export class FalkorDBGraph {
}

async close(): Promise<void> {
await this.driver.quit();
await this.driver.close();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/* eslint-disable no-process-env */

import { test } from "@jest/globals";
import { FalkorDBGraph } from "../falkordb_graph.js";
import { FalkorDBGraph } from "../falkordb.js";

describe("FalkorDB Graph Tests", () => {
const url = process.env.FALKORDB_URI as string;
let graph: FalkorDBGraph;

beforeEach(async () => {
graph = await FalkorDBGraph.initialize({ url });
await graph.selectGraph("falkordbGraph");
await graph.refreshSchema();
await graph.query("MATCH (n) DETACH DELETE n");
});

Expand All @@ -22,7 +24,20 @@ describe("FalkorDB Graph Tests", () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return graph.query('RETURN "test" AS output').then((output: any) => {
const expectedOutput = [{ output: "test" }];
expect(output).toEqual(expectedOutput);
expect(output.data).toEqual(expectedOutput);
});
});

test("Verify refreshSchema accurately updates the schema", async () => {
await graph.query(`
CREATE (:Person {name: 'Alice', age: 30})-[:FRIENDS_WITH]->(:Person {name: 'Bob', age: 25})
`);
await graph.refreshSchema();

const schema = graph.getSchema();
expect(schema).toContain("Person");
expect(schema).toContain("FRIENDS_WITH");
expect(schema).toContain("name");
expect(schema).toContain("age");
});
});
2 changes: 1 addition & 1 deletion libs/langchain-community/src/load/import_constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export const optionalImportEntrypoints: string[] = [
"langchain_community/retrievers/zep_cloud",
"langchain_community/graphs/neo4j_graph",
"langchain_community/graphs/memgraph_graph",
"langchain_community/graphs/falkordb_graph",
"langchain_community/graphs/falkordb",
"langchain_community/document_compressors/ibm",
"langchain_community/document_transformers/html_to_text",
"langchain_community/document_transformers/mozilla_readability",
Expand Down

0 comments on commit 9aad38e

Please sign in to comment.