Skip to content

Commit 31b1d95

Browse files
committed
feat: Add llama-3-vision-alpha chat format
1 parent 4f01c45 commit 31b1d95

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

llama_cpp/llama_chat_format.py

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,7 +2165,7 @@ def create_completion(stop):
21652165

21662166

21672167
class Llava15ChatHandler:
2168-
DEFAULT_SYSTEM_MESSAGE = "A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions."
2168+
DEFAULT_SYSTEM_MESSAGE: Optional[str] = "A chat between a curious human and an artificial intelligence assistant. The assistant gives helpful, detailed, and polite answers to the human's questions."
21692169

21702170
CHAT_FORMAT = (
21712171
"{% for message in messages %}"
@@ -2288,7 +2288,7 @@ def __call__(
22882288
assert self.clip_ctx is not None
22892289

22902290
system_prompt = _get_system_message(messages)
2291-
if system_prompt == "":
2291+
if system_prompt == "" and self.DEFAULT_SYSTEM_MESSAGE is not None:
22922292
messages = [llama_types.ChatCompletionRequestSystemMessage(role="system", content=self.DEFAULT_SYSTEM_MESSAGE)] + messages
22932293

22942294
image_urls = self.get_image_urls(messages)
@@ -2771,6 +2771,66 @@ class NanoLlavaChatHandler(Llava15ChatHandler):
27712771
"{% endif %}"
27722772
)
27732773

2774+
class Llama3VisionAlpha(Llava15ChatHandler):
2775+
# question = "<image>" + q
2776+
2777+
# prompt = f"<|start_header_id|>user<|end_header_id|>\n\n{question}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n"
2778+
DEFAULT_SYSTEM_MESSAGE = None
2779+
2780+
CHAT_FORMAT = (
2781+
"{% for message in messages %}"
2782+
2783+
"<|start_header_id|>"
2784+
2785+
"{% if message.role == 'user' %}"
2786+
2787+
"user<|end_header_id|>\n\n"
2788+
2789+
"{% if message.content is iterable %}"
2790+
2791+
# <image>
2792+
"{% for content in message.content %}"
2793+
"{% if content.type == 'image_url' %}"
2794+
"{% if content.image_url is string %}"
2795+
"{{ content.image_url }}"
2796+
"{% endif %}"
2797+
"{% if content.image_url is mapping %}"
2798+
"{{ content.image_url.url }}"
2799+
"{% endif %}"
2800+
"{% endif %}"
2801+
"{% endfor %}"
2802+
2803+
# Question:
2804+
"{% for content in message.content %}"
2805+
"{% if content.type == 'text' %}"
2806+
"{{ content.text }}"
2807+
"{% endif %}"
2808+
"{% endfor %}"
2809+
2810+
"{% endif %}"
2811+
2812+
# Question:
2813+
"{% if message.content is string %}"
2814+
"{{ message.content }}"
2815+
"{% endif %}"
2816+
2817+
"{% endif %}"
2818+
2819+
# Answer:
2820+
"{% if message.role == 'assistant' %}"
2821+
"assistant<|end_header_id|>\n\n"
2822+
"{{ message.content }}"
2823+
"{% endif %}"
2824+
2825+
"<|eot_id|>"
2826+
2827+
"{% endfor %}"
2828+
2829+
# Generation prompt
2830+
"{% if add_generation_prompt %}"
2831+
"<|start_header_id|>assistant<|end_header_id|>\n\n"
2832+
"{% endif %}"
2833+
)
27742834

27752835
@register_chat_completion_handler("chatml-function-calling")
27762836
def chatml_function_calling(

llama_cpp/server/model.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,20 @@ def load_llama_from_model_settings(settings: ModelSettings) -> llama_cpp.Llama:
140140
chat_handler = llama_cpp.llama_chat_format.NanoLlavaChatHandler(
141141
clip_model_path=settings.clip_model_path, verbose=settings.verbose
142142
)
143+
elif settings.chat_format == "llama-3-vision-alpha":
144+
assert settings.clip_model_path is not None, "clip model not found"
145+
if settings.hf_model_repo_id is not None:
146+
chat_handler = (
147+
llama_cpp.llama_chat_format.Llama3VisionAlpha.from_pretrained(
148+
repo_id=settings.hf_model_repo_id,
149+
filename=settings.clip_model_path,
150+
verbose=settings.verbose,
151+
)
152+
)
153+
else:
154+
chat_handler = llama_cpp.llama_chat_format.Llama3VisionAlpha(
155+
clip_model_path=settings.clip_model_path, verbose=settings.verbose
156+
)
143157
elif settings.chat_format == "hf-autotokenizer":
144158
assert (
145159
settings.hf_pretrained_model_name_or_path is not None

0 commit comments

Comments
 (0)