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76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
---
license: other
license_name: katanemo-research
license_link: >-
  https://huggingface.co/katanemolabs/Arch-Function-1.5B/blob/main/LICENSE
base_model:
- Qwen/Qwen2.5-1.5B-Instruct
language:
- en
pipeline_tag: text-generation
library_name: transformers
---

# katanemo/Arch-Function-1.5B

## Overview
The Katanemo Arch-Function collection of large language models (LLMs) is a collection state-of-the-art (SOTA) LLMs specifically designed for **function calling** tasks. The models are designed to understand complex function signatures, identify required parameters, and produce accurate function call outputs based on natural language prompts. Achieving performance on par with GPT-4, these models set a new benchmark in the domain of function-oriented tasks, making them suitable for scenarios where automated API interaction and function execution is crucial.

In summary, the Katanemo Arch-Function collection demonstrates:
- **State-of-the-art performance** in function calling
- **Accurate parameter identification and suggestion**, even in ambiguous or incomplete inputs
- **High generalization** across multiple function calling use cases, from API interactions to automated backend tasks.
- Optimized **low-latency, high-throughput** performance, making it suitable for real-time, production environments.

Arch-Function is the core LLM used in then open source [Arch Gateway](https://github.com/katanemo/arch) to seamlessly integrate user prompts with developers APIs

## Key Features
<table>
  <tr style="text-align: left; vertical-align: middle; font-weight: bold;">
    <td>Functionality</td>
    <td>Definition</td>
  </tr>
  <tr style="text-left: left; vertical-align: middle;">
    <td>Single Function Calling</td>
    <td>Call only one function per user query </td>
  </tr>
  <tr style="text-left: left; vertical-align: middle;">
    <td>Parallel Function Calling</td>
    <td>Call the same function multiple times but with different set of parameter values</td>
  </tr>
  <tr style="text-left: left; vertical-align: middle;">
    <td>Multiple Function Calling</td>
    <td>Call different functions per user query</td>
  </tr>
  <tr style="text-left: left; vertical-align: middle;">
    <td>Parallel & Multiple</td>
    <td>Perform both parallel and multiple function calling</td>
  </tr>
</table>


## Training Details
Katanemo Arch-Function collection is built on top of the [Qwen 2.5](https://huggingface.co/collections/Qwen/qwen25-66e81a666513e518adb90d9e). A blog with technical details leading to our models will be published soon.


## Performance Benchmarks
We evaluate Katanemo Arch-Function series on the [Berkeley Function-Calling Leaderboard (BFCL)](https://gorilla.cs.berkeley.edu/leaderboard.html#leaderboard). We compare with commonly-used models and the results (as of Oct 21st, 2024) are shwon below. For each model family, we select the one with the highest rank.

<table>
  <tr style="text-align: center; vertical-align: middle; font-weight: bold;">
    <td rowspan=2>Rank</td>
    <td rowspan=2>Model</td>
    <td rowspan=2>Overall</td>
    <td colspan=3>Single Turn</td>
    <td rowspan=1>Multi Turn</td>
    <td colspan=2>Hallucination</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle; font-weight: bold;">
    <td>Non-live (AST)</td>
    <td>Non-live (Exec)</td>
    <td>Live (AST)</td>
    <td>Overall</td>
    <td>Relevance</td>
    <td>Irrelevance</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle;">
    <td>1</td>
    <td>GPT-4o-2024-08-06 (FC)</td>
    <td>62.19%</td>
    <td>85.90%</td>
    <td>85.64%</td>
    <td>75.43%</td>
    <td>25.00%</td>
    <td>63.41%</td>
    <td>82.93%</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle;">
    <td>6</td>
    <td>o1-preview-2024-09-12 (Prompt)</td>
    <td>59.27%</td>
    <td>86.42%</td>
    <td>88.88%</td>
    <td>73.08%</td>
    <td>17.62%</td>
    <td>73.17%</td>
    <td>74.60%</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle; font-weight: bold;">
    <td> </td>
    <td>Arch-Function-7B</td>
    <td>58.44%</td>
    <td>85.58%</td>
    <td>88.14%</td>
    <td>69.08%</td>
    <td>20.50%</td>
    <td>92.68%</td>
    <td>74.05%</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle; ">
    <td>9</td>
    <td>Gemini-1.5-Flash-002 (Prompt)</td>
    <td>57.92%</td>
    <td>86.58%</td>
    <td>89.48%</td>
    <td>76.28%</td>
    <td>9.88%</td>
    <td>85.37%</td>
    <td>78.54%</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle; ">
    <td>12</td>
    <td>Claude-3.5-Sonnet-20240620 (FC)</td>
    <td>57.42%</td>
    <td>70.04%</td>
    <td>66.27%</td>
    <td>74.68%</td>
    <td>28.38%</td>
    <td>68.29%</td>
    <td>74.58%</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle; ">
    <td>13</td>
    <td>mistral-large-2407 (FC)</td>
    <td>56.80%</td>
    <td>86.62%</td>
    <td>84.57%</td>
    <td>68.37%</td>
    <td>20.62%</td>
    <td>75.61%</td>
    <td>49.44%</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle; font-weight: bold;">
    <td> </td>
    <td>Arch-Function-3B</td>
    <td>56.57%</td>
    <td>83.62%</td>
    <td>85.36%</td>
    <td>66.90%</td>
    <td>19.50%</td>
    <td>97.56%</td>
    <td>70.99%</td>
  </tr>
  </tr>
  <tr style="text-align: center; vertical-align: middle; font-weight: bold;">
    <td> </td>
    <td>Arch-Function-1.5B</td>
    <td>54.52%</td>
    <td>80.31%</td>
    <td>82.04%</td>
    <td>66.19%</td>
    <td>17.25%</td>
    <td>97.56%</td>
    <td>69.95%</td>
  </tr>

  <tr style="text-align: center; vertical-align: middle; ">
    <td>21</td>
    <td>Llama-3.1-70B-Instruct (Prompt)</td>
    <td>53.67%</td>
    <td>88.90%</td>
    <td>89.34%</td>
    <td>61.13%</td>
    <td>12.38%</td>
    <td>92.68%</td>
    <td>58.38%</td>
  </tr>
  <tr style="text-align: center; vertical-align: middle; ">
    <td>22</td>
    <td>Gemma-2-27b-it (Prompt)</td>
    <td>53.66%</td>
    <td>88.52%</td>
    <td>87.89%</td>
    <td>69.48%</td>
    <td>4.12%</td>
    <td>87.8%</td>
    <td>68.76%</td>
  </tr>
</table>


# Requirements
The code of Arch-Function-1.5B has been in the Hugging Face `transformers` library and we advise you to install latest version:
```bash
pip install transformers>=4.37.0
```


# How to use
We use the following example to illustrate how to use our model to perform function calling tasks. Please note that, our model works best with our provided prompt format. 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).


### Single Turn Example
````python
import json
from typing import Any, Dict, List
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "katanemo/Arch-Function-1.5B"
model = AutoModelForCausalLM.from_pretrained(
    model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(model_name)

# Please use our provided prompt for best performance
TASK_PROMPT = """
You are a helpful assistant.
""".strip()

TOOL_PROMPT = """
# Tools

You may call one or more functions to assist with the user query.

You are provided with function signatures within <tools></tools> XML tags:
<tools>
{tool_text}
</tools>
""".strip()

FORMAT_PROMPT = """
For each function call, return a json object with function name and arguments within <tool_call></tool_call> XML tags:
<tool_call>
{"name": <function-name>, "arguments": <args-json-object>}
</tool_call>
""".strip()

# Define available tools
get_weather_api = {
    "type": "function",
    "function": {
        "name": "get_weather",
        "description": "Get the current weather for a location",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "str",
                    "description": "The city and state, e.g. San Francisco, New York",
                },
                "unit": {
                    "type": "str",
                    "enum": ["celsius", "fahrenheit"],
                    "description": "The unit of temperature to return",
                },
            },
            "required": ["location"],
        },
    },
}

openai_format_tools = [get_weather_api]


def convert_tools(tools: List[Dict[str, Any]]):
    return "\n".join([json.dumps(tool) for tool in tools])

# Helper function to create the system prompt for our model
def format_prompt(tools: List[Dict[str, Any]]):
    tool_text = convert_tools(tools)

    return (
        TASK_PROMPT
        + "\n\n"
        + TOOL_PROMPT.format(tool_text=tool_text)
        + "\n\n"
        + FORMAT_PROMPT
        + "\n"
    )


system_prompt = format_prompt(openai_format_tools)

messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": "What is the weather in Seattle?"},
]

inputs = tokenizer.apply_chat_template(
    messages, add_generation_prompt=True, return_tensors="pt"
).to(model.device)

outputs = model.generate(
    inputs,
    max_new_tokens=512,
    do_sample=False,
    num_return_sequences=1,
    eos_token_id=tokenizer.eos_token_id,
)

response = tokenizer.decode(outputs[0][len(inputs[0]) :], skip_special_tokens=True)
print(response)
````

Then you should be able to see the following output string in JSON format:
````python
<tool_call>
{"name": "get_weather", "arguments": {"location": "Seattle"}}
</tool_call>
````

### Multi Turn Example
Upon getting results from functions, you can add it to the `messages` list as a `user` message and pass it to the model to get responses for users.

````python
# Suppose we receive the following result from the function:
get_weather_api_result = {'name': 'get_weather', 'results': {'temperature': '62°', 'unit': 'fahrenheit'}}
execution_results = [get_weather_api_result]

def add_execution_results(messages: List[Dict[str, Any]], execution_results: List[Dict[str, Any]]):
    content = "\n".join([f"<tool_response>\n{json.dumps(result)}</tool_response>" for result in execution_results])
    messages.append({"role": "user", "content": content})
    return messages

messages = add_execution_results(messages, execution_results)

inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)

outputs = model.generate(
    inputs,
    max_new_tokens=512,
    do_sample=False,
    num_return_sequences=1,
    eos_token_id=tokenizer.eos_token_id,
)

response = tokenizer.decode(outputs[0][len(inputs[0]) :], skip_special_tokens=True)
print(response)
````

Then you should be able to see the following output:
```
The current temperature in Seattle is 62 degrees in Fahrenheit.
```


# License
Katanemo Arch-Function collection is distributed under the [Katanemo license](https://huggingface.co/katanemolabs/Arch-Function-1.5B/blob/main/LICENSE).