Spaces:
Paused
Paused
File size: 36,943 Bytes
4b03eed | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 | # -*- coding: utf-8 -*-
"""Comprehensive formatter unit tests for DashScopeChatFormatter and
DashScopeMultiAgentFormatter, following the reference test style with exact
ground-truth comparisons.
"""
from unittest import IsolatedAsyncioTestCase
from unittest.mock import patch
from agentscope.formatter import (
DashScopeChatFormatter,
DashScopeMultiAgentFormatter,
)
from agentscope.message import (
UserMsg,
AssistantMsg,
SystemMsg,
TextBlock,
DataBlock,
ToolCallBlock,
ToolResultBlock,
ToolResultState,
Base64Source,
URLSource,
ThinkingBlock,
HintBlock,
)
# A fixed short-uuid used to make promote-to-multimodal tests deterministic.
_FIXED_ID = "TESTID1234567"
class TestDashScopeFormatter(IsolatedAsyncioTestCase):
"""Comprehensive tests for DashScope Chat and MultiAgent formatters."""
async def asyncSetUp(self) -> None:
"""Set up shared message fixtures and expected ground-truth dicts."""
# --- URL strings ---
# Normalise through URLSource so that pydantic URL normalisation is
# applied consistently to both input and expected values.
_img_src = URLSource(
url="https://example.com/image.png",
media_type="image/png",
)
_aud_src = URLSource(
url="https://example.com/audio.mp3",
media_type="audio/mpeg",
)
self.image_url = str(_img_src.url)
self.audio_url = str(_aud_src.url)
# --- Base64 fixtures ---
self.image_b64 = "ZmFrZSBpbWFnZSBkYXRh"
self.image_data_uri = f"data:image/png;base64,{self.image_b64}"
# ---------------------------------------------------------------
# Message fixtures
# ---------------------------------------------------------------
self.msgs_system = [
SystemMsg(
name="system",
content="You're a helpful assistant.",
),
]
self.msgs_conversation = [
UserMsg(
name="user",
content=[
TextBlock(text="What is the capital of France?"),
DataBlock(
source=URLSource(
url=self.image_url,
media_type="image/png",
),
),
],
),
AssistantMsg(
name="assistant",
content="The capital of France is Paris.",
),
UserMsg(
name="user",
content=[
TextBlock(text="What is the capital of Germany?"),
DataBlock(
source=URLSource(
url=self.audio_url,
media_type="audio/mpeg",
),
),
],
),
AssistantMsg(
name="assistant",
content="The capital of Germany is Berlin.",
),
UserMsg(
name="user",
content="What is the capital of Japan?",
),
]
# Messages with ToolResultBlock must use role="assistant" because the
# system-role validator rejects non-text blocks.
self.msgs_tools = [
AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
id="call_1",
name="get_capital",
input='{"country": "Japan"}',
),
ToolResultBlock(
id="call_1",
name="get_capital",
output=[
TextBlock(text="The capital of Japan is Tokyo."),
],
state=ToolResultState.SUCCESS,
),
TextBlock(text="The capital of Japan is Tokyo."),
],
),
]
# ---------------------------------------------------------------
# Ground truth: DashScopeChatFormatter (OpenAI-compatible format)
# - Content blocks use {"type": "text", "text": ...} format.
# - Images use {"type": "image_url", "image_url": {"url": ...}}.
# - Audio uses {"type": "input_audio", "input_audio": {...}}.
# - Tool-result content is a plain string.
# ---------------------------------------------------------------
self.gt_chat = [
{
"role": "system",
"content": [
{"type": "text", "text": "You're a helpful assistant."},
],
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is the capital of France?",
},
{
"type": "image_url",
"image_url": {"url": self.image_url},
},
],
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The capital of France is Paris.",
},
],
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is the capital of Germany?",
},
{
"type": "input_audio",
"input_audio": {
"data": self.audio_url,
"format": "mpeg",
},
},
],
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The capital of Germany is Berlin.",
},
],
},
{
"role": "user",
"content": [
{
"type": "text",
"text": "What is the capital of Japan?",
},
],
},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {
"name": "get_capital",
"arguments": '{"country": "Japan"}',
},
},
],
},
{
"role": "tool",
"tool_call_id": "call_1",
"content": "The capital of Japan is Tokyo.",
"name": "get_capital",
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "The capital of Japan is Tokyo.",
},
],
},
]
# ---------------------------------------------------------------
# Ground truth: DashScopeMultiAgentFormatter
# - System content is a plain string (via get_text_content()).
# - Conversation history is collapsed into a single user message.
# ---------------------------------------------------------------
_hist_prompt = (
DashScopeMultiAgentFormatter().conversation_history_prompt
)
self._gt_trailing_asst = {
"role": "assistant",
"content": [
{
"type": "text",
"text": "The capital of Japan is Tokyo.",
},
],
}
self._gt_tool_call = {
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {
"name": "get_capital",
"arguments": '{"country": "Japan"}',
},
},
],
}
self._gt_tool_result = {
"role": "tool",
"tool_call_id": "call_1",
"content": "The capital of Japan is Tokyo.",
"name": "get_capital",
}
self.gt_multiagent = [
{
"role": "system",
"content": "You're a helpful assistant.",
},
{
"role": "user",
"content": [
{
"type": "text",
"text": (
_hist_prompt + "<history>\n"
"user: What is the capital of France?\n"
"assistant: The capital of France is Paris.\n"
"user: What is the capital of Germany?\n"
"assistant: The capital of Germany is Berlin.\n"
"user: What is the capital of Japan?\n"
"</history>"
),
},
{
"type": "image_url",
"image_url": {"url": self.image_url},
},
{
"type": "input_audio",
"input_audio": {
"data": self.audio_url,
"format": "mpeg",
},
},
],
},
self._gt_tool_call,
self._gt_tool_result,
self._gt_trailing_asst,
]
# -------------------------------------------------------------------
# DashScopeChatFormatter tests
# -------------------------------------------------------------------
async def test_chat_formatter(self) -> None:
"""Chat formatter produces exact output for various subsets."""
fmt = DashScopeChatFormatter()
# Full history
res = await fmt.format(
[*self.msgs_system, *self.msgs_conversation, *self.msgs_tools],
)
self.assertListEqual(self.gt_chat, res)
# Without system
res = await fmt.format([*self.msgs_conversation, *self.msgs_tools])
self.assertListEqual(self.gt_chat[1:], res)
# Without conversation
n_tools_gt = len(self.gt_chat) - 1 - len(self.msgs_conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[self.gt_chat[0]] + self.gt_chat[-n_tools_gt:],
res,
)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
self.assertListEqual(self.gt_chat[:-n_tools_gt], res)
# Empty
res = await fmt.format([])
self.assertListEqual([], res)
async def test_chat_formatter_base64_image(self) -> None:
"""Base64-encoded image is inlined as a data URI."""
fmt = DashScopeChatFormatter()
msgs = [
UserMsg(
name="user",
content=[
TextBlock(text="What's in this image?"),
DataBlock(
source=Base64Source(
data=self.image_b64,
media_type="image/png",
),
),
],
),
]
res = await fmt.format(msgs)
self.assertListEqual(
[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {"url": self.image_data_uri},
},
],
},
],
res,
)
@patch(
"agentscope.formatter._formatter_base.shortuuid.uuid",
return_value=_FIXED_ID,
)
async def test_chat_formatter_url_image_in_tool_result(
self,
_mock_uuid: object,
) -> None:
"""URL images in tool results are promoted to a follow-up user message.
The textual part of the tool result contains a system-reminder with a
unique identifier; the identifier is mocked to be deterministic.
"""
fmt = DashScopeChatFormatter()
msgs = [
AssistantMsg(
name="assistant",
content=[
ToolCallBlock(
id="call_img",
name="get_map",
input='{"city": "Tokyo"}',
),
ToolResultBlock(
id="call_img",
name="get_map",
output=[
TextBlock(text="Here is the map."),
DataBlock(
source=URLSource(
url=self.image_url,
media_type="image/png",
),
),
],
state=ToolResultState.SUCCESS,
),
TextBlock(text="Here is the map of Tokyo."),
],
),
]
res = await fmt.format(msgs)
expected_tool_content = (
"Here is the map.\n"
f"<system-reminder>A(n) image file is returned "
f"and will be presented to you with the identifier "
f"[{_FIXED_ID}].</system-reminder>"
)
self.assertListEqual(
[
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_img",
"type": "function",
"function": {
"name": "get_map",
"arguments": '{"city": "Tokyo"}',
},
},
],
},
{
"role": "tool",
"tool_call_id": "call_img",
"content": expected_tool_content,
"name": "get_map",
},
{
"role": "user",
"content": [
{
"type": "text",
"text": (
"<system-reminder>The multimodal data "
"and their identifiers are listed as "
"follows:"
),
},
{
"type": "text",
"text": f"- {_FIXED_ID} (image file): ",
},
{
"type": "image_url",
"image_url": {"url": self.image_url},
},
{
"type": "text",
"text": "</system-reminder>",
},
],
},
{
"role": "assistant",
"content": [
{
"type": "text",
"text": "Here is the map of Tokyo.",
},
],
},
],
res,
)
async def test_chat_formatter_thinking_dropped_without_flag(self) -> None:
"""ThinkingBlock is silently dropped when application/x-thinking is
absent from input_types."""
fmt = DashScopeChatFormatter()
msgs = [
AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
TextBlock(text="reply"),
],
),
]
res = await fmt.format(msgs)
self.assertListEqual(
[
{
"role": "assistant",
"content": [{"type": "text", "text": "reply"}],
},
],
res,
)
async def test_chat_formatter_thinking_becomes_reasoning_content(
self,
) -> None:
"""ThinkingBlock becomes reasoning_content when application/x-thinking
is in input_types."""
fmt = DashScopeChatFormatter(
input_types=["text/plain", "application/x-thinking"],
)
msgs = [
AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="inner thoughts"),
TextBlock(text="reply"),
],
),
]
res = await fmt.format(msgs)
self.assertListEqual(
[
{
"role": "assistant",
"content": [{"type": "text", "text": "reply"}],
"reasoning_content": "inner thoughts",
},
],
res,
)
async def test_chat_formatter_multiple_thinking_blocks_joined(
self,
) -> None:
"""Multiple ThinkingBlocks are joined with a newline into a single
reasoning_content field."""
fmt = DashScopeChatFormatter(
input_types=["text/plain", "application/x-thinking"],
)
msgs = [
AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="part one"),
ThinkingBlock(thinking="part two"),
TextBlock(text="answer"),
],
),
]
res = await fmt.format(msgs)
self.assertListEqual(
[
{
"role": "assistant",
"content": [{"type": "text", "text": "answer"}],
"reasoning_content": "part one\npart two",
},
],
res,
)
# -------------------------------------------------------------------
# DashScopeMultiAgentFormatter tests
# -------------------------------------------------------------------
async def test_multiagent_formatter(self) -> None:
"""MultiAgent formatter produces exact output for various subsets."""
fmt = DashScopeMultiAgentFormatter()
# Full history
res = await fmt.format(
[*self.msgs_system, *self.msgs_conversation, *self.msgs_tools],
)
self.assertListEqual(self.gt_multiagent, res)
# Without system
res = await fmt.format([*self.msgs_conversation, *self.msgs_tools])
self.assertListEqual(self.gt_multiagent[1:], res)
# Without tools
res = await fmt.format([*self.msgs_system, *self.msgs_conversation])
self.assertListEqual(self.gt_multiagent[:2], res)
# System only
res = await fmt.format(self.msgs_system)
self.assertListEqual([self.gt_multiagent[0]], res)
# Conversation only
res = await fmt.format(self.msgs_conversation)
self.assertListEqual([self.gt_multiagent[1]], res)
# Tools only — no prior agent_message group, so the trailing assistant
# is formatted with is_first=True (includes the full history prompt).
res = await fmt.format(self.msgs_tools)
self.assertListEqual(
[
self._gt_tool_call,
self._gt_tool_result,
self._gt_trailing_asst,
],
res,
)
# System + tools (no conversation)
res = await fmt.format([*self.msgs_system, *self.msgs_tools])
self.assertListEqual(
[
self.gt_multiagent[0],
self._gt_tool_call,
self._gt_tool_result,
self._gt_trailing_asst,
],
res,
)
# Empty
res = await fmt.format([])
self.assertListEqual([], res)
async def test_multiagent_formatter_thinking_in_tool_sequence(
self,
) -> None:
"""ThinkingBlocks inside a tool sequence are forwarded as
reasoning_content when application/x-thinking is in input_types."""
fmt = DashScopeMultiAgentFormatter(
input_types=["text/plain", "application/x-thinking"],
)
tc = ToolCallBlock(
id="call_1",
name="get_capital",
input='{"country": "Japan"}',
)
tr = ToolResultBlock(
id="call_1",
name="get_capital",
output=[TextBlock(text="Tokyo")],
state=ToolResultState.SUCCESS,
)
msgs = [
AssistantMsg(
name="assistant",
content=[ThinkingBlock(thinking="Need to check"), tc, tr],
),
]
res = await fmt.format(msgs)
asst_msgs = [m for m in res if m.get("role") == "assistant"]
self.assertListEqual(
[m["reasoning_content"] for m in asst_msgs],
["Need to check"],
)
async def test_chat_formatter_complex_multi_step(self) -> None:
"""Complex multi-step sequence with interleaved thinking, text,
tool calls, and tool results (thinking dropped without flag)."""
fmt = DashScopeChatFormatter()
msgs = [
AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="thinking_1"),
TextBlock(text="text_1"),
ToolCallBlock(
id="call_1",
name="func_1",
input='{"arg": "value1"}',
),
ToolCallBlock(
id="call_2",
name="func_2",
input='{"arg": "value2"}',
),
ToolResultBlock(
id="call_1",
name="func_1",
output=[TextBlock(text="result_1")],
state=ToolResultState.SUCCESS,
),
ToolResultBlock(
id="call_2",
name="func_2",
output=[TextBlock(text="result_2")],
state=ToolResultState.SUCCESS,
),
ThinkingBlock(thinking="thinking_2"),
TextBlock(text="text_2"),
ToolCallBlock(
id="call_3",
name="func_3",
input='{"arg": "value3"}',
),
ToolResultBlock(
id="call_3",
name="func_3",
output=[TextBlock(text="result_3")],
state=ToolResultState.SUCCESS,
),
ToolCallBlock(
id="call_4",
name="func_4",
input='{"arg": "value4"}',
),
ToolResultBlock(
id="call_4",
name="func_4",
output=[TextBlock(text="result_4")],
state=ToolResultState.SUCCESS,
),
ThinkingBlock(thinking="thinking_3"),
TextBlock(text="text_3"),
],
),
]
res = await fmt.format(msgs)
self.assertListEqual(
[
{
"role": "assistant",
"content": [{"type": "text", "text": "text_1"}],
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {
"name": "func_1",
"arguments": '{"arg": "value1"}',
},
},
{
"id": "call_2",
"type": "function",
"function": {
"name": "func_2",
"arguments": '{"arg": "value2"}',
},
},
],
},
{
"role": "tool",
"tool_call_id": "call_1",
"content": "result_1",
"name": "func_1",
},
{
"role": "tool",
"tool_call_id": "call_2",
"content": "result_2",
"name": "func_2",
},
{
"role": "assistant",
"content": [{"type": "text", "text": "text_2"}],
"tool_calls": [
{
"id": "call_3",
"type": "function",
"function": {
"name": "func_3",
"arguments": '{"arg": "value3"}',
},
},
],
},
{
"role": "tool",
"tool_call_id": "call_3",
"content": "result_3",
"name": "func_3",
},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_4",
"type": "function",
"function": {
"name": "func_4",
"arguments": '{"arg": "value4"}',
},
},
],
},
{
"role": "tool",
"tool_call_id": "call_4",
"content": "result_4",
"name": "func_4",
},
{
"role": "assistant",
"content": [{"type": "text", "text": "text_3"}],
},
],
res,
)
async def test_chat_formatter_complex_multi_step_with_thinking(
self,
) -> None:
"""Complex multi-step sequence with thinking preserved via
application/x-thinking flag."""
fmt = DashScopeChatFormatter(
input_types=["text/plain", "application/x-thinking"],
)
msgs = [
AssistantMsg(
name="assistant",
content=[
ThinkingBlock(thinking="thinking_1"),
TextBlock(text="text_1"),
ToolCallBlock(
id="call_1",
name="func_1",
input='{"arg": "value1"}',
),
ToolCallBlock(
id="call_2",
name="func_2",
input='{"arg": "value2"}',
),
ToolResultBlock(
id="call_1",
name="func_1",
output=[TextBlock(text="result_1")],
state=ToolResultState.SUCCESS,
),
ToolResultBlock(
id="call_2",
name="func_2",
output=[TextBlock(text="result_2")],
state=ToolResultState.SUCCESS,
),
ThinkingBlock(thinking="thinking_2"),
TextBlock(text="text_2"),
ToolCallBlock(
id="call_3",
name="func_3",
input='{"arg": "value3"}',
),
ToolResultBlock(
id="call_3",
name="func_3",
output=[TextBlock(text="result_3")],
state=ToolResultState.SUCCESS,
),
ToolCallBlock(
id="call_4",
name="func_4",
input='{"arg": "value4"}',
),
ToolResultBlock(
id="call_4",
name="func_4",
output=[TextBlock(text="result_4")],
state=ToolResultState.SUCCESS,
),
ThinkingBlock(thinking="thinking_3"),
TextBlock(text="text_3"),
],
),
]
res = await fmt.format(msgs)
self.assertListEqual(
[
{
"role": "assistant",
"content": [{"type": "text", "text": "text_1"}],
"tool_calls": [
{
"id": "call_1",
"type": "function",
"function": {
"name": "func_1",
"arguments": '{"arg": "value1"}',
},
},
{
"id": "call_2",
"type": "function",
"function": {
"name": "func_2",
"arguments": '{"arg": "value2"}',
},
},
],
"reasoning_content": "thinking_1",
},
{
"role": "tool",
"tool_call_id": "call_1",
"content": "result_1",
"name": "func_1",
},
{
"role": "tool",
"tool_call_id": "call_2",
"content": "result_2",
"name": "func_2",
},
{
"role": "assistant",
"content": [{"type": "text", "text": "text_2"}],
"tool_calls": [
{
"id": "call_3",
"type": "function",
"function": {
"name": "func_3",
"arguments": '{"arg": "value3"}',
},
},
],
"reasoning_content": "thinking_2",
},
{
"role": "tool",
"tool_call_id": "call_3",
"content": "result_3",
"name": "func_3",
},
{
"role": "assistant",
"content": None,
"tool_calls": [
{
"id": "call_4",
"type": "function",
"function": {
"name": "func_4",
"arguments": '{"arg": "value4"}',
},
},
],
},
{
"role": "tool",
"tool_call_id": "call_4",
"content": "result_4",
"name": "func_4",
},
{
"role": "assistant",
"content": [{"type": "text", "text": "text_3"}],
"reasoning_content": "thinking_3",
},
],
res,
)
async def test_chat_formatter_hint_block(self) -> None:
"""HintBlock flushes preceding content and becomes a user message."""
fmt = DashScopeChatFormatter()
msgs = [
AssistantMsg(
name="assistant",
content=[
TextBlock(text="Let me think about that."),
HintBlock(hint="Remember to be concise."),
TextBlock(text="Here is my answer."),
],
),
]
res = await fmt.format(msgs)
self.assertListEqual(
[
{
"role": "assistant",
"content": [
{"type": "text", "text": "Let me think about that."},
],
},
{
"role": "user",
"content": [
{"type": "text", "text": "Remember to be concise."},
],
},
{
"role": "assistant",
"content": [
{"type": "text", "text": "Here is my answer."},
],
},
],
res,
)
async def test_chat_formatter_hint_block_multimodal(self) -> None:
"""Multimodal HintBlock becomes a single user message with text
+ image."""
fmt = DashScopeChatFormatter()
msgs = [
AssistantMsg(
name="assistant",
content=[
HintBlock(
hint=[
TextBlock(text="Inspect this screenshot:"),
DataBlock(
source=Base64Source(
data=self.image_b64,
media_type="image/png",
),
),
],
),
],
),
]
res = await fmt.format(msgs)
self.assertListEqual(
[
{
"role": "user",
"content": [
{
"type": "text",
"text": "Inspect this screenshot:",
},
{
"type": "image_url",
"image_url": {"url": self.image_data_uri},
},
],
},
],
res,
)
|