danielhanchen commited on
Commit
9b33e24
·
verified ·
1 Parent(s): 99c746a

Create template

Browse files
Files changed (1) hide show
  1. template +155 -0
template ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {{- /*
2
+
3
+ ------ MESSAGE PARSING ------
4
+
5
+ */}}
6
+ {{- /*
7
+ Declare the system prompt chunks used for different features
8
+ */}}
9
+ {{- $tools_system_message_prefix := "You are a helpful assistant with access to the following tools. You may call one or more tools to assist with the user query.\n\nYou are provided with function signatures within <tools></tools> XML tags:\n<tools>" }}
10
+ {{- $tools_system_message_suffix := "\n</tools>\n\nFor each tool call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:\n<tool_call>\n{\"name\": <function-name>, \"arguments\": <args-json-object>}\n</tool_call>. If a tool does not exist in the provided list of tools, notify the user that you do not have the ability to fulfill the request." }}
11
+ {{- $documents_system_message_prefix := "You are a helpful assistant with access to the following documents. You may use one or more documents to assist with the user query.\n\nYou are given a list of documents within <documents></documents> XML tags:\n<documents>" }}
12
+ {{- $documents_system_message_suffix := "\n</documents>\n\nWrite the response to the user's input by strictly aligning with the facts in the provided documents. If the information needed to answer the question is not available in the documents, inform the user that the question cannot be answered based on the available data." }}
13
+
14
+ {{- /*
15
+ Declare the prompt structure variables to be filled in from messages
16
+ */}}
17
+ {{- $tools_system_message := "" }}
18
+ {{- $documents_system_message := "" }}
19
+ {{- $system_message := "" }}
20
+ {{- $document_counter := 0 }}
21
+ {{- $last_query_index := 0 }}
22
+
23
+ {{- /*
24
+ Output parsing heuristic
25
+
26
+ Ollama has very specific heuristics for parsing the tags for thinking and tool
27
+ calling. Rather than contorting the actual message expansion to match the
28
+ expectations, we explicitly put them here behind an unreachable condition.
29
+ */}}
30
+ {{- if false }}
31
+ {{- /* tool calls: https://github.com/ollama/ollama/blob/main/tools/template.go#L17 */}}
32
+ {{- if .ToolCalls }}<tool_call>{{- end }}
33
+ {{- end }}
34
+
35
+ {{- /*
36
+ Create tools system message chunk
37
+ */}}
38
+ {{- if .Tools }}
39
+ {{- $tools_system_message = print $tools_system_message_prefix }}
40
+ {{- range $_, $tool_body := .Tools }}
41
+ {{- $tools_system_message = print $tools_system_message "\n" (json $tool_body) }}
42
+ {{- end }}
43
+ {{- $tools_system_message = print $tools_system_message $tools_system_message_suffix }}
44
+ {{- end }}
45
+
46
+
47
+ {{- /*
48
+ Loop over messages to parse variables:
49
+
50
+ - User provided documents in the "document" role
51
+ - Last user query index
52
+ - Initial system message
53
+
54
+ NOTE: Since Ollama collates consecutive roles, for documents, we work around
55
+ this by allowing the role to contain a qualifier after the role string. This
56
+ is also then used as the title of the document.
57
+ */ -}}
58
+ {{- range $index, $_ := .Messages }}
59
+ {{- if (and (eq .Role "system") (eq $index 0)) }}
60
+ {{- if ne $system_message "" }}
61
+ {{- $system_message = print $system_message "\n" }}
62
+ {{- end }}
63
+ {{- $system_message = print $system_message .Content }}
64
+ {{- else if eq .Role "user" }}
65
+ {{- /*
66
+ NOTE: 31 == len '<tool_response></tool_response>'. We only check the
67
+ prefix match since go template doesn't support negative indexing.
68
+ */}}
69
+ {{- if or (lt (len .Content) 31) (ne (slice .Content 0 15) "<tool_response>") }}
70
+ {{- $last_query_index = $index }}
71
+ {{- end }}
72
+ {{- else if (and (ge (len .Role) 8) (eq (slice .Role 0 8) "document")) }}
73
+ {{- if (eq $document_counter 0)}}
74
+ {{- $documents_system_message = print $documents_system_message_prefix}}
75
+ {{- end }}
76
+ {{- $identifier := ""}}
77
+ {{- if (ge (len .Role) 9) }}
78
+ {{- $identifier = slice .Role 9}}
79
+ {{- end }}
80
+ {{- if (eq $identifier "") }}
81
+ {{- $identifier := print $document_counter}}
82
+ {{- end }}
83
+ {{- $documents_system_message = print $documents_system_message "\n{\"doc_id\": " $document_counter ", \"title\": \"" $identifier "\", \"text\": \"" .Content "\"}"}}
84
+ {{- $document_counter = len (printf "a%*s" $document_counter "")}}
85
+ {{- end }}
86
+ {{- end }}
87
+ {{- if (ne $document_counter 0) }}
88
+ {{- $documents_system_message = print $documents_system_message $documents_system_message_suffix}}
89
+ {{- end }}
90
+
91
+ {{- /*
92
+ Construct the full system message
93
+ */}}
94
+ {{- if ne $tools_system_message "" }}
95
+ {{- if ne $system_message "" }}
96
+ {{- $system_message = print $system_message "\n\n" }}
97
+ {{- end }}
98
+ {{- $system_message = print $system_message $tools_system_message }}
99
+ {{- end }}
100
+ {{- if ne $documents_system_message "" }}
101
+ {{- if ne $system_message "" }}
102
+ {{- $system_message = print $system_message "\n\n" }}
103
+ {{- end }}
104
+ {{- $system_message = print $system_message $documents_system_message }}
105
+ {{- end }}
106
+
107
+ {{- /*
108
+
109
+ ------ TEMPLATE EXPANSION ------
110
+
111
+ */}}
112
+ {{- if ne $system_message "" -}}
113
+ <|start_of_role|>system<|end_of_role|>{{ $system_message }}<|end_of_text|>{{ "\n" }}
114
+ {{- end }}
115
+ {{- $prev_role := "" }}
116
+ {{- range $message_index, $_ := .Messages }}
117
+ {{- $next_message_index := len (printf "a%*s" $message_index "") }}
118
+ {{- if or (eq .Role "user") (and (eq .Role "system") (ne $message_index 0)) }}
119
+ {{- "" }}<|start_of_role|>{{- .Role }}<|end_of_role|>{{- .Content }}<|end_of_text|>{{ "\n" }}
120
+ {{- else if eq .Role "assistant" -}}
121
+ {{- "" }}<|start_of_role|>{{ .Role }}<|end_of_role|>{{ .Content }}
122
+
123
+ {{- /* Expand tool calls */}}
124
+ {{- $content := .Content }}
125
+ {{- if .ToolCalls }}
126
+ {{- range $tool_idx, $tool_call := .ToolCalls }}
127
+ {{- if or (ne $content "") (ne $tool_idx 0) }}
128
+ {{- "\n" }}
129
+ {{- end }}
130
+ {{- print "<tool_call>\n{\"name\": \"" $tool_call.Function.Name "\", \"arguments\": " (json $tool_call.Function.Arguments) "}\n</tool_call>" }}
131
+ {{- end }}
132
+ {{- end }}
133
+
134
+ {{- /* End assistant block */}}<|end_of_text|>{{ "\n" }}
135
+
136
+ {{- else if eq .Role "tool" }}
137
+ {{- if (ne $prev_role "tool") -}}
138
+ <|start_of_role>user<|end_of_role|>
139
+ {{- end }}
140
+ {{- "\n" }}<tool_response>{{ print "\n" .Content "\n" }}</tool_response>
141
+ {{- if ne $next_message_index (len $.Messages) }}
142
+ {{- $next_element := index $.Messages $next_message_index }}
143
+ {{- if ne $next_element.Role "tool" -}}
144
+ <|end_of_text|>{{ "\n" }}
145
+ {{- end }}
146
+ {{- else -}}
147
+ <|end_of_text|>{{ "\n" }}
148
+ {{- end }}
149
+ {{- end }}
150
+
151
+ {{- /* If not an assistant message at the end, add generation prompt */}}
152
+ {{- if and (eq $next_message_index (len $.Messages)) (ne .Role "assistant") }}
153
+ {{- "<|start_of_role|>assistant<|end_of_role|>" }}
154
+ {{- end }}
155
+ {{- end }}