Skip to content

Commit bcc4a49

Browse files
fix: Bug OpenAI AssistantMessage (#511)
* Creation of the ete test as well as fixing the empty tools array bug. * Editing the release notes as well as index.html file of fix: Bug OpenAI AssistantMessage #511 pull request. * Adding the messagesHistory method in the OpenAiController. * Adding the messagesHistory method in the OpenAiController. * Formatting * Moving created methods and tests to v2. * Formatting * Moving created methods and tests to v2. --------- Co-authored-by: SAP Cloud SDK Bot <[email protected]>
1 parent 34d8549 commit bcc4a49

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

docs/release_notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424

2525
### 🐛 Fixed Issues
2626

27-
-
27+
- OpenAi: Fix AssistantMessage Bug by now being able to send Assistant Messages using our API Client.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ public class OpenAiAssistantMessage implements OpenAiMessage {
7272
ChatCompletionRequestAssistantMessage createChatCompletionRequestMessage() {
7373
final var message =
7474
new ChatCompletionRequestAssistantMessage()
75-
.role(ChatCompletionRequestAssistantMessage.RoleEnum.fromValue(role()));
75+
.role(ChatCompletionRequestAssistantMessage.RoleEnum.fromValue(role()))
76+
.toolCalls(null);
7677

7778
final var items = content().items();
7879
if (!items.isEmpty() && items.get(0) instanceof OpenAiTextItem textItem) {

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.stream.Stream;
2020
import javax.annotation.Nonnull;
2121
import lombok.extern.slf4j.Slf4j;
22+
import lombok.val;
2223
import org.springframework.stereotype.Service;
2324

2425
/** Service class for OpenAI service using latest convenience api */
@@ -37,6 +38,28 @@ public OpenAiChatCompletionResponse chatCompletion(@Nonnull final String prompt)
3738
.chatCompletion(new OpenAiChatCompletionRequest(prompt));
3839
}
3940

41+
/**
42+
* Chat requests to OpenAI and updating the messages history
43+
*
44+
* @param previousMessage The request to send to the assistant
45+
* @return the assistant message response
46+
*/
47+
@Nonnull
48+
public OpenAiChatCompletionResponse messagesHistory(@Nonnull final String previousMessage) {
49+
val messagesList = new ArrayList<OpenAiMessage>();
50+
messagesList.add(OpenAiMessage.user(previousMessage));
51+
52+
final OpenAiChatCompletionResponse result =
53+
OpenAiClient.forModel(GPT_4O_MINI)
54+
.chatCompletion(new OpenAiChatCompletionRequest(messagesList));
55+
56+
messagesList.add(result.getMessage());
57+
messagesList.add(OpenAiMessage.user("What is the typical food there?"));
58+
59+
return OpenAiClient.forModel(GPT_4O_MINI)
60+
.chatCompletion(new OpenAiChatCompletionRequest(messagesList));
61+
}
62+
4063
/**
4164
* Asynchronous stream of an OpenAI chat request
4265
*

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ void chatCompletion() {
3333
assertThat(completion.getContent()).isNotEmpty();
3434
}
3535

36+
@Test
37+
void testMessagesHistory() {
38+
assertThat(service.messagesHistory("What is the capital of France?").getContent()).isNotEmpty();
39+
}
40+
3641
@Test
3742
void chatCompletionImage() {
3843
final var completion =

0 commit comments

Comments
 (0)