|
34 | 34 | import com.google.genai.types.FunctionResponse; |
35 | 35 | import com.google.genai.types.GenerateContentConfig; |
36 | 36 | import com.google.genai.types.Part; |
| 37 | +import com.google.genai.types.Schema; |
37 | 38 | import com.google.genai.types.Tool; |
38 | 39 | import com.google.genai.types.ToolConfig; |
39 | 40 | import java.util.AbstractMap; |
@@ -567,6 +568,84 @@ public void testFromLlmRequest_withFunctionCall() throws Exception { |
567 | 568 | assertThat(msg.toolCalls.get(0).function.arguments).isEqualTo("{\"location\":\"Paris\"}"); |
568 | 569 | } |
569 | 570 |
|
| 571 | + @Test |
| 572 | + public void testFromLlmRequest_withAbsentFunctionArguments() throws Exception { |
| 573 | + FunctionCall functionCall = FunctionCall.builder().id("call_123").name("get_time").build(); |
| 574 | + Part part = Part.builder().functionCall(functionCall).build(); |
| 575 | + Content content = Content.builder().role("model").parts(ImmutableList.of(part)).build(); |
| 576 | + |
| 577 | + LlmRequest llmRequest = |
| 578 | + LlmRequest.builder().model("gemini-1.5-pro").contents(ImmutableList.of(content)).build(); |
| 579 | + |
| 580 | + ChatCompletionsRequest request = ChatCompletionsRequest.fromLlmRequest(llmRequest, false); |
| 581 | + |
| 582 | + assertThat(request.messages).hasSize(1); |
| 583 | + ChatCompletionsRequest.Message msg = request.messages.get(0); |
| 584 | + assertThat(msg.role).isEqualTo("assistant"); |
| 585 | + assertThat(msg.toolCalls).hasSize(1); |
| 586 | + assertThat(msg.toolCalls.get(0).function.name).isEqualTo("get_time"); |
| 587 | + assertThat(msg.toolCalls.get(0).function.arguments).isEqualTo("{}"); |
| 588 | + } |
| 589 | + |
| 590 | + @Test |
| 591 | + public void testFromLlmRequest_withAbsentParameters() throws Exception { |
| 592 | + FunctionDeclaration function = |
| 593 | + FunctionDeclaration.builder().name("test_func").description("A test function").build(); |
| 594 | + |
| 595 | + Tool tool = Tool.builder().functionDeclarations(ImmutableList.of(function)).build(); |
| 596 | + GenerateContentConfig config = |
| 597 | + GenerateContentConfig.builder().tools(ImmutableList.of(tool)).build(); |
| 598 | + |
| 599 | + LlmRequest llmRequest = |
| 600 | + LlmRequest.builder() |
| 601 | + .model("gemini-1.5-pro") |
| 602 | + .config(config) |
| 603 | + .contents(ImmutableList.of()) |
| 604 | + .build(); |
| 605 | + |
| 606 | + ChatCompletionsRequest request = ChatCompletionsRequest.fromLlmRequest(llmRequest, false); |
| 607 | + |
| 608 | + assertThat(request.tools).hasSize(1); |
| 609 | + Map<String, Object> params = (Map<String, Object>) request.tools.get(0).function.parameters; |
| 610 | + assertThat(params.get("type")).isEqualTo("object"); |
| 611 | + @SuppressWarnings("unchecked") |
| 612 | + Map<String, Object> props = (Map<String, Object>) params.get("properties"); |
| 613 | + assertThat(props).isEmpty(); |
| 614 | + } |
| 615 | + |
| 616 | + @Test |
| 617 | + public void testFromLlmRequest_normalizesSchemaTypeToLowerCase() throws Exception { |
| 618 | + Schema param1Schema = Schema.builder().type("STRING").build(); |
| 619 | + |
| 620 | + Schema functionSchema = |
| 621 | + Schema.builder().type("OBJECT").properties(ImmutableMap.of("param1", param1Schema)).build(); |
| 622 | + |
| 623 | + FunctionDeclaration function = |
| 624 | + FunctionDeclaration.builder().name("test_func").parameters(functionSchema).build(); |
| 625 | + |
| 626 | + Tool tool = Tool.builder().functionDeclarations(ImmutableList.of(function)).build(); |
| 627 | + GenerateContentConfig config = |
| 628 | + GenerateContentConfig.builder().tools(ImmutableList.of(tool)).build(); |
| 629 | + |
| 630 | + LlmRequest llmRequest = |
| 631 | + LlmRequest.builder() |
| 632 | + .model("gemini-1.5-pro") |
| 633 | + .config(config) |
| 634 | + .contents(ImmutableList.of()) |
| 635 | + .build(); |
| 636 | + |
| 637 | + ChatCompletionsRequest request = ChatCompletionsRequest.fromLlmRequest(llmRequest, false); |
| 638 | + |
| 639 | + assertThat(request.tools).hasSize(1); |
| 640 | + Map<String, Object> params = (Map<String, Object>) request.tools.get(0).function.parameters; |
| 641 | + assertThat(params.get("type")).isEqualTo("object"); |
| 642 | + @SuppressWarnings("unchecked") |
| 643 | + Map<String, Object> props = (Map<String, Object>) params.get("properties"); |
| 644 | + @SuppressWarnings("unchecked") |
| 645 | + Map<String, Object> param1 = (Map<String, Object>) props.get("param1"); |
| 646 | + assertThat(param1.get("type")).isEqualTo("string"); |
| 647 | + } |
| 648 | + |
570 | 649 | @Test |
571 | 650 | public void testFromLlmRequest_withStreamOptions() throws Exception { |
572 | 651 | LlmRequest llmRequest = |
@@ -628,11 +707,11 @@ public void testFromLlmRequest_withFunctionResponse() throws Exception { |
628 | 707 |
|
629 | 708 | assertThat(request.messages.get(1).role).isEqualTo("tool"); |
630 | 709 | assertThat(request.messages.get(1).toolCallId).isEmpty(); |
631 | | - assertThat(request.messages.get(1).content).isNull(); |
| 710 | + assertThat(request.messages.get(1).content.getValue()).isEqualTo("{}"); |
632 | 711 |
|
633 | 712 | assertThat(request.messages.get(2).role).isEqualTo("tool"); |
634 | 713 | assertThat(request.messages.get(2).toolCallId).isEqualTo("call_faulty"); |
635 | | - assertThat(request.messages.get(2).content).isNull(); |
| 714 | + assertThat(request.messages.get(2).content.getValue()).isEqualTo("{}"); |
636 | 715 | } |
637 | 716 |
|
638 | 717 | @Test |
|
0 commit comments