forked from theroyallab/tabbyAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Jinja2 is a lightweight template parser that's used in Transformers for parsing chat completions. It's much more efficient than Fastchat and can be imported as part of requirements. Also allows for unblocking Pydantic's version. Users now have to provide their own template if needed. A separate repo may be usable for common prompt template storage. Signed-off-by: kingbri <bdashore3@proton.me>
- Loading branch information
Showing
14 changed files
with
115 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ pydantic < 2,>= 1 | |
PyYAML | ||
progress | ||
uvicorn | ||
jinja2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ pydantic < 2,>= 1 | |
PyYAML | ||
progress | ||
uvicorn | ||
jinja2 | ||
|
||
# Flash attention v2 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Templates | ||
|
||
NOTE: This folder will be replaced by a submodule or something similar in the future | ||
|
||
These templates are examples from [Aphrodite Engine](https://github.com/PygmalionAI/aphrodite-engine/tree/main/examples) | ||
|
||
Please look at [Huggingface's documentation](https://huggingface.co/docs/transformers/main/chat_templating) for making Jinja2 templates. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{{ (messages|selectattr('role', 'equalto', 'system')|list|last).content|trim if (messages|selectattr('role', 'equalto', 'system')|list) else '' }} | ||
|
||
{% for message in messages %} | ||
{% if message['role'] == 'user' %} | ||
### Instruction: | ||
{{ message['content']|trim -}} | ||
{% if not loop.last %} | ||
|
||
|
||
{% endif %} | ||
{% elif message['role'] == 'assistant' %} | ||
### Response: | ||
{{ message['content']|trim -}} | ||
{% if not loop.last %} | ||
|
||
|
||
{% endif %} | ||
{% elif message['role'] == 'user_context' %} | ||
### Input: | ||
{{ message['content']|trim -}} | ||
{% if not loop.last %} | ||
|
||
|
||
{% endif %} | ||
{% endif %} | ||
{% endfor %} | ||
{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %} | ||
### Response: | ||
{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{% for message in messages %}{{'<|im_start|>' + message['role'] + '\n' + message['content']}}{% if (loop.last and add_generation_prompt) or not loop.last %}{{ '<|im_end|>' + '\n'}}{% endif %}{% endfor %} | ||
{% if add_generation_prompt and messages[-1]['role'] != 'assistant' %}{{ '<|im_start|>assistant\n' }}{% endif %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from functools import lru_cache | ||
from importlib.metadata import version as package_version | ||
from packaging import version | ||
from jinja2.sandbox import ImmutableSandboxedEnvironment | ||
from pydantic import BaseModel | ||
|
||
# Small replication of AutoTokenizer's chat template system for efficiency | ||
|
||
class PromptTemplate(BaseModel): | ||
name: str | ||
template: str | ||
|
||
def get_prompt_from_template(messages, prompt_template: PromptTemplate): | ||
if version.parse(package_version("jinja2")) < version.parse("3.0.0"): | ||
raise ImportError( | ||
"Parsing these chat completion messages requires fastchat 0.2.23 or greater. " | ||
f"Current version: {version('jinja2')}\n" | ||
"Please upgrade fastchat by running the following command: " | ||
"pip install -U fschat[model_worker]" | ||
) | ||
|
||
compiled_template = _compile_template(prompt_template.template) | ||
return compiled_template.render(messages = messages) | ||
|
||
# Inspired from https://github.com/huggingface/transformers/blob/main/src/transformers/tokenization_utils_base.py#L1761 | ||
@lru_cache | ||
def _compile_template(template: str): | ||
jinja_env = ImmutableSandboxedEnvironment(trim_blocks = True, lstrip_blocks = True) | ||
jinja_template = jinja_env.from_string(template) | ||
return jinja_template |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters