@langchain/community
Version:
Third-party integrations for LangChain.js
1 lines • 4.96 kB
Source Map (JSON)
{"version":3,"file":"memgraph_graph.cjs","names":["Neo4jGraph","config: MemgraphGraphConfig","error: any","el: { end: string; start: string; type: string }"],"sources":["../../src/graphs/memgraph_graph.ts"],"sourcesContent":["import { Neo4jGraph } from \"./neo4j_graph.js\";\n\ninterface MemgraphGraphConfig {\n url: string;\n username: string;\n password: string;\n database?: string;\n}\n\nconst rawSchemaQuery = `\nCALL llm_util.schema(\"raw\")\nYIELD *\nRETURN *\n`;\n\n/**\n * @security *Security note*: Make sure that the database connection uses credentials\n * that are narrowly-scoped to only include necessary permissions.\n * Failure to do so may result in data corruption or loss, since the calling\n * code may attempt commands that would result in deletion, mutation\n * of data if appropriately prompted or reading sensitive data if such\n * data is present in the database.\n * The best way to guard against such negative outcomes is to (as appropriate)\n * limit the permissions granted to the credentials used with this tool.\n * For example, creating read only users for the database is a good way to\n * ensure that the calling code cannot mutate or delete data.\n *\n * @link See https://js.langchain.com/docs/security for more information.\n */\nclass MemgraphGraph extends Neo4jGraph {\n constructor({\n url,\n username,\n password,\n database = \"memgraph\",\n }: MemgraphGraphConfig) {\n super({ url, username, password, database });\n }\n\n static async initialize(config: MemgraphGraphConfig): Promise<MemgraphGraph> {\n const graph = new MemgraphGraph(config);\n\n try {\n await graph.verifyConnectivity();\n } catch {\n console.error(\"Failed to verify connection.\");\n }\n\n try {\n await graph.refreshSchema();\n console.debug(\"Schema refreshed successfully.\");\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n } catch (error: any) {\n throw new Error(error.message);\n }\n\n return graph;\n }\n\n async refreshSchema() {\n const rawSchemaQueryResult = await this.query(rawSchemaQuery);\n if (rawSchemaQueryResult?.[0]?.schema) {\n const rawSchema = rawSchemaQueryResult?.[0]?.schema;\n\n this.structuredSchema = {\n nodeProps: rawSchema.node_props,\n relProps: rawSchema.rel_props,\n relationships: rawSchema.relationships,\n };\n\n // Format node properties\n const formattedNodeProps = Object.entries(rawSchema.node_props)\n .map(([nodeName, properties]) => {\n const propertiesStr = JSON.stringify(properties);\n return `Node name: '${nodeName}', Node properties: ${propertiesStr}`;\n })\n .join(\"\\n\");\n\n // Format relationship properties\n const formattedRelProps = Object.entries(rawSchema.rel_props)\n .map(([relationshipName, properties]) => {\n const propertiesStr = JSON.stringify(properties);\n return `Relationship name: '${relationshipName}', Relationship properties: ${propertiesStr}`;\n })\n .join(\"\\n\");\n\n // Format relationships\n const formattedRels = rawSchema.relationships\n ?.map(\n (el: { end: string; start: string; type: string }) =>\n `(:${el.start})-[:${el.type}]->(:${el.end})`\n )\n .join(\"\\n\");\n\n // Combine all formatted elements into a single string\n this.schema = [\n \"Node properties are the following:\",\n formattedNodeProps,\n \"Relationship properties are the following:\",\n formattedRelProps,\n \"The relationships are the following:\",\n formattedRels,\n ].join(\"\\n\");\n }\n }\n}\n\nexport { MemgraphGraph };\n"],"mappings":";;;;;;AASA,MAAM,iBAAiB,CAAC;;;;AAIxB,CAAC;;;;;;;;;;;;;;;AAgBD,IAAM,gBAAN,MAAM,sBAAsBA,sCAAW;CACrC,YAAY,EACV,KACA,UACA,UACA,WAAW,YACS,EAAE;EACtB,MAAM;GAAE;GAAK;GAAU;GAAU;EAAU,EAAC;CAC7C;CAED,aAAa,WAAWC,QAAqD;EAC3E,MAAM,QAAQ,IAAI,cAAc;AAEhC,MAAI;GACF,MAAM,MAAM,oBAAoB;EACjC,QAAO;GACN,QAAQ,MAAM,+BAA+B;EAC9C;AAED,MAAI;GACF,MAAM,MAAM,eAAe;GAC3B,QAAQ,MAAM,iCAAiC;EAEhD,SAAQC,OAAY;AACnB,SAAM,IAAI,MAAM,MAAM;EACvB;AAED,SAAO;CACR;CAED,MAAM,gBAAgB;EACpB,MAAM,uBAAuB,MAAM,KAAK,MAAM,eAAe;AAC7D,MAAI,uBAAuB,IAAI,QAAQ;GACrC,MAAM,YAAY,uBAAuB,IAAI;GAE7C,KAAK,mBAAmB;IACtB,WAAW,UAAU;IACrB,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