11package com .sap .ai .sdk .app .controllers ;
22
33import static com .sap .ai .sdk .foundationmodels .openai .OpenAiModel .GPT_4O_MINI ;
4+ import static com .sap .ai .sdk .foundationmodels .openai .generated .model .ChatCompletionResponseMessageRole .ASSISTANT ;
45import static org .assertj .core .api .Assertions .assertThat ;
56
67import com .sap .ai .sdk .app .services .OpenAiService ;
8+ import com .sap .ai .sdk .foundationmodels .openai .OpenAiChatCompletionRequest ;
79import com .sap .ai .sdk .foundationmodels .openai .OpenAiClient ;
8- import com .sap .ai .sdk .foundationmodels .openai .model . OpenAiChatCompletionOutput ;
9- import com .sap .ai .sdk .foundationmodels .openai .model .OpenAiChatCompletionParameters ;
10- import com . sap . ai . sdk . foundationmodels . openai . model . OpenAiChatMessage . OpenAiChatUserMessage ;
10+ import com .sap .ai .sdk .foundationmodels .openai .OpenAiMessage ;
11+ import com .sap .ai .sdk .foundationmodels .openai .generated . model .CompletionUsage ;
12+ import java . util . ArrayList ;
1113import java .util .concurrent .atomic .AtomicInteger ;
14+ import java .util .concurrent .atomic .AtomicReference ;
1215import lombok .extern .slf4j .Slf4j ;
1316import org .junit .jupiter .api .BeforeEach ;
1417import org .junit .jupiter .api .Test ;
@@ -26,9 +29,13 @@ void setUp() {
2629 void chatCompletion () {
2730 final var completion = service .chatCompletion ("Who is the prettiest" );
2831
29- final var message = completion .getChoices ().get (0 ).getMessage ();
30- assertThat (message .getRole ()).isEqualTo ("assistant" );
31- assertThat (message .getContent ()).isNotEmpty ();
32+ assertThat (completion .getChoice ().getMessage ().getRole ()).isEqualTo (ASSISTANT );
33+ assertThat (completion .getContent ()).isNotEmpty ();
34+ }
35+
36+ @ Test
37+ void testMessagesHistory () {
38+ assertThat (service .messagesHistory ("What is the capital of France?" ).getContent ()).isNotEmpty ();
3239 }
3340
3441 @ Test
@@ -37,59 +44,58 @@ void chatCompletionImage() {
3744 service .chatCompletionImage (
3845 "https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/SAP_2011_logo.svg/440px-SAP_2011_logo.svg.png" );
3946
40- final var message = completion .getChoices ().get (0 ).getMessage ();
41- assertThat (message .getRole ()).isEqualTo ("assistant" );
42- assertThat (message .getContent ()).isNotEmpty ();
47+ assertThat (completion .getContent ()).isNotEmpty ();
48+ assertThat (completion .getChoice ().getMessage ().getRole ()).isEqualTo (ASSISTANT );
4349 }
4450
4551 @ Test
4652 void streamChatCompletion () {
47- final var request =
48- new OpenAiChatCompletionParameters ()
49- .addMessages (new OpenAiChatUserMessage ().addText ("Who is the prettiest?" ));
53+ final var userMessage = OpenAiMessage .user ("Who is the prettiest?" );
54+ final var prompt = new OpenAiChatCompletionRequest (userMessage );
5055
51- final var totalOutput = new OpenAiChatCompletionOutput ();
56+ final var usageRef = new AtomicReference < CompletionUsage > ();
5257 final var filledDeltaCount = new AtomicInteger (0 );
58+
5359 OpenAiClient .forModel (GPT_4O_MINI )
54- .streamChatCompletionDeltas (request )
55- .peek (totalOutput ::addDelta )
60+ .streamChatCompletionDeltas (prompt )
5661 // foreach consumes all elements, closing the stream at the end
5762 .forEach (
5863 delta -> {
64+ usageRef .compareAndExchange (null , delta .getCompletionUsage ());
5965 final String deltaContent = delta .getDeltaContent ();
6066 log .info ("delta: {}" , delta );
6167 if (!deltaContent .isEmpty ()) {
6268 filledDeltaCount .incrementAndGet ();
6369 }
6470 });
6571
66- // the first two and the last delta don't have any content
67- // see OpenAiChatCompletionDelta#getDeltaContent
6872 assertThat (filledDeltaCount .get ()).isGreaterThan (0 );
6973
70- assertThat (totalOutput .getChoices ()).isNotEmpty ();
71- assertThat (totalOutput .getChoices ().get (0 ).getMessage ().getContent ()).isNotEmpty ();
72- assertThat (totalOutput .getPromptFilterResults ()).isNotNull ();
73- assertThat (totalOutput .getChoices ().get (0 ).getContentFilterResults ()).isNotNull ();
74+ assertThat (usageRef .get ().getTotalTokens ()).isGreaterThan (0 );
75+ assertThat (usageRef .get ().getPromptTokens ()).isGreaterThan (0 );
76+ assertThat (usageRef .get ().getCompletionTokens ()).isGreaterThan (0 );
7477 }
7578
7679 @ Test
7780 void embedding () {
7881 final var embedding = service .embedding ("Hello world" );
7982
80- assertThat (embedding .getData ().get (0 ).getEmbedding ()).hasSizeGreaterThan (1 );
81- assertThat (embedding .getModel ()).isEqualTo ("text-embedding-3-small" );
82- assertThat (embedding .getObject ()).isEqualTo ("list" );
83+ assertThat (embedding .getOriginalResponse ().getData ().get (0 ).getEmbedding ())
84+ .hasSizeGreaterThan (1 );
85+ assertThat (embedding .getEmbeddingVectors ()).isInstanceOf (ArrayList .class );
86+ assertThat (embedding .getEmbeddingVectors ().get (0 )).isInstanceOf (float [].class );
87+
88+ assertThat (embedding .getOriginalResponse ().getModel ()).isEqualTo ("text-embedding-3-small" );
89+ assertThat (embedding .getOriginalResponse ().getObject ()).isEqualTo ("list" );
8390 }
8491
8592 @ Test
8693 void chatCompletionWithResource () {
8794 final var completion =
8895 service .chatCompletionWithResource ("ai-sdk-java-e2e" , "Where is the nearest coffee shop?" );
8996
90- final var message = completion .getChoices ().get (0 ).getMessage ();
91- assertThat (message .getRole ()).isEqualTo ("assistant" );
92- assertThat (message .getContent ()).isNotEmpty ();
97+ assertThat (completion .getChoice ().getMessage ().getRole ()).isEqualTo (ASSISTANT );
98+ assertThat (completion .getContent ()).isNotEmpty ();
9399 }
94100
95101 @ Test
0 commit comments