Skip to content

Commit 1b84182

Browse files
ThiloteEcebtenzzre
andauthored
Add replacement templates for OLMoE and granite-3.1 (#3471)
Signed-off-by: ThiloteE <[email protected]> Signed-off-by: Jared Van Bortel <[email protected]> Co-authored-by: Jared Van Bortel <[email protected]>
1 parent 02e1208 commit 1b84182

File tree

2 files changed

+88
-1
lines changed

2 files changed

+88
-1
lines changed

gpt4all-chat/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
99
### Added
1010
- Whitelist Granite (non-MoE) model architecture (by [@ThiloteE](https://github.com/ThiloteE) in [#3487](https://github.com/nomic-ai/gpt4all/pull/3487))
1111

12+
### Changed
13+
- Substitute prettier default templates for OLMoE 7B 0924/0125 and Granite 3.1 3B/8B (by [@ThiloteE](https://github.com/ThiloteE) in [#3471](https://github.com/nomic-ai/gpt4all/pull/3471))
14+
1215
### Fixed
1316
- Fix several potential crashes ([#3465](https://github.com/nomic-ai/gpt4all/pull/3465))
1417
- Fix visual spacing issues with deepseek models ([#3470](https://github.com/nomic-ai/gpt4all/pull/3470))

gpt4all-chat/src/jinja_replacements.cpp

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,31 @@ const std::unordered_map<std::string_view, std::string_view> CHAT_TEMPLATE_SUBST
113113
{%- elif message['role'] == 'system' %}
114114
{{- '<|system|>\n' + message['content'] + eos_token }}
115115
{%- elif message['role'] == 'assistant' %}
116-
{{- '<|assistant|>\n' + message['content'] + eos_token }}
116+
{{- '<|assistant|>\n' + message['content'] + eos_token }}
117117
{%- endif %}
118118
{%- if loop.last and add_generation_prompt %}
119119
{{- '<|assistant|>' }}
120120
{%- endif %}
121+
{%- endfor %})TEMPLATE",
122+
},
123+
// granite-3.1-3b-a800m-instruct-Q4_0.gguf, granite-3.1-8b-instruct-Q4_0.gguf (nomic-ai/gpt4all#3471)
124+
{
125+
// original
126+
R"TEMPLATE({%- if messages[0]['role'] == 'system' %}{%- set system_message = messages[0]['content'] %}{%- set loop_messages = messages[1:] %}{%- else %}{%- set system_message = "Knowledge Cutoff Date: April 2024. You are Granite, developed by IBM." %}{%- if tools and documents %}{%- set system_message = system_message + " You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. 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. Write 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." %}{%- elif tools %}{%- set system_message = system_message + " You are a helpful AI assistant with access to the following tools. When a tool is required to answer the user's query, respond with <|tool_call|> followed by a JSON list of tools used. 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." %}{%- elif documents %}{%- set system_message = system_message + " Write 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." %}{%- else %}{%- set system_message = system_message + " You are a helpful AI assistant." %}{%- endif %}{%- if controls and 'citations' in controls and documents %}{%- set system_message = system_message + ' In your response, use the symbols <co> and </co> to indicate when a fact comes from a document in the search result, e.g <co>0</co> for a fact from document 0. Afterwards, list all the citations with their corresponding documents in an ordered list.' %}{%- endif %}{%- if controls and 'hallucinations' in controls and documents %}{%- set system_message = system_message + ' Finally, after the response is written, include a numbered list of sentences from the response that are potentially hallucinated and not based in the documents.' %}{%- endif %}{%- set loop_messages = messages %}{%- endif %}{{- '<|start_of_role|>system<|end_of_role|>' + system_message + '<|end_of_text|> ' }}{%- if tools %}{{- '<|start_of_role|>tools<|end_of_role|>' }}{{- tools | tojson(indent=4) }}{{- '<|end_of_text|> ' }}{%- endif %}{%- if documents %}{{- '<|start_of_role|>documents<|end_of_role|>' }}{%- for document in documents %}{{- 'Document ' + loop.index0 | string + ' ' }}{{- document['text'] }}{%- if not loop.last %}{{- ' '}}{%- endif%}{%- endfor %}{{- '<|end_of_text|> ' }}{%- endif %}{%- for message in loop_messages %}{{- '<|start_of_role|>' + message['role'] + '<|end_of_role|>' + message['content'] + '<|end_of_text|> ' }}{%- if loop.last and add_generation_prompt %}{{- '<|start_of_role|>assistant' }}{%- if controls %}{{- ' ' + controls | tojson()}}{%- endif %}{{- '<|end_of_role|>' }}{%- endif %}{%- endfor %})TEMPLATE",
127+
// replacement
128+
R"TEMPLATE({%- if messages[0]['role'] == 'system' %}
129+
{%- set system_message = messages[0]['content'] %}
130+
{%- set loop_messages = messages[1:] %}
131+
{%- else %}
132+
{%- set system_message = "Knowledge Cutoff Date: April 2024. You are Granite, developed by IBM. You are a helpful AI assistant." %}
133+
{%- set loop_messages = messages %}
134+
{%- endif %}
135+
{{- '<|start_of_role|>system<|end_of_role|>' + system_message + '<|end_of_text|> ' }}
136+
{%- for message in loop_messages %}
137+
{{- '<|start_of_role|>' + message['role'] + '<|end_of_role|>' + message['content'] + '<|end_of_text|> ' }}
138+
{%- if loop.last and add_generation_prompt %}
139+
{{- '<|start_of_role|>assistant<|end_of_role|>' }}
140+
{%- endif %}
121141
{%- endfor %})TEMPLATE",
122142
},
123143
// Hermes-3-Llama-3.2-3B.Q4_0.gguf, mistral-7b-openorca.gguf2.Q4_0.gguf
@@ -618,6 +638,70 @@ const std::unordered_map<std::string_view, std::string_view> CHAT_TEMPLATE_SUBST
618638
{%- if add_generation_prompt %}
619639
{{- '<|im_start|>assistant\n' }}
620640
{%- endif %})TEMPLATE",
641+
},
642+
// OLMoE-1B-7B-0125-Instruct-Q4_0.gguf (nomic-ai/gpt4all#3471)
643+
{
644+
// original
645+
R"TEMPLATE({{ bos_token }}{% for message in messages %}{% if message['role'] == 'system' %}{{ '<|system|>
646+
' + message['content'] + '
647+
' }}{% elif message['role'] == 'user' %}{{ '<|user|>
648+
' + message['content'] + '
649+
' }}{% elif message['role'] == 'assistant' %}{% if not loop.last %}{{ '<|assistant|>
650+
' + message['content'] + eos_token + '
651+
' }}{% else %}{{ '<|assistant|>
652+
' + message['content'] + eos_token }}{% endif %}{% endif %}{% if loop.last and add_generation_prompt %}{{ '<|assistant|>
653+
' }}{% endif %}{% endfor %})TEMPLATE",
654+
// replacement
655+
R"TEMPLATE({{- bos_token }}
656+
{%- for message in messages %}
657+
{%- if message['role'] == 'system' %}
658+
{{- '<|system|>\n' + message['content'] + '\n' }}
659+
{%- elif message['role'] == 'user' %}
660+
{{- '<|user|>\n' + message['content'] + '\n' }}
661+
{%- elif message['role'] == 'assistant' %}
662+
{%- if not loop.last %}
663+
{{- '<|assistant|>\n' + message['content'] + eos_token + '\n' }}
664+
{%- else %}
665+
{{- '<|assistant|>\n' + message['content'] + eos_token }}
666+
{%- endif %}
667+
{%- endif %}
668+
{%- if loop.last and add_generation_prompt %}
669+
{{- '<|assistant|>\n' }}
670+
{%- endif %}
671+
{%- endfor %})TEMPLATE",
672+
},
673+
// OLMoE-1B-7B-0924-Instruct-Q4_0.gguf (nomic-ai/gpt4all#3471)
674+
{
675+
// original
676+
R"TEMPLATE({{ bos_token }}{% for message in messages %}
677+
{% if message['role'] == 'system' %}
678+
{{ '<|system|>
679+
' + message['content'] }}
680+
{% elif message['role'] == 'user' %}
681+
{{ '<|user|>
682+
' + message['content'] }}
683+
{% elif message['role'] == 'assistant' %}
684+
{{ '<|assistant|>
685+
' + message['content'] + eos_token }}
686+
{% endif %}
687+
{% if loop.last and add_generation_prompt %}
688+
{{ '<|assistant|>' }}
689+
{% endif %}
690+
{% endfor %})TEMPLATE",
691+
// replacement
692+
R"TEMPLATE({{- bos_token }}
693+
{%- for message in messages %}
694+
{%- if message['role'] == 'system' %}
695+
{{- '<|system|>\n' + message['content'] }}
696+
{%- elif message['role'] == 'user' %}
697+
{{- '<|user|>\n' + message['content'] }}
698+
{%- elif message['role'] == 'assistant' %}
699+
{{- '<|assistant|>\n' + message['content'] + eos_token }}
700+
{%- endif %}
701+
{%- if loop.last and add_generation_prompt %}
702+
{{- '<|assistant|>' }}
703+
{%- endif %}
704+
{%- endfor %})TEMPLATE",
621705
},
622706
// Phi-3.1-mini-128k-instruct-Q4_0.gguf (nomic-ai/gpt4all#3346)
623707
{

0 commit comments

Comments
 (0)