Skip to content

Commit b71057f

Browse files
committed
feat: add support for SmolVLM
1 parent 362795c commit b71057f

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

examples/granite_docling/main.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
filename="granite*Q8_0*",
1212
chat_handler=chat_handler,
1313
n_ctx=8192,
14+
n_gpu_layers=-1,
1415
)
1516
response = llama.create_chat_completion(
1617
messages=[

examples/smolvlm/main.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from llama_cpp import Llama
2+
from llama_cpp.llama_chat_format import SmolVLMChatHandler
3+
4+
5+
chat_handler = SmolVLMChatHandler.from_pretrained(
6+
repo_id="ggml-org/SmolVLM-256M-Instruct-GGUF",
7+
filename="mmproj*Q8_0*",
8+
)
9+
llama = Llama.from_pretrained(
10+
repo_id="ggml-org/SmolVLM-256M-Instruct-GGUF",
11+
filename="SmolVLM*Q8_0*",
12+
chat_handler=chat_handler,
13+
n_ctx=8192,
14+
n_gpu_layers=-1,
15+
)
16+
response = llama.create_chat_completion(
17+
messages=[
18+
{
19+
"role": "user",
20+
"content": [
21+
{"type": "image_url", "image_url": "https://huggingface.co/spaces/ibm-granite/granite-docling-258m-demo/resolve/main/data/images/lake-zurich-switzerland-view-nature-landscapes-7bbda4-1024.jpg"},
22+
{"type": "text", "text": "Describe this image."},
23+
],
24+
}
25+
],
26+
stream=True,
27+
special=True,
28+
)
29+
30+
for chunk in response:
31+
delta = chunk["choices"][0]["delta"]
32+
if "content" not in delta:
33+
continue
34+
print(delta["content"], end="", flush=True)
35+
36+
print()

llama_cpp/llama_chat_format.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3530,6 +3530,33 @@ def __call__(self, **kwargs):
35303530
return super().__call__(**kwargs)
35313531

35323532

3533+
class SmolVLMChatHandler(Llava15ChatHandler):
3534+
DEFAULT_SYSTEM_MESSAGE = None
3535+
CHAT_FORMAT = (
3536+
"<|im_start|>"
3537+
"{% for message in messages %}"
3538+
"{{message['role'] | capitalize}}"
3539+
"{% if message['content'][0]['type'] == 'image' %}"
3540+
"{{':'}}{% else %}{{': '}}{% endif %}"
3541+
"{% for line in message['content'] %}"
3542+
"{% if line['type'] == 'text' %}"
3543+
"{{line['text']}}"
3544+
"{% elif line['type'] == 'image_url' %}"
3545+
"{% if line['image_url'] is string %}"
3546+
"{{ line['image_url'] }}"
3547+
"{% elif line['image_url'] is mapping %}"
3548+
"{{ line['image_url']['url'] }}"
3549+
"{% endif %}"
3550+
"{% endif %}"
3551+
"{% endfor %}"
3552+
"<end_of_utterance>\n"
3553+
"{% endfor %}"
3554+
"{% if add_generation_prompt %}"
3555+
"{{ 'Assistant:' }}"
3556+
"{% endif %}"
3557+
)
3558+
3559+
35333560
class GraniteDoclingChatHandler(Llava15ChatHandler):
35343561
DEFAULT_SYSTEM_MESSAGE = None
35353562

0 commit comments

Comments
 (0)