aashish1904 commited on
Commit
8bdef08
1 Parent(s): 7e6dbcc

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +315 -0
README.md ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ---
3
+
4
+ extra_gated_heading: >-
5
+ Acknowledge to follow corresponding license to access the
6
+ repository
7
+ extra_gated_button_content: Agree and access repository
8
+ extra_gated_fields:
9
+ First Name: text
10
+ Last Name: text
11
+ Country: country
12
+ Affiliation: text
13
+ license: cc-by-nc-4.0
14
+ datasets:
15
+ - Salesforce/xlam-function-calling-60k
16
+ language:
17
+ - en
18
+ pipeline_tag: text-generation
19
+ tags:
20
+ - function-calling
21
+ - LLM Agent
22
+ - tool-use
23
+ - deepseek
24
+ - pytorch
25
+
26
+ ---
27
+
28
+ ![](https://cdn.discordapp.com/attachments/791342238541152306/1264099835221381251/image.png?ex=669ca436&is=669b52b6&hm=129f56187c31e1ed22cbd1bcdbc677a2baeea5090761d2f1a458c8b1ec7cca4b&)
29
+
30
+ # QuantFactory/xLAM-1b-fc-r-GGUF
31
+ This is quantized version of [Salesforce/xLAM-1b-fc-r](https://huggingface.co/Salesforce/xLAM-1b-fc-r) created using llama.cpp
32
+
33
+ # Original Model Card
34
+
35
+
36
+ <p align="center">
37
+ <img width="300px" alt="xLAM" src="https://huggingface.co/datasets/jianguozhang/logos/resolve/main/xlam-no-background.png">
38
+ </p>
39
+ <p align="center">
40
+ <a href="https://apigen-pipeline.github.io/">[Homepage]</a> |
41
+ <a href="https://arxiv.org/abs/2406.18518">[Paper]</a> |
42
+ <a href="https://huggingface.co/datasets/Salesforce/xlam-function-calling-60k">[Dataset]</a> |
43
+ <a href="https://github.com/SalesforceAIResearch/xLAM">[Github]</a>
44
+ </p>
45
+ <hr>
46
+
47
+ 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.
48
+
49
+ ## Table of Contents
50
+ - [Model Series](#model-series)
51
+ - [Repository Overview](#repository-overview)
52
+ - [Benchmark Results](#benchmark-results)
53
+ - [Usage](#usage)
54
+ - [Basic Usage with Huggingface](#basic-usage-with-huggingface)
55
+ - [Usage with vLLM](#usage-with-vllm)
56
+ - [License](#license)
57
+ - [Citation](#citation)
58
+
59
+ ## Model Series
60
+
61
+ We provide a series of xLAMs in different sizes to cater to various applications, including those optimized for function-calling and general agent applications:
62
+
63
+ | Model | # Total Params | Context Length | Download Model | Download GGUF files |
64
+ |------------------------|----------------|----------------|----------------|----------|
65
+ | 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) |
66
+ | 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) | |
67
+
68
+ 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.
69
+
70
+ 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.
71
+
72
+ For more details, check our [GitHub](https://github.com/SalesforceAIResearch/xLAM) and [paper](https://arxiv.org/abs/2406.18518).
73
+
74
+ ## Repository Overview
75
+
76
+ This repository is focused on our tiny `xLAM-1b-fc-r` model, which is optimized for function-calling and can be easily deployed on personal devices.
77
+
78
+ <div align="center">
79
+ <img src="https://github.com/apigen-pipeline/apigen-pipeline.github.io/blob/main/img/function-call-overview.png?raw=true"
80
+ alt="drawing" width="620"/>
81
+ </div>
82
+
83
+ 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.
84
+
85
+ The instructions will guide you through the setup, usage, and integration of `xLAM-1b-fc-r` with HuggingFace and vLLM.
86
+ 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-1b-fc-r/tree/main/examples) folder.
87
+
88
+ ### Framework Versions
89
+
90
+ - Transformers 4.41.0
91
+ - Pytorch 2.3.0+cu121
92
+ - Datasets 2.19.1
93
+ - Tokenizers 0.19.1
94
+
95
+ ## Benchmark Results
96
+
97
+ 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.
98
+
99
+
100
+ <div align="center">
101
+ <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">
102
+ <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>
103
+ </div>
104
+
105
+ <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.
106
+ Both models exhibit balanced performance across various categories, showing their strong function-calling capabilities despite their small sizes.</p>
107
+
108
+ See our [paper](https://arxiv.org/abs/2406.18518) and Github [repo](https://github.com/SalesforceAIResearch/xLAM) for more detailed analysis.
109
+
110
+
111
+ ## Usage
112
+
113
+ ### Basic Usage with Huggingface
114
+
115
+ To use the `xLAM-1b-fc-r` model from Huggingface, please first install the `transformers` library:
116
+ ```bash
117
+ pip install transformers>=4.41.0
118
+ ```
119
+
120
+ We use the following example to illustrate how to use our model to perform function-calling tasks.
121
+ Please note that, our model works best with our provided prompt format.
122
+ 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).
123
+
124
+ ````python
125
+ import json
126
+ import torch
127
+ from transformers import AutoModelForCausalLM, AutoTokenizer
128
+
129
+ torch.random.manual_seed(0)
130
+
131
+ model_name = "Salesforce/xLAM-1b-fc-r"
132
+ model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
133
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
134
+
135
+ # Please use our provided instruction prompt for best performance
136
+ task_instruction = """
137
+ You are an expert in composing functions. You are given a question and a set of possible functions.
138
+ Based on the question, you will need to make one or more function/tool calls to achieve the purpose.
139
+ If none of the functions can be used, point it out and refuse to answer.
140
+ If the given question lacks the parameters required by the function, also point it out.
141
+ """.strip()
142
+
143
+ format_instruction = """
144
+ The output MUST strictly adhere to the following JSON format, and NO other text MUST be included.
145
+ 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 '[]'.
146
+ ```
147
+ {
148
+ "tool_calls": [
149
+ {"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
150
+ ... (more tool calls as required)
151
+ ]
152
+ }
153
+ ```
154
+ """.strip()
155
+
156
+ # Define the input query and available tools
157
+ query = "What's the weather like in New York in fahrenheit?"
158
+
159
+ get_weather_api = {
160
+ "name": "get_weather",
161
+ "description": "Get the current weather for a location",
162
+ "parameters": {
163
+ "type": "object",
164
+ "properties": {
165
+ "location": {
166
+ "type": "string",
167
+ "description": "The city and state, e.g. San Francisco, New York"
168
+ },
169
+ "unit": {
170
+ "type": "string",
171
+ "enum": ["celsius", "fahrenheit"],
172
+ "description": "The unit of temperature to return"
173
+ }
174
+ },
175
+ "required": ["location"]
176
+ }
177
+ }
178
+
179
+ search_api = {
180
+ "name": "search",
181
+ "description": "Search for information on the internet",
182
+ "parameters": {
183
+ "type": "object",
184
+ "properties": {
185
+ "query": {
186
+ "type": "string",
187
+ "description": "The search query, e.g. 'latest news on AI'"
188
+ }
189
+ },
190
+ "required": ["query"]
191
+ }
192
+ }
193
+
194
+ openai_format_tools = [get_weather_api, search_api]
195
+
196
+ # Helper function to convert openai format tools to our more concise xLAM format
197
+ def convert_to_xlam_tool(tools):
198
+ ''''''
199
+ if isinstance(tools, dict):
200
+ return {
201
+ "name": tools["name"],
202
+ "description": tools["description"],
203
+ "parameters": {k: v for k, v in tools["parameters"].get("properties", {}).items()}
204
+ }
205
+ elif isinstance(tools, list):
206
+ return [convert_to_xlam_tool(tool) for tool in tools]
207
+ else:
208
+ return tools
209
+
210
+ # Helper function to build the input prompt for our model
211
+ def build_prompt(task_instruction: str, format_instruction: str, tools: list, query: str):
212
+ prompt = f"[BEGIN OF TASK INSTRUCTION]\n{task_instruction}\n[END OF TASK INSTRUCTION]\n\n"
213
+ prompt += f"[BEGIN OF AVAILABLE TOOLS]\n{json.dumps(xlam_format_tools)}\n[END OF AVAILABLE TOOLS]\n\n"
214
+ prompt += f"[BEGIN OF FORMAT INSTRUCTION]\n{format_instruction}\n[END OF FORMAT INSTRUCTION]\n\n"
215
+ prompt += f"[BEGIN OF QUERY]\n{query}\n[END OF QUERY]\n\n"
216
+ return prompt
217
+
218
+ # Build the input and start the inference
219
+ xlam_format_tools = convert_to_xlam_tool(openai_format_tools)
220
+ content = build_prompt(task_instruction, format_instruction, xlam_format_tools, query)
221
+
222
+ messages=[
223
+ { 'role': 'user', 'content': content}
224
+ ]
225
+ inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
226
+
227
+ # tokenizer.eos_token_id is the id of <|EOT|> token
228
+ outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
229
+ print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))
230
+ ````
231
+
232
+ Then you should be able to see the following output string in JSON format:
233
+
234
+ ```shell
235
+ {"tool_calls": [{"name": "get_weather", "arguments": {"location": "New York", "unit": "fahrenheit"}}]}
236
+ ```
237
+
238
+ We highly recommend to use our provided prompt format and helper functions to yield the best function-calling performance of our model.
239
+
240
+ ### Usage with vLLM
241
+
242
+ We provide example scripts to deploy our model with `vllm` and run inferences. First, install the required packages:
243
+
244
+ ```bash
245
+ pip install vllm openai argparse jinja2
246
+ ```
247
+
248
+ The example scripts are located in the [examples](https://huggingface.co/Salesforce/xLAM-1b-fc-r/tree/main/examples) folder.
249
+
250
+ #### 1. Test Prompt Template
251
+
252
+ To build prompts using the chat template and output formatted prompts ready for various test cases, run:
253
+
254
+ ```bash
255
+ python test_prompt_template.py --model
256
+ ```
257
+
258
+ #### 2. Test xLAM Model with a Manually Served Endpoint
259
+
260
+ a. Serve the model with vLLM:
261
+
262
+ ```bash
263
+ python -m vllm.entrypoints.openai.api_server --model Salesforce/xLAM-1b-fc-r --served-model-name xlam-1b-fc-r --dtype bfloat16 --port 8001
264
+ ```
265
+
266
+ b. Run the test script:
267
+
268
+ ```bash
269
+ python test_xlam_model_with_endpoint.py --model_name xlam-1b-fc-r --port 8001 [OPTIONS]
270
+ ```
271
+
272
+ Options:
273
+ - `--temperature`: Default 0.3
274
+ - `--top_p`: Default 1.0
275
+ - `--max_tokens`: Default 512
276
+
277
+ This test script provides a handler implementation that can be easily applied to your customized function-calling applications.
278
+
279
+ #### 3. Test xLAM Model by Directly Using vLLM Library
280
+
281
+ To test the xLAM model directly with the vLLM library, run:
282
+
283
+ ```bash
284
+ python test_xlam_model_with_vllm.py --model Salesforce/xLAM-1b-fc-r [OPTIONS]
285
+ ```
286
+
287
+ 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.
288
+
289
+ #### Customization
290
+
291
+ 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.
292
+
293
+ Additional customization tips:
294
+ - Modify the `--dtype` parameter when serving the model based on your GPU capacity.
295
+ - Refer to the vLLM documentation for more detailed configuration options.
296
+ - Explore the `demo.ipynb` file for a comprehensive description of the entire workflow, including how to execute APIs.
297
+
298
+ These resources provide a robust foundation for integrating xLAM models into your applications, allowing for tailored and efficient deployment.
299
+
300
+ ## License
301
+
302
+ `xLAM-1b-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).
303
+
304
+ ## Citation
305
+
306
+ If you find this repo helpful, please cite our paper:
307
+ ```bibtex
308
+ @article{liu2024apigen,
309
+ title={APIGen: Automated Pipeline for Generating Verifiable and Diverse Function-Calling Datasets},
310
+ 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},
311
+ journal={arXiv preprint arXiv:2406.18518},
312
+ year={2024}
313
+ }
314
+ ```
315
+