In previous analysis, we have discussed the design concept of cline prompt in depth (Cline technical analysis: How to drive big models to achieve independent changes to local files), revealing its potential in stimulating language model capabilities. Now, we put these theories into practice and explore how to use the Cline's Prompt design idea to activate the tool calling capabilities of small models.
Small-scale language models (LLMs) such as Qwen2.5 0.5B have attracted much attention for their lightweight and low resource requirements, but their native capabilities are limited and difficult to deal with complex tasks. Tool Calling significantly extends the functionality of small models by integrating external APIs or functions.
This article takes Qwen2.5 0.5B as an example, show how to inspire its tool calling capabilities through carefully designed propt, providing practical guidance to developers.
The importance of tool calls to small models
Small models such as Qwen2.5 0.5B have limited parameters, knowledge updates are lagging and lack professional computing capabilities. Tool calls make up for these shortcomings by connecting external resources, for example:
- Query real-time weather information.
- Perform mathematical calculations or data processing.
This makes the small model more competitive in practical applications.
The power of prompt
prompt plays a key role in tool calls, acting as a directive, guiding the model how to use tools effectively. Well-designed propt:
- Define the tool and its parameters.
- Instructs when and how the model calls the tool.
- Shape the output for easy processing.
Think of it as a guide to action for the model.
Design efficient prompt
Here are the principles for building effective propts:
- Clarity: Clarify tool functions and parameter requirements.
- Sample Driver: Provide specific call examples.
- Structured: Use JSON or XML format for easy parsing.
- streamline: Adapt to model context window limitations.
Take the weather tool as an example:
You are a compact AI assistant designed to help users complete tasks with a limited set of tools. You process the task step by step, calling one tool at a time, and waiting for feedback before continuing. Tool calls are formatted using XML-style tags.
---
## Available Tools
### 1. WeatherQuery
**Description**: Query the current weather information of the specified location. **Parameter**: - `location`: Location (string, required). **Usage**: <WeatherQuery>
<location>Shanghai</location>
</WeatherQuery>
---
## Processing Rules
1. **Step by step**: Analyze user requests, use only one tool at a time, and wait for feedback before continuing. 2. **Simplicity**: Keep responsive and focus on tasks.
---
## Example
### User input
"What's the weather like in Shanghai?"
### Model Response
<WeatherQuery>
<location>Shanghai</location>
</WeatherQuery>
Analysis: XML structures are easy to generate and parse, and examples guide the output.
Qwen2.5 0.5B tool call practice
Taking weather query as an example, we will demonstrate the design and implementation of propt.
Step 1: Define the tool
def WeatherQuery(location: str, date: str = None) -> dict:
# Simulate API response
return {"temperature": "22°C", "condition": "Xing"}
Step 2: Design the Prompt
You are a compact AI assistant designed to help users complete tasks with a limited set of tools. You process the task step by step, calling one tool at a time, and waiting for feedback before continuing. Tool calls are formatted using XML-style tags.
---
## Available Tools
### 1. WeatherQuery
**Description**: Query the current weather information of the specified location. **Parameter**: - `location`: Location (string, required). **Usage**: <WeatherQuery>
<location>Shanghai</location>
</WeatherQuery>
---
## Processing Rules
1. **Step by step**: Analyze user requests, use only one tool at a time, and wait for feedback before continuing. 2. **Simplicity**: Keep responsive and focus on tasks.
---
## Example
### User input
"What's the weather like in Shanghai?"
### Model Response
<WeatherQuery>
<location>Shanghai</location>
</WeatherQuery>
Design Analysis:
- Role positioning: Clearly "compact AI assistant" to adapt to the contextual limitations of small models.
- XML Structure: Label-style syntax is easy to generate and parse models.
- Step by step: Simplify model decisions and reduce error rates.
- Sample Driver: Guide the model output through specific examples.
Step 3: Analyze the output
import re
def parse_tool_call(output: str) -> dict:
match = (r'<tool_call>(.*?)</tool_call>', output, )
if match:
xml = (1)
name = (r'<name>(.*?)</name>', xml).group(1)
params = {m[0]: m[1] for m in (r'<(\w+)>(.*?)</\1>', xml) if m[0] != "name"}
return {"name": name, "parameters": params}
return None
Analysis: Regular analysis, dynamic extraction of parameters, applicable to the situation where additional information is included in the model output, and increase fault tolerance.
Step 4: Execute the call
def execute_tool(call: dict) -> dict:
if call["name"] == "WeatherQuery":
return WeatherQuery(**call["parameters"])
return {"error": "Tool not found"}
Analysis: Dynamic call, support for extension.
Step 5: Execute results
This experiment successfully implemented the tool call of the small model under the 4060 consumer graphics card, occupying 1.3G video memory
<WeatherQuery>
<location>Chengdu</location>
</WeatherQuery>
It can be seen that the small model can output stably under the propt project. Combined with fine-tuning, small models have great potential.
The value and challenges of tool calls
Tool calls greatly expand the capabilities of small models to handle tasks beyond their native capabilities. It is flexible and adaptable to a variety of tools. But there are challenges: Prompt needs to be precise, small models can make mistakes on complex instructions, and the reliability and security of external tools are crucial.
Summarize
Through clever prompt design, small models such as Qwen2.5 0.5B can efficiently call tools and expand their application scenarios. Developers need to master tool definition, parameter specifications and output analysis techniques to achieve concise and powerful functional integration. This technology provides a feasible path for the practicalization of small models.
If you are interested in the technical details and source code implementation of this article, please follow my WeChat official account【Song Ge Ai Automation】. Every week, I will publish an in-depth technical article on my official account to analyze the implementation principles of various practical tools from the perspective of source code.
Review of the last issue: (Cline technical analysis: How to drive big models to achieve independent changes to local files)