mingshan hanseungwook commited on
Commit
9d280ff
·
1 Parent(s): d6e5432

Support OpenAI-style text content lists (#7)

Browse files

- Support OpenAI-style text content lists (f3f919efceb5a91e3ec20424ad93dde931199cea)


Co-authored-by: Seungwook Han <hanseungwook@users.noreply.huggingface.co>

Files changed (1) hide show
  1. chat_template.jinja +27 -7
chat_template.jinja CHANGED
@@ -888,6 +888,26 @@ false
888
  {{- "\n</ifm|tool_calls>" }}
889
  {%- endmacro -%}
890
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
891
  {%- macro render_tool_response_messages(raw_content) -%}
892
  {%- if raw_content is string -%}
893
  {{- '<|ifm|im_start|>tool\n' + raw_content + '<|ifm|im_end|>' }}
@@ -916,22 +936,22 @@ false
916
  {%- if (not available_tools) and messages[0].role == 'system' and messages[0].get('tools') -%}
917
  {%- set available_tools = messages[0]['tools'] -%}
918
  {%- endif -%}
 
 
 
 
919
  {%- if available_tools -%}
920
  {{- validate_tools(available_tools, tool_presentation_fmt != 'json') }}
921
- {%- set system_content = '' -%}
922
- {%- if messages[0].role == 'system' and messages[0].content -%}
923
- {%- set system_content = messages[0].content -%}
924
- {%- endif -%}
925
  {{- render_system_with_tools(available_tools, system_content, tool_presentation_fmt, tool_call_fmt) }}
926
  {%- else -%}
927
  {%- if messages[0].role == 'system' -%}
928
- {{- '<|ifm|im_start|>system\n' + messages[0].content + '<|ifm|im_end|>' }}
929
  {%- endif -%}
930
  {%- endif -%}
931
 
932
  {%- for message in messages -%}
933
- {%- if message.content is string -%}
934
- {%- set content = message.content -%}
935
  {%- else -%}
936
  {%- set content = '' -%}
937
  {%- endif -%}
 
888
  {{- "\n</ifm|tool_calls>" }}
889
  {%- endmacro -%}
890
 
891
+ {#- Normalize text content for user, system, and assistant messages. File text -#}
892
+ {#- must be extracted upstream and supplied as strings or typed text parts. -#}
893
+ {%- macro render_text_content(raw_content, role) -%}
894
+ {%- if raw_content is string -%}
895
+ {{- raw_content -}}
896
+ {%- elif role == 'assistant' and (raw_content is none or raw_content is undefined) -%}
897
+ {{- '' -}}
898
+ {%- elif raw_content is defined and raw_content is sequence and raw_content is not mapping -%}
899
+ {%- for part in raw_content -%}
900
+ {%- if part is mapping and part.type == 'text' and part.text is string -%}
901
+ {{- part.text -}}
902
+ {%- else -%}
903
+ {{- raise_exception("Unsupported " ~ role ~ " content part at index " ~ loop.index0 ~ ". Expected type='text' with a string text field; extract file contents to text before templating.") -}}
904
+ {%- endif -%}
905
+ {%- endfor -%}
906
+ {%- else -%}
907
+ {{- raise_exception("Unsupported " ~ role ~ " message content. Expected a string or a list of text parts.") -}}
908
+ {%- endif -%}
909
+ {%- endmacro -%}
910
+
911
  {%- macro render_tool_response_messages(raw_content) -%}
912
  {%- if raw_content is string -%}
913
  {{- '<|ifm|im_start|>tool\n' + raw_content + '<|ifm|im_end|>' }}
 
936
  {%- if (not available_tools) and messages[0].role == 'system' and messages[0].get('tools') -%}
937
  {%- set available_tools = messages[0]['tools'] -%}
938
  {%- endif -%}
939
+ {%- set system_content = '' -%}
940
+ {%- if messages[0].role == 'system' -%}
941
+ {%- set system_content = render_text_content(messages[0].content, 'system') -%}
942
+ {%- endif -%}
943
  {%- if available_tools -%}
944
  {{- validate_tools(available_tools, tool_presentation_fmt != 'json') }}
 
 
 
 
945
  {{- render_system_with_tools(available_tools, system_content, tool_presentation_fmt, tool_call_fmt) }}
946
  {%- else -%}
947
  {%- if messages[0].role == 'system' -%}
948
+ {{- '<|ifm|im_start|>system\n' + system_content + '<|ifm|im_end|>' }}
949
  {%- endif -%}
950
  {%- endif -%}
951
 
952
  {%- for message in messages -%}
953
+ {%- if message.role == 'user' or message.role == 'assistant' or (message.role == 'system' and not loop.first) -%}
954
+ {%- set content = render_text_content(message.content, message.role) -%}
955
  {%- else -%}
956
  {%- set content = '' -%}
957
  {%- endif -%}