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
@@ -33,11 +33,8 @@ This section provides a guide to the `EmbeddingClient` interface and associated
33
33
----
34
34
public interface EmbeddingClient extends ModelClient<EmbeddingRequest, EmbeddingResponse> {
35
35
36
-
// EmbeddingResponse call(EmbeddingRequest request); from ModelClient
37
-
38
-
// Call method inherited from ModelClient<EmbeddingRequest, EmbeddingResponse>
39
-
// and the embed methods defined below are the primary methods of the interface
40
-
// the user needs to implement.
36
+
@Override
37
+
EmbeddingResponse call(EmbeddingRequest request);
41
38
42
39
43
40
/**
@@ -92,23 +89,70 @@ public interface EmbeddingClient extends ModelClient<EmbeddingRequest, Embedding
92
89
}
93
90
----
94
91
92
+
The embed methods offer various options for converting text into embeddings, accommodating single strings, structured `Document` objects, or batches of text.
95
93
94
+
Multiple shortcut methods are provided for embedding text, including the `embed(String text)` method, which takes a single string and returns the corresponding embedding vector.
95
+
All shortcuts are implemented around the `call` method, which is the primary method for invoking the embedding model.
96
96
97
-
The embed methods offer various options for converting text into embeddings, accommodating single strings, structured `Document` objects, or batches of text.
98
-
The returned values are lists of doubles, representing the embeddings in a numerical vector format.
97
+
Typically the embedding returns a lists of doubles, representing the embeddings in a numerical vector format.
99
98
100
99
The `embedForResponse` method provides a more comprehensive output, potentially including additional information about the embeddings.
101
100
102
101
The dimensions method is a handy tool for developers to quickly ascertain the size of the embedding vectors, which is important for understanding the embedding space and for subsequent processing steps.
103
102
103
+
==== EmbeddingRequest
104
+
105
+
The `EmbeddingRequest` is a `ModelRequest` that takes a list of text objects and optional embedding request options.
106
+
The following listing shows a truncated version of the EmbeddingRequest class, excluding constructors and other utility methods:
107
+
108
+
[source,java]
109
+
----
110
+
public class EmbeddingRequest implements ModelRequest<List<String>> {
111
+
private final List<String> inputs;
112
+
private final EmbeddingOptions options;
113
+
// other methods omitted
114
+
}
115
+
----
116
+
117
+
==== EmbeddingResponse
118
+
119
+
The structure of the `EmbeddingResponse` class is as follows:
120
+
121
+
[source,java]
122
+
----
123
+
public class EmbeddingResponse implements ModelResponse<Embedding> {
124
+
125
+
private List<Embedding> embeddings;
126
+
private EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata();
127
+
// other methods omitted
128
+
}
129
+
----
130
+
131
+
The `EmbeddingResponse` class holds the AI Model's output, with each `Embedding` instance containing the result vector data from a single text input.
132
+
133
+
The `EmbeddingResponse` class also carries a `EmbeddingResponseMetadata` metadata about the AI Model's response.
134
+
135
+
==== Embedding
136
+
137
+
The `Embedding` represents a single embedding vector.
138
+
139
+
[source,java]
140
+
----
141
+
public class Embedding implements ModelResult<List<Double>> {
142
+
private List<Double> embedding;
143
+
private Integer index;
144
+
private EmbeddingResultMetadata metadata;
145
+
// other methods omitted
146
+
}
147
+
----
104
148
105
149
== Available Implementations
106
150
107
151
Internally the various `EmbeddingClient` implementations use different low-level libraries and APIs to perform the embedding tasks. The following are some of the available implementations of the `EmbeddingClient` implementations:
108
152
109
-
* OpenAI: Using the https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/openai/api/OpenAiApi.java[Sprig AI OpenAiApi library].
110
-
* Azure OpenAI: Using https://learn.microsoft.com/en-us/java/api/overview/azure/ai-openai-readme?view=azure-java-preview[Microsoft's OpenAI client library].
111
-
* PostgresML: https://postgresml.org/docs/[PostgresML is a complete MLOps platform built on PostgreSQL]
112
-
* Sentence embedding with local ONNX models: The https://djl.ai/[Deep Java Library] and the Microsoft https://onnxruntime.ai/docs/get-started/with-java.html[ONNX Java Runtime] libraries are applied to run the ONNX models and compute the embeddings in Java.
113
-
* Vertex AI: Using the https://cloud.google.com/vertex-ai/docs[Google Cloud Vertex AI] client library.
114
-
153
+
* xref:api/embeddings/openai-embeddings.adoc[Spring AI OpenAI Embeddings]
154
+
* xref:api/embeddings/azure-openai-embeddings.adoc[Spring AI Azure OpenAI Embeddings]
155
+
* xref:api/embeddings/ollama-embeddings.adoc[Spring AI Ollama Embeddings]
156
+
* xref:api/embeddings/onnx.adoc[Spring AI Transformers (ONNX) Embeddings]
157
+
* xref:api/embeddings/postgresml-embeddings.adoc[Spring AI PostgresML Embeddings]
158
+
* xref:api/embeddings/bedrock-cohere-embedding.adoc[Spring AI Bedrock Cohere Embeddings]
0 commit comments