Updated several vector db docs to fix errors and to expand on instructions, making it easier to follow and complete successfully: Azure AI Search, Neo4j, and Postgres/PGvector
This commit is contained in:
committed by
Christian Tzolov
parent
dc86957a07
commit
214858bec5
@@ -43,47 +43,47 @@ Add these dependencies to your project:
|
||||
|
||||
* OpenAI Embedding:
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
* Or Azure AI Embedding:
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-azure-openai-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
* Or Local Sentence Transformers Embedding:
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-transformers-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-transformers-spring-boot-starter</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
2. Azure (AI Search) Vector Store
|
||||
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-azure-vector-store</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
[source,xml]
|
||||
----
|
||||
<dependency>
|
||||
<groupId>org.springframework.ai</groupId>
|
||||
<artifactId>spring-ai-azure-vector-store</artifactId>
|
||||
<version>0.8.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
----
|
||||
|
||||
== Sample Code
|
||||
|
||||
|
||||
@@ -15,14 +15,14 @@ link:https://neo4j.com/docs/cypher-manual/current/indexes-for-vector-search/[Neo
|
||||
1. OpenAI Account: Create an account at link:https://platform.openai.com/signup[OpenAI Signup] and generate the token at link:https://platform.openai.com/account/api-keys[API Keys].
|
||||
|
||||
2. A running Neo4j (5.13+) instance
|
||||
a. link:https://hub.docker.com/_/neo4j[Docker] image _neo4j:5.13_
|
||||
a. link:https://hub.docker.com/_/neo4j[Docker] image _neo4j:5.15_
|
||||
b. link:https://neo4j.com/download/[Neo4j Desktop]
|
||||
c. link:https://neo4j.com/cloud/aura-free/[Neo4j Aura]
|
||||
d. link:https://neo4j.com/deployment-center/[Neo4j Server] instance
|
||||
|
||||
== Configuration
|
||||
|
||||
To connect to Neo4j and use the `Neo4jVectorStore`, you need to provide (e.g. via `application.properties`) configurations for your instance.
|
||||
To connect to Neo4j and use the `Neo4jVectorStore`, you need to provide (e.g. via `application.properties`, environment variables, etc.) configurations for your instance.
|
||||
|
||||
Additionally, you'll need to provide your OpenAI API Key. Set it as an environment variable like so:
|
||||
|
||||
@@ -77,5 +77,38 @@ Add these dependencies to your project:
|
||||
|
||||
To configure `Neo4jVectorStore` in your application, you can use the following setup:
|
||||
|
||||
Add to `application.properties` (using your Neo4j credentials):
|
||||
Add to your environment (using your own Neo4j credentials and the appropriate access protocol+endpoint) the following properties either by updating and executing the following commands or creating a shell script to be run from your command prompt (Linux/Mac/WSL2):
|
||||
|
||||
[source,bash]
|
||||
----
|
||||
export SPRING_NEO4J_URI=<uri_for_your_neo4j_instance>
|
||||
export SPRING_NEO4J_AUTHENTICATION_USERNAME=<your_username>
|
||||
export SPRING_NEO4J_AUTHENTICATION_PASSWORD=<your_password>
|
||||
----
|
||||
|
||||
NOTE: If you choose to create a shell script for ease in future work, be sure to run it prior to starting your application by "sourcing" the file, i.e. `source <your_script_name>.sh`.
|
||||
|
||||
You'll need a `VectorStore` to store the embeddings. You can use the `Neo4jVectorStore` for this purpose, but first, you must create two beans the `Neo4jVectorStore` constructor requires. Here are examples of all of the beans you'll need:
|
||||
|
||||
[source,java]
|
||||
----
|
||||
@Bean
|
||||
public Driver driver() {
|
||||
return GraphDatabase.driver(System.getenv("SPRING_NEO4J_URI"),
|
||||
AuthTokens.basic(System.getenv("SPRING_NEO4J_AUTHENTICATION_USERNAME"),
|
||||
System.getenv("SPRING_NEO4J_AUTHENTICATION_PASSWORD")));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public EmbeddingClient embeddingClient() {
|
||||
return new OpenAiEmbeddingClient(new OpenAiApi(System.getenv("SPRING_AI_OPENAI_API_KEY")));
|
||||
}
|
||||
|
||||
@Bean
|
||||
public VectorStore vectorStore(Driver driver, EmbeddingClient embeddingClient) {
|
||||
return new Neo4jVectorStore(driver, embeddingClient,
|
||||
Neo4jVectorStore.Neo4jVectorStoreConfig.defaultConfig());
|
||||
}
|
||||
----
|
||||
|
||||
The `Neo4jVectorStore` is now ready to be used in your application. You can use it to store embeddings and perform similarity searches.
|
||||
|
||||
@@ -18,18 +18,18 @@ On startup, the `PgVectorStore` will attempt to install the required database ex
|
||||
|
||||
[sql]
|
||||
----
|
||||
CREATE EXTENSION IF NOT EXISTS vector
|
||||
CREATE EXTENSION IF NOT EXISTS hstore
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"
|
||||
CREATE EXTENSION IF NOT EXISTS vector;
|
||||
CREATE EXTENSION IF NOT EXISTS hstore;
|
||||
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
|
||||
|
||||
CREATE TABLE IF NOT EXISTS vector_store (
|
||||
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
||||
content text,
|
||||
metadata json,
|
||||
embedding vector(1536)
|
||||
)
|
||||
);
|
||||
|
||||
CREATE INDEX ON vector_store USING HNSW (embedding vector_cosine_ops)
|
||||
CREATE INDEX ON vector_store USING HNSW (embedding vector_cosine_ops);
|
||||
----
|
||||
|
||||
== Configuration
|
||||
@@ -111,7 +111,7 @@ Add to `application.yml` (using your DB credentials):
|
||||
----
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:postgresql://localhost:5432/vector_store
|
||||
url: jdbc:postgresql://localhost:5432/postgres
|
||||
username: postgres
|
||||
password: postgres
|
||||
----
|
||||
|
||||
Reference in New Issue
Block a user