Skip to content

Commit 2a40b20

Browse files
committed
Charles preferences
1 parent fc152d1 commit 2a40b20

File tree

7 files changed

+23
-47
lines changed

7 files changed

+23
-47
lines changed

docs/guides/SPRING_AI_INTEGRATION.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,19 @@ Please find [an example in our Spring Boot application](../../sample-code/spring
170170

171171
## OpenAI
172172

173+
### Introduction
174+
175+
Our OpenAI client is integrated in Spring AI classes:
176+
173177
### Embedding
174178

175-
With SpringAI integration (_since v1.5.0_), you may now obtain embedding vectors for a list of strings as follows:
179+
Here is how to obtain embedding vectors for a list of strings:
176180

177181
You first initialize the OpenAI client for your model of choice and attach it `OpenAiSpringEmbeddingModel` object.
178182

179183
```java
180184
OpenAiClient client = OpenAiClient.forModel(OpenAiModel.TEXT_EMBEDDING_3_SMALL);
181185
OpenAiSpringEmbeddingModel embeddingModel = new OpenAiSpringEmbeddingModel(client);
182-
```
183-
184-
Then you can invoke `embded` method on the `embeddingModel` object with the text items to embed.
185-
186-
```java
187186
List<String> texts = List.of("Hello", "World");
188187
float[] embeddings = embeddingModel.embed(texts);
189188
```

foundation-models/openai/src/main/java/com/sap/ai/sdk/foundationmodels/openai/spring/OpenAiSpringEmbeddingModel.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public EmbeddingResponse call(@Nonnull final EmbeddingRequest request)
6868

6969
if (request.getOptions().getModel() != null) {
7070
throw new IllegalArgumentException(
71-
"Invalid EmbeddingRequest: the model option must be null, as the client already defines the model.");
71+
"Do not set a model in EmbeddingOptions, as the OpenAiClient already defines the model.");
7272
}
7373

7474
final var openAiRequest = createEmbeddingsCreateRequest(request);
@@ -80,12 +80,10 @@ public EmbeddingResponse call(@Nonnull final EmbeddingRequest request)
8080
@Override
8181
@Nonnull
8282
public float[] embed(@Nonnull final Document document) throws UnsupportedOperationException {
83-
if (document.isText()) {
84-
return embed(
85-
Objects.requireNonNull(
86-
document.getFormattedContent(this.metadataMode), "Document is missing text content"));
87-
}
88-
throw new UnsupportedOperationException("Only text type document supported for embedding");
83+
return embed(
84+
Objects.requireNonNull(
85+
document.getFormattedContent(this.metadataMode),
86+
"Formatted content of the document should not be null."));
8987
}
9088

9189
private EmbeddingsCreateRequest createEmbeddingsCreateRequest(

foundation-models/openai/src/test/java/com/sap/ai/sdk/foundationmodels/openai/spring/EmbeddingModelTest.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void testCallWithModelOptionSetThrows() {
8080
assertThatThrownBy(() -> model.call(springAiRequest))
8181
.isInstanceOf(IllegalArgumentException.class)
8282
.hasMessage(
83-
"Invalid EmbeddingRequest: the model option must be null, as the client already defines the model.");
83+
"Do not set a model in EmbeddingOptions, as the OpenAiClient already defines the model.");
8484
}
8585

8686
@Test
@@ -106,19 +106,6 @@ void testEmbedDocument() {
106106
assertThat(result).isEqualTo(new float[] {1, 2, 3});
107107
}
108108

109-
@Test
110-
@DisplayName("Embed document with missing text content throws exception")
111-
void testEmbedNonTextDocumentThrows() {
112-
val document = mock(Document.class);
113-
when(document.isText()).thenReturn(false);
114-
115-
val model = new OpenAiSpringEmbeddingModel(client);
116-
117-
assertThatThrownBy(() -> model.embed(document))
118-
.isInstanceOf(UnsupportedOperationException.class)
119-
.hasMessage("Only text type document supported for embedding");
120-
}
121-
122109
private static <T> Consumer<T> assertRecursiveEquals(T expected) {
123110
return (actual) -> {
124111
assertThat(actual).usingRecursiveComparison().isEqualTo(expected);

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/SpringAiOpenAiController.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.sap.ai.sdk.app.controllers;
22

33
import com.sap.ai.sdk.app.services.SpringAiOpenAiService;
4-
import java.util.List;
54
import javax.annotation.Nullable;
65
import lombok.val;
76
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,10 +15,9 @@
1615
class SpringAiOpenAiController {
1716
@Autowired private SpringAiOpenAiService service;
1817

19-
@GetMapping("/embed")
18+
@GetMapping("/embed/strings")
2019
Object embed(@Nullable @RequestParam(value = "format", required = false) final String format) {
21-
val response =
22-
service.embedWithEmbeddingRequest(List.of("The quick brown fox jumps over the lazy dog."));
20+
val response = service.embedStrings();
2321

2422
if ("json".equals(format)) {
2523
return response;

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/SpringAiOpenAiService.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,25 @@ public class SpringAiOpenAiService {
2020
/**
2121
* Embeds a list of strings using the OpenAI embedding model.
2222
*
23-
* @param strings the list of strings to embed
2423
* @return an {@code EmbeddingResponse} containing the embeddings and metadata
2524
*/
2625
@Nonnull
27-
public EmbeddingResponse embedWithEmbeddingRequest(@Nonnull final List<String> strings) {
26+
public EmbeddingResponse embedStrings() {
2827
final var options = EmbeddingOptionsBuilder.builder().withDimensions(128).build();
29-
final var springAiRequest = new EmbeddingRequest(strings, options);
28+
final var springAiRequest =
29+
new EmbeddingRequest(List.of("The quick brown fox jumps over the lazy dog."), options);
3030

3131
return new OpenAiSpringEmbeddingModel(client).call(springAiRequest);
3232
}
3333

3434
/**
3535
* Embeds the content of a document using the OpenAI embedding model.
3636
*
37-
* @param content the content of the document to embed
3837
* @return a float array representing the embedding of the document's content
3938
*/
4039
@Nonnull
41-
public float[] embedWithDocument(@Nonnull final String content) {
42-
final var document = new Document(content);
40+
public float[] embedDocument() {
41+
final var document = new Document("The quick brown fox jumps over the lazy dog.");
4342
return new OpenAiSpringEmbeddingModel(client).embed(document);
4443
}
4544
}

sample-code/spring-app/src/main/resources/static/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,8 +631,8 @@ <h5 class="mb-1">Orchestration Integration</h5>
631631
</div>
632632
<div class="card-body">
633633
<div class="d-flex align-items-center">
634-
<img src="Open-AI-Logo.svg" alt="OpenAI Logo" width="35" class="me-1 mb-2">
635-
<h5 class="mb-1">OpenAI Integration</h5>
634+
<img src="Open-AI-Logo.svg" alt="OpenAI Logo" class="me-1 mb-1">
635+
<h5 class="mb-1">OpenAI</h5>
636636
</div>
637637
<ul class="list-group">
638638
<li class="list-group-item">

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/SpringAiOpenAiTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,19 @@
44

55
import com.sap.ai.sdk.app.services.SpringAiOpenAiService;
66
import com.sap.ai.sdk.foundationmodels.openai.OpenAiModel;
7-
import java.util.List;
87
import org.junit.jupiter.api.Test;
98

109
class SpringAiOpenAiTest {
1110

1211
private final SpringAiOpenAiService service = new SpringAiOpenAiService();
1312

1413
@Test
15-
void testEmbedWithEmbeddingRequest() {
14+
void testEmbedStrings() {
1615

17-
var response =
18-
service.embedWithEmbeddingRequest(
19-
List.of(
20-
"The quick brown fox jumps over the lazy dog.",
21-
"To be or not to be, that is the question."));
16+
var response = service.embedStrings();
2217

2318
assertThat(response).isNotNull();
24-
assertThat(response.getResults()).hasSize(2);
19+
assertThat(response.getResults()).hasSize(1);
2520
assertThat(response.getResults().get(0).getOutput()).hasSize(128);
2621
assertThat(response.getMetadata().getUsage().getPromptTokens()).isNotNull();
2722
assertThat(response.getMetadata().getUsage().getTotalTokens()).isNotNull();

0 commit comments

Comments
 (0)