Skip to content

Commit 14d6f58

Browse files
ThomasVitaleilayaperumalg
authored andcommitted
feat: Align EmbeddingOptions builder to ChatOptions
* Add Builder interface under EmbeddingOptions, and provide default implementation with DefaultEmbeddingOptions and DefaultEmbeddingOptionsBuilder. This is to match the design adopted alredy for ChatOptions in 1.0.0 Signed-off-by: Thomas Vitale <[email protected]>
1 parent f4a1c96 commit 14d6f58

File tree

5 files changed

+114
-31
lines changed

5 files changed

+114
-31
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2023-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.embedding;
18+
19+
/**
20+
* Default implementation of {@link EmbeddingOptions}.
21+
*
22+
* @author Thomas Vitale
23+
*/
24+
public class DefaultEmbeddingOptions implements EmbeddingOptions {
25+
26+
private String model;
27+
28+
private Integer dimensions;
29+
30+
@Override
31+
public String getModel() {
32+
return this.model;
33+
}
34+
35+
public void setModel(String model) {
36+
this.model = model;
37+
}
38+
39+
@Override
40+
public Integer getDimensions() {
41+
return this.dimensions;
42+
}
43+
44+
public void setDimensions(Integer dimensions) {
45+
this.dimensions = dimensions;
46+
}
47+
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2023-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.ai.embedding;
18+
19+
/**
20+
* Default implementation of {@link EmbeddingOptions.Builder}.
21+
*
22+
* @author Thomas Vitale
23+
*/
24+
public class DefaultEmbeddingOptionsBuilder implements EmbeddingOptions.Builder {
25+
26+
private final DefaultEmbeddingOptions embeddingOptions = new DefaultEmbeddingOptions();
27+
28+
@Override
29+
public EmbeddingOptions.Builder model(String model) {
30+
this.embeddingOptions.setModel(model);
31+
return this;
32+
}
33+
34+
@Override
35+
public EmbeddingOptions.Builder dimensions(Integer dimensions) {
36+
this.embeddingOptions.setDimensions(dimensions);
37+
return this;
38+
}
39+
40+
@Override
41+
public EmbeddingOptions build() {
42+
return this.embeddingOptions;
43+
}
44+
45+
}

spring-ai-model/src/main/java/org/springframework/ai/embedding/EmbeddingOptions.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,4 +33,18 @@ public interface EmbeddingOptions extends ModelOptions {
3333
@Nullable
3434
Integer getDimensions();
3535

36+
static Builder builder() {
37+
return new DefaultEmbeddingOptionsBuilder();
38+
}
39+
40+
interface Builder {
41+
42+
Builder model(String model);
43+
44+
Builder dimensions(Integer dimensions);
45+
46+
EmbeddingOptions build();
47+
48+
}
49+
3650
}

spring-ai-model/src/main/java/org/springframework/ai/embedding/EmbeddingOptionsBuilder.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 the original author or authors.
2+
* Copyright 2023-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,8 +21,10 @@
2121
*
2222
* @author Thomas Vitale
2323
* @since 1.0.0
24+
* @deprecated in favor of {@link EmbeddingOptions#builder()}
2425
*/
25-
public final class EmbeddingOptionsBuilder {
26+
@Deprecated
27+
public final class EmbeddingOptionsBuilder implements EmbeddingOptions.Builder {
2628

2729
private final DefaultEmbeddingOptions embeddingOptions = new DefaultEmbeddingOptions();
2830

@@ -57,30 +59,4 @@ public EmbeddingOptions build() {
5759
return this.embeddingOptions;
5860
}
5961

60-
private static class DefaultEmbeddingOptions implements EmbeddingOptions {
61-
62-
private String model;
63-
64-
private Integer dimensions;
65-
66-
@Override
67-
public String getModel() {
68-
return this.model;
69-
}
70-
71-
public void setModel(String model) {
72-
this.model = model;
73-
}
74-
75-
@Override
76-
public Integer getDimensions() {
77-
return this.dimensions;
78-
}
79-
80-
public void setDimensions(Integer dimensions) {
81-
this.dimensions = dimensions;
82-
}
83-
84-
}
85-
8662
}

vector-stores/spring-ai-pgvector-store/src/main/java/org/springframework/ai/vectorstore/pgvector/PgVectorStore.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import org.springframework.ai.document.Document;
3737
import org.springframework.ai.document.DocumentMetadata;
3838
import org.springframework.ai.embedding.EmbeddingModel;
39-
import org.springframework.ai.embedding.EmbeddingOptionsBuilder;
39+
import org.springframework.ai.embedding.EmbeddingOptions;
4040
import org.springframework.ai.observation.conventions.VectorStoreProvider;
4141
import org.springframework.ai.observation.conventions.VectorStoreSimilarityMetric;
4242
import org.springframework.ai.util.JacksonUtils;
@@ -256,7 +256,7 @@ public static PgVectorStoreBuilder builder(JdbcTemplate jdbcTemplate, EmbeddingM
256256

257257
@Override
258258
public void doAdd(List<Document> documents) {
259-
List<float[]> embeddings = this.embeddingModel.embed(documents, EmbeddingOptionsBuilder.builder().build(),
259+
List<float[]> embeddings = this.embeddingModel.embed(documents, EmbeddingOptions.builder().build(),
260260
this.batchingStrategy);
261261

262262
List<List<Document>> batchedDocuments = batchDocuments(documents);

0 commit comments

Comments
 (0)