mahmoudhas9 commited on
Commit
bdc41f7
·
verified ·
1 Parent(s): 573fa39

Update chat_template.jinja

Browse files
Files changed (1) hide show
  1. chat_template.jinja +46 -32
chat_template.jinja CHANGED
@@ -1,12 +1,17 @@
1
- {% macro render_extra_keys(json_dict, handled_keys) %}
2
- {%- if json_dict is mapping %}
3
- {%- for json_key in json_dict if json_key not in handled_keys %}
4
- {%- if json_dict[json_key] is mapping or (json_dict[json_key] is sequence and json_dict[json_key] is not string) %}
5
- {{- '\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | tojson | safe) ~ '</' ~ json_key ~ '>' }}
6
- {%- else %}
7
- {{-'\n<' ~ json_key ~ '>' ~ (json_dict[json_key] | string) ~ '</' ~ json_key ~ '>' }}
8
- {%- endif %}
9
- {%- endfor %}
 
 
 
 
 
10
  {%- endif %}
11
  {% endmacro %}
12
 
@@ -29,37 +34,46 @@
29
  {%- endif %}
30
  {%- endif %}
31
  {%- if tools is iterable and tools | length > 0 %}
32
- {{- "\n\n# Tools\n\nYou have access to the following functions:\n\n" }}
33
  {{- "<tools>" }}
34
  {%- for tool in tools %}
35
  {%- if tool.function is defined %}
36
  {%- set tool = tool.function %}
37
  {%- endif %}
38
  {{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
39
- {%- if tool.description is defined %}
40
- {{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
41
- {%- endif %}
42
  {{- '\n<parameters>' }}
43
- {%- if tool.parameters is defined and tool.parameters is mapping and tool.parameters.properties is defined and tool.parameters.properties is mapping %}
44
- {%- for param_name, param_fields in tool.parameters.properties|items %}
45
- {{- '\n<parameter>' }}
46
- {{- '\n<name>' ~ param_name ~ '</name>' }}
47
- {%- if param_fields.type is defined %}
48
- {{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
49
- {%- endif %}
50
- {%- if param_fields.description is defined %}
51
- {{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
 
 
 
 
 
 
 
 
52
  {%- endif %}
53
- {%- set handled_keys = ['name', 'type', 'description'] %}
54
- {{- render_extra_keys(param_fields, handled_keys) }}
55
- {{- '\n</parameter>' }}
56
  {%- endfor %}
57
- {%- endif %}
58
- {% set handled_keys = ['type', 'properties'] %}
59
- {{- render_extra_keys(tool.parameters, handled_keys) }}
 
60
  {{- '\n</parameters>' }}
61
- {%- set handled_keys = ['type', 'name', 'description', 'parameters'] %}
62
- {{- render_extra_keys(tool, handled_keys) }}
 
 
 
 
 
63
  {{- '\n</function>' }}
64
  {%- endfor %}
65
  {{- "\n</tools>" }}
@@ -86,7 +100,7 @@
86
  {%- if tool_call.arguments is defined %}
87
  {%- for args_name, args_value in tool_call.arguments|items %}
88
  {{- '<parameter=' + args_name + '>\n' }}
89
- {%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
90
  {{- args_value }}
91
  {{- '\n</parameter>\n' }}
92
  {%- endfor %}
@@ -114,4 +128,4 @@
114
  {%- endfor %}
115
  {%- if add_generation_prompt %}
116
  {{- '<|im_start|>assistant\n' }}
117
- {%- endif %}
 
1
+ {% macro render_item_list(item_list, tag_name='required') %}
2
+ {%- if item_list is defined and item_list is iterable and item_list | length > 0 %}
3
+ {%- if tag_name %}{{- '\n<' ~ tag_name ~ '>' -}}{% endif %}
4
+ {{- '[' }}
5
+ {%- for item in item_list -%}
6
+ {%- if loop.index > 1 %}{{- ", "}}{% endif -%}
7
+ {%- if item is string -%}
8
+ {{ "`" ~ item ~ "`" }}
9
+ {%- else -%}
10
+ {{ item }}
11
+ {%- endif -%}
12
+ {%- endfor -%}
13
+ {{- ']' }}
14
+ {%- if tag_name %}{{- '</' ~ tag_name ~ '>' -}}{% endif %}
15
  {%- endif %}
16
  {% endmacro %}
17
 
 
34
  {%- endif %}
35
  {%- endif %}
36
  {%- if tools is iterable and tools | length > 0 %}
37
+ {{- "\n\nYou have access to the following functions:\n\n" }}
38
  {{- "<tools>" }}
39
  {%- for tool in tools %}
40
  {%- if tool.function is defined %}
41
  {%- set tool = tool.function %}
42
  {%- endif %}
43
  {{- "\n<function>\n<name>" ~ tool.name ~ "</name>" }}
44
+ {{- '\n<description>' ~ (tool.description | trim) ~ '</description>' }}
 
 
45
  {{- '\n<parameters>' }}
46
+ {%- for param_name, param_fields in tool.parameters.properties|items %}
47
+ {{- '\n<parameter>' }}
48
+ {{- '\n<name>' ~ param_name ~ '</name>' }}
49
+ {%- if param_fields.type is defined %}
50
+ {{- '\n<type>' ~ (param_fields.type | string) ~ '</type>' }}
51
+ {%- endif %}
52
+ {%- if param_fields.description is defined %}
53
+ {{- '\n<description>' ~ (param_fields.description | trim) ~ '</description>' }}
54
+ {%- endif %}
55
+ {{- render_item_list(param_fields.enum, 'enum') }}
56
+ {%- set handled_keys = ['type', 'description', 'enum', 'required'] %}
57
+ {%- for json_key in param_fields.keys() | reject("in", handled_keys) %}
58
+ {%- set normed_json_key = json_key | replace("-", "_") | replace(" ", "_") | replace("$", "") %}
59
+ {%- if param_fields[json_key] is mapping %}
60
+ {{- '\n<' ~ normed_json_key ~ '>' ~ (param_fields[json_key] | tojson | safe) ~ '</' ~ normed_json_key ~ '>' }}
61
+ {%- else %}
62
+ {{-'\n<' ~ normed_json_key ~ '>' ~ (param_fields[json_key] | string) ~ '</' ~ normed_json_key ~ '>' }}
63
  {%- endif %}
 
 
 
64
  {%- endfor %}
65
+ {{- render_item_list(param_fields.required, 'required') }}
66
+ {{- '\n</parameter>' }}
67
+ {%- endfor %}
68
+ {{- render_item_list(tool.parameters.required, 'required') }}
69
  {{- '\n</parameters>' }}
70
+ {%- if tool.return is defined %}
71
+ {%- if tool.return is mapping %}
72
+ {{- '\n<return>' ~ (tool.return | tojson | safe) ~ '</return>' }}
73
+ {%- else %}
74
+ {{- '\n<return>' ~ (tool.return | string) ~ '</return>' }}
75
+ {%- endif %}
76
+ {%- endif %}
77
  {{- '\n</function>' }}
78
  {%- endfor %}
79
  {{- "\n</tools>" }}
 
100
  {%- if tool_call.arguments is defined %}
101
  {%- for args_name, args_value in tool_call.arguments|items %}
102
  {{- '<parameter=' + args_name + '>\n' }}
103
+ {%- set args_value = args_value if args_value is string else args_value | string %}
104
  {{- args_value }}
105
  {{- '\n</parameter>\n' }}
106
  {%- endfor %}
 
128
  {%- endfor %}
129
  {%- if add_generation_prompt %}
130
  {{- '<|im_start|>assistant\n' }}
131
+ {%- endif %}