Spring AI

The Spring AI project aims to be something like LangChain for the Java world.

Combining Spring AI with Spring Data Neo4j makes it possible to build on existing domain models, turn them into a graph, and enrich them with vector embeddings.

Installation

Currently Spring AI is available from the Spring Snapshot Repository:

<repository>
	<id>spring-snapshots</id>
	<name>Spring Snapshots</name>
	<url>https://repo.spring.io/snapshot</url>
	<releases>
		<enabled>false</enabled>
	</releases>
</repository>

The Spring AI project is available on Maven Central. To use it in your project, add the following dependency: org.springframework.ai:spring-ai-neo4j-store:version

The following example is taken from the integration test

@SpringBootConfiguration
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class })
public static class TestApplication {

	@Bean
	public VectorStore vectorStore(Driver driver, EmbeddingClient embeddingClient) {

		return new Neo4jVectorStore(driver, embeddingClient,
				Neo4jVectorStore.Neo4jVectorStoreConfig.defaultConfig());
	}

	@Bean
	public Driver driver() {
		return GraphDatabase.driver(neo4jContainer.getBoltUrl(),
				AuthTokens.basic("neo4j", neo4jContainer.getAdminPassword()));
	}

	@Bean
	public EmbeddingClient embeddingClient() {
		return new OpenAiEmbeddingClient(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
	}

}
VectorStore vectorStore = context.getBean(VectorStore.class);

vectorStore.add(this.documents);

List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Great").withTopK(1));

assertThat(results).hasSize(1);
Document resultDoc = results.get(0);
assertThat(resultDoc.getId()).isEqualTo(this.documents.get(2).getId());
assertThat(resultDoc.getContent()).isEqualTo(
		"Great Depression Great Depression Great Depression Great Depression Great Depression Great Depression");
assertThat(resultDoc.getMetadata()).containsKey("meta2");
assertThat(resultDoc.getMetadata()).containsKey("distance");

Functionality Includes

  • Create vector index

  • Populate nodes and vector index from documents

  • Query vector index

Documentation

The Neo4j Vector integration documentation is avalable at: https://docs.spring.io/spring-ai/reference/api/vectordbs/neo4j.html