|
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 that performs 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 with no downtime. As your data size requirements increase to support high-performance, real-time apps, GemFire can scale linearly with ease. |
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] |
| 13 | + |
| 14 | +2. An `EmbeddingModel` bean to compute the document embeddings. Refer to the xref:api/embeddings.adoc#available-implementations[EmbeddingModel] section for more information. |
18 | 15 |
|
19 | 16 | == Dependencies |
20 | 17 |
|
21 | | -Add these dependencies to your project: |
| 18 | +=== Maven |
22 | 19 |
|
23 | | -- Embedding Client boot starter, required for calculating embeddings. |
24 | | -- Transformers Embedding (Local) and follow the ONNX Transformers Embedding instructions. |
| 20 | +To use just the `GemFireVectorStore`, add the following dependency to your project’s Maven `pom.xml`: |
25 | 21 |
|
26 | | -[source,xml] |
| 22 | +[source, xml] |
27 | 23 | ---- |
28 | 24 | <dependency> |
29 | | - <groupId>org.springframework.ai</groupId> |
30 | | - <artifactId>spring-ai-transformers</artifactId> |
| 25 | + <groupId>org.springframework.ai</groupId> |
| 26 | + <artifactId>spring-ai-gemfire-store</artifactId> |
31 | 27 | </dependency> |
32 | 28 | ---- |
33 | 29 |
|
34 | | -- Add the GemFire VectorDB dependencies |
| 30 | +To enable Spring Boot’s Auto-Configuration for the `GemFireVectorStore`, also add the following to your project’s Maven `pom.xml`: |
35 | 31 |
|
36 | | -[source,xml] |
| 32 | +[source, xml] |
37 | 33 | ---- |
38 | 34 | <dependency> |
39 | 35 | <groupId>org.springframework.ai</groupId> |
40 | | - <artifactId>spring-ai-gemfire-store</artifactId> |
| 36 | + <artifactId>spring-ai-gemfire-store-spring-boot-starter</artifactId> |
41 | 37 | </dependency> |
| 38 | +
|
42 | 39 | ---- |
43 | 40 |
|
44 | 41 |
|
45 | | -TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. |
| 42 | +=== Gradle |
46 | 43 |
|
| 44 | +For Gradle users, add the following to your `build.gradle` file under the dependencies block to use just the `GemFireVectorStore`: |
47 | 45 |
|
48 | | -== Sample Code |
| 46 | +[souce, xml] |
| 47 | +---- |
| 48 | +dependencies { |
| 49 | + implementation 'org.springframework.ai:spring-ai-gemfire-store' |
| 50 | +} |
| 51 | +---- |
49 | 52 |
|
50 | | -- To configure GemFire in your application, use the following setup: |
| 53 | +To enable Spring Boot’s Auto-Configuration for the `GemFireVectorStore`, also include this dependency: |
51 | 54 |
|
52 | | -[source,java] |
| 55 | +[source, xml] |
53 | 56 | ---- |
54 | | -@Bean |
55 | | -public GemFireVectorStoreConfig gemFireVectorStoreConfig() { |
56 | | - return GemFireVectorStoreConfig.builder() |
57 | | - .withUrl("http://localhost:8080") |
58 | | - .withIndexName("spring-ai-test-index") |
59 | | - .build(); |
| 57 | +dependencies { |
| 58 | + implementation 'org.springframework.ai:spring-ai-gemfire-store-spring-boot-starter' |
60 | 59 | } |
61 | 60 | ---- |
62 | 61 |
|
63 | | -- Create a GemFireVectorStore instance connected to your GemFire VectorDB: |
| 62 | +== Usage |
| 63 | + |
| 64 | +- Create a `GemFireVectorStore` instance connected to the GemFire cluster: |
64 | 65 |
|
65 | 66 | [source,java] |
66 | 67 | ---- |
67 | 68 | @Bean |
68 | | -public VectorStore vectorStore(GemFireVectorStoreConfig config, EmbeddingModel embeddingModel) { |
69 | | - return new GemFireVectorStore(config, embeddingModel); |
| 69 | +public VectorStore vectorStore(EmbeddingModel embeddingModel) { |
| 70 | + return new GemFireVectorStore(new GemFireVectorStoreConfig() |
| 71 | + .setIndexName("my-vector-index") |
| 72 | + .setPort(7071), embeddingClient); |
70 | 73 | } |
71 | 74 | ---- |
72 | | -- Create a Vector Index which will configure GemFire region. |
| 75 | + |
| 76 | +[NOTE] |
| 77 | +==== |
| 78 | +The default configuration connects to a GemFire cluster at `localhost:8080` |
| 79 | +==== |
| 80 | + |
| 81 | +- In your application, create a few documents: |
73 | 82 |
|
74 | 83 | [source,java] |
75 | 84 | ---- |
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: |
| 85 | +List<Document> documents = List.of( |
| 86 | + new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("country", "UK", "year", 2020)), |
| 87 | + new Document("The World is Big and Salvation Lurks Around the Corner", Map.of()), |
| 88 | + new Document("You walk forward facing the past and you turn back toward the future.", Map.of("country", "NL", "year", 2023))); |
| 89 | +---- |
| 90 | + |
| 91 | +- Add the documents to the vector store: |
98 | 92 |
|
99 | 93 | [source,java] |
100 | 94 | ---- |
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"))); |
| 95 | +vectorStore.add(documents); |
105 | 96 | ---- |
106 | 97 |
|
107 | | -- Add the documents to GemFire VectorDB: |
| 98 | +- And to retrieve documents using similarity search: |
108 | 99 |
|
109 | 100 | [source,java] |
110 | 101 | ---- |
111 | | -vectorStore.add(List.of(document)); |
| 102 | +List<Document> results = vectorStore.similaritySearch( |
| 103 | + SearchRequest.query("Spring").withTopK(5)); |
112 | 104 | ---- |
113 | 105 |
|
114 | | -- And finally, retrieve documents similar to a query: |
| 106 | +You should retrieve the document containing the text "Spring AI rocks!!". |
115 | 107 |
|
| 108 | +You can also limit the number of results using a similarity threshold: |
116 | 109 | [source,java] |
117 | 110 | ---- |
118 | | - List<Document> results = vectorStore.similaritySearch("Spring", 5); |
| 111 | +List<Document> results = vectorStore.similaritySearch( |
| 112 | + SearchRequest.query("Spring").withTopK(5) |
| 113 | + .withSimilarityThreshold(0.5d)); |
119 | 114 | ---- |
120 | 115 |
|
121 | | -If all goes well, you should retrieve the document containing the text "Spring AI rocks!!". |
| 116 | +== GemFireVectorStore properties |
| 117 | + |
| 118 | +You can use the following properties in your Spring Boot configuration to further configure the `GemFireVectorStore`. |
| 119 | + |
| 120 | +|=== |
| 121 | +|Property|Default value |
122 | 122 |
|
| 123 | +|`spring.ai.vectorstore.gemfire.host`|localhost |
| 124 | +|`spring.ai.vectorstore.gemfire.port`|8080 |
| 125 | +|`spring.ai.vectorstore.gemfire.index-name`|spring-ai-gemfire-store |
| 126 | +|`spring.ai.vectorstore.gemfire.beam-width`|100 |
| 127 | +|`spring.ai.vectorstore.gemfire.max-connections`|16 |
| 128 | +|`spring.ai.vectorstore.gemfire.vector-similarity-function`|COSINE |
| 129 | +|`spring.ai.vectorstore.gemfire.fields`|[] |
| 130 | +|`spring.ai.vectorstore.gemfire.buckets`|0 |
| 131 | +|=== |
0 commit comments