Skip to content

Commit f70df82

Browse files
committed
feat: Add MiniCPMv26 chat handler.
1 parent 82ae7f9 commit f70df82

File tree

1 file changed

+67
-25
lines changed

1 file changed

+67
-25
lines changed

llama_cpp/llama_chat_format.py

+67-25
Original file line numberDiff line numberDiff line change
@@ -2707,6 +2707,31 @@ def last_image_embed_free():
27072707
def load_image(self, image_url: str) -> bytes:
27082708
return self._load_image(image_url)
27092709

2710+
def _embed_image_bytes(self, image_bytes: bytes, n_threads_batch: int = 1):
2711+
if (
2712+
self._last_image_embed is not None
2713+
and self._last_image_hash is not None
2714+
and hash(image_bytes) == self._last_image_hash
2715+
):
2716+
return self._last_image_embed
2717+
with suppress_stdout_stderr(disable=self.verbose):
2718+
# Free the previous image embed
2719+
if self._last_image_embed is not None:
2720+
self._llava_cpp.llava_image_embed_free(self._last_image_embed)
2721+
self._last_image_embed = None
2722+
self._last_image_hash = None
2723+
embed = self._llava_cpp.llava_image_embed_make_with_bytes(
2724+
self.clip_ctx,
2725+
n_threads_batch,
2726+
(ctypes.c_uint8 * len(image_bytes)).from_buffer(
2727+
bytearray(image_bytes)
2728+
),
2729+
len(image_bytes),
2730+
)
2731+
self._last_image_embed = embed
2732+
self._last_image_hash = hash(image_bytes)
2733+
return embed
2734+
27102735
def __call__(
27112736
self,
27122737
*,
@@ -2769,30 +2794,9 @@ def __call__(
27692794
)
27702795
split_text = self.split_text_on_image_urls(text, image_urls)
27712796

2772-
def embed_image_bytes(image_bytes: bytes):
2773-
if (
2774-
self._last_image_embed is not None
2775-
and self._last_image_hash is not None
2776-
and hash(image_bytes) == self._last_image_hash
2777-
):
2778-
return self._last_image_embed
2779-
with suppress_stdout_stderr(disable=self.verbose):
2780-
# Free the previous image embed
2781-
if self._last_image_embed is not None:
2782-
self._llava_cpp.llava_image_embed_free(self._last_image_embed)
2783-
self._last_image_embed = None
2784-
self._last_image_hash = None
2785-
embed = self._llava_cpp.llava_image_embed_make_with_bytes(
2786-
self.clip_ctx,
2787-
llama.context_params.n_threads_batch,
2788-
(ctypes.c_uint8 * len(image_bytes)).from_buffer(
2789-
bytearray(image_bytes)
2790-
),
2791-
len(image_bytes),
2792-
)
2793-
self._last_image_embed = embed
2794-
self._last_image_hash = hash(image_bytes)
2795-
return embed
2797+
if self.verbose:
2798+
print(text, file=sys.stderr)
2799+
27962800

27972801
# Evaluate prompt
27982802
llama.reset()
@@ -2809,7 +2813,7 @@ def embed_image_bytes(image_bytes: bytes):
28092813
llama.eval(tokens)
28102814
else:
28112815
image_bytes = self.load_image(value)
2812-
embed = embed_image_bytes(image_bytes)
2816+
embed = self._embed_image_bytes(image_bytes, llama.context_params.n_threads_batch)
28132817
if llama.n_tokens + embed.contents.n_image_pos > llama.n_ctx():
28142818
raise ValueError(
28152819
f"Prompt exceeds n_ctx: {llama.n_tokens + embed.contents.n_image_pos} > {llama.n_ctx()}"
@@ -3308,6 +3312,44 @@ class Llama3VisionAlphaChatHandler(Llava15ChatHandler):
33083312
Llama3VisionAlpha = Llama3VisionAlphaChatHandler
33093313

33103314

3315+
class MiniCPMv26(Llava15ChatHandler):
3316+
DEFAULT_SYSTEM_MESSAGE = "You are a helpful assistant."
3317+
3318+
CHAT_FORMAT = (
3319+
"{% for message in messages %}"
3320+
"{% if loop.first and messages[0]['role'] != 'system' %}"
3321+
"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n"
3322+
"{% endif %}"
3323+
"<|im_start|>{{ message['role'] }}\n"
3324+
"{% if message['content'] is iterable %}"
3325+
"{% for content in message['content'] %}"
3326+
"{% if content.type == 'image_url' %}"
3327+
"{% if content.image_url is string %}"
3328+
"{{ content.image_url }}"
3329+
"{% endif %}"
3330+
"{% if content.image_url is mapping %}"
3331+
"{{ content.image_url.url }}"
3332+
"{% endif %}"
3333+
"{% endif %}"
3334+
"{% endfor %}"
3335+
3336+
"{% for content in message['content'] %}"
3337+
"{% if content.type == 'text' %}"
3338+
"{{ content.text }}"
3339+
"{% endif %}"
3340+
"{% endfor %}"
3341+
"{% endif %}"
3342+
"{% if message['content'] is string %}"
3343+
"{{ message['content'] }}"
3344+
"{% endif %}"
3345+
"<|im_end|>\n"
3346+
"{% endfor %}"
3347+
"{% if add_generation_prompt %}"
3348+
"<|im_start|>assistant\n"
3349+
"{% endif %}"
3350+
)
3351+
3352+
33113353
@register_chat_completion_handler("chatml-function-calling")
33123354
def chatml_function_calling(
33133355
llama: llama.Llama,

0 commit comments

Comments
 (0)