Skip to content

Commit 3a90c99

Browse files
committed
Sample app controller and minor test update
- make embedding document test response - add controller for spring open ai - include in static files of sample app
1 parent b666f26 commit 3a90c99

File tree

3 files changed

+75
-24
lines changed

3 files changed

+75
-24
lines changed

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,42 @@ void setUp() {
3333

3434
@Test
3535
void callWithValidRequest() {
36-
var request =
36+
final var request =
3737
new EmbeddingRequest(
38-
List.of("instructions"),
39-
EmbeddingOptionsBuilder.builder().withModel("model").withDimensions(128).build());
38+
List.of("instructions"), EmbeddingOptionsBuilder.builder().withDimensions(128).build());
4039

41-
var vector = new float[] {0.0f};
42-
var expectedResponse =
40+
final var vector = new float[] {0.0f};
41+
final var modelName = ""; // defined by client object and options not honoured
42+
final var expectedResponse =
4343
new EmbeddingResponse(
4444
List.of(new Embedding(vector, 0)),
45-
new EmbeddingResponseMetadata("model", new DefaultUsage(0, null, 0)));
45+
new EmbeddingResponseMetadata(modelName, new DefaultUsage(0, null, 0)));
4646

47-
var openAiResponse =
47+
final var openAiResponse =
4848
new EmbeddingsCreate200Response()
4949
.data(List.of(new EmbeddingsCreate200ResponseDataInner().embedding(vector)))
50-
.model("model")
5150
.usage(new EmbeddingsCreate200ResponseUsage().promptTokens(0).totalTokens(0));
5251

5352
when(client.embedding(any(EmbeddingsCreateRequest.class))).thenReturn(openAiResponse);
5453

55-
var actualResponse = new OpenAiSpringEmbeddingModel(client).call(request);
54+
final var actualResponse = new OpenAiSpringEmbeddingModel(client).call(request);
5655

5756
assertThat(expectedResponse).usingRecursiveComparison().isEqualTo(actualResponse);
5857
}
5958

6059
@Test
61-
void embedDocumentInvokesDefaultMethod() {
60+
void embedDocument() {
6261
Document document = new Document("Some content");
6362

64-
OpenAiSpringEmbeddingModel model =
65-
new OpenAiSpringEmbeddingModel(client) {
66-
@Override
67-
public float[] embed(String text) {
68-
// For testing, just return any array
69-
return new float[] {1, 2, 3};
70-
}
71-
};
63+
var vector = new float[] {1, 2, 3};
64+
var openAiResponse =
65+
new EmbeddingsCreate200Response()
66+
.data(List.of(new EmbeddingsCreate200ResponseDataInner().embedding(vector)))
67+
.usage(new EmbeddingsCreate200ResponseUsage().promptTokens(0).totalTokens(0));
68+
69+
when(client.embedding(any(EmbeddingsCreateRequest.class))).thenReturn(openAiResponse);
7270

73-
float[] result = model.embed(document);
71+
float[] result = new OpenAiSpringEmbeddingModel(client).embed(document);
7472
assertArrayEquals(new float[] {1, 2, 3}, result);
7573
}
7674

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.sap.ai.sdk.app.controllers;
2+
3+
import com.sap.ai.sdk.app.services.SpringAiOpenAiService;
4+
import java.util.List;
5+
import javax.annotation.Nullable;
6+
import lombok.val;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
@SuppressWarnings("unused")
14+
@RestController
15+
@RequestMapping("/spring-ai-openai")
16+
class SpringAiOpenAiController {
17+
@Autowired private SpringAiOpenAiService service;
18+
19+
@GetMapping("/embed")
20+
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."));
23+
24+
if ("json".equals(format)) {
25+
return response;
26+
}
27+
return response.getResult().getOutput();
28+
}
29+
}

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ <h2>Orchestration</h2>
379379
<code>/orchestration/responseFormatJsonSchema</code>
380380
</button>
381381
<div class="tooltip-content">
382-
Chat request to an LLM through the Orchestration service where the output will adhere to a given JSON schema.
382+
Chat request to an LLM through the Orchestration service where the output will
383+
adhere to a given JSON schema.
383384
</div>
384385
</div>
385386
</li>
@@ -390,7 +391,8 @@ <h2>Orchestration</h2>
390391
<code>/orchestration/responseFormatJsonObject</code>
391392
</button>
392393
<div class="tooltip-content">
393-
Chat request to an LLM through the Orchestration service where the output will consist of valid JSON.
394+
Chat request to an LLM through the Orchestration service where the output will
395+
consist of valid JSON.
394396
</div>
395397
</div>
396398
</li>
@@ -566,7 +568,8 @@ <h5 class="mb-1">Orchestration Integration</h5>
566568
</li>
567569
<li class="list-group-item">
568570
<div class="info-tooltip">
569-
<button type="submit" formaction="/spring-ai-orchestration/toolCalling" class="link-offset-2-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover endpoint">
571+
<button type="submit" formaction="/spring-ai-orchestration/toolCalling"
572+
class="link-offset-2-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover endpoint">
570573
<code>/spring-ai-orchestration/toolCalling</code>
571574
</button>
572575
<div class="tooltip-content">
@@ -576,6 +579,25 @@ <h5 class="mb-1">Orchestration Integration</h5>
576579
</li>
577580
</ul>
578581
</div>
582+
<div class="card-body">
583+
<div class="d-flex align-items-center">
584+
<img src="Open-AI-Logo.svg" alt="OpenAI Logo" width="35" class="me-1 mb-2">
585+
<h5 class="mb-1">OpenAI Integration</h5>
586+
</div>
587+
<ul class="list-group">
588+
<li class="list-group-item">
589+
<div class="info-tooltip">
590+
<button type="submit" formaction="/spring-ai-openai/embed"
591+
class="link-offset-2-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover endpoint">
592+
<code>/spring-ai-openai/embed</code>
593+
</button>
594+
<div class="tooltip-content">
595+
Get the embedding for a given string using SpringAI from OpenAI.
596+
</div>
597+
</div>
598+
</li>
599+
</ul>
600+
</div>
579601
</div>
580602
</div>
581603

@@ -586,7 +608,8 @@ <h5 class="mb-1">Orchestration Integration</h5>
586608
<img src="grounding.png" alt="Grounding Logo" width="35" class="me-1 mb-2">
587609
<h2>Document Grounding</h2>
588610
</div>
589-
The Document Grounding API offers additional context for enhancing your Orchestration LLM calls.
611+
The Document Grounding API offers additional context for enhancing your Orchestration LLM
612+
calls.
590613
For more information, check the <a
591614
href="https://wiki.one.int.sap/wiki/display/AI/Consume+Grounding+via+GenAI+Hub"
592615
target="_blank">Wiki</a>
@@ -600,7 +623,8 @@ <h2>Document Grounding</h2>
600623
<code>/grounding/pipelines/list</code>
601624
</button>
602625
<div class="tooltip-content">
603-
List all active pipelines in the document grounding module. They can be used to index document storages like Sharepoint pages or SFTP servers.
626+
List all active pipelines in the document grounding module. They can be used
627+
to index document storages like Sharepoint pages or SFTP servers.
604628
</div>
605629
</div>
606630
</li>

0 commit comments

Comments
 (0)