|
4 | 4 |
|
5 | 5 | import com.sap.ai.sdk.foundationmodels.openai.generated.model.ChatCompletionRequestUserMessage; |
6 | 6 | import com.sap.ai.sdk.foundationmodels.openai.generated.model.ChatCompletionRequestUserMessageContent; |
| 7 | +import com.sap.ai.sdk.foundationmodels.openai.generated.model.ChatCompletionTool; |
7 | 8 | import com.sap.ai.sdk.foundationmodels.openai.generated.model.ChatCompletionToolChoiceOption; |
8 | 9 | import com.sap.ai.sdk.foundationmodels.openai.generated.model.CreateChatCompletionRequestAllOfStop; |
9 | 10 | import java.math.BigDecimal; |
10 | 11 | import java.util.ArrayList; |
11 | 12 | import java.util.List; |
| 13 | +import java.util.Map; |
12 | 14 | import org.junit.jupiter.api.Test; |
13 | 15 |
|
14 | 16 | class OpenAiChatCompletionRequestTest { |
@@ -114,4 +116,45 @@ void messageListExternallyUnmodifiable() { |
114 | 116 | .as("Modifying the original list should not affect the messages in the request object.") |
115 | 117 | .hasSize(1); |
116 | 118 | } |
| 119 | + |
| 120 | + @Test |
| 121 | + void withOpenAiTools() { |
| 122 | + record DummyRequest(String param1, int param2) {} |
| 123 | + |
| 124 | + var request = |
| 125 | + new OpenAiChatCompletionRequest(OpenAiMessage.user("Hello, world")) |
| 126 | + .withOpenAiTools( |
| 127 | + List.of( |
| 128 | + new OpenAiFunctionTool("toolA", DummyRequest.class) |
| 129 | + .withDescription("descA") |
| 130 | + .withStrict(true), |
| 131 | + new OpenAiFunctionTool("toolB", String.class) |
| 132 | + .withDescription("descB") |
| 133 | + .withStrict(false))); |
| 134 | + |
| 135 | + var lowLevelRequest = request.createCreateChatCompletionRequest(); |
| 136 | + assertThat(lowLevelRequest.getTools()).hasSize(2); |
| 137 | + |
| 138 | + var toolA = lowLevelRequest.getTools().get(0); |
| 139 | + assertThat(toolA).isInstanceOf(ChatCompletionTool.class); |
| 140 | + assertThat(toolA.getType()).isEqualTo(ChatCompletionTool.TypeEnum.FUNCTION); |
| 141 | + assertThat(toolA.getFunction().getName()).isEqualTo("toolA"); |
| 142 | + assertThat(toolA.getFunction().getDescription()).isEqualTo("descA"); |
| 143 | + assertThat(toolA.getFunction().isStrict()).isTrue(); |
| 144 | + assertThat(toolA.getFunction().getParameters()) |
| 145 | + .isEqualTo( |
| 146 | + Map.of( |
| 147 | + "properties", |
| 148 | + Map.of("param1", Map.of("type", "string"), "param2", Map.of("type", "integer")), |
| 149 | + "type", |
| 150 | + "object")); |
| 151 | + |
| 152 | + var toolB = lowLevelRequest.getTools().get(1); |
| 153 | + assertThat(toolB).isInstanceOf(ChatCompletionTool.class); |
| 154 | + assertThat(toolB.getType()).isEqualTo(ChatCompletionTool.TypeEnum.FUNCTION); |
| 155 | + assertThat(toolB.getFunction().getName()).isEqualTo("toolB"); |
| 156 | + assertThat(toolB.getFunction().getDescription()).isEqualTo("descB"); |
| 157 | + assertThat(toolB.getFunction().isStrict()).isFalse(); |
| 158 | + assertThat(toolB.getFunction().getParameters()).isEqualTo(Map.of("type", "string")); |
| 159 | + } |
117 | 160 | } |
0 commit comments