Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,148 @@
|
|
1 |
-
---
|
2 |
-
license: cc-by-4.0
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: cc-by-4.0
|
3 |
+
datasets:
|
4 |
+
- Salesforce/xlam-function-calling-60k
|
5 |
+
base_model:
|
6 |
+
- Qwen/Qwen2.5-Coder-7B-Instruct
|
7 |
+
---
|
8 |
+
|
9 |
+
# Hammer2.0-7b Function Calling Model
|
10 |
+
|
11 |
+
## Introduction
|
12 |
+
The Hammer2.0 series models have been released, including versions such as [0.5b](https://huggingface.co/MadeAgents/Hammer2.0-0.5b), [1.5b](https://huggingface.co/MadeAgents/Hammer2.0-1.5b), [3b](https://huggingface.co/MadeAgents/Hammer2.0-3b), and [7b](https://huggingface.co/MadeAgents/Hammer2.0-7b). Compared to the [Hammer1.0](https://huggingface.co/MadeAgents/Hammer-7b,https://huggingface.co/MadeAgents/Hammer-1.5b,https://huggingface.co/MadeAgents/Hammer-4b) series, the Hammer2.0 series has stronger performance in function calling.
|
13 |
+
|
14 |
+
## Model Details
|
15 |
+
Take Hammer2.0-7b as an example, it is a fine-tuned model based on [Qwen2.5-Coder-7B-Instruct](https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct). It's trained using the [APIGen Function Calling Datasets](https://huggingface.co/datasets/Salesforce/xlam-function-calling-60k) containing 60,000 samples, supplemented by [7,500 irrelevance detection data](https://huggingface.co/datasets/MadeAgents/XLAM-7.5k-Irrelevance) we generated. Employing innovative training techniques like function masking, function shuffling, and prompt optimization, Hammer2.0-7b has achieved exceptional performances across numerous benchmarks including [Berkley Function Calling Leaderboard](https://gorilla.cs.berkeley.edu/leaderboard.html), [API-Bank](https://arxiv.org/abs/2304.08244), [Tool-Alpaca](https://arxiv.org/abs/2306.05301), [Nexus Raven](https://github.com/nexusflowai/NexusRaven-V2) and [Seal-Tools](https://arxiv.org/abs/2405.08355).
|
16 |
+
|
17 |
+
|
18 |
+
## Tuning Details
|
19 |
+
Thanks so much for your attention, a report with all the technical details leading to our models will be published soon.
|
20 |
+
|
21 |
+
## Evaluation
|
22 |
+
First, we evaluate Hammer series on the Berkeley Function-Calling Leaderboard (BFCL):
|
23 |
+
|
24 |
+
|
25 |
+
In addition, we evaluated our Hammer2.0 series (0.5b, 1.5b, 3b, 7b) on other academic benchmarks to further show our model's generalization ability:
|
26 |
+
|
27 |
+
|
28 |
+
## Requiements
|
29 |
+
The code of Hammer2.0-7b has been in the latest Hugging face transformers and we advise you to install `transformers>=4.37.0`.
|
30 |
+
|
31 |
+
## How to Use
|
32 |
+
This is a simple example of how to use our model.
|
33 |
+
~~~python
|
34 |
+
import json
|
35 |
+
import torch
|
36 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
37 |
+
|
38 |
+
model_name = "MadeAgents/Hammer2.0-7b"
|
39 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True)
|
40 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
41 |
+
|
42 |
+
# Please use our provided instruction prompt for best performance
|
43 |
+
TASK_INSTRUCTION = """You are a tool calling assistant. In order to complete the user's request, you need to select one or more appropriate tools from the following tools and fill in the correct values for the tool parameters. Your specific tasks are:
|
44 |
+
1. Make one or more function/tool calls to meet the request based on the question.
|
45 |
+
2. If none of the function can be used, point it out and refuse to answer.
|
46 |
+
3. If the given question lacks the parameters required by the function, also point it out.
|
47 |
+
"""
|
48 |
+
|
49 |
+
FORMAT_INSTRUCTION = """
|
50 |
+
The output MUST strictly adhere to the following JSON format, and NO other text MUST be included.
|
51 |
+
The example format is as follows. Please make sure the parameter type is correct. If no function call is needed, please directly output an empty list '[]'
|
52 |
+
```
|
53 |
+
[
|
54 |
+
{"name": "func_name1", "arguments": {"argument1": "value1", "argument2": "value2"}},
|
55 |
+
... (more tool calls as required)
|
56 |
+
]
|
57 |
+
```
|
58 |
+
"""
|
59 |
+
|
60 |
+
# Define the input query and available tools
|
61 |
+
query = "Where can I find live giveaways for beta access and games? And what's the weather like in New York, US?"
|
62 |
+
|
63 |
+
live_giveaways_by_type = {
|
64 |
+
"name": "live_giveaways_by_type",
|
65 |
+
"description": "Retrieve live giveaways from the GamerPower API based on the specified type.",
|
66 |
+
"parameters": {
|
67 |
+
"type": "object",
|
68 |
+
"properties": {
|
69 |
+
"type": {
|
70 |
+
"type": "string",
|
71 |
+
"description": "The type of giveaways to retrieve (e.g., game, loot, beta).",
|
72 |
+
"default": "game"
|
73 |
+
}
|
74 |
+
},
|
75 |
+
"required": ["type"]
|
76 |
+
}
|
77 |
+
}
|
78 |
+
get_current_weather={
|
79 |
+
"name": "get_current_weather",
|
80 |
+
"description": "Get the current weather",
|
81 |
+
"parameters": {
|
82 |
+
"type": "object",
|
83 |
+
"properties": {
|
84 |
+
"location": {
|
85 |
+
"type": "string",
|
86 |
+
"description": "The city and state, e.g. San Francisco, CA"
|
87 |
+
}
|
88 |
+
},
|
89 |
+
"required": ["location"]
|
90 |
+
}
|
91 |
+
}
|
92 |
+
get_stock_price={
|
93 |
+
"name": "get_stock_price",
|
94 |
+
"description": "Retrieves the current stock price for a given ticker symbol. The ticker symbol must be a valid symbol for a publicly traded company on a major US stock exchange like NYSE or NASDAQ. The tool will return the latest trade price in USD. It should be used when the user asks about the current or most recent price of a specific stock. It will not provide any other information about the stock or company.",
|
95 |
+
"parameters": {
|
96 |
+
"type": "object",
|
97 |
+
"properties": {
|
98 |
+
"ticker": {
|
99 |
+
"type": "string",
|
100 |
+
"description": "The stock ticker symbol, e.g. AAPL for Apple Inc."
|
101 |
+
}
|
102 |
+
},
|
103 |
+
"required": ["ticker"]
|
104 |
+
}
|
105 |
+
}
|
106 |
+
|
107 |
+
def convert_to_format_tool(tools):
|
108 |
+
''''''
|
109 |
+
if isinstance(tools, dict):
|
110 |
+
format_tools = {
|
111 |
+
"name": tools["name"],
|
112 |
+
"description": tools["description"],
|
113 |
+
"parameters": tools["parameters"].get("properties", {}),
|
114 |
+
}
|
115 |
+
required = tools["parameters"].get("required", [])
|
116 |
+
for param in required:
|
117 |
+
format_tools["parameters"][param]["required"] = True
|
118 |
+
for param in format_tools["parameters"].keys():
|
119 |
+
if "default" in format_tools["parameters"][param]:
|
120 |
+
default = format_tools["parameters"][param]["default"]
|
121 |
+
format_tools["parameters"][param]["description"]+=f"default is \'{default}\'"
|
122 |
+
return format_tools
|
123 |
+
elif isinstance(tools, list):
|
124 |
+
return [convert_to_format_tool(tool) for tool in tools]
|
125 |
+
else:
|
126 |
+
return tools
|
127 |
+
# Helper function to build the input prompt for our model
|
128 |
+
def build_prompt(task_instruction: str, format_instruction: str, tools: list, query: str):
|
129 |
+
prompt = f"[BEGIN OF TASK INSTRUCTION]\n{task_instruction}\n[END OF TASK INSTRUCTION]\n\n"
|
130 |
+
prompt += f"[BEGIN OF AVAILABLE TOOLS]\n{json.dumps(tools)}\n[END OF AVAILABLE TOOLS]\n\n"
|
131 |
+
prompt += f"[BEGIN OF FORMAT INSTRUCTION]\n{format_instruction}\n[END OF FORMAT INSTRUCTION]\n\n"
|
132 |
+
prompt += f"[BEGIN OF QUERY]\n{query}\n[END OF QUERY]\n\n"
|
133 |
+
return prompt
|
134 |
+
|
135 |
+
# Build the input and start the inference
|
136 |
+
openai_format_tools = [live_giveaways_by_type, get_current_weather,get_stock_price]
|
137 |
+
format_tools = convert_to_format_tool(openai_format_tools)
|
138 |
+
content = build_prompt(TASK_INSTRUCTION, FORMAT_INSTRUCTION, format_tools, query)
|
139 |
+
|
140 |
+
messages=[
|
141 |
+
{ 'role': 'user', 'content': content}
|
142 |
+
]
|
143 |
+
inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt").to(model.device)
|
144 |
+
|
145 |
+
# tokenizer.eos_token_id is the id of <|EOT|> token
|
146 |
+
outputs = model.generate(inputs, max_new_tokens=512, do_sample=False, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
|
147 |
+
print(tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True))
|
148 |
+
~~~
|