Skip to content

Commit 555f664

Browse files
committed
Refactor OCI Gen AI builder methods
- Deprecate the builder methods with the prefix `with` - Fix OCI cohere chat model options and OCI Embedding model options - Update references and docs
1 parent d77c950 commit 555f664

File tree

9 files changed

+191
-39
lines changed

9 files changed

+191
-39
lines changed

models/spring-ai-oci-genai/src/main/java/org/springframework/ai/oci/OCIEmbeddingOptions.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* The configuration information for OCI embedding requests
2727
*
2828
* @author Anders Swanson
29+
* @author Ilayaperumal Gopinathan
2930
*/
3031
@JsonInclude(JsonInclude.Include.NON_NULL)
3132
public class OCIEmbeddingOptions implements EmbeddingOptions {
@@ -87,21 +88,57 @@ public static class Builder {
8788

8889
private final OCIEmbeddingOptions options = new OCIEmbeddingOptions();
8990

91+
public Builder model(String model) {
92+
this.options.setModel(model);
93+
return this;
94+
}
95+
96+
public Builder compartment(String compartment) {
97+
this.options.setCompartment(compartment);
98+
return this;
99+
}
100+
101+
public Builder servingMode(String servingMode) {
102+
this.options.setServingMode(servingMode);
103+
return this;
104+
}
105+
106+
public Builder truncate(EmbedTextDetails.Truncate truncate) {
107+
this.options.truncate = truncate;
108+
return this;
109+
}
110+
111+
/**
112+
* @deprecated use {@link #model(String)} instead.
113+
*/
114+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
90115
public Builder withModel(String model) {
91116
this.options.setModel(model);
92117
return this;
93118
}
94119

120+
/**
121+
* @deprecated use {@link #compartment(String)} instead.
122+
*/
123+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
95124
public Builder withCompartment(String compartment) {
96125
this.options.setCompartment(compartment);
97126
return this;
98127
}
99128

129+
/**
130+
* @deprecated use {@link #servingMode(String)} instead.
131+
*/
132+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
100133
public Builder withServingMode(String servingMode) {
101134
this.options.setServingMode(servingMode);
102135
return this;
103136
}
104137

138+
/**
139+
* @deprecated use {@link #truncate(EmbedTextDetails.Truncate)} instead.
140+
*/
141+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
105142
public Builder withTruncate(EmbedTextDetails.Truncate truncate) {
106143
this.options.truncate = truncate;
107144
return this;

models/spring-ai-oci-genai/src/main/java/org/springframework/ai/oci/cohere/OCICohereChatOptions.java

Lines changed: 132 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
import org.springframework.ai.chat.prompt.ChatOptions;
2626

2727
/**
28-
* The configuration information for OCI chat requests
28+
* The configuration information for OCI chat requests.
2929
*
3030
* @author Anders Swanson
31+
* @author Ilayaperumal Gopinathan
3132
*/
3233
@JsonInclude(JsonInclude.Include.NON_NULL)
3334
public class OCICohereChatOptions implements ChatOptions {
@@ -115,19 +116,19 @@ public class OCICohereChatOptions implements ChatOptions {
115116
private List<CohereTool> tools;
116117

117118
public static OCICohereChatOptions fromOptions(OCICohereChatOptions fromOptions) {
118-
return builder().withModel(fromOptions.model)
119-
.withMaxTokens(fromOptions.maxTokens)
120-
.withCompartment(fromOptions.compartment)
121-
.withServingMode(fromOptions.servingMode)
122-
.withPreambleOverride(fromOptions.preambleOverride)
123-
.withTemperature(fromOptions.temperature)
124-
.withTopP(fromOptions.topP)
125-
.withTopK(fromOptions.topK)
126-
.withStop(fromOptions.stop)
127-
.withFrequencyPenalty(fromOptions.frequencyPenalty)
128-
.withPresencePenalty(fromOptions.presencePenalty)
129-
.withDocuments(fromOptions.documents)
130-
.withTools(fromOptions.tools)
119+
return builder().model(fromOptions.model)
120+
.maxTokens(fromOptions.maxTokens)
121+
.compartment(fromOptions.compartment)
122+
.servingMode(fromOptions.servingMode)
123+
.preambleOverride(fromOptions.preambleOverride)
124+
.temperature(fromOptions.temperature)
125+
.topP(fromOptions.topP)
126+
.topK(fromOptions.topK)
127+
.stop(fromOptions.stop)
128+
.frequencyPenalty(fromOptions.frequencyPenalty)
129+
.presencePenalty(fromOptions.presencePenalty)
130+
.documents(fromOptions.documents)
131+
.tools(fromOptions.tools)
131132
.build();
132133
}
133134

@@ -272,66 +273,183 @@ public Builder(OCICohereChatOptions chatOptions) {
272273
this.chatOptions = chatOptions;
273274
}
274275

276+
public Builder model(String model) {
277+
this.chatOptions.model = model;
278+
return this;
279+
}
280+
281+
public Builder maxTokens(Integer maxTokens) {
282+
this.chatOptions.maxTokens = maxTokens;
283+
return this;
284+
}
285+
286+
public Builder compartment(String compartment) {
287+
this.chatOptions.compartment = compartment;
288+
return this;
289+
}
290+
291+
public Builder servingMode(String servingMode) {
292+
this.chatOptions.servingMode = servingMode;
293+
return this;
294+
}
295+
296+
public Builder preambleOverride(String preambleOverride) {
297+
this.chatOptions.preambleOverride = preambleOverride;
298+
return this;
299+
}
300+
301+
public Builder temperature(Double temperature) {
302+
this.chatOptions.temperature = temperature;
303+
return this;
304+
}
305+
306+
public Builder topP(Double topP) {
307+
this.chatOptions.topP = topP;
308+
return this;
309+
}
310+
311+
public Builder topK(Integer topK) {
312+
this.chatOptions.topK = topK;
313+
return this;
314+
}
315+
316+
public Builder frequencyPenalty(Double frequencyPenalty) {
317+
this.chatOptions.frequencyPenalty = frequencyPenalty;
318+
return this;
319+
}
320+
321+
public Builder presencePenalty(Double presencePenalty) {
322+
this.chatOptions.presencePenalty = presencePenalty;
323+
return this;
324+
}
325+
326+
public Builder stop(List<String> stop) {
327+
this.chatOptions.stop = stop;
328+
return this;
329+
}
330+
331+
public Builder documents(List<Object> documents) {
332+
this.chatOptions.documents = documents;
333+
return this;
334+
}
335+
336+
public Builder tools(List<CohereTool> tools) {
337+
this.chatOptions.tools = tools;
338+
return this;
339+
}
340+
341+
/**
342+
* @deprecated use {@link #model(String)} instead.
343+
*/
344+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
275345
public Builder withModel(String model) {
276346
this.chatOptions.model = model;
277347
return this;
278348
}
279349

350+
/**
351+
* @deprecated use {@link #maxTokens(Integer)} instead.
352+
*/
353+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
280354
public Builder withMaxTokens(Integer maxTokens) {
281355
this.chatOptions.maxTokens = maxTokens;
282356
return this;
283357
}
284358

359+
/**
360+
* @deprecated use {@link #compartment(String)} instead.
361+
*/
362+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
285363
public Builder withCompartment(String compartment) {
286364
this.chatOptions.compartment = compartment;
287365
return this;
288366
}
289367

368+
/**
369+
* @deprecated use {@link #servingMode(String)} instead.
370+
*/
371+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
290372
public Builder withServingMode(String servingMode) {
291373
this.chatOptions.servingMode = servingMode;
292374
return this;
293375
}
294376

377+
/**
378+
* @deprecated use {@link #preambleOverride(String)} instead.
379+
*/
380+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
295381
public Builder withPreambleOverride(String preambleOverride) {
296382
this.chatOptions.preambleOverride = preambleOverride;
297383
return this;
298384
}
299385

386+
/**
387+
* @deprecated use {@link #temperature(Double)} instead.
388+
*/
389+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
300390
public Builder withTemperature(Double temperature) {
301391
this.chatOptions.temperature = temperature;
302392
return this;
303393
}
304394

395+
/**
396+
* @deprecated use {@link #topP(Double)} instead.
397+
*/
398+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
305399
public Builder withTopP(Double topP) {
306400
this.chatOptions.topP = topP;
307401
return this;
308402
}
309403

404+
/**
405+
* @deprecated use {@link #topK(Integer)} instead.
406+
*/
407+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
310408
public Builder withTopK(Integer topK) {
311409
this.chatOptions.topK = topK;
312410
return this;
313411
}
314412

413+
/**
414+
* @deprecated use {@link #frequencyPenalty(Double)} instead.
415+
*/
416+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
315417
public Builder withFrequencyPenalty(Double frequencyPenalty) {
316418
this.chatOptions.frequencyPenalty = frequencyPenalty;
317419
return this;
318420
}
319421

422+
/**
423+
* @deprecated use {@link #presencePenalty(Double)} instead.
424+
*/
425+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
320426
public Builder withPresencePenalty(Double presencePenalty) {
321427
this.chatOptions.presencePenalty = presencePenalty;
322428
return this;
323429
}
324430

431+
/**
432+
* @deprecated use {@link #stop(List)} instead.
433+
*/
434+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
325435
public Builder withStop(List<String> stop) {
326436
this.chatOptions.stop = stop;
327437
return this;
328438
}
329439

440+
/**
441+
* @deprecated use {@link #documents(List)} instead.
442+
*/
443+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
330444
public Builder withDocuments(List<Object> documents) {
331445
this.chatOptions.documents = documents;
332446
return this;
333447
}
334448

449+
/**
450+
* @deprecated use {@link #tools(List)} instead.
451+
*/
452+
@Deprecated(forRemoval = true, since = "1.0.0-M5")
335453
public Builder withTools(List<CohereTool> tools) {
336454
this.chatOptions.tools = tools;
337455
return this;

models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/BaseEmbeddingModelTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public class BaseEmbeddingModelTest extends BaseOCIGenAITest {
2828
*/
2929
public static OCIEmbeddingModel getEmbeddingModel() {
3030
OCIEmbeddingOptions options = OCIEmbeddingOptions.builder()
31-
.withModel(EMBEDDING_MODEL_V2)
32-
.withCompartment(COMPARTMENT_ID)
33-
.withServingMode("on-demand")
31+
.model(EMBEDDING_MODEL_V2)
32+
.compartment(COMPARTMENT_ID)
33+
.servingMode("on-demand")
3434
.build();
3535
return new OCIEmbeddingModel(getGenerativeAIClient(), options);
3636
}

models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/BaseOCIGenAITest.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ public static GenerativeAiInference getGenerativeAIClient() {
5454
}
5555

5656
public static OCICohereChatOptions.Builder options() {
57-
return OCICohereChatOptions.builder()
58-
.withModel(CHAT_MODEL_ID)
59-
.withCompartment(COMPARTMENT_ID)
60-
.withServingMode("on-demand");
57+
return OCICohereChatOptions.builder().model(CHAT_MODEL_ID).compartment(COMPARTMENT_ID).servingMode("on-demand");
6158
}
6259

6360
}

models/spring-ai-oci-genai/src/test/java/org/springframework/ai/oci/OCIEmbeddingModelIT.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ void call() {
5151

5252
@Test
5353
void callWithOptions() {
54-
EmbeddingResponse response = this.embeddingModel.call(new EmbeddingRequest(this.content,
55-
OCIEmbeddingOptions.builder().withModel(EMBEDDING_MODEL_V3).build()));
54+
EmbeddingResponse response = this.embeddingModel
55+
.call(new EmbeddingRequest(this.content, OCIEmbeddingOptions.builder().model(EMBEDDING_MODEL_V3).build()));
5656
assertThat(response).isNotNull();
5757
assertThat(response.getResults()).hasSize(2);
5858
assertThat(response.getMetadata().getModel()).isEqualTo(EMBEDDING_MODEL_V3);

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/oci-genai/cohere-chat.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ ChatResponse response = chatModel.call(
102102
new Prompt(
103103
"Generate the names of 5 famous pirates.",
104104
OCICohereChatOptions.builder()
105-
.withModel("my-model-ocid")
106-
.withCompartment("my-compartment-ocid")
107-
.withTemperature(0.5)
105+
.model("my-model-ocid")
106+
.compartment("my-compartment-ocid")
107+
.temperature(0.5)
108108
.build()
109109
));
110110
----
@@ -195,9 +195,9 @@ var genAi = GenerativeAiInferenceClient.builder()
195195
.build(authProvider);
196196
197197
var chatModel = new OCICohereChatModel(genAi, OCICohereChatOptions.builder()
198-
.withModel(MODEL_ID)
199-
.withCompartment(COMPARTMENT_ID)
200-
.withServingMode("on-demand")
198+
.model(MODEL_ID)
199+
.compartment(COMPARTMENT_ID)
200+
.servingMode("on-demand")
201201
.build());
202202
203203
ChatResponse response = chatModel.call(

spring-ai-docs/src/main/antora/modules/ROOT/pages/api/embeddings/oci-genai-embeddings.adoc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ For example to override the default model name for a specific request:
8888
EmbeddingResponse embeddingResponse = embeddingModel.call(
8989
new EmbeddingRequest(List.of("Hello World", "World is big and salvation is near"),
9090
OCIEmbeddingOptions.builder()
91-
.withModel("my-other-embedding-model")
91+
.model("my-other-embedding-model")
9292
.build()
9393
));
9494
----
@@ -164,9 +164,9 @@ var aiClient = GenerativeAiInferenceClient.builder()
164164
.region(Region.valueOf(this.REGION))
165165
.build(this.authProvider);
166166
var options = OCIEmbeddingOptions.builder()
167-
.withModel(this.EMBEDDING_MODEL)
168-
.withCompartment(this.COMPARTMENT_ID)
169-
.withServingMode("on-demand")
167+
.model(this.EMBEDDING_MODEL)
168+
.compartment(this.COMPARTMENT_ID)
169+
.servingMode("on-demand")
170170
.build();
171171
var embeddingModel = new OCIEmbeddingModel(this.aiClient, this.options);
172172
List<Double> embedding = this.embeddingModel.embed(new Document("How many provinces are in Canada?"));

spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/oci/genai/OCICohereChatModelProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ public class OCICohereChatModelProperties {
3838

3939
@NestedConfigurationProperty
4040
private OCICohereChatOptions options = OCICohereChatOptions.builder()
41-
.withServingMode(DEFAULT_SERVING_MODE)
42-
.withTemperature(DEFAULT_TEMPERATURE)
41+
.servingMode(DEFAULT_SERVING_MODE)
42+
.temperature(DEFAULT_TEMPERATURE)
4343
.build();
4444

4545
public boolean isEnabled() {

0 commit comments

Comments
 (0)