Closed
Description
Context
See here: #185
Currently in Langchain4j we can create a Neo4j KnowledgeGraph via the KnowledgeGraphWriter class,
but to retrieve the results we would have to use manual queries (e.g. `MATCH (c:Chunk)-[]->(:Document) RETURN c).
No vectors are saved, instead just string texts are saved.
Use case solution
To create something similar, i.e. with text embedded from the document texts,
we could add an argument to KnowledgeGraphWriter
builder, for example:
String retrievalQuery = """
MATCH (chunk)-[:PART_OF]->(d:Document)
// aggregate chunk-details
WITH d, collect(DISTINCT {chunk: chunk, score: score}) AS chunks, avg(score) as avg_score
// ....
""";
final Neo4jEmbeddingStore neo4jEmbeddingStore = Neo4jEmbeddingStore.builder()
.driver(null)
.dimension(384)
.label("Chunk")
.retrievalQuery(retrievalQuery).build();
knowledgeGraphWriter = KnowledgeGraphWriter.builder()
.embeddingStore(embeddingStore)
// .... other methods
.build()
so it can save vectors in nodes with label Chunk
to be retrieved in this way
List<EmbeddingMatch<TextSegment>> matches = neo4jEmbeddingStore.search(null).matches();
/*
which will search `Chunk` with embedding using a query like:
CALL db.index.vector.queryNodes($indexName, $maxResults, $embeddingValue) YIELD node, score WHERE score >= $minScore
MATCH (node)-[:PART_OF]->(d:Document)
*/
\cc @Martin7-1