Skip to content

Commit 0334e6d

Browse files
committed
Improve vectordbs documentation
1 parent 8f4d840 commit 0334e6d

File tree

4 files changed

+114
-110
lines changed

4 files changed

+114
-110
lines changed

spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626
*** xref:api/clients/vertexai-chat.adoc[]
2727
** xref:api/prompt.adoc[]
2828
** xref:api/output-parser.adoc[]
29-
** xref:api/etl-pipeline.adoc[]
3029
** xref:api/vectordbs.adoc[]
30+
** xref:api/etl-pipeline.adoc[]
3131
*** xref:api/vectordbs/azure.adoc[]
3232
*** xref:api/vectordbs/chroma.adoc[]
3333
*** xref:api/vectordbs/milvus.adoc[]
3434
*** xref:api/vectordbs/neo4j.adoc[]
3535
*** xref:api/vectordbs/pgvector.adoc[]
3636
*** xref:api/vectordbs/weaviate.adoc[]
3737
*** xref:api/vectordbs/redis.adoc[]
38+
*** xref:api/vectordbs/pinecone.adoc[]
3839
** xref:api/testing.adoc[]
3940
* Appendices
4041
** xref:glossary.adoc[]

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The <<generic-model-api>> can help you understand the basic constructs used impl
99
== APIs
1010
* xref:api/embeddings.adoc[Embeddings API]
1111
* xref:api/chatclient.adoc[Chat Completion API]
12+
* xref:api/vectordbs.adoc[Vector Database API]
1213
* xref:api/[Image Generation API](WIP)
1314

1415
== Spring AI Generic Model API [[generic-model-api]]

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

Lines changed: 16 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A vector databases is a specialized type of database that plays an essential rol
66
In vector databases, queries differ from traditional relational databases.
77
Instead of exact matches, they perform similarity searches.
88
When given a vector as a query, a vector database returns vectors that are "`similar`" to the query vector.
9-
Further details on how this similarity is calculated at a high-level is provided in a <<vectordbs-similarity,later section>>.
9+
Further details on how this similarity is calculated at a high-level is provided in a xref:api/vectordbs/understand-vectordbs.adoc#vectordbs-similarity[Vector Similarity].
1010

1111
Vector databases are used to integrate your data with AI models.
1212
The first step in their usage is to load your data into a vector database.
@@ -49,6 +49,7 @@ public class SearchRequest {
4949
private Filter.Expression filterExpression;
5050

5151
public static SearchRequest query(String query) { return new SearchRequest(query); }
52+
5253
private SearchRequest(String query) { this.query = query; }
5354

5455
public SearchRequest withTopK(int topK) {...}
@@ -78,24 +79,23 @@ The `similaritySearch` methods in the interface allow for retrieving documents s
7879
* `k`: An integer that specifies the maximum number of similar documents to return. This is often referred to as a 'top K' search, or 'K nearest neighbors' (KNN).
7980
* `threshold`: A double value ranging from 0 to 1, where values closer to 1 indicate higher similarity. By default, if you set a threshold of 0.75, for instance, only documents with a similarity above this value are returned.
8081
* `Filter.Expression`: A class used for passing a fluent DSL (Domain-Specific Language) expression that functions similarly to a 'where' clause in SQL, but it applies exclusively to the metadata key-value pairs of a `Document`.
81-
* `filterExpression`: An external DSL based on ANTLR4 that accepts filter expressions as strings. For example, with metadata keys like country, year, and `isActive`, you could use an expression such as
82-
``` java
83-
country == 'UK' && year >= 2020 && isActive == true.
84-
```
82+
* `filterExpression`: An external DSL based on ANTLR4 that accepts filter expressions as strings. For example, with metadata keys like country, year, and `isActive`, you could use an expression such as: `country == 'UK' && year >= 2020 && isActive == true.`
83+
84+
Find more information on the `Filter.Expression` in the <<metadata-filters>> section.
8585

8686
== Available Implementations
8787

8888
These are the available implementations of the `VectorStore` interface:
8989

90-
* Azure Vector Search [`AzureVectorStore`]: The https://learn.microsoft.com/en-us/azure/search/vector-search-overview[Azure] vector store
91-
* Chroma [`ChromaVectorStore`]: The https://www.trychroma.com/[Chroma] vector store
92-
* Milvus [`MilvusVectorStore`]: The https://milvus.io/[Milvus] vector store
93-
* Neo4j [`Neo4jVectorStore`]: The https://neo4j.com/[Neo4j] vector store
94-
* PgVector [`PgVectorStore`]: The https://github.com/pgvector/pgvector[PostgreSQL/PGVector] vector store
95-
* Pinecone: https://www.pinecone.io/[PineCone] vector store
96-
* Redis [`RedisVectorStore`]: The https://redis.io/[Redis] vector store
97-
* Simple Vector Store [`SimpleVectorStore`]: A simple implementation of persistent vector storage, good for educational purposes
98-
* Weaviate [`WeaviateVectorStore`] The https://weaviate.io/[Weaviate] vector store
90+
* xref:api/vectordbs/azure.adoc[ Azure Vector Search] - The https://learn.microsoft.com/en-us/azure/search/vector-search-overview[Azure] vector store.
91+
* xref:api/vectordbs/chroma.adoc[ChromaVectorStore] - The https://www.trychroma.com/[Chroma] vector store.
92+
* xref:api/vectordbs/milvus.adoc[MilvusVectorStore] - The https://milvus.io/[Milvus] vector store.
93+
* xref:api/vectordbs/neo4j.adoc[Neo4jVectorStore] - The https://neo4j.com/[Neo4j] vector store.
94+
* xref:api/vectordbs/pgvector.adoc[PgVectorStore] - The https://github.com/pgvector/pgvector[PostgreSQL/PGVector] vector store.
95+
* xref:api/vectordbs/pinecone.adoc[PineconeVectorStore] - https://www.pinecone.io/[PineCone] vector store.
96+
* xref:api/vectordbs/redis.adoc[RedisVectorStore] - The https://redis.io/[Redis] vector store.
97+
* xref:api/vectordbs/weaviate.adoc[WeaviateVectorStore] - The https://weaviate.io/[Weaviate] vector store.
98+
* link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/vectorstore/SimpleVectorStore.java[SimpleVectorStore] - A simple implementation of persistent vector storage, good for educational purposes.
9999

100100
More implementations may be supported in future releases.
101101

@@ -137,7 +137,7 @@ Later, when a user question is passed into the AI model, a similarity search is
137137

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

140-
== Metadata Filters
140+
== Metadata Filters [[metadata-filters]]
141141

142142
This section describes various filters that you can use against the results of a query.
143143

@@ -209,99 +209,6 @@ Expression exp = b.and(b.eq("genre", "drama"), b.gte("year", 2020)).build();
209209

210210
== Understanding Vectors
211211

212-
Vectors have dimensionality and a direction.
213-
For example, the following image depicts a two-dimensional vector stem:[\vec{a}] in the cartesian coordinate system pictured as an arrow.
214-
215-
image::vector_2d_coordinates.png[]
216-
217-
The head of the vector stem:[\vec{a}] is at the point stem:[(a_1, a_2)].
218-
The *x* coordinate value is stem:[a_1] and the *y* coordinate value is stem:[a_2]. The coordinates are also referred to as the components of the vector.
219-
220-
[[vectordbs-similarity]]
221-
== Similarity
222-
223-
Several mathematical formulas can be used to determine if two vectors are similar.
224-
225-
One of the most intuitive to visualize and understand is cosine similarity.
226-
227-
Consider the following images that show three sets of graphs:
228-
229-
image::vector_similarity.png[]
230-
231-
The vectors stem:[\vec{A}] and stem:[\vec{B}] are considered similar, when they are pointing close to each other, as in the first diagram.
232-
The vectors are considered unrelated when pointing perpendicular to each other and opposite when they point away from each other.
233-
234-
The angle between them, stem:[\theta], is a good measure of their similarity.
235-
How can the angle stem:[\theta] be computed?
236-
237-
We are all familiar with the https://en.wikipedia.org/wiki/Pythagorean_theorem#History[Pythagorean Theorem].
238-
239-
image:pythagorean-triangle.png[]
240-
241-
What about when the angle between *a* and *b* is not 90 degrees?
242-
243-
Enter the https://en.wikipedia.org/wiki/Law_of_cosines[Law of cosines].
244-
245-
246-
.Law of Cosines
247-
****
248-
stem:[a^2 + b^2 - 2ab\cos\theta = c^2]
249-
****
250-
251-
The following image shows this approach as a vector diagram:
252-
253-
image:lawofcosines.png[]
254-
255-
256-
The magnitude of this vector is defined in terms of its components as:
257-
258-
.Magnitude
259-
****
260-
stem:[\vec{A} * \vec{A} = ||\vec{A}||^2 = A_1^2 + A_2^2 ]
261-
****
262-
263-
The dot product between two vectors stem:[\vec{A}] and stem:[\vec{B}] is defined in terms of its components as:
264-
265-
266-
.Dot Product
267-
****
268-
stem:[\vec{A} * \vec{B} = A_1B_1 + A_2B_2]
269-
****
270-
271-
Rewriting the Law of Cosines with vector magnitudes and dot products gives the following:
272-
273-
.Law of Cosines in Vector form
274-
****
275-
stem:[||\vec{A}||^2 + ||\vec{B}||^2 - 2||\vec{A}||||\vec{B}||\cos\theta = ||\vec{C}||^2]
276-
****
277-
278-
279-
Replacing stem:[||\vec{C}||^2] with stem:[||\vec{B} - \vec{A}||^2] gives the following:
280-
281-
.Law of Cosines in Vector form only in terms of stem:[\vec{A}] and stem:[\vec{B}]
282-
283-
****
284-
stem:[||\vec{A}||^2 + ||\vec{B}||^2 - 2||\vec{A}||||\vec{B}||\cos\theta = ||\vec{B} - \vec{A}||^2]
285-
****
286-
287-
288-
https://towardsdatascience.com/cosine-similarity-how-does-it-measure-the-similarity-maths-behind-and-usage-in-python-50ad30aad7db[Expanding this out] gives us the formula for https://en.wikipedia.org/wiki/Cosine_similarity[Cosine Similarity].
289-
290-
.Cosine Similarity
291-
****
292-
stem:[similarity(vec{A},vec{B}) = \cos(\theta) = \frac{\vec{A}\cdot\vec{B}}{||\vec{A}\||\cdot||\vec{B}||]
293-
****
294-
295-
This formula works for dimensions higher than 2 or 3, though it is hard to visualize. However, https://projector.tensorflow.org/[it can be visualized to some extent].
296-
It is common for vectors in AI/ML applications to have hundreds or even thousands of dimensions.
297-
298-
The similarity function in higher dimensions using the components of the vector is shown below.
299-
It expands the two-dimensional definitions of Magnitude and Dot Product given previously to *N* dimensions by using https://en.wikipedia.org/wiki/Summation[Summation mathematical syntax].
300-
301-
.Cosine Similarity with vector components
302-
****
303-
stem:[similarity(vec{A},vec{B}) = \cos(\theta) = \frac{ \sum_{i=1}^{n} {A_i B_i} }{ \sqrt{\sum_{i=1}^{n}{A_i^2} \cdot \sum_{i=1}^{n}{B_i^2}}]
304-
****
212+
xref:api/vectordbs/understand-vectordbs.adoc[Understanding Vectors]
305213

306-
This is the key formula used in the simple implementation of a vector store and can be found in the `InMemoryVectorStore` implementation.
307214

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
[[understand-vector-databases]]
2+
= Understanding Vectors
3+
4+
image::vector_2d_coordinates.png[width=150, role = "right"]
5+
6+
Vectors have dimensionality and a direction.
7+
For example, the following image depicts a two-dimensional vector stem:[\vec{a}] in the cartesian coordinate system pictured as an arrow.
8+
9+
The head of the vector stem:[\vec{a}] is at the point stem:[(a_1, a_2)].
10+
The *x* coordinate value is stem:[a_1] and the *y* coordinate value is stem:[a_2]. The coordinates are also referred to as the components of the vector.
11+
12+
[[vectordbs-similarity]]
13+
== Similarity
14+
15+
Several mathematical formulas can be used to determine if two vectors are similar.
16+
One of the most intuitive to visualize and understand is cosine similarity.
17+
Consider the following images that show three sets of graphs:
18+
19+
image::vector_similarity.png[align="center",width=600]
20+
21+
The vectors stem:[\vec{A}] and stem:[\vec{B}] are considered similar, when they are pointing close to each other, as in the first diagram.
22+
The vectors are considered unrelated when pointing perpendicular to each other and opposite when they point away from each other.
23+
24+
The angle between them, stem:[\theta], is a good measure of their similarity.
25+
How can the angle stem:[\theta] be computed?
26+
27+
image:pythagorean-triangle.png[align="center",width=100, role="left", trim="10 10 10 100"]
28+
29+
We are all familiar with the https://en.wikipedia.org/wiki/Pythagorean_theorem#History[Pythagorean Theorem].
30+
31+
What about when the angle between *a* and *b* is not 90 degrees?
32+
33+
34+
Enter the https://en.wikipedia.org/wiki/Law_of_cosines[Law of cosines].
35+
36+
37+
.Law of Cosines
38+
****
39+
stem:[a^2 + b^2 - 2ab\cos\theta = c^2]
40+
****
41+
42+
The following image shows this approach as a vector diagram:
43+
image:lawofcosines.png[align="center",width=200]
44+
45+
The magnitude of this vector is defined in terms of its components as:
46+
47+
.Magnitude
48+
****
49+
stem:[\vec{A} * \vec{A} = ||\vec{A}||^2 = A_1^2 + A_2^2 ]
50+
****
51+
52+
The dot product between two vectors stem:[\vec{A}] and stem:[\vec{B}] is defined in terms of its components as:
53+
54+
.Dot Product
55+
****
56+
stem:[\vec{A} * \vec{B} = A_1B_1 + A_2B_2]
57+
****
58+
59+
Rewriting the Law of Cosines with vector magnitudes and dot products gives the following:
60+
61+
.Law of Cosines in Vector form
62+
****
63+
stem:[||\vec{A}||^2 + ||\vec{B}||^2 - 2||\vec{A}||||\vec{B}||\cos\theta = ||\vec{C}||^2]
64+
****
65+
66+
67+
Replacing stem:[||\vec{C}||^2] with stem:[||\vec{B} - \vec{A}||^2] gives the following:
68+
69+
.Law of Cosines in Vector form only in terms of stem:[\vec{A}] and stem:[\vec{B}]
70+
71+
****
72+
stem:[||\vec{A}||^2 + ||\vec{B}||^2 - 2||\vec{A}||||\vec{B}||\cos\theta = ||\vec{B} - \vec{A}||^2]
73+
****
74+
75+
76+
https://towardsdatascience.com/cosine-similarity-how-does-it-measure-the-similarity-maths-behind-and-usage-in-python-50ad30aad7db[Expanding this out] gives us the formula for https://en.wikipedia.org/wiki/Cosine_similarity[Cosine Similarity].
77+
78+
.Cosine Similarity
79+
****
80+
stem:[similarity(vec{A},vec{B}) = \cos(\theta) = \frac{\vec{A}\cdot\vec{B}}{||\vec{A}\||\cdot||\vec{B}||]
81+
****
82+
83+
This formula works for dimensions higher than 2 or 3, though it is hard to visualize. However, https://projector.tensorflow.org/[it can be visualized to some extent].
84+
It is common for vectors in AI/ML applications to have hundreds or even thousands of dimensions.
85+
86+
The similarity function in higher dimensions using the components of the vector is shown below.
87+
It expands the two-dimensional definitions of Magnitude and Dot Product given previously to *N* dimensions by using https://en.wikipedia.org/wiki/Summation[Summation mathematical syntax].
88+
89+
.Cosine Similarity with vector components
90+
****
91+
stem:[similarity(vec{A},vec{B}) = \cos(\theta) = \frac{ \sum_{i=1}^{n} {A_i B_i} }{ \sqrt{\sum_{i=1}^{n}{A_i^2} \cdot \sum_{i=1}^{n}{B_i^2}}]
92+
****
93+
94+
This is the key formula used in the simple implementation of a vector store and can be found in the `InMemoryVectorStore` implementation.
95+

0 commit comments

Comments
 (0)