Instructions to use LiquidAI/LFM2-24B-A2B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use LiquidAI/LFM2-24B-A2B with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="LiquidAI/LFM2-24B-A2B")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("LiquidAI/LFM2-24B-A2B", dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use LiquidAI/LFM2-24B-A2B with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "LiquidAI/LFM2-24B-A2B" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LiquidAI/LFM2-24B-A2B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/LiquidAI/LFM2-24B-A2B
- SGLang
How to use LiquidAI/LFM2-24B-A2B with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "LiquidAI/LFM2-24B-A2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LiquidAI/LFM2-24B-A2B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "LiquidAI/LFM2-24B-A2B" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "LiquidAI/LFM2-24B-A2B", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use LiquidAI/LFM2-24B-A2B with Docker Model Runner:
docker model run hf.co/LiquidAI/LFM2-24B-A2B
Update chat_template.jinja: add generation markers, tool_calls, and thinking support
#17
by logan-vegna-shopify - opened
- chat_template.jinja +76 -40
chat_template.jinja
CHANGED
|
@@ -1,64 +1,100 @@
|
|
| 1 |
{{- bos_token -}}
|
| 2 |
{%- set keep_past_thinking = keep_past_thinking | default(false) -%}
|
| 3 |
-
{%-
|
| 4 |
-
{%- if
|
| 5 |
-
|
| 6 |
-
{%-
|
| 7 |
-
{
|
| 8 |
-
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
{%- endif -%}
|
| 11 |
{%- endfor -%}
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
{%- endif -%}
|
| 15 |
{%- set messages = messages[1:] -%}
|
| 16 |
{%- endif -%}
|
| 17 |
{%- if tools -%}
|
| 18 |
-
{%- set ns.system_prompt = ns.system_prompt + ("\n" if ns.system_prompt else "") + "
|
| 19 |
-
{%- for tool in tools -%}
|
| 20 |
-
{%- if tool is not string -%}
|
| 21 |
-
{%- set tool = tool | tojson -%}
|
| 22 |
-
{%- endif -%}
|
| 23 |
-
{%- set ns.system_prompt = ns.system_prompt + tool -%}
|
| 24 |
-
{%- if not loop.last -%}
|
| 25 |
-
{%- set ns.system_prompt = ns.system_prompt + ", " -%}
|
| 26 |
-
{%- endif -%}
|
| 27 |
-
{%- endfor -%}
|
| 28 |
-
{%- set ns.system_prompt = ns.system_prompt + "]" -%}
|
| 29 |
{%- endif -%}
|
| 30 |
{%- if ns.system_prompt -%}
|
| 31 |
{{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
|
| 32 |
{%- endif -%}
|
| 33 |
-
{%- set ns.last_assistant_index = -1 -%}
|
| 34 |
{%- for message in messages -%}
|
| 35 |
{%- if message["role"] == "assistant" -%}
|
| 36 |
{%- set ns.last_assistant_index = loop.index0 -%}
|
| 37 |
{%- endif -%}
|
| 38 |
{%- endfor -%}
|
| 39 |
{%- for message in messages -%}
|
| 40 |
-
{{- "<|im_start|>" + message
|
| 41 |
-
{%-
|
| 42 |
-
|
| 43 |
-
{%-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
{%- else -%}
|
| 50 |
-
{
|
| 51 |
{%- endif -%}
|
| 52 |
-
{%- endfor -%}
|
| 53 |
-
{%- set content = ns.content -%}
|
| 54 |
-
{%- endif -%}
|
| 55 |
-
{%- if message["role"] == "assistant" and not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}
|
| 56 |
-
{%- if "</think>" in content -%}
|
| 57 |
-
{%- set content = content.split("</think>")[-1] | trim -%}
|
| 58 |
{%- endif -%}
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
{%- endfor -%}
|
| 62 |
{%- if add_generation_prompt -%}
|
| 63 |
{{- "<|im_start|>assistant\n" -}}
|
| 64 |
-
{%- endif -%}
|
|
|
|
| 1 |
{{- bos_token -}}
|
| 2 |
{%- set keep_past_thinking = keep_past_thinking | default(false) -%}
|
| 3 |
+
{%- macro format_arg_value(arg_value) -%}
|
| 4 |
+
{%- if arg_value is string -%}
|
| 5 |
+
{{- "'" + arg_value + "'" -}}
|
| 6 |
+
{%- elif arg_value is mapping -%}
|
| 7 |
+
{{- arg_value | tojson -}}
|
| 8 |
+
{%- else -%}
|
| 9 |
+
{{- arg_value | string -}}
|
| 10 |
+
{%- endif -%}
|
| 11 |
+
{%- endmacro -%}
|
| 12 |
+
{%- macro parse_content(content) -%}
|
| 13 |
+
{%- if content is string -%}
|
| 14 |
+
{{- content -}}
|
| 15 |
+
{%- else -%}
|
| 16 |
+
{%- set _ns = namespace(result="") -%}
|
| 17 |
+
{%- for item in content -%}
|
| 18 |
+
{%- if item["type"] == "image" -%}
|
| 19 |
+
{%- set _ns.result = _ns.result + "<image>" -%}
|
| 20 |
+
{%- elif item["type"] == "text" -%}
|
| 21 |
+
{%- set _ns.result = _ns.result + item["text"] -%}
|
| 22 |
+
{%- else -%}
|
| 23 |
+
{%- set _ns.result = _ns.result + item | tojson -%}
|
| 24 |
{%- endif -%}
|
| 25 |
{%- endfor -%}
|
| 26 |
+
{{- _ns.result -}}
|
| 27 |
+
{%- endif -%}
|
| 28 |
+
{%- endmacro -%}
|
| 29 |
+
{%- macro render_tool_calls(tool_calls) -%}
|
| 30 |
+
{%- set tool_calls_ns = namespace(tool_calls=[]) -%}
|
| 31 |
+
{%- for tool_call in tool_calls -%}
|
| 32 |
+
{%- set func_name = tool_call["function"]["name"] -%}
|
| 33 |
+
{%- set func_args = tool_call["function"]["arguments"] -%}
|
| 34 |
+
{%- set args_ns = namespace(arg_strings=[]) -%}
|
| 35 |
+
{%- for arg_name, arg_value in func_args.items() -%}
|
| 36 |
+
{%- set args_ns.arg_strings = args_ns.arg_strings + [arg_name + "=" + format_arg_value(arg_value)] -%}
|
| 37 |
+
{%- endfor -%}
|
| 38 |
+
{%- set tool_calls_ns.tool_calls = tool_calls_ns.tool_calls + [func_name + "(" + (args_ns.arg_strings | join(", ")) + ")"] -%}
|
| 39 |
+
{%- endfor -%}
|
| 40 |
+
{{- "<|tool_call_start|>[" + (tool_calls_ns.tool_calls | join(", ")) + "]<|tool_call_end|>" -}}
|
| 41 |
+
{%- endmacro -%}
|
| 42 |
+
{%- set ns = namespace(system_prompt="", last_assistant_index=-1) -%}
|
| 43 |
+
{%- if messages[0]["role"] == "system" -%}
|
| 44 |
+
{%- if messages[0].get("content") -%}
|
| 45 |
+
{%- set ns.system_prompt = parse_content(messages[0]["content"]) -%}
|
| 46 |
{%- endif -%}
|
| 47 |
{%- set messages = messages[1:] -%}
|
| 48 |
{%- endif -%}
|
| 49 |
{%- if tools -%}
|
| 50 |
+
{%- set ns.system_prompt = ns.system_prompt + ("\n\n" if ns.system_prompt else "") + "Today's date: " + strftime_now("%Y-%m-%d") + "\n\nList of tools: " + (tools | tojson) -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
{%- endif -%}
|
| 52 |
{%- if ns.system_prompt -%}
|
| 53 |
{{- "<|im_start|>system\n" + ns.system_prompt + "<|im_end|>\n" -}}
|
| 54 |
{%- endif -%}
|
|
|
|
| 55 |
{%- for message in messages -%}
|
| 56 |
{%- if message["role"] == "assistant" -%}
|
| 57 |
{%- set ns.last_assistant_index = loop.index0 -%}
|
| 58 |
{%- endif -%}
|
| 59 |
{%- endfor -%}
|
| 60 |
{%- for message in messages -%}
|
| 61 |
+
{{- "<|im_start|>" + message.role + "\n" -}}
|
| 62 |
+
{%- if message.role == "assistant" -%}
|
| 63 |
+
{%- generation -%}
|
| 64 |
+
{%- if message.thinking is defined and (keep_past_thinking or loop.index0 == ns.last_assistant_index) -%}
|
| 65 |
+
{{- "<think>" + message.thinking + "</think>" -}}
|
| 66 |
+
{%- endif -%}
|
| 67 |
+
{%- set _cfm_tag = "CONTINUE_FINAL_MESSAGE_TAG " -%}
|
| 68 |
+
{%- set _has_cfm = false -%}
|
| 69 |
+
{%- if message.content is defined -%}
|
| 70 |
+
{%- set content = parse_content(message.content) -%}
|
| 71 |
+
{%- if not keep_past_thinking and loop.index0 != ns.last_assistant_index -%}
|
| 72 |
+
{%- if "</think>" in content -%}
|
| 73 |
+
{%- set content = content.split("</think>")[-1] | trim -%}
|
| 74 |
+
{%- endif -%}
|
| 75 |
+
{%- endif -%}
|
| 76 |
+
{%- if message.tool_calls is defined and content.endswith(_cfm_tag) -%}
|
| 77 |
+
{%- set _has_cfm = true -%}
|
| 78 |
+
{{- content[:-(_cfm_tag | length)] -}}
|
| 79 |
{%- else -%}
|
| 80 |
+
{{- content -}}
|
| 81 |
{%- endif -%}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
{%- endif -%}
|
| 83 |
+
{%- if message.tool_calls is defined -%}
|
| 84 |
+
{{- render_tool_calls(message.tool_calls) -}}
|
| 85 |
+
{%- endif -%}
|
| 86 |
+
{%- if _has_cfm -%}
|
| 87 |
+
{{- _cfm_tag -}}
|
| 88 |
+
{%- endif -%}
|
| 89 |
+
{{- "<|im_end|>\n" -}}
|
| 90 |
+
{%- endgeneration -%}
|
| 91 |
+
{%- else %}
|
| 92 |
+
{%- if message.get("content") -%}
|
| 93 |
+
{{- parse_content(message["content"]) -}}
|
| 94 |
+
{%- endif -%}
|
| 95 |
+
{{- "<|im_end|>\n" -}}
|
| 96 |
+
{%- endif %}
|
| 97 |
{%- endfor -%}
|
| 98 |
{%- if add_generation_prompt -%}
|
| 99 |
{{- "<|im_start|>assistant\n" -}}
|
| 100 |
+
{%- endif -%}
|