Skip to content

Commit

Permalink
Add pgvector support for spring-ai-chat
Browse files Browse the repository at this point in the history
  • Loading branch information
trisberg committed Dec 3, 2024
1 parent 69720a2 commit 8860b3f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 7 deletions.
53 changes: 49 additions & 4 deletions spring-ai-chat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,30 @@ In order to compile the production code:
./mvnw clean compile
```

<!--- #IF(#vectorStoreType == 'simple') -->

After that it is a good habit to compile the test classes and execute those tests to see if your application is still behaving as you would expect:

```sh
./mvnw verify
```
<!--- #ELSE -->
### Using PostgreSQL/pgvector

If you chose PostgreSQL/pgvector as your vector store, you'll need to run a PostgresSQL database instance before running the application. The PostgreSQL database needs to have the "vector" extension available.

You can use this command to start a PostgreSQL container that includes the "vector" extension:

```sh
docker run --rm --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password -d pgvector/pgvector:pg17
```

After that it is a good habit to compile the test classes and execute those tests to see if your application is still behaving as you would expect:

```sh
./mvnw -Dspring.profiles.active=pgvector verify
```
<!--- #ENDIF -->

## Start and interact
Spring Boot has its own integrated Web Server (Apache Tomcat (https://tomcat.apache.org/)).
Expand All @@ -35,19 +54,35 @@ Set the `AI_API_KEY` environment variable with the API key to be used by your ap
export AI_API_KEY='<your-api-key>'
```

Launch application using default profile:
<!--- #IF(#vectorStoreType == 'simple') -->
### Using embedded simple store

Launch application using the default profile:

```sh
./mvnw spring-boot:run
```
<!--- #ELSE -->
### Using PostgreSQL/pgvector

### Accessing home page
If you chose PostgreSQL/pgvector as your vector store, you'll need to run a PostgresSQL database instance before running the application. The PostgreSQL database needs to have the "vector" extension available.

You can use this command to start a PostgreSQL container that includes the "vector" extension:

```sh
docker run --rm --name postgres -p 5432:5432 -e POSTGRES_PASSWORD=password -d pgvector/pgvector:pg17
```

You can access the public page at `http://localhost:8080/` by a web browser or using `curl`:
Run the application using the pgvector profile:

```sh
curl -L -H 'Content-Type: application/html' http://localhost:8080/
./mvnw -Dspring-boot.run.profiles=pgvector spring-boot:run
```
<!--- #ENDIF -->

### Accessing home page

You can access the public page at http://localhost:8080/ via a web browser.

You'll be presented with a login page. You may login with either of the following sets
of credentials:
Expand Down Expand Up @@ -103,6 +138,16 @@ You can set an environment variable for the app using this command:
tanzu app env set spring-ai-chat AI_API_KEY=<your-api-key>
```

<!--- #IF(#vectorStoreType == 'simple') -->

### PostgreSQL/pgvector

If you chose PostgreSQL/pgvector as your vector store, you'll need to create
a PostgresSQL database instance before deploying the application. The PostgreSQL database needs to have the "vector" extension available.

Instructions TBD.

<!--- #ENDIF -->
### Scale the number of instances

Run this command to scale to 1 instance
Expand Down
9 changes: 6 additions & 3 deletions spring-ai-chat/accelerator.axl
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
engine {

applyTo("pom.xml" || "src/main/resources/application.properties") {
applyTo("README.md" || "pom.xml" || "src/main/resources/application.properties") {
TextPreprocessor()
}

if (#vectorStoreType != 'simple') {
Exclude({"**/SimpleVectorStoreConfig.java"})
if (#vectorStoreType == 'simple') {
Exclude({"**/schema-pgvector.sql", "**/application-pgvector.properties"})
}
else {
Exclude({"**/SimpleVectorStoreConfig.java"})
}

applyTo("pom.xml") {
Expand Down
10 changes: 10 additions & 0 deletions spring-ai-chat/src/main/resources/application-pgvector.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
spring.ai.vectorstore.pgvector.index-type=ivfflat
spring.ai.vectorstore.pgvector.distance-type=euclidean_distance

spring.datasource.url=jdbc:postgresql://localhost/postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.username=postgres
spring.datasource.password=password
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.sql.init.mode=always
spring.sql.init.platform=pgvector
10 changes: 10 additions & 0 deletions spring-ai-chat/src/main/resources/schema-pgvector.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
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);

0 comments on commit 8860b3f

Please sign in to comment.