Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a format based on Huggingface format #988

Merged
merged 3 commits into from
Jul 4, 2024

Conversation

yoavkatz
Copy link
Member

@yoavkatz yoavkatz commented Jul 3, 2024

Formats the complete input for the model using the Hugginface chat template of a given model.

HFSystemFormat expects the input instance to contain:
1. A field named "system_prompt" whose value is a string (potentially empty) that delivers a task independent opening text.
2. A field named "source" whose value is a string verbalizing the original values in the instance (as read
from the source dataset), in the context of the underlying task.
3. A field named "instruction" that contains a (non-None) string.
4. A field named with the value in arg 'demos_field', containing a list of dicts, each dict with fields "source"
and "target", representing a single demo.
5. A field named "target_prefx" that contains a string to prefix the target in both each demo, and to end the whole generated prompt

SystemFormat formats the above fields into a single string to be inputted to the model. This string overwrites
field "source" of the instance.

Example:
    HFSystemFormat(model_name="HuggingFaceH4/zephyr-7b-beta")

    Uses the template defined the in tokenizer_config.json of the model:

    "chat_template": "{% for message in messages %}\n{% if message['role'] == 'user' %}\n{{ '<|user|>\n' + message['content'] + eos_token }}\n{% elif message['role'] == 'system' %}\n{{ '<|system|>\n' + message['content'] + eos_token }}\n{% elif message['role'] == 'assistant' %}\n{{ '<|assistant|>\n'  + message['content'] + eos_token }}\n{% endif %}\n{% if loop.last and add_generation_prompt %}\n{{ '<|assistant|>' }}\n{% endif %}\n{% endfor %}",

    See more details in https://huggingface.co/docs/transformers/main/en/chat_templating

Signed-off-by: Yoav Katz <katz@il.ibm.com>
src/unitxt/formats.py Outdated Show resolved Hide resolved
Signed-off-by: Yoav Katz <katz@il.ibm.com>
Copy link
Member

@elronbandel elronbandel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR achieve its goal.
For future we should consider:
(1) Seperate the structured representation and make it available at a dedicated field for people want to use it externally (e.g for using open ai api)
(2) change existing formats to use this mechanism

@yoavkatz yoavkatz enabled auto-merge (squash) July 3, 2024 14:44
@yoavkatz
Copy link
Member Author

yoavkatz commented Jul 3, 2024

This PR achieve its goal. For future we should consider: (1) Seperate the structured representation and make it available at a dedicated field for people want to use it externally (e.g for using open ai api) (2) change existing formats to use this mechanism

There are downsides for it - it requires access to the HF model page (e.g. sometime requires huggingface token login ).
I think we should consider a general jinja format - so people can just copy the jinja string and use it for formatting.

@yoavkatz yoavkatz merged commit 2c3b774 into main Jul 4, 2024
8 checks passed
@yoavkatz yoavkatz deleted the create-format-that-uses-hugginface-chat-template branch July 4, 2024 04:42
gitMichal pushed a commit that referenced this pull request Jul 15, 2024
* Added a format based on Huggingface format

Signed-off-by: Yoav Katz <katz@il.ibm.com>

* Simplified format generation (no need to tokenize)

Signed-off-by: Yoav Katz <katz@il.ibm.com>

---------

Signed-off-by: Yoav Katz <katz@il.ibm.com>
csrajmohan pushed a commit that referenced this pull request Aug 29, 2024
* Added a format based on Huggingface format

Signed-off-by: Yoav Katz <katz@il.ibm.com>

* Simplified format generation (no need to tokenize)

Signed-off-by: Yoav Katz <katz@il.ibm.com>

---------

Signed-off-by: Yoav Katz <katz@il.ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants