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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
---
|
3 |
+
|
4 |
+
license: cc-by-nc-4.0
|
5 |
+
datasets:
|
6 |
+
- Salesforce/xlam-function-calling-60k
|
7 |
+
language:
|
8 |
+
- en
|
9 |
+
pipeline_tag: text-generation
|
10 |
+
tags:
|
11 |
+
- function-calling
|
12 |
+
- LLM Agent
|
13 |
+
- tool-use
|
14 |
+
- deepseek
|
15 |
+
- pytorch
|
16 |
+
|
17 |
+
---
|
18 |
+
|
19 |
+
![](https://lh7-us.googleusercontent.com/docsz/AD_4nXfrlKyH6elkxeyrKw4el9j8V3IOQLsqTVngg19Akt6se1Eq2xaocCEjOmc1w8mq5ENHeYfpzRWjYB8D4mtmMPsiH7QyX_Ii1kEM7bk8eMzO68y9JEuDcoJxJBgbNDzRbTdVXylN9_zjrEposDwsoN7csKiD?key=xt3VSDoCbmTY7o-cwwOFwQ)
|
20 |
+
|
21 |
+
# QuantFactory/xLAM-7b-fc-r-GGUF
|
22 |
+
This is quantized version of [Salesforce/xLAM-7b-fc-r](https://huggingface.co/Salesforce/xLAM-7b-fc-r) created using llama.cpp
|
23 |
+
|
24 |
+
# Original Model Card
|
25 |
+
|
26 |
+
|
27 |
+
<p align="center">
|
28 |
+
<img width="300px" alt="xLAM" src="https://huggingface.co/Salesforce/xLAM-v0.1-r/resolve/main/xlam-no-background.png">
|
29 |
+
</p>
|
30 |
+
<p align="center">
|
31 |
+
<a href="https://apigen-pipeline.github.io/">[Homepage]</a> |
|
32 |
+
<a href="https://arxiv.org/abs/2406.18518">[Paper]</a> |
|
33 |
+
<a href="https://huggingface.co/datasets/Salesforce/xlam-function-calling-60k">[Dataset]</a> |
|
34 |
+
<a href="https://github.com/SalesforceAIResearch/xLAM">[Github]</a>
|
35 |
+
</p>
|
36 |
+
<hr>
|
37 |
+
|
38 |
+
|
39 |
+
Welcome to the xLAM model family! [Large Action Models (LAMs)](https://blog.salesforceairesearch.com/large-action-models/) are advanced large language models designed to enhance decision-making and translate user intentions into executable actions that interact with the world. LAMs autonomously plan and execute tasks to achieve specific goals, serving as the brains of AI agents. They have the potential to automate workflow processes across various domains, making them invaluable for a wide range of applications.
|
40 |
+
|
41 |
+
## Table of Contents
|
42 |
+
- [Model Series](#model-series)
|
43 |
+
- [Repository Overview](#repository-overview)
|
44 |
+
- [Benchmark Results](#benchmark-results)
|
45 |
+
- [Usage](#usage)
|
46 |
+
- [Basic Usage with Huggingface](#basic-usage-with-huggingface)
|
47 |
+
- [Usage with vLLM](#usage-with-vllm)
|
48 |
+
- [License](#license)
|
49 |
+
- [Citation](#citation)
|
50 |
+
|
51 |
+
## Model Series
|
52 |
+
|
53 |
+
We provide a series of xLAMs in different sizes to cater to various applications, including those optimized for function-calling and general agent applications:
|
54 |
+
|
55 |
+
| Model | # Total Params | Context Length | Download Model | Download GGUF files |
|
56 |
+
|------------------------|----------------|----------------|----------------|----------|
|
57 |
+
| xLAM-1b-fc-r | 1.35B | 16384 | [🤗 Link](https://huggingface.co/Salesforce/xLAM-1b-fc-r) | [🤗 Link](https://huggingface.co/Salesforce/xLAM-1b-fc-r-gguf) |
|
58 |
+
| xLAM-7b-fc-r | 6.91B | 4096 | [🤗 Link](https://huggingface.co/Salesforce/xLAM-7b-fc-r) | [🤗 Link](https://huggingface.co/Salesforce/xLAM-7b-fc-r-gguf) |
|
59 |
+
|
60 |
+
The `fc` series of models are optimized for function-calling capability, providing fast, accurate, and structured responses based on input queries and available APIs. These models are fine-tuned based on the [deepseek-coder](https://huggingface.co/collections/deepseek-ai/deepseek-coder-65f295d7d8a0a29fe39b4ec4) models and are designed to be small enough for deployment on personal devices like phones or computers.
|
61 |
+
|
62 |
+
We also provide their quantized [GGUF](https://huggingface.co/docs/hub/en/gguf) files for efficient deployment and execution. GGUF is a file format designed to efficiently store and load large language models, making GGUF ideal for running AI models on local devices with limited resources, enabling offline functionality and enhanced privacy.
|
63 |
+
|
64 |
+
For more details, check our [GitHub](https://github.com/SalesforceAIResearch/xLAM) and [paper](https://arxiv.org/abs/2406.18518).
|
65 |
+
|
66 |
+
|
67 |
+
## Repository Overview
|
68 |
+
|
69 |
+
This repository is focused on our small `xLAM-7b-fc-r` model, which is optimized for function-calling and can be easily deployed on personal devices.
|
70 |
+
|
71 |
+
<div align="center">
|
72 |
+
<img src="https://github.com/apigen-pipeline/apigen-pipeline.github.io/blob/main/img/function-call-overview.png?raw=true"
|
73 |
+
alt="drawing" width="620"/>
|
74 |
+
</div>
|
75 |
+
|
76 |
+
Function-calling, or tool use, is one of the key capabilities for AI agents. It requires the model not only understand and generate human-like text but also to execute functional API calls based on natural language instructions. This extends the utility of LLMs beyond simple conversation tasks to dynamic interactions with a variety of digital services and applications, such as retrieving weather information, managing social media platforms, and handling financial services.
|
77 |
+
|
78 |
+
The instructions will guide you through the setup, usage, and integration of `xLAM-7b-fc-r` with HuggingFace and vLLM.
|
79 |
+
We will first introduce the basic usage, and then walk through the provided tutorial and example scripts in the [examples](https://huggingface.co/Salesforce/xLAM-7b-fc-r/tree/main/examples) folder.
|
80 |
+
|
81 |
+
### Framework Versions
|
82 |
+
|
83 |
+
- Transformers 4.41.0
|
84 |
+
- Pytorch 2.3.0+cu121
|
85 |
+
- Datasets 2.19.1
|
86 |
+
- Tokenizers 0.19.1
|
87 |
+
|
88 |
+
## Benchmark Results
|
89 |
+
|
90 |
+
We mainly test our function-calling models on the [Berkeley Function-Calling Leaderboard (BFCL)](https://gorilla.cs.berkeley.edu/leaderboard.html), which offers a comprehensive evaluation framework for assessing LLMs' function-calling capabilities across various programming languages and application domains like Java, JavaScript, and Python.
|
91 |
+
|
92 |
+
|
93 |
+
<div align="center">
|
94 |
+
<img src="https://github.com/apigen-pipeline/apigen-pipeline.github.io/blob/main/img/table-result-0718.png?raw=true" width="620" alt="Performance comparison on Berkeley Function-Calling Leaderboard">
|
95 |
+
<p>Performance comparison on the BFCL benchmark as of date 07/18/2024. Evaluated with <code>temperature=0.001</code> and <code>top_p=1</code></p>
|
96 |
+
</div>
|
97 |
+
|
98 |
+
<p>Our <code>xLAM-7b-fc-r</code> secures the 3rd place with an overall accuracy of 88.24% on the leaderboard, outperforming many strong models. Notably, our <code>xLAM-1b-fc-r</code> model is the only tiny model with less than 2B parameters on the leaderboard, but still achieves a competitive overall accuracy of 78.94% and outperforming GPT3-Turbo and many larger models.
|
99 |
+
Both models exhibit balanced performance across various categories, showing their strong function-calling capabilities despite their small sizes.</p>
|
100 |
+
|
101 |
+
See our [paper](https://arxiv.org/abs/2406.18518) and Github [repo](https://github.com/SalesforceAIResearch/xLAM) for more detailed analysis.
|
102 |
+
|
103 |
+
|
104 |
+
## Usage
|
105 |
+
|
106 |
+
### Basic Usage with Huggingface
|
107 |
+
|
108 |
+
To use the `xLAM-7b-fc-r` model from Huggingface, please first install the `transformers` library:
|
109 |
+
```bash
|
110 |
+
pip install transformers>=4.41.0
|
111 |
+
```
|
112 |
+
|
113 |
+
We use the following example to illustrate how to use our model to perform function-calling tasks.
|
114 |
+
Please note that, our model works best with our provided prompt format.
|
115 |
+
It allows us to extract JSON output that is similar to the [function-calling mode of ChatGPT](https://platform.openai.com/docs/guides/function-calling).
|
116 |
+
|
117 |
+
````python
|
118 |
+
import json
|
119 |
+
import torch
|
120 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
121 |
+
|
122 |
+
torch.random.manual_seed(0)
|
123 |
+
|
124 |
+
model_name = "Salesforce/xLAM-7b-fc-r"
|
125 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
|
126 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
127 |
+
|
128 |
+
# Please use our provided instruction prompt for best performance
|
129 |
+
task_instruction = """
|
130 |
+
You are an expert in composing functions. You are given a question and a set of possible functions.
|
131 |
+
Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
|
132 |
+
If none of the functions can be used, point it out and refuse to answer.
|
133 |
+
If the given question lacks the parameters required by the function, also point it out.
|
134 |
+
""".strip()
|
135 |
+
|
136 |
+
format_instruction = """
|
137 |
+
The output MUST strictly adhere to the following JSON format, and NO other text MUST be included.
|
138 |
+
The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please make tool_calls an empty list '[]'.
|
139 |
+
```
|
140 |
+
{
|
141 |
+
"tool_calls": [
|
142 |
+
{"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
|
143 |
+
... (more tool calls as required)
|
144 |
+
]
|
145 |
+
}
|
146 |
+
```
|
147 |
+
""".strip()
|
148 |
+
|
149 |
+
# Define the input query and available tools
|
150 |
+
query = "What's the weather like in New York in fahrenheit?"
|
151 |
+
|
152 |
+
get_weather_api = {
|
153 |
+
"name": "get_weather",
|
154 |
+
"description": "Get the current weather for a location",
|
155 |
+
"parameters": {
|
156 |
+
"type": "object",
|
157 |
+
"properties": {
|
158 |
+
"location": {
|
159 |
+
"type": "string",
|
160 |
+
"description": "The city and state, e.g. San Francisco, New York"
|
161 |
+
},
|
162 |
+
"unit": {
|
163 |
+
"type": "string",
|
164 |
+
"enum": ["celsius", "fahrenheit"],
|
165 |
+
"description": "The unit of temperature to return"
|
166 |
+
}
|
167 |
+
},
|
168 |
+
"required": ["location"]
|
169 |
+
}
|
170 |
+
}
|
171 |
+
|
172 |
+
search_api = {
|
173 |
+
"name": "search",
|
174 |
+
"description": "Search for information on the internet",
|
175 |
+
"parameters": {
|
176 |
+
"type": "object",
|
177 |
+
"properties": {
|
178 |
+
"query": {
|
179 |
+
"type": "string",
|
180 |
+
"description": "The search query, e.g. 'latest news on AI'"
|
181 |
+
}
|
182 |
+
},
|
183 |
+
"required": ["query"]
|
184 |
+
}
|
185 |
+
}
|
186 |
+
|
187 |
+
openai_format_tools = [get_weather_api, search_api]
|
188 |
+
|
189 |
+
# Helper function to convert openai format tools to our more concise xLAM format
|
190 |
+
def convert_to_xlam_tool(tools):
|
191 |
+
''''''
|
192 |
+
if isinstance(tools, dict):
|
193 |
+
return {
|
194 |
+
"name": tools["name"],
|
195 |
+
"description": tools["description"],
|
196 |
+
"parameters": {k: v for k, v in tools["parameters"].get("properties", {}).items()}
|
197 |
+
}
|
198 |
+
elif isinstance(tools, list):
|
199 |
+
return [convert_to_xlam_tool(tool) for tool in tools]
|
200 |
+
else:
|
201 |
+
return tools
|
202 |
+
|
203 |
+
# Helper function to build the input prompt for our model
|
204 |
+
def build_prompt(task_instruction: str, format_instruction: str, tools: list, query: str):
|
205 |
+
prompt = f"[BEGIN OF TASK INSTRUCTION]\n{task_instruction}\n[END OF TASK INSTRUCTION]\n\n"
|
206 |
+
prompt += f"[BEGIN OF AVAILABLE TOOLS]\n{json.dumps(xlam_format_tools)}\n[END OF AVAILABLE TOOLS]\n\n"
|
207 |
+
prompt += f"[BEGIN OF FORMAT INSTRUCTION]\n{format_instruction}\n[END OF FORMAT INSTRUCTION]\n\n"
|
208 |
+
prompt += f"[BEGIN OF QUERY]\n{query}\n[END OF QUERY]\n\n"
|
209 |
+
return prompt
|
210 |
+
|
211 |
+
# Build the input and start the inference
|
212 |
+
xlam_format_tools = convert_to_xlam_tool(openai_format_tools)
|
213 |
+
content = build_prompt(task_instruction, format_instruction, xlam_format_tools, query)
|
214 |
+
|
215 |
+
messages=[
|
216 |
+
{ 'role': 'user', 'content': content}
|
217 |
+
]
|
218 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
219 |
+
|
220 |
+
# tokenizer.eos_token_id is the id of <|EOT|> token
|
221 |
+
outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
|
222 |
+
print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))
|
223 |
+
````
|
224 |
+
|
225 |
+
Then you should be able to see the following output string in JSON format:
|
226 |
+
|
227 |
+
```shell
|
228 |
+
{"tool_calls": [{"name": "get_weather", "arguments": {"location": "New York", "unit": "fahrenheit"}}]}
|
229 |
+
```
|
230 |
+
|
231 |
+
We highly recommend to use our provided prompt format and helper functions to yield the best function-calling performance of our model.
|
232 |
+
|
233 |
+
### Usage with vLLM
|
234 |
+
|
235 |
+
We provide example scripts to deploy our model with `vllm` and run inferences. First, install the required packages:
|
236 |
+
|
237 |
+
```bash
|
238 |
+
pip install vllm openai argparse jinja2
|
239 |
+
```
|
240 |
+
|
241 |
+
The example scripts are located in the [examples](https://huggingface.co/Salesforce/xLAM-7b-fc-r/tree/main/examples) folder.
|
242 |
+
|
243 |
+
#### 1. Test Prompt Template
|
244 |
+
|
245 |
+
To build prompts using the chat template and output formatted prompts ready for various test cases, run:
|
246 |
+
|
247 |
+
```bash
|
248 |
+
python test_prompt_template.py --model
|
249 |
+
```
|
250 |
+
|
251 |
+
#### 2. Test xLAM Model with a Manually Served Endpoint
|
252 |
+
|
253 |
+
a. Serve the model with vLLM:
|
254 |
+
|
255 |
+
```bash
|
256 |
+
python -m vllm.entrypoints.openai.api_server --model Salesforce/xLAM-7b-fc-r --served-model-name xLAM-7b-fc-r --dtype bfloat16 --port 8001
|
257 |
+
```
|
258 |
+
|
259 |
+
b. Run the test script:
|
260 |
+
|
261 |
+
```bash
|
262 |
+
python test_xlam_model_with_endpoint.py --model_name xLAM-7b-fc-r --port 8001 [OPTIONS]
|
263 |
+
```
|
264 |
+
|
265 |
+
Options:
|
266 |
+
- `--temperature`: Default 0.3
|
267 |
+
- `--top_p`: Default 1.0
|
268 |
+
- `--max_tokens`: Default 512
|
269 |
+
|
270 |
+
This test script provides a handler implementation that can be easily applied to your customized function-calling applications.
|
271 |
+
|
272 |
+
#### 3. Test xLAM Model by Directly Using vLLM Library
|
273 |
+
|
274 |
+
To test the xLAM model directly with the vLLM library, run:
|
275 |
+
|
276 |
+
```bash
|
277 |
+
python test_xlam_model_with_vllm.py --model Salesforce/xLAM-7b-fc-r [OPTIONS]
|
278 |
+
```
|
279 |
+
|
280 |
+
Options are the same as for the endpoint test. This test script also provides a handler implementation that can be easily applied to your customized function-calling applications.
|
281 |
+
|
282 |
+
#### Customization
|
283 |
+
|
284 |
+
These examples are designed to be flexible and easily integrated into your own projects. Feel free to modify the scripts to suit your specific needs and applications. You can adjust test queries or API definitions in each script to test different scenarios or model capabilities.
|
285 |
+
|
286 |
+
Additional customization tips:
|
287 |
+
- Modify the `--dtype` parameter when serving the model based on your GPU capacity.
|
288 |
+
- Refer to the vLLM documentation for more detailed configuration options.
|
289 |
+
- Explore the `demo.ipynb` file for a comprehensive description of the entire workflow, including how to execute APIs.
|
290 |
+
|
291 |
+
These resources provide a robust foundation for integrating xLAM models into your applications, allowing for tailored and efficient deployment.
|
292 |
+
|
293 |
+
## License
|
294 |
+
|
295 |
+
`xLAM-7b-fc-r` is distributed under the CC-BY-NC-4.0 license, with additional terms specified in the [Deepseek license](https://github.com/deepseek-ai/DeepSeek-LLM/blob/main/LICENSE-MODEL).
|
296 |
+
|
297 |
+
## Citation
|
298 |
+
|
299 |
+
If you find this repo helpful, please cite our paper:
|
300 |
+
```bibtex
|
301 |
+
@article{liu2024apigen,
|
302 |
+
title={APIGen: Automated Pipeline for Generating Verifiable and Diverse Function-Calling Datasets},
|
303 |
+
author={Liu, Zuxin and Hoang, Thai and Zhang, Jianguo and Zhu, Ming and Lan, Tian and Kokane, Shirley and Tan, Juntao and Yao, Weiran and Liu, Zhiwei and Feng, Yihao and others},
|
304 |
+
journal={arXiv preprint arXiv:2406.18518},
|
305 |
+
year={2024}
|
306 |
+
}
|
307 |
+
```
|
308 |
+
|