sigjhl commited on
Commit
beaa600
·
verified ·
1 Parent(s): 145dc25

Update chat_template.jinja

Browse files

This updates the Gemma chat_template.jinja tool declaration rendering so nullable referenced JSON Schema fields (in common constructs likely to matter for tool declarations) are not collapsed into empty type declarations.

Files changed (1) hide show
  1. chat_template.jinja +50 -11
chat_template.jinja CHANGED
@@ -11,12 +11,39 @@
11
  description:<|"|>{{ value['description'] }}<|"|>
12
  {%- set add_comma = true -%}
13
  {%- endif -%}
14
- {%- if value['type'] | upper == 'STRING' -%}
15
- {%- if value['enum'] -%}
16
- {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
17
- enum:{{ format_argument(value['enum']) }}
 
 
18
  {%- endif -%}
19
- {%- elif value['type'] | upper == 'ARRAY' -%}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  {%- if value['items'] is mapping and value['items'] -%}
21
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
22
  items:{
@@ -52,11 +79,11 @@
52
  }
53
  {%- endif -%}
54
  {%- endif -%}
55
- {%- if value['nullable'] %}
56
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
57
  nullable:true
58
  {%- endif -%}
59
- {%- if value['type'] | upper == 'OBJECT' -%}
60
  {%- if value['properties'] is defined and value['properties'] is mapping -%}
61
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
62
  properties:{
@@ -68,7 +95,7 @@
68
  {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
69
  }
70
  {%- endif -%}
71
- {%- if value['required'] -%}
72
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
73
  required:[
74
  {%- for item in value['required'] | default([]) -%}
@@ -78,8 +105,15 @@
78
  ]
79
  {%- endif -%}
80
  {%- endif -%}
81
- {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
82
- type:<|"|>{{ value['type'] | upper }}<|"|>}
 
 
 
 
 
 
 
83
  {%- endif -%}
84
  {%- endfor -%}
85
  {%- endmacro -%}
@@ -91,6 +125,9 @@
91
  {%- if params['properties'] -%}
92
  properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
93
  {%- endif -%}
 
 
 
94
  {%- if params['required'] -%}
95
  required:[
96
  {%- for item in params['required'] -%}
@@ -120,6 +157,8 @@
120
  {{- '<|"|>' + argument + '<|"|>' -}}
121
  {%- elif argument is boolean -%}
122
  {{- 'true' if argument else 'false' -}}
 
 
123
  {%- elif argument is mapping -%}
124
  {{- '{' -}}
125
  {%- set ns = namespace(found_first=false) -%}
@@ -351,4 +390,4 @@
351
  {{- '<|channel>thought\n<channel|>' -}}
352
  {%- endif -%}
353
  {%- endif -%}
354
- {%- endif -%}
 
11
  description:<|"|>{{ value['description'] }}<|"|>
12
  {%- set add_comma = true -%}
13
  {%- endif -%}
14
+ {%- set type_names = namespace(names=[]) -%}
15
+ {%- if value['type'] is defined and value['type'] -%}
16
+ {%- if value['type'] is string -%}
17
+ {%- set type_names.names = [value['type'] | upper] -%}
18
+ {%- else -%}
19
+ {%- set type_names.names = value['type'] | map('upper') | list -%}
20
  {%- endif -%}
21
+ {%- endif -%}
22
+ {%- if value['anyOf'] is defined and value['anyOf'] -%}
23
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
24
+ anyOf:{{ format_argument(value['anyOf'], escape_keys=false) }}
25
+ {%- endif -%}
26
+ {%- if value['oneOf'] is defined and value['oneOf'] -%}
27
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
28
+ oneOf:{{ format_argument(value['oneOf'], escape_keys=false) }}
29
+ {%- endif -%}
30
+ {%- if value['allOf'] is defined and value['allOf'] -%}
31
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
32
+ allOf:{{ format_argument(value['allOf'], escape_keys=false) }}
33
+ {%- endif -%}
34
+ {%- if value['$ref'] is defined and value['$ref'] -%}
35
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
36
+ $ref:<|"|>{{ value['$ref'] }}<|"|>
37
+ {%- endif -%}
38
+ {%- if value['enum'] is defined and value['enum'] -%}
39
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
40
+ enum:{{ format_argument(value['enum']) }}
41
+ {%- endif -%}
42
+ {%- if value['const'] is defined -%}
43
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
44
+ const:{{ format_argument(value['const'], escape_keys=false) }}
45
+ {%- endif -%}
46
+ {%- if 'ARRAY' in type_names.names -%}
47
  {%- if value['items'] is mapping and value['items'] -%}
48
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
49
  items:{
 
79
  }
80
  {%- endif -%}
81
  {%- endif -%}
82
+ {%- if value['nullable'] is defined and value['nullable'] %}
83
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
84
  nullable:true
85
  {%- endif -%}
86
+ {%- if 'OBJECT' in type_names.names -%}
87
  {%- if value['properties'] is defined and value['properties'] is mapping -%}
88
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
89
  properties:{
 
95
  {{- format_parameters(value, value['required'] | default([]), filter_keys=true) -}}
96
  }
97
  {%- endif -%}
98
+ {%- if value['required'] is defined and value['required'] -%}
99
  {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
100
  required:[
101
  {%- for item in value['required'] | default([]) -%}
 
105
  ]
106
  {%- endif -%}
107
  {%- endif -%}
108
+ {%- if value['type'] is defined and value['type'] -%}
109
+ {%- if add_comma %},{%- else -%} {%- set add_comma = true -%} {% endif -%}
110
+ {%- if value['type'] is string -%}
111
+ type:<|"|>{{ value['type'] | upper }}<|"|>
112
+ {%- else -%}
113
+ type:{{ format_argument(value['type'] | map('upper') | list) }}
114
+ {%- endif -%}
115
+ {%- endif -%}
116
+ }
117
  {%- endif -%}
118
  {%- endfor -%}
119
  {%- endmacro -%}
 
125
  {%- if params['properties'] -%}
126
  properties:{ {{- format_parameters(params['properties'], params['required']) -}} },
127
  {%- endif -%}
128
+ {%- if params['$defs'] is defined and params['$defs'] -%}
129
+ $defs:{{ format_argument(params['$defs'], escape_keys=false) }},
130
+ {%- endif -%}
131
  {%- if params['required'] -%}
132
  required:[
133
  {%- for item in params['required'] -%}
 
157
  {{- '<|"|>' + argument + '<|"|>' -}}
158
  {%- elif argument is boolean -%}
159
  {{- 'true' if argument else 'false' -}}
160
+ {%- elif argument is none -%}
161
+ {{- 'null' -}}
162
  {%- elif argument is mapping -%}
163
  {{- '{' -}}
164
  {%- set ns = namespace(found_first=false) -%}
 
390
  {{- '<|channel>thought\n<channel|>' -}}
391
  {%- endif -%}
392
  {%- endif -%}
393
+ {%- endif -%}