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

If you are using the Spring Initializr, you can add the Spring AI model of your choosing and Neo4j Vector Database as dependencies to your project, and it will pull in all the related pieces.

If you add the dependencies manually, Spring AI is not yet a general release library, so you will need to add the dependencies, as well as the snapshot and milestone repositories to your pom.xml file, as outlined in the Getting Started documentation.

The next piece you will need to add is the configuration for the Neo4j Vector Database, which also needs to be accompanied by custom driver and embedding client configuration. Probably the simplest approach is to create Spring Beans for each of these components in the main application class. This is mentioned in the documentation, but the full code is not provided. An example of how to configure the Neo4j Vector Database is shown below (note that you will need to alter the vectorStore bean to match your specific configuration):

    return new OpenAiEmbeddingClient(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
  }

  @Bean
  public Neo4jVectorStore vectorStore(Driver driver, EmbeddingClient embeddingClient) {
    return new Neo4jVectorStore(driver, embeddingClient,
        Neo4jVectorStore.Neo4jVectorStoreConfig.builder()
            .withIndexName("form_10k_chunks")
            .withLabel("Chunk")
            .withEmbeddingProperty("textEmbedding")
            .build());
  }

}

Now that we have the vector store configured, we can use Spring AI for retrieval augmented generation (RAG) via a three-step process.

  1. Call the vector similarity search method to retrieve the most similar documents.

  2. Use the similar documents as input to a retrieval query that pulls related entities from the graph.

  3. Provide the similar documents (with their related graph entities) as input to a prompt that the LLM will use to generate a response.

The code snippet below demonstrates how to use Spring AI for RAG with Neo4j Vector Store:

    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-fbdbeff9329d.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/global-4ff23c0615da.css" />
    <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/github-c62b650ef489.css" />
  <link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/repository-7247b57543b3.css" />

  


  <script type="application/json" id="client-env">{"locale":"en","featureFlags":["code_vulnerability_scanning","copilot_chat_static_thread_suggestions","copilot_conversational_ux_history_refs","copilot_followup_to_agent","copilot_smell_icebreaker_ux","copilot_implicit_context","copilot_stop_response","failbot_handle_non_errors","geojson_azure_maps","marketing_forms_api_integration_contact_request","marketing_pages_search_explore_provider","turbo_experiment_risky","sample_network_conn_type","react_start_transition_for_navigations","custom_inp","remove_child_patch","issues_react_is_read","kb_source_repos","copilot_beta_features_opt_in"]}</script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/wp-runtime-5aee6f8f4ad0.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_dompurify_dist_purify_js-810e4b1b9abd.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_oddbird_popover-polyfill_dist_popover_js-4ac41d0a76fd.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_mini-throttle_dist_index_js-node_modules_smoothscroll-polyfill_di-75db2e-e091a6d939e9.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/environment-041d6b79df7b.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_primer_behaviors_dist_esm_focus-zone_js-03bcda509ec9.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_selector-observer_dist_index_esm_js-9f960d9b217c.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_auto-complete-element_dist_index_js-node_modules_github_details-d-ed9a97-dfdebffa4a55.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_text-expander-element_dist_index_js-19ce25503c82.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_mini-throttle_dist_index_js-node_modules_delegated-events_dist_in-af795d-767d6f041dd5.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_filter-input-element_dist_index_js-node_modules_github_remote-inp-b7d8f4-6e6f83bcc978.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_catalyst_lib_index_js-node_modules_github_clipboard-copy-element_-782ca5-14181f295dc0.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_file-attachment-element_dist_index_js-node_modules_primer_view-co-15cdfa-fdfdefb25b02.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/app_assets_modules_github_onfocus_ts-ui_packages_trusted-types-policies_policy_ts-ui_packages-6fe316-88898a485083.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/github-elements-41a524cdc4d4.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/element-registry-c121cf6e8178.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_github_catalyst_lib_index_js-node_modules_github_hydro-analytics-client_-7901e7-f8af173502c4.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_braintree_browser-detection_dist_browser-detection_js-node_modules_stack-68835d-59206c834a41.js"></script>
<script crossorigin="anonymous" defer="defer" type="application/javascript" src="https://github.githubassets.com/assets/vendors-node_modules_lit-html_lit-html_js-cc7cb714ead5.js"></script>

Functionality Includes

  • Create vector index

  • Populate nodes and vector index from documents

  • Query vector index

Documentation

The Neo4j Vector integration documentation is avalable in the Spring AI Reference Guide.

Starter Kit

Getting started in any new technology space can feel intimidating. Neo4j has been working on some pre-packaged solutions for GenAI and Neo4j to hopefully make the process easier by providing starter kit projects with a few key technologies, including Spring AI. You can find the Spring AI starter kit code on Github, as well as a [blog post with more information.