Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bbffdae

Browse filesBrowse files
committed
Refactor autotokenizer format to reusable function
1 parent b0e597e commit bbffdae
Copy full SHA for bbffdae

File tree

Expand file treeCollapse file tree

1 file changed

+22
-20
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+22
-20
lines changed

‎llama_cpp/llama_chat_format.py

Copy file name to clipboardExpand all lines: llama_cpp/llama_chat_format.py
+22-20Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
from __future__ import annotations
22

3+
import os
34
import dataclasses
45
from typing import Any, Dict, Iterator, List, Optional, Tuple, Union, Protocol
6+
57
from . import llama_types
68
from . import llama
79

@@ -327,6 +329,26 @@ def get_chat_format(name: str):
327329
)
328330

329331

332+
def hf_autotokenizer_to_chat_formatter(pretrained_model_name_or_path: Union[str, os.PathLike[str]]) -> ChatFormatter:
333+
# https://huggingface.co/docs/transformers/main/chat_templating
334+
# https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1#instruction-format
335+
# https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1/blob/main/tokenizer_config.json
336+
from transformers import AutoTokenizer
337+
338+
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path)
339+
340+
def format_autotokenizer(
341+
messages: List[llama_types.ChatCompletionRequestMessage],
342+
**kwargs: Any,
343+
) -> ChatFormatterResponse:
344+
tokenizer.use_default_system_prompt = False
345+
_prompt = tokenizer.apply_chat_template(messages, tokenize=False)
346+
# Return formatted prompt and eos token by default
347+
return ChatFormatterResponse(prompt=_prompt, stop=tokenizer.eos_token)
348+
349+
return format_autotokenizer
350+
351+
330352
# see https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/tokenization_llama.py
331353
# system prompt is "embedded" in the first message
332354
@register_chat_format("llama-2")
@@ -510,26 +532,6 @@ def format_chatml(
510532
_prompt = _format_chatml(system_message, _messages, _sep)
511533
return ChatFormatterResponse(prompt=_prompt)
512534

513-
# eg, export HF_MODEL=mistralai/Mistral-7B-Instruct-v0.1
514-
@register_chat_format("autotokenizer")
515-
def format_autotokenizer(
516-
messages: List[llama_types.ChatCompletionRequestMessage],
517-
**kwargs: Any,
518-
) -> ChatFormatterResponse:
519-
# https://huggingface.co/docs/transformers/main/chat_templating
520-
# https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1#instruction-format
521-
# https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1/blob/main/tokenizer_config.json
522-
import os
523-
from transformers import AutoTokenizer
524-
huggingFaceModel = os.getenv("HF_MODEL") # eg, mistralai/Mistral-7B-Instruct-v0.1
525-
print(huggingFaceModel)
526-
if not huggingFaceModel:
527-
raise Exception("HF_MODEL needs to be set in env to use chat format 'autotokenizer'")
528-
tokenizer = AutoTokenizer.from_pretrained(huggingFaceModel)
529-
tokenizer.use_default_system_prompt = False
530-
_prompt = tokenizer.apply_chat_template(messages, tokenize=False)
531-
# Return formatted prompt and eos token by default
532-
return ChatFormatterResponse(prompt=_prompt, stop=tokenizer.eos_token)
533535

534536
@register_chat_completion_handler("functionary")
535537
def functionary_chat_handler(

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.