|
24 | 24 | {%- endif -%} |
25 | 25 | {%- endmacro -%} |
26 | 26 |
|
| 27 | +{%- set tool_response_queue = namespace(ids=[]) -%} |
| 28 | +{%- set tool_call_counter = namespace(value=0) -%} |
27 | 29 |
|
28 | 30 | {%- macro render_toolcalls(message) -%} |
29 | 31 | <|tool_calls_section_begin|> |
30 | 32 | {%- for tool_call in message['tool_calls'] -%} |
31 | | - <|tool_call_begin|>functions.{{ tool_call['function']['name'] }}:{{ loop.index }}<|tool_call_argument_begin|>{% if tool_call['function']['arguments'] is string %}{{ tool_call['function']['arguments'] }}{% else %}{{ tool_call['function']['arguments'] | tojson }}{% endif %}<|tool_call_end|> |
| 33 | + {%- if tool_call['id'] is defined -%} |
| 34 | + {%- set formatted_id = tool_call['id'] -%} |
| 35 | + {%- else -%} |
| 36 | + {%- set formatted_id = 'functions.' + tool_call['function']['name'] + ':' + (tool_call_counter.value | string) -%} |
| 37 | + {%- set tool_call_counter.value = tool_call_counter.value + 1 -%} |
| 38 | + {%- endif -%} |
| 39 | + {%- set _ = tool_response_queue.ids.append(formatted_id) -%} |
| 40 | + <|tool_call_begin|>{{ formatted_id }}<|tool_call_argument_begin|>{% if tool_call['function']['arguments'] is string %}{{ tool_call['function']['arguments'] }}{% else %}{{ tool_call['function']['arguments'] | tojson }}{% endif %}<|tool_call_end|> |
32 | 41 | {%- endfor -%} |
33 | 42 | <|tool_calls_section_end|> |
34 | 43 | {%- endmacro -%} |
35 | 44 |
|
36 | 45 |
|
37 | 46 | {# Find last non-tool-call assisitant message #} |
38 | | -{%- set ns = namespace(found=false, last_non_tool_call_assistant_msg=-1) -%} |
| 47 | +{%- set ns = namespace(last_non_tool_call_assistant_msg=-1) -%} |
39 | 48 | {%- for idx in range(messages|length-1, -1, -1) -%} |
40 | | - {%- if not ns.found and messages[idx]['role'] == 'assistant' and not messages[idx].get('tool_calls') -%} |
| 49 | + {%- if messages[idx]['role'] == 'assistant' and not messages[idx].get('tool_calls') -%} |
41 | 50 | {%- set ns.last_non_tool_call_assistant_msg = idx -%} |
42 | | - {%- set ns.found = true -%} |
43 | 51 | {%- endif -%} |
44 | 52 | {%- endfor -%} |
45 | 53 |
|
|
48 | 56 | {%- set suffix_msgs = messages[ns.last_non_tool_call_assistant_msg+1:] -%} |
49 | 57 |
|
50 | 58 | {%- if tools -%} |
51 | | - <|im_system|>tool_declare<|im_middle|>{{ tools | tojson }}<|im_end|>{# needs support for tojson(separators=(',', ':')) #} |
| 59 | + <|im_system|>tool_declare<|im_middle|>{{ tools | tojson }}<|im_end|> |
52 | 60 | {%- endif -%} |
53 | 61 |
|
54 | 62 | {%- if messages|length == 0 or messages[0]['role'] != 'system' -%} |
|
63 | 71 | {{render_toolcalls(message)}} |
64 | 72 | {%- endif -%} |
65 | 73 | {%- elif message['role'] == 'tool' -%} |
66 | | - {%- set tool_call_id = message.tool_call_id -%} |
| 74 | + {%- if tool_response_queue.ids -%} |
| 75 | + {%- set tool_call_id = tool_response_queue.ids.pop(0) -%} |
| 76 | + {%- else -%} |
| 77 | + {%- set tool_call_id = 'functions.' + message.get('name', 'unknown') + ':' + (tool_call_counter.value | string) -%} |
| 78 | + {%- endif -%} |
67 | 79 | ## Return of {{ tool_call_id }} |
68 | 80 | {{render_content(message)}} |
69 | 81 | {%- elif message['content'] is not none -%} |
|
81 | 93 | {{render_toolcalls(message)}} |
82 | 94 | {%- endif -%} |
83 | 95 | {%- elif message['role'] == 'tool' -%} |
84 | | - {%- set tool_call_id = message.tool_call_id -%} |
| 96 | + {%- if tool_response_queue.ids -%} |
| 97 | + {%- set tool_call_id = tool_response_queue.ids.pop(0) -%} |
| 98 | + {%- else -%} |
| 99 | + {%- set tool_call_id = 'functions.' + message.get('name', 'unknown') + ':' + (tool_call_counter.value | string) -%} |
| 100 | + {%- endif -%} |
85 | 101 | ## Return of {{ tool_call_id }} |
86 | 102 | {{render_content(message)}} |
87 | 103 | {%- elif message['content'] is not none -%} |
|
0 commit comments