Skip to content

Commit d7977aa

Browse files
ngocnhan-tran1996ericbottard
authored andcommitted
Convert to ascii doc
Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
1 parent b2dd1c9 commit d7977aa

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/vectordbs.adoc

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Spring AI offers an abstracted API for interacting with vector databases through
2828

2929
Spring AI provides a read-only interface called `VectorStoreRetriever` that exposes only the document retrieval functionality:
3030

31-
```java
31+
[source,java]
32+
----
3233
@FunctionalInterface
3334
public interface VectorStoreRetriever {
3435
@@ -38,15 +39,16 @@ public interface VectorStoreRetriever {
3839
return this.similaritySearch(SearchRequest.builder().query(query).build());
3940
}
4041
}
41-
```
42+
----
4243

4344
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.
4445

4546
=== VectorStore Interface
4647

4748
The `VectorStore` interface extends `VectorStoreRetriever` and adds mutation capabilities:
4849

49-
```java
50+
[source,java]
51+
----
5052
public interface VectorStore extends DocumentWriter, VectorStoreRetriever {
5153
5254
default String getName() {
@@ -65,13 +67,14 @@ public interface VectorStore extends DocumentWriter, VectorStoreRetriever {
6567
return Optional.empty();
6668
}
6769
}
68-
```
70+
----
6971

7072
The `VectorStore` interface combines both read and write operations, allowing you to add, delete, and search for documents in a vector database.
7173

7274
=== SearchRequest Builder
7375

74-
```java
76+
[source,java]
77+
----
7578
public class SearchRequest {
7679
7780
public static final double SIMILARITY_THRESHOLD_ACCEPT_ALL = 0.0;
@@ -144,7 +147,7 @@ public class SearchRequest {
144147
public Filter.Expression getFilterExpression() {...}
145148
}
146149
147-
```
150+
----
148151

149152
To insert data into the vector database, encapsulate it within a `Document` object.
150153
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
417420
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.
418421
The `VectorStore` implementation computes the embeddings and stores the JSON and the embedding in the vector database:
419422

420-
```java
423+
[source,java]
424+
----
421425
@Autowired
422426
VectorStore vectorStore;
423427
@@ -427,15 +431,16 @@ void load(String sourceFile) {
427431
List<Document> documents = jsonReader.get();
428432
this.vectorStore.add(documents);
429433
}
430-
```
434+
----
431435

432436
=== Reading from a Vector Store
433437

434438
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.
435439

436440
For read-only operations, you can use either the `VectorStore` interface or the more focused `VectorStoreRetriever` interface:
437441

438-
```java
442+
[source,java]
443+
----
439444
@Autowired
440445
VectorStoreRetriever retriever; // Could also use VectorStore here
441446
@@ -450,15 +455,16 @@ SearchRequest request = SearchRequest.builder()
450455
.build();
451456
452457
List<Document> filteredDocuments = retriever.similaritySearch(request);
453-
```
458+
----
454459

455460
Additional options can be passed into the `similaritySearch` method to define how many documents to retrieve and a threshold of the similarity search.
456461

457462
=== Separation of Read and Write Operations
458463

459464
Using the separate interfaces allows you to clearly define which components need write access and which only need read access:
460465

461-
```java
466+
[source,java]
467+
----
462468
// Write operations in a service that needs full access
463469
@Service
464470
class DocumentIndexer {
@@ -486,7 +492,7 @@ class DocumentRetriever {
486492
return retriever.similaritySearch(query);
487493
}
488494
}
489-
```
495+
----
490496

491497
This separation of concerns helps create more maintainable and secure applications by limiting access to mutation operations only to components that truly need them.
492498

@@ -505,7 +511,8 @@ The `VectorStoreRetriever` interface provides a read-only view of a vector store
505511

506512
You can use `VectorStoreRetriever` directly when you only need to perform similarity searches:
507513

508-
```java
514+
[source,java]
515+
----
509516
@Service
510517
public class DocumentRetrievalService {
511518
@@ -529,15 +536,16 @@ public class DocumentRetrievalService {
529536
return retriever.similaritySearch(request);
530537
}
531538
}
532-
```
539+
----
533540

534541
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.
535542

536543
=== Integration with RAG Applications
537544

538545
The `VectorStoreRetriever` interface is particularly useful in RAG applications, where you need to retrieve relevant documents to provide context for an AI model:
539546

540-
```java
547+
[source,java]
548+
----
541549
@Service
542550
public class RagService {
543551
@@ -563,7 +571,7 @@ public class RagService {
563571
return chatModel.generate(prompt);
564572
}
565573
}
566-
```
574+
----
567575

568576
This pattern allows for a clean separation between the retrieval component and the generation component in RAG applications.
569577

0 commit comments

Comments
 (0)