|
1 | 1 | = GemFire Vector Store
|
2 | 2 |
|
3 |
| -This section walks you through setting up the GemFire VectorStore to store document embeddings and perform similarity searches. |
| 3 | +This section walks you through setting up the `GemFireVectorStore` to store document embeddings and perform similarity searches. |
4 | 4 |
|
5 |
| -link:https://tanzu.vmware.com/gemfire[GemFire] is an ultra high speed in-memory data and compute grid, with vector extensions to store and search vectors efficiently. |
| 5 | +link:https://tanzu.vmware.com/gemfire[GemFire] is a distributed, in-memory, key-value store performing read and write operations at blazingly fast speeds. It offers highly available parallel message queues, continuous availability, and an event-driven architecture you can scale dynamically without downtime. As your data size requirements increase to support high-performance, real-time apps, GemFire can easily scale linearly. |
6 | 6 |
|
7 |
| -link:https://docs.vmware.com/en/VMware-GemFire-VectorDB/1.0/gemfire-vectordb/overview.html[GemFire VectorDB] extends GemFire's capabilities, serving as a versatile vector database that efficiently stores, retrieves, and performs vector searches through a distributed and resilient infrastructure: |
8 |
| - |
9 |
| -Capabilities: |
10 |
| -- Create Indexes |
11 |
| -- Store vectors and the associated metadata |
12 |
| -- Perform vector searches based on similarity |
| 7 | +link:https://docs.vmware.com/en/VMware-GemFire-VectorDB/1.0/gemfire-vectordb/overview.html[GemFire VectorDB] extends GemFire's capabilities, serving as a versatile vector database that efficiently stores, retrieves, and performs vector similarity searches. |
13 | 8 |
|
14 | 9 | == Prerequisites
|
15 | 10 |
|
16 |
| -Access to a GemFire cluster with the link:https://docs.vmware.com/en/VMware-GemFire-VectorDB/1.0/gemfire-vectordb/install.html[GemFire Vector Database] extension installed. |
17 |
| -You can download the GemFire VectorDB extension from the link:https://network.pivotal.io/products/gemfire-vectordb/[VMware Tanzu Network] after signing in. |
| 11 | +1. A GemFire cluster with the GemFire VectorDB extension enabled |
| 12 | +- link:https://docs.vmware.com/en/VMware-GemFire-VectorDB/1.0/gemfire-vectordb/install.html[Install GemFire VectorDB extension] |
18 | 13 |
|
19 |
| -== Dependencies |
| 14 | +2. An `EmbeddingModel` bean to compute the document embeddings. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] section for more information. |
| 15 | +An option that runs locally on your machine is xref:api/embeddings/onnx.adoc[ONNX] and the all-MiniLM-L6-v2 Sentence Transformers. |
20 | 16 |
|
21 |
| -Add these dependencies to your project: |
| 17 | +== Auto-configuration |
22 | 18 |
|
23 |
| -- Embedding Model boot starter, required for calculating embeddings. |
24 |
| -- Transformers Embedding (Local) and follow the ONNX Transformers Embedding instructions. |
| 19 | +Add the GemFire VectorStore Spring Boot starter to you project's Maven build file `pom.xml`: |
25 | 20 |
|
26 |
| -[source,xml] |
| 21 | +[source, xml] |
27 | 22 | ----
|
28 | 23 | <dependency>
|
29 |
| - <groupId>org.springframework.ai</groupId> |
30 |
| - <artifactId>spring-ai-transformers</artifactId> |
| 24 | + <groupId>org.springframework.ai</groupId> |
| 25 | + <artifactId>spring-ai-gemfire-store-spring-boot-starter</artifactId> |
31 | 26 | </dependency>
|
32 | 27 | ----
|
33 | 28 |
|
34 |
| -- Add the GemFire VectorDB dependencies |
| 29 | +or to your Gradle `build.gradle` file |
| 30 | + |
| 31 | +[source, xml] |
| 32 | +---- |
| 33 | +dependencies { |
| 34 | + implementation 'org.springframework.ai:spring-ai-gemfire-store-spring-boot-starter' |
| 35 | +} |
| 36 | +---- |
| 37 | + |
| 38 | +=== Configuration properties |
| 39 | + |
| 40 | +You can use the following properties in your Spring Boot configuration to further configure the `GemFireVectorStore`. |
| 41 | + |
| 42 | +|=== |
| 43 | +|Property|Default value |
| 44 | + |
| 45 | +|`spring.ai.vectorstore.gemfire.host`|localhost |
| 46 | +|`spring.ai.vectorstore.gemfire.port`|8080 |
| 47 | +|`spring.ai.vectorstore.gemfire.index-name`|spring-ai-gemfire-store |
| 48 | +|`spring.ai.vectorstore.gemfire.beam-width`|100 |
| 49 | +|`spring.ai.vectorstore.gemfire.max-connections`|16 |
| 50 | +|`spring.ai.vectorstore.gemfire.vector-similarity-function`|COSINE |
| 51 | +|`spring.ai.vectorstore.gemfire.fields`|[] |
| 52 | +|`spring.ai.vectorstore.gemfire.buckets`|0 |
| 53 | +|=== |
| 54 | + |
| 55 | + |
| 56 | +== Manual Configuration |
35 | 57 |
|
36 |
| -[source,xml] |
| 58 | +To use just the `GemFireVectorStore`, without Spring Boot's Auto-configuration add the following dependency to your project’s Maven `pom.xml`: |
| 59 | + |
| 60 | +[source, xml] |
37 | 61 | ----
|
38 | 62 | <dependency>
|
39 | 63 | <groupId>org.springframework.ai</groupId>
|
40 | 64 | <artifactId>spring-ai-gemfire-store</artifactId>
|
41 | 65 | </dependency>
|
42 | 66 | ----
|
43 | 67 |
|
| 68 | +For Gradle users, add the following to your `build.gradle` file under the dependencies block to use just the `GemFireVectorStore`: |
44 | 69 |
|
45 |
| -TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. |
46 |
| - |
| 70 | +[souce, xml] |
| 71 | +---- |
| 72 | +dependencies { |
| 73 | + implementation 'org.springframework.ai:spring-ai-gemfire-store' |
| 74 | +} |
| 75 | +---- |
47 | 76 |
|
48 |
| -== Sample Code |
| 77 | +== Usage |
49 | 78 |
|
50 |
| -- To configure GemFire in your application, use the following setup: |
| 79 | +Here is a sample that creates an instance of the `GemfireVectorStore` instead of using AutoConfiguration |
51 | 80 |
|
52 | 81 | [source,java]
|
53 | 82 | ----
|
54 | 83 | @Bean
|
55 |
| -public GemFireVectorStoreConfig gemFireVectorStoreConfig() { |
56 |
| - return GemFireVectorStoreConfig.builder() |
57 |
| - .withUrl("http://localhost:8080") |
58 |
| - .withIndexName("spring-ai-test-index") |
59 |
| - .build(); |
| 84 | +public VectorStore vectorStore(EmbeddingModel embeddingModel) { |
| 85 | + return new GemFireVectorStore(new GemFireVectorStoreConfig() |
| 86 | + .setIndexName("my-vector-index") |
| 87 | + .setPort(7071), embeddingClient); |
60 | 88 | }
|
61 | 89 | ----
|
62 | 90 |
|
63 |
| -- Create a GemFireVectorStore instance connected to your GemFire VectorDB: |
| 91 | +[NOTE] |
| 92 | +==== |
| 93 | +The GemFire VectorStore does not yet support xref:api/vectordbs.adoc#metadata-filters[metadata filters]. |
| 94 | +==== |
| 95 | + |
| 96 | +[NOTE] |
| 97 | +==== |
| 98 | +The default configuration connects to a GemFire cluster at `localhost:8080` |
| 99 | +==== |
| 100 | + |
| 101 | +- In your application, create a few documents: |
64 | 102 |
|
65 | 103 | [source,java]
|
66 | 104 | ----
|
67 |
| -@Bean |
68 |
| -public VectorStore vectorStore(GemFireVectorStoreConfig config, EmbeddingModel embeddingModel) { |
69 |
| - return new GemFireVectorStore(config, embeddingModel); |
70 |
| -} |
| 105 | +List<Document> documents = List.of( |
| 106 | + new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "UK", "year", 2020)), |
| 107 | + new Document("The World is Big and Salvation Lurks Around the Corner", Map.of()), |
| 108 | + new Document("You walk forward facing the past and you turn back toward the future.", Map.of("country", "NL", "year", 2023))); |
71 | 109 | ----
|
72 |
| -- Create a Vector Index which will configure GemFire region. |
73 | 110 |
|
74 |
| -[source,java] |
75 |
| ----- |
76 |
| - public void createIndex() { |
77 |
| - try { |
78 |
| - CreateRequest createRequest = new CreateRequest(); |
79 |
| - createRequest.setName(INDEX_NAME); |
80 |
| - createRequest.setBeamWidth(20); |
81 |
| - createRequest.setMaxConnections(16); |
82 |
| - ObjectMapper objectMapper = new ObjectMapper(); |
83 |
| - String index = objectMapper.writeValueAsString(createRequest); |
84 |
| - client.post() |
85 |
| - .contentType(MediaType.APPLICATION_JSON) |
86 |
| - .bodyValue(index) |
87 |
| - .retrieve() |
88 |
| - .bodyToMono(Void.class) |
89 |
| - .block(); |
90 |
| - } |
91 |
| - catch (Exception e) { |
92 |
| - logger.warn("An unexpected error occurred while creating the index"); |
93 |
| - } |
94 |
| - } |
95 |
| ----- |
96 |
| - |
97 |
| -- Create some documents: |
| 111 | +- Add the documents to the vector store: |
98 | 112 |
|
99 | 113 | [source,java]
|
100 | 114 | ----
|
101 |
| - List<Document> documents = List.of( |
102 |
| - new Document("1", getText("classpath:/test/data/spring.ai.txt"), Map.of("meta1", "meta1")), |
103 |
| - new Document("2", getText("classpath:/test/data/time.shelter.txt"), Map.of()), |
104 |
| - new Document("3", getText("classpath:/test/data/great.depression.txt"), Map.of("meta2", "meta2"))); |
| 115 | +vectorStore.add(documents); |
105 | 116 | ----
|
106 | 117 |
|
107 |
| -- Add the documents to GemFire VectorDB: |
| 118 | +- And to retrieve documents using similarity search: |
108 | 119 |
|
109 | 120 | [source,java]
|
110 | 121 | ----
|
111 |
| -vectorStore.add(List.of(document)); |
| 122 | +List<Document> results = vectorStore.similaritySearch( |
| 123 | + SearchRequest.query("Spring").withTopK(5)); |
112 | 124 | ----
|
113 | 125 |
|
114 |
| -- And finally, retrieve documents similar to a query: |
| 126 | +You should retrieve the document containing the text "Spring AI rocks!!". |
115 | 127 |
|
| 128 | +You can also limit the number of results using a similarity threshold: |
116 | 129 | [source,java]
|
117 | 130 | ----
|
118 |
| - List<Document> results = vectorStore.similaritySearch("Spring", 5); |
| 131 | +List<Document> results = vectorStore.similaritySearch( |
| 132 | + SearchRequest.query("Spring").withTopK(5) |
| 133 | + .withSimilarityThreshold(0.5d)); |
119 | 134 | ----
|
120 | 135 |
|
121 |
| -If all goes well, you should retrieve the document containing the text "Spring AI rocks!!". |
122 |
| - |
|
0 commit comments