|
| 1 | +package dev.langchain4j.rag.content.retriever.neo4j; |
| 2 | + |
| 3 | +import dev.langchain4j.internal.ValidationUtils; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.stream.Collectors; |
| 7 | +import org.neo4j.driver.Driver; |
| 8 | +import org.neo4j.driver.Record; |
| 9 | +import org.neo4j.driver.Session; |
| 10 | +import org.neo4j.driver.Value; |
| 11 | +import org.neo4j.driver.exceptions.ClientException; |
| 12 | +import org.neo4j.driver.summary.ResultSummary; |
| 13 | + |
| 14 | +public class Neo4jGraph implements AutoCloseable { |
| 15 | + |
| 16 | + public static class Builder { |
| 17 | + private Driver driver; |
| 18 | + |
| 19 | + /** |
| 20 | + * @param driver the {@link Driver} (required) |
| 21 | + */ |
| 22 | + Builder driver(Driver driver) { |
| 23 | + this.driver = driver; |
| 24 | + return this; |
| 25 | + } |
| 26 | + |
| 27 | + Neo4jGraph build() { |
| 28 | + return new Neo4jGraph(driver); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private static final String NODE_PROPERTIES_QUERY = |
| 33 | + """ |
| 34 | + CALL apoc.meta.data() |
| 35 | + YIELD label, other, elementType, type, property |
| 36 | + WHERE NOT type = "RELATIONSHIP" AND elementType = "node" |
| 37 | + WITH label AS nodeLabels, collect({property:property, type:type}) AS properties |
| 38 | + RETURN {labels: nodeLabels, properties: properties} AS output |
| 39 | + """; |
| 40 | + |
| 41 | + private static final String REL_PROPERTIES_QUERY = |
| 42 | + """ |
| 43 | + CALL apoc.meta.data() |
| 44 | + YIELD label, other, elementType, type, property |
| 45 | + WHERE NOT type = "RELATIONSHIP" AND elementType = "relationship" |
| 46 | + WITH label AS nodeLabels, collect({property:property, type:type}) AS properties |
| 47 | + RETURN {type: nodeLabels, properties: properties} AS output |
| 48 | + """; |
| 49 | + |
| 50 | + private static final String RELATIONSHIPS_QUERY = |
| 51 | + """ |
| 52 | + CALL apoc.meta.data() |
| 53 | + YIELD label, other, elementType, type, property |
| 54 | + WHERE type = "RELATIONSHIP" AND elementType = "node" |
| 55 | + UNWIND other AS other_node |
| 56 | + RETURN {start: label, type: property, end: toString(other_node)} AS output |
| 57 | + """; |
| 58 | + |
| 59 | + private final Driver driver; |
| 60 | + |
| 61 | + private String schema; |
| 62 | + |
| 63 | + public Neo4jGraph(final Driver driver) { |
| 64 | + |
| 65 | + this.driver = ValidationUtils.ensureNotNull(driver, "driver"); |
| 66 | + this.driver.verifyConnectivity(); |
| 67 | + try { |
| 68 | + refreshSchema(); |
| 69 | + } catch (ClientException e) { |
| 70 | + if ("Neo.ClientError.Procedure.ProcedureNotFound".equals(e.code())) { |
| 71 | + throw new Neo4jException("Please ensure the APOC plugin is installed in Neo4j", e); |
| 72 | + } |
| 73 | + throw e; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public String getSchema() { |
| 78 | + return schema; |
| 79 | + } |
| 80 | + |
| 81 | + static Builder builder() { |
| 82 | + return new Builder(); |
| 83 | + } |
| 84 | + |
| 85 | + public ResultSummary executeWrite(String queryString) { |
| 86 | + |
| 87 | + try (Session session = this.driver.session()) { |
| 88 | + return session.executeWrite(tx -> tx.run(queryString).consume()); |
| 89 | + } catch (ClientException e) { |
| 90 | + throw new Neo4jException("Error executing query: " + queryString, e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + public List<Record> executeRead(String queryString) { |
| 95 | + |
| 96 | + return this.driver.executableQuery(queryString).execute().records(); |
| 97 | + } |
| 98 | + |
| 99 | + public void refreshSchema() { |
| 100 | + |
| 101 | + List<String> nodeProperties = formatNodeProperties(executeRead(NODE_PROPERTIES_QUERY)); |
| 102 | + List<String> relationshipProperties = formatRelationshipProperties(executeRead(REL_PROPERTIES_QUERY)); |
| 103 | + List<String> relationships = formatRelationships(executeRead(RELATIONSHIPS_QUERY)); |
| 104 | + |
| 105 | + this.schema = "Node properties are the following:\n" + String.join("\n", nodeProperties) |
| 106 | + + "\n\n" + "Relationship properties are the following:\n" |
| 107 | + + String.join("\n", relationshipProperties) |
| 108 | + + "\n\n" + "The relationships are the following:\n" |
| 109 | + + String.join("\n", relationships); |
| 110 | + } |
| 111 | + |
| 112 | + private List<String> formatNodeProperties(List<Record> records) { |
| 113 | + |
| 114 | + return records.stream() |
| 115 | + .map(this::getOutput) |
| 116 | + .map(r -> String.format( |
| 117 | + "%s %s", |
| 118 | + r.asMap().get("labels"), formatMap(r.get("properties").asList(Value::asMap)))) |
| 119 | + .toList(); |
| 120 | + } |
| 121 | + |
| 122 | + private List<String> formatRelationshipProperties(List<Record> records) { |
| 123 | + |
| 124 | + return records.stream() |
| 125 | + .map(this::getOutput) |
| 126 | + .map(r -> String.format( |
| 127 | + "%s %s", r.get("type"), formatMap(r.get("properties").asList(Value::asMap)))) |
| 128 | + .toList(); |
| 129 | + } |
| 130 | + |
| 131 | + private List<String> formatRelationships(List<Record> records) { |
| 132 | + |
| 133 | + return records.stream() |
| 134 | + .map(r -> getOutput(r).asMap()) |
| 135 | + .map(r -> String.format("(:%s)-[:%s]->(:%s)", r.get("start"), r.get("type"), r.get("end"))) |
| 136 | + .toList(); |
| 137 | + } |
| 138 | + |
| 139 | + private Value getOutput(Record record) { |
| 140 | + |
| 141 | + return record.get("output"); |
| 142 | + } |
| 143 | + |
| 144 | + private String formatMap(List<Map<String, Object>> properties) { |
| 145 | + |
| 146 | + return properties.stream() |
| 147 | + .map(prop -> prop.get("property") + ":" + prop.get("type")) |
| 148 | + .collect(Collectors.joining(", ", "{", "}")); |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public void close() { |
| 153 | + |
| 154 | + this.driver.close(); |
| 155 | + } |
| 156 | +} |
0 commit comments