andthattoo commited on
Commit
fdda5b3
·
verified ·
1 Parent(s): 1a62e5c

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +239 -3
README.md CHANGED
@@ -1,3 +1,239 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ base_model:
6
+ - Qwen/Qwen2.5-Coder-1.5B-Instruct
7
+ pipeline_tag: text-generation
8
+ library_name: transformers
9
+ tags:
10
+ - code
11
+ - chat
12
+ - qwen
13
+ - qwen-coder
14
+ - agent
15
+ ---
16
+
17
+
18
+ # Tiny-Agent-α
19
+
20
+ ## Introduction
21
+
22
+ ***Tiny-Agent-α*** is an extension of Dria-Agent-a, trained on top of the [Qwen2.5-Coder](https://huggingface.co/collections/Qwen/qwen25-coder-66eaa22e6f99801bf65b0c2f) series to be used in edge devices. These models are carefully fine tuned with quantization aware training to minimize performance degradation after quantization.
23
+
24
+ Tiny-Agent-α employs ***Pythonic function calling***, which is LLMs using blocks of Python code to interact with provided tools and output actions. This method was inspired by many previous work, including but not limited to [DynaSaur](https://arxiv.org/pdf/2411.01747), [RLEF](https://arxiv.org/pdf/2410.02089), [ADAS](https://arxiv.org/pdf/2408.08435) and [CAMEL](https://arxiv.org/pdf/2303.17760). This way of function calling has a few advantages over traditional JSON-based function calling methods:
25
+
26
+ 1. **One-shot Parallel Multiple Function Calls:** The model can can utilise many synchronous processes in one chat turn to arrive to a solution, which would require other function calling models multiple turns of conversation.
27
+ 2. **Free-form Reasoning and Actions:** The model provides reasoning traces freely in natural language and the actions in between \`\`\`python \`\`\` blocks, as it already tends to do without special prompting or tuning. This tries to mitigate the possible performance loss caused by imposing specific formats on LLM outputs discussed in [Let Me Speak Freely?](https://arxiv.org/pdf/2408.02442)
28
+ 3. **On-the-fly Complex Solution Generation:** The solution provided by the model is essentially a Python program with the exclusion of some "risky" builtins like `exec`, `eval` and `compile` (see full list in **Quickstart** below). This enables the model to implement custom complex logic with conditionals and synchronous pipelines (using the output of one function in the next function's arguments) which would not be possible with the current JSON-based function calling methods (as far as we know).
29
+
30
+ ## Quickstart
31
+
32
+ You can use tiny-agents easily with the dria_agent package:
33
+
34
+ ````bash
35
+ pip install dria_agent
36
+ ````
37
+
38
+ This package handles models, tools, code execution, and backend (supports mlx, ollama, and transformers).
39
+
40
+ ### Usage
41
+
42
+ Decorate functions with @tool to expose them to the agent.
43
+
44
+ ````python
45
+ from dria_agent import tool
46
+
47
+ @tool
48
+ def check_availability(day: str, start_time: str, end_time: str) -> bool:
49
+ """
50
+ Checks if a given time slot is available.
51
+
52
+ :param day: The date in "YYYY-MM-DD" format.
53
+ :param start_time: The start time of the desired slot (HH:MM format, 24-hour).
54
+ :param end_time: The end time of the desired slot (HH:MM format, 24-hour).
55
+ :return: True if the slot is available, otherwise False.
56
+ """
57
+ # Mock implementation
58
+ if start_time == "12:00" and end_time == "13:00":
59
+ return False
60
+ return True
61
+ ````
62
+
63
+ Create an agent with custom tools:
64
+
65
+ ```python
66
+ from dria_agent import ToolCallingAgent
67
+
68
+ agent = ToolCallingAgent(
69
+ tools=[check_availability]
70
+ )
71
+ ```
72
+
73
+ Use agent.run(query) to execute tasks with tools.
74
+
75
+ ```python
76
+ execution = agent.run("Check my calendar for tomorrow noon", print_results=True)
77
+ ```
78
+
79
+ Where output is
80
+ ````
81
+ let me help you check your availability for a 1-hour meditation session
82
+ starting at noon tomorrow.
83
+
84
+ Step-by-step reasoning:
85
+ 1. We need to check availability for a specific time slot (noon)
86
+ 2. The duration is 1 hour, so we'll use the same start and end times
87
+ 3. Since it's tomorrow, we should format the date as "YYYY-MM-DD"
88
+ 4. Use the check_availability() function with these parameters
89
+
90
+ Here's the code to check your availability:
91
+
92
+ ```python
93
+ tomorrow = (datetime.now() + timedelta(days=1)).strftime("%Y-%m-%d")
94
+ start_time = "12:00" # Noon in 24-hour format
95
+ end_time = "13:00" # One hour after noon
96
+
97
+ availability = check_availability(tomorrow, start_time, end_time)
98
+ ```
99
+
100
+ The code will:
101
+ - Calculate tomorrow's date using datetime and timedelta
102
+ - Set the time slot to noon (12:00) for 1 hour duration
103
+ - Check if this time slot is available using the check_availability function
104
+
105
+ The availability variable will contain True if you're available, or False if
106
+ not.
107
+ ````
108
+
109
+ If using dria_agent, system prompts and tooling work out of the box—no extra setup needed.
110
+
111
+ You can use Tiny-Agent-a-3B directly with transformers:
112
+
113
+ ````python
114
+ import json
115
+ from typing import Any, Dict, List
116
+ from transformers import AutoModelForCausalLM, AutoTokenizer
117
+
118
+ # Load model and tokenizer
119
+ model_name = "driaforall/Tiny-Agent-a-3B"
120
+ model = AutoModelForCausalLM.from_pretrained(
121
+ model_name, device_map="auto", torch_dtype="auto", trust_remote_code=True
122
+ )
123
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
124
+
125
+ # System prompt for optimal performance
126
+ SYSTEM_PROMPT = """
127
+ You are an expert AI assistant that specializes in providing Python code to solve the task/problem at hand provided by the user.
128
+
129
+ You can use Python code freely, including the following available functions:
130
+
131
+ <|functions_schema|>
132
+ {{functions_schema}}
133
+ <|end_functions_schema|>
134
+
135
+ The following dangerous builtins are restricted for security:
136
+ - exec
137
+ - eval
138
+ - execfile
139
+ - compile
140
+ - importlib
141
+ - input
142
+ - exit
143
+
144
+ Think step by step and provide your reasoning, outside of function calls.
145
+ You can write Python code and use the available functions. Provide all your Python code in a SINGLE markdown code block.
146
+
147
+ DO NOT use print() statements AT ALL. Avoid mutating variables whenever possible.
148
+ """.strip()
149
+
150
+ # Example function schema (define the functions available to the model)
151
+ FUNCTIONS_SCHEMA = """
152
+ def check_availability(day: str, start_time: str, end_time: str) -> bool:
153
+ """
154
+ Checks if a given time slot is available.
155
+
156
+ :param day: The date in "YYYY-MM-DD" format.
157
+ :param start_time: The start time of the desired slot (HH:MM format, 24-hour).
158
+ :param end_time: The end time of the desired slot (HH:MM format, 24-hour).
159
+ :return: True if the slot is available, otherwise False.
160
+ """
161
+ pass
162
+ """
163
+
164
+ # Format system prompt
165
+ system_prompt = SYSTEM_PROMPT.replace("{{functions_schema}}", FUNCTIONS_SCHEMA)
166
+
167
+ # Example user query
168
+ USER_QUERY = "Check if I'm available for an hour long meditation at tomorrow noon."
169
+
170
+ # Format messages for the model
171
+ messages = [
172
+ {"role": "system", "content": system_prompt},
173
+ {"role": "user", "content": USER_QUERY},
174
+ ]
175
+
176
+ # Prepare input for the model
177
+ text = tokenizer.apply_chat_template(
178
+ messages,
179
+ tokenize=False,
180
+ add_generation_prompt=True
181
+ )
182
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
183
+
184
+ # Generate response
185
+ generated_ids = model.generate(
186
+ **model_inputs,
187
+ max_new_tokens=512
188
+ )
189
+
190
+ # Extract new generated tokens
191
+ generated_ids = [
192
+ output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
193
+ ]
194
+
195
+ # Decode response
196
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
197
+ print(response)
198
+ ````
199
+
200
+ ## Evaluation & Performance
201
+
202
+ We evaluate the model on the **Dria-Pythonic-Agent-Benchmark ([DPAB](https://github.com/firstbatchxyz/function-calling-eval)):** The benchmark we curated with a synthetic data generation +model-based validation + filtering and manual selection to evaluate LLMs on their Pythonic function calling ability, spanning multiple scenarios and tasks. See [blog](https://huggingface.co/blog/andthattoo/dpab-a) for more information.
203
+
204
+ Below are the DPAB results:
205
+
206
+ Current benchmark results for various models **(strict)**:
207
+
208
+ | Model Name | Pythonic | JSON |
209
+ |---------------------------------|----------|------|
210
+ | **Closed Models** | | |
211
+ | Claude 3.5 Sonnet | 87 | 45 |
212
+ | gpt-4o-2024-11-20 | 60 | 30 |
213
+ | **Open Models** | | |
214
+ | **> 100B Parameters** | | |
215
+ | DeepSeek V3 (685B) | 63 | 33 |
216
+ | MiniMax-01 | 62 | 40 |
217
+ | Llama-3.1-405B-Instruct | 60 | 38 |
218
+ | **> 30B Parameters** | | |
219
+ | Qwen-2.5-Coder-32b-Instruct | 68 | 32 |
220
+ | Qwen-2.5-72b-instruct | 65 | 39 |
221
+ | Llama-3.3-70b-Instruct | 59 | 40 |
222
+ | QwQ-32b-Preview | 47 | 21 |
223
+ | **< 20B Parameters** | | |
224
+ | Phi-4 (14B) | 55 | 35 |
225
+ | Qwen2.5-Coder-7B-Instruct | 44 | 39 |
226
+ | Qwen-2.5-7B-Instruct | 47 | 34 |
227
+ | **Tiny-Agent-a-3B** | **72** | 34 |
228
+ | Qwen2.5-Coder-3B-Instruct | 26 | 37 |
229
+ | **Tiny-Agent-a-1.5B** | **73** | 30 |
230
+
231
+ #### Citation
232
+
233
+ ```
234
+ @misc{Dria-Agent-a,
235
+ url={https://huggingface.co/blog/andthattoo/dria-agent-a},
236
+ title={Dria-Agent-a},
237
+ author={"andthattoo", "Atakan Tekparmak"}
238
+ }
239
+ ```