Skip to content

[FEATURE] Add the ability to store the KnowledgeGraphWriter results in vector indexes via Neo4jEmbeddingStore #216

Closed
@vga91

Description

@vga91

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions