Notebooks
M
Mistral AI
Property Graph Custom Retriever

Property Graph Custom Retriever

mistral-cookbookLlamaIndexthird_partypropertygraphs

Custom Retriever in PropertyGraph

In this notebook we demonstrate how to define a custom retriever for a property graph. Although this approach is more complex than using standard graph retrievers, it offers detailed control over the retrieval process, allowing for customization that aligns closely with your specific application needs.

Also, we will walk you through an advanced retrieval workflow using the property graph store directly. We’ll conduct both vector search and text-to-Cypher retrieval, and subsequently integrate the results using a reranking module.

We will be using cohere reranker, so we will need cohere-api-key

[ ]

Setup

[13]
[14]
[15]

Download Data

[16]
--2024-07-05 09:33:47--  https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 2606:50c0:8000::154, 2606:50c0:8001::154, 2606:50c0:8002::154, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|2606:50c0:8000::154|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 75042 (73K) [text/plain]
Saving to: ‘data/paul_graham/paul_graham_essay.txt’

data/paul_graham/pa 100%[===================>]  73.28K  --.-KB/s    in 0.02s   

2024-07-05 09:33:47 (4.74 MB/s) - ‘data/paul_graham/paul_graham_essay.txt’ saved [75042/75042]

Load Data

[17]

Docker Setup

You need to login and set password for the first time.

  1. username: neo4j

  2. password: neo4j

[ ]

Setup Neo4j GraphStore

[18]
Received notification from DBMS server: {severity: WARNING} {code: Neo.ClientNotification.Statement.FeatureDeprecationWarning} {category: DEPRECATION} {title: This feature is deprecated and will be removed in future versions.} {description: The procedure has a deprecated field. ('config' used by 'apoc.meta.graphSample' is deprecated.)} {position: line: 1, column: 1, offset: 0} for query: "CALL apoc.meta.graphSample() YIELD nodes, relationships RETURN nodes, [rel in relationships | {name:apoc.any.property(rel, 'type'), count: apoc.any.property(rel, 'count')}] AS relationships"

PropertyGraphIndex Construction

[19]
Parsing nodes: 100%|██████████| 1/1 [00:00<00:00, 19.73it/s]
Extracting paths from text: 100%|██████████| 22/22 [00:24<00:00,  1.12s/it]
Extracting implicit paths: 100%|██████████| 22/22 [00:00<00:00, 11714.45it/s]
Generating embeddings: 100%|██████████| 46/46 [00:14<00:00,  3.16it/s]
Received notification from DBMS server: {severity: WARNING} {code: Neo.ClientNotification.Statement.FeatureDeprecationWarning} {category: DEPRECATION} {title: This feature is deprecated and will be removed in future versions.} {description: The procedure has a deprecated field. ('config' used by 'apoc.meta.graphSample' is deprecated.)} {position: line: 1, column: 1, offset: 0} for query: "CALL apoc.meta.graphSample() YIELD nodes, relationships RETURN nodes, [rel in relationships | {name:apoc.any.property(rel, 'type'), count: apoc.any.property(rel, 'count')}] AS relationships"

CustomRetriever

Define a custom retriever that combines VectorContextRetriever, TextToCypherRetriever and Reranker.

[20]
[21]

Setup CustomRetriever

[22]

QueryEngine

[23]
[25]
Received notification from DBMS server: {severity: WARNING} {code: Neo.ClientNotification.Statement.UnknownRelationshipTypeWarning} {category: UNRECOGNIZED} {title: The provided relationship type is not in the database.} {description: One of the relationship types in your query is not available in the database, make sure you didn't misspell it or that the label is available when you run this statement in your application (the missing relationship type is: WORKS_AT)} {position: line: 1, column: 20, offset: 19} for query: "MATCH (p:PERSON)-[:WORKS_AT]->(o:ORGANIZATION)\nWHERE p.name = 'author' AND o.name = 'Interleaf'\nRETURN p.name, o.name, p.last_modified_date AS last_modified_date_person, o.last_modified_date AS last_modified_date_organization, p.creation_date AS creation_date_person, o.creation_date AS creation_date_organization"
[ ]