You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This functional interface is designed for use cases where you only need to retrieve documents from a vector store without performing any mutation operations. It follows the principle of least privilege by exposing only the necessary functionality for document retrieval.
44
45
45
46
=== VectorStore Interface
46
47
47
48
The `VectorStore` interface extends `VectorStoreRetriever` and adds mutation capabilities:
48
49
49
-
```java
50
+
[source,java]
51
+
----
50
52
public interface VectorStore extends DocumentWriter, VectorStoreRetriever {
The `VectorStore` interface combines both read and write operations, allowing you to add, delete, and search for documents in a vector database.
71
73
72
74
=== SearchRequest Builder
73
75
74
-
```java
76
+
[source,java]
77
+
----
75
78
public class SearchRequest {
76
79
77
80
public static final double SIMILARITY_THRESHOLD_ACCEPT_ALL = 0.0;
@@ -144,7 +147,7 @@ public class SearchRequest {
144
147
public Filter.Expression getFilterExpression() {...}
145
148
}
146
149
147
-
```
150
+
----
148
151
149
152
To insert data into the vector database, encapsulate it within a `Document` object.
150
153
The `Document` class encapsulates content from a data source, such as a PDF or Word document, and includes text represented as a string.
@@ -417,7 +420,8 @@ The general usage of loading data into a vector store is something you would do
417
420
Given a `String` reference to a source file that represents a JSON file with data we want to load into the vector database, we use Spring AI's `JsonReader` to load specific fields in the JSON, which splits them up into small pieces and then passes those small pieces to the vector store implementation.
418
421
The `VectorStore` implementation computes the embeddings and stores the JSON and the embedding in the vector database:
Later, when a user question is passed into the AI model, a similarity search is done to retrieve similar documents, which are then "stuffed" into the prompt as context for the user's question.
435
439
436
440
For read-only operations, you can use either the `VectorStore` interface or the more focused `VectorStoreRetriever` interface:
437
441
438
-
```java
442
+
[source,java]
443
+
----
439
444
@Autowired
440
445
VectorStoreRetriever retriever; // Could also use VectorStore here
Additional options can be passed into the `similaritySearch` method to define how many documents to retrieve and a threshold of the similarity search.
456
461
457
462
=== Separation of Read and Write Operations
458
463
459
464
Using the separate interfaces allows you to clearly define which components need write access and which only need read access:
460
465
461
-
```java
466
+
[source,java]
467
+
----
462
468
// Write operations in a service that needs full access
463
469
@Service
464
470
class DocumentIndexer {
@@ -486,7 +492,7 @@ class DocumentRetriever {
486
492
return retriever.similaritySearch(query);
487
493
}
488
494
}
489
-
```
495
+
----
490
496
491
497
This separation of concerns helps create more maintainable and secure applications by limiting access to mutation operations only to components that truly need them.
492
498
@@ -505,7 +511,8 @@ The `VectorStoreRetriever` interface provides a read-only view of a vector store
505
511
506
512
You can use `VectorStoreRetriever` directly when you only need to perform similarity searches:
507
513
508
-
```java
514
+
[source,java]
515
+
----
509
516
@Service
510
517
public class DocumentRetrievalService {
511
518
@@ -529,15 +536,16 @@ public class DocumentRetrievalService {
529
536
return retriever.similaritySearch(request);
530
537
}
531
538
}
532
-
```
539
+
----
533
540
534
541
In this example, the service only depends on the `VectorStoreRetriever` interface, making it clear that it only performs retrieval operations and doesn't modify the vector store.
535
542
536
543
=== Integration with RAG Applications
537
544
538
545
The `VectorStoreRetriever` interface is particularly useful in RAG applications, where you need to retrieve relevant documents to provide context for an AI model:
539
546
540
-
```java
547
+
[source,java]
548
+
----
541
549
@Service
542
550
public class RagService {
543
551
@@ -563,7 +571,7 @@ public class RagService {
563
571
return chatModel.generate(prompt);
564
572
}
565
573
}
566
-
```
574
+
----
567
575
568
576
This pattern allows for a clean separation between the retrieval component and the generation component in RAG applications.
0 commit comments