This is an example application using Spring AI, Spring Data Neo4j, Neo4j (utilizing both vector search and graph search capabilities), and Goodreads book data.
Data is maintained and pulled from a public data source. Load scripts and more information is available in this data set Github repository. An example of the data model is shown below.
For this project, we are focusing on the Book and Review entities and the relationship between them.
There were a few "gotchas" I found as I built this application.
-
Create a specific
Neo4jVectorStore
bean, even though the integration test in the guide shows a generic VectorStore one. -
Spring AI with Neo4j uses a default vector index name of
spring-ai-document-index
and default entity type ofDocument
. You can customize these with the.withIndexName("<index-name>")
and.withLabel("<label>")
methods attached to the Neo4jVectorStore configuration. -
Spring AI requires property names of
id
andtext
to return in the list of Documents from the similaritySearch method (no matter what label is configured for the Neo4jVectorStore bean). If your properties are named something else, the Spring app cannot map them from the Neo4j node to the expected Document object. You can refactor the data in Neo4j to change the property names toid
andtext
.
To run the application, you will need the following:
-
Neo4j database credentials. You can set these in the
application.properties
file or as environment variables on the machine. -
OpenAI API key: they offer a free tier that works for this.
Once that’s set up, execute the application with ./mvnw spring-boot:run
.
You can test a few different things. The /hello
endpoint only sends the question to the LLM as a test. The /rag
endpoint goes to Neo4j before forwarding the returned graph data to the LLM for formatting the response. The /llm
endpoint only sends the prompt to the LLM, and the /vector
only retrieves reviews from the vector search in Neo4j. Here are some ideas for values to test the /rag
endpoint:
http ":8080/rag?searchPhrase=happy%20ending"
http ":8080/rag?searchPhrase=encouragement"
http ":8080/rag?searchPhrase=high tech"
http ":8080/rag?searchPhrase=caffeine"
Note: The above commands are using the
HTTPie command line tool, but you can use curl
similarly.
PDF versions of accompanying presentations are published to SpeakerDeck.
-
Site: Neo4j Aura
-
Documentation: Spring AI Neo4j Vector Store
-
Guide: Spring AI Neo4j
-
Online training: GraphAcademy LLM courses
-
Documentation: OpenAI Embeddings