File size: 2,101 Bytes
7d87f88
 
 
 
 
 
fe9bf3c
7d87f88
 
 
 
 
 
2325ef0
7d87f88
 
 
 
2325ef0
 
 
7d87f88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2325ef0
7d87f88
2325ef0
7d87f88
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
---
title: LFM2.5 Tool Use
emoji: 🛠️
colorFrom: blue
colorTo: purple
sdk: gradio
sdk_version: 5.49.1
app_file: app.py
pinned: false
tags:
  - tool-use
  - liquid
  - lfm2.5
  - zerogpu
---

# LFM2.5-1.2B-Thinking Tool-Calling Demo

A Hugging Face **ZeroGPU** Space that runs LiquidAI's [LFM2.5-1.2B-Thinking](https://huggingface.co/LiquidAI/LFM2.5-1.2B-Thinking) **in-process with `transformers`** and shows it performing tool (function) calling in a Gradio chat UI.

> **Hardware:** set this Space to **ZeroGPU** in *Settings → Hardware* (then Restart). The `respond` handler is decorated with `@spaces.GPU`, so the model loads in `bfloat16` on an attached GPU per request.

The model can call two mocked tools:

- `web_search(query)` — returns canned search results
- `send_email(to, subject, body)` — pretends to send an email

## How it works

LFM2.5 emits tool calls in its native *Pythonic* format, wrapped in special tokens:

```
<|tool_call_start|>[web_search(query="liquid ai lfm")]<|tool_call_end|>
```

`app.py` parses that with the `ast` module, executes the matching tool, feeds the JSON result back as a `tool`-role message, and lets the model produce a final answer — looping up to 5 turns. Tokens stream into the UI as they generate.

> Note: the LFM2.5-1.2B chat template has a known bug where a structured `tool_calls` field is dropped on re-render, which breaks multi-turn tool calling. To avoid it we store the raw assistant text (special tokens intact) in the conversation history instead of relying on `tool_calls`.

## ZeroGPU & bfloat16

The Space runs on **ZeroGPU** (shared GPU allocated per request). The model is loaded in `bfloat16` onto the attached GPU inside the `@spaces.GPU`-decorated handler — ~2.4 GB, well within a T4's 16 GB. Generation streams into the UI; the whole multi-turn loop runs within one `@spaces.GPU(duration=180)` call.

## Run locally

```bash
pip install -r requirements.txt
python app.py
```

Adapted from the original Ollama-based `main.py`. Tool-calling reference: [Liquid docs](https://docs.liquid.ai/lfm/key-concepts/tool-use).