File size: 17,046 Bytes
1fa3c6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
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
# Copyright 2020-2026 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from dataclasses import dataclass, field

from datasets import Dataset
from transformers import HfArgumentParser
from transformers.utils import get_json_schema


@dataclass
class ScriptArguments:
    r"""

    Arguments for the script.



    Args:

        test_size (`float`, *optional*, defaults to `0.1`):

            Fraction of the dataset to include in the test split.

        push_to_hub (`bool`, *optional*, defaults to `False`):

            Whether to push the dataset to the Hugging Face Hub.

        repo_id (`str`, *optional*, defaults to `"trl-internal-testing/zen"`):

            Hugging Face repository ID to push the dataset to.

    """

    test_size: float = field(
        default=0.1,
        metadata={"help": "Fraction of the dataset to include in the test split."},
    )
    push_to_hub: bool = field(
        default=False,
        metadata={"help": "Whether to push the dataset to the Hugging Face Hub."},
    )
    repo_id: str = field(
        default="trl-internal-testing/toolcall",
        metadata={"help": "Hugging Face repository ID to push the dataset to."},
    )


def main(test_size, push_to_hub, repo_id):
    # Fictitious functions to simulate tool calls
    def start_timer(duration: int) -> int:
        """

        Starts a timer for the specified duration in seconds.



        Args:

            duration: Duration in seconds to set the timer for.



        Returns:

            The duration set for the timer.

        """
        return duration

    def get_current_time(location: str) -> str:
        """

        Returns the current time in the specified location.



        Args:

            location: The location for which to get the current time.



        Returns:

            The current time in the specified location.

        """
        return "06:22:48"

    def get_air_quality_index(location: str) -> int:
        """

        Returns the air quality index for the specified location.



        Args:

            location: The location for which to get the air quality index.



        Returns:

            The air quality index for the specified location.

        """
        return 53

    def play_music(title: str, artist: str) -> dict:
        """

        Plays music by the specified title and artist.



        Args:

            title: The title of the music to play.

            artist: The artist of the music to play.



        Returns:

            A dictionary indicating the status of the music playback.

        """
        return {"status": "Playing"}

    def get_weather_forecast(city: str, date: str) -> dict:
        """

        Returns the weather forecast for the specified city and date.



        Args:

            city: The city for which to get the weather forecast.

            date: The date for which to get the weather forecast.



        Returns:

            A dictionary containing the temperature and weather condition.

        """
        return {"temperature": 22, "condition": "partly cloudy"}

    def control_light(room: str, state: str) -> dict:
        """

        Controls the light in the specified room.



        Args:

            room: The room where the light should be controlled.

            state: The desired state of the light ("on" or "off").



        Returns:

            A dictionary indicating the state of the light.

        """
        return {"state": state}

    def create_reminder(time: str, note: str) -> str:
        """

        Creates a reminder for the specified time and note.



        Args:

            time: The time for the reminder.

            note: The note for the reminder.



        Returns:

            A confirmation message indicating that the reminder has been set.

        """
        return "I'll remind you to call mom at 7 PM."

    def get_wind_conditions(city: str, unit: str) -> tuple[int, str]:
        """

        Returns the wind conditions for the specified city.



        Args:

            city: The city for which to get the wind conditions.

            unit: The unit of measurement for the wind speed (e.g., "mph").



        Returns:

            A tuple containing the wind speed and direction.

        """
        return 14, "NW"

    start_timer = get_json_schema(start_timer)
    get_current_time = get_json_schema(get_current_time)
    get_air_quality_index = get_json_schema(get_air_quality_index)
    play_music = get_json_schema(play_music)
    get_weather_forecast = get_json_schema(get_weather_forecast)
    control_light = get_json_schema(control_light)
    create_reminder = get_json_schema(create_reminder)
    get_wind_conditions = get_json_schema(get_wind_conditions)

    # fmt: off
    language_modeling_dataset = Dataset.from_dict({
        "messages": [
            [
                {"role": "user", "content": "Set a timer for 10 minutes."},
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "start_timer", "arguments": {"duration": 600}}}]},
                {"role": "tool", "name": "start_timer", "content": "600"},
                {"role": "assistant", "content": "Timer set for 10 minutes."},
            ],
            [
                {"role": "user", "content": "What time is it in Tokyo?"},
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_current_time", "arguments": {"location": "Tokyo"}}}]},
                {"role": "tool", "name": "get_current_time", "content": "06:22:48"},
                {"role": "assistant", "content": "The current time in Tokyo is 06:22 AM."},
            ],
            [
                {"role": "user", "content": "Is the air clean today in Lisbon?"},
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_air_quality_index", "arguments": {"location": "Lisbon, Portugal"}}}]},
                {"role": "tool", "name": "get_air_quality_index", "content": "53"},
                {"role": "assistant", "content": "The air quality is moderate."},
            ],
            [
                {"role": "user", "content": "Play some music."},
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "play_music", "arguments": {"title": "Take Five", "artist": "Dave Brubeck"}}}]},
                {"role": "tool", "name": "play_music", "content": "{'status': 'Playing'}"},
                {"role": "assistant", "content": "Enjoy the jazz tunes!"},
            ],
            [
                {"role": "user", "content": "What's the weather like tomorrow in Berlin?"},
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_weather_forecast", "arguments": {"city": "Berlin", "date": "2025-06-16"}}}]},
                {"role": "tool", "name": "get_weather_forecast", "content": "{'temperature': 22, 'condition': 'partly cloudy'}"},
                {"role": "assistant", "content": "Tomorrow in Berlin will be partly cloudy with a high of 22°C."}
            ],
            [
                {"role": "user", "content": "Turn on the living room lights."},
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "control_light", "arguments": {"room": "living room", "state": "on"}}}]},
                {"role": "tool", "name": "control_light", "content": "{'state': 'on'}"},
                {"role": "assistant", "content": "The living room lights are now on."}
            ],
            [
                {"role": "user", "content": "Remind me to call mom at 7 PM."},
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "create_reminder", "arguments": {"time": "19:00", "note": "Call mom"}}}]},
                {"role": "tool", "name": "create_reminder", "content": "Reminder set"},
                {"role": "assistant", "content": "Okay, I'll remind you to call mom at 7 PM."}
            ],
            [
                {"role": "user", "content": "How strong is the wind in Chicago right now?"},
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_wind_conditions", "arguments": {"city": "Chicago", "unit": "mph"}}}]},
                {"role": "tool", "name": "get_wind_conditions", "content": "(14, 'NW')"},
                {"role": "assistant", "content": "The wind in Chicago is blowing at 14 mph from the northwest."}
            ]
        ],
        "tools": [
            [start_timer, create_reminder],
            [get_current_time],
            [get_air_quality_index, get_weather_forecast, get_wind_conditions],
            [play_music, control_light],
            [get_weather_forecast, get_wind_conditions],
            [control_light],
            [start_timer, create_reminder],
            [get_weather_forecast, get_wind_conditions],
        ]
    }, on_mixed_types="use_json")
    language_modeling_dataset = language_modeling_dataset.train_test_split(test_size=test_size, shuffle=False)
    if push_to_hub:
        language_modeling_dataset.push_to_hub(repo_id, config_name="language_modeling")

    preference_dataset = Dataset.from_dict({
        "prompt": [
            [{"role": "user", "content": "Set a timer for 10 minutes."}],
            [{"role": "user", "content": "What time is it in Tokyo?"}],
            [{"role": "user", "content": "Is the air clean today in Lisbon?"}],
            [{"role": "user", "content": "Play some music."}],
            [{"role": "user", "content": "What's the weather like tomorrow in Berlin?"}],
            [{"role": "user", "content": "Turn on the living room lights."}],
            [{"role": "user", "content": "Remind me to call mom at 7 PM."}],
            [{"role": "user", "content": "How strong is the wind in Chicago right now?"}],
        ],
        "chosen": [
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "start_timer", "arguments": {"duration": 600}}}]},
                {"role": "tool", "name": "start_timer", "content": "600"},
                {"role": "assistant", "content": "Timer set for 10 minutes."},
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_current_time", "arguments": {"location": "Tokyo"}}}]},
                {"role": "tool", "name": "get_current_time", "content": "06:22:48"},
                {"role": "assistant", "content": "The current time in Tokyo is 06:22 AM."},
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_air_quality_index", "arguments": {"location": "Lisbon, Portugal"}}}]},
                {"role": "tool", "name": "get_air_quality_index", "content": "53"},
                {"role": "assistant", "content": "The air quality is moderate."},
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "play_music", "arguments": {"title": "Take Five", "artist": "Dave Brubeck"}}}]},
                {"role": "tool", "name": "play_music", "content": "{'status': 'Playing'}"},
                {"role": "assistant", "content": "Enjoy the jazz tunes!"},
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_weather_forecast", "arguments": {"city": "Berlin", "date": "2025-06-16"}}}]},
                {"role": "tool", "name": "get_weather_forecast", "content": "{'temperature': 22, 'condition': 'partly cloudy'}"},
                {"role": "assistant", "content": "Tomorrow in Berlin will be partly cloudy with a high of 22°C."}
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "control_light", "arguments": {"room": "living room", "state": "on"}}}]},
                {"role": "tool", "name": "control_light", "content": "{'state': 'on'}"},
                {"role": "assistant", "content": "The living room lights are now on."}
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "create_reminder", "arguments": {"time": "19:00", "note": "Call mom"}}}]},
                {"role": "tool", "name": "create_reminder", "content": "Reminder set"},
                {"role": "assistant", "content": "Okay, I’ll remind you to call mom at 7 PM."}
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_wind_conditions", "arguments": {"city": "Chicago", "unit": "mph"}}}]},
                {"role": "tool", "name": "get_wind_conditions", "content": "(14, 'NW')"},
                {"role": "assistant", "content": "The wind in Chicago is blowing at 14 mph from the northwest."}
            ],
        ],
        "rejected": [
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "start_timer", "arguments": {"duration": 10}}}]},
                {"role": "tool", "name": "start_timer", "content": "10"},
                {"role": "assistant", "content": "Timer set for 10 seconds."},
            ],
            [
                {"role": "assistant", "content": "It is 6:22 AM in Tokyo."},
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_air_quality_index", "arguments": {"location": "Lisbon"}}}]},
                {"role": "tool", "name": "get_air_quality_index", "content": "53"},
                {"role": "assistant", "content": "The air quality is great."},
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "play_music", "arguments": {"title": "Take Five", "artist": "Daft Punk"}}}]},
                {"role": "tool", "name": "play_music", "content": "{'status': 'Playing'}"},
                {"role": "assistant", "content": "Playing your song."},
            ],
            [
                {"role": "assistant", "content": "Tomorrow in Berlin will be hot and sunny."},
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "control_light", "arguments": {"room": "living room", "state": "off"}}}]},
                {"role": "tool", "name": "control_light", "content": "{'state': 'off'}"},
                {"role": "assistant", "content": "The living room lights are now off."}
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "create_reminder", "arguments": {"time": "07:00", "note": "Call mom"}}}]},
                {"role": "tool", "name": "create_reminder", "content": "Reminder set"},
                {"role": "assistant", "content": "Okay, I'll remind you to call mom at 7 AM."}
            ],
            [
                {"role": "assistant", "tool_calls": [{"type": "function", "function": {"name": "get_weather_forecast", "arguments": {"city": "Chicago", "date": "2025-06-16"}}}]},
                {"role": "tool", "name": "get_weather_forecast", "content": "{'temperature': 22, 'condition': 'partly cloudy'}"},
                {"role": "assistant", "content": "Tomorrow in Chicago will be partly cloudy with a high of 22°C."}
            ],
        ],
        "tools": [
            [start_timer],
            [get_current_time],
            [get_air_quality_index],
            [play_music],
            [get_weather_forecast],
            [control_light],
            [create_reminder],
            [get_wind_conditions],
        ],
    }, on_mixed_types="use_json")
    preference_dataset = preference_dataset.train_test_split(test_size=test_size, shuffle=False)
    if push_to_hub:
        preference_dataset.push_to_hub(repo_id, config_name="preference")
    # fmt: on


if __name__ == "__main__":
    parser = HfArgumentParser(ScriptArguments)
    script_args = parser.parse_args_into_dataclasses()[0]
    main(script_args.test_size, script_args.push_to_hub, script_args.repo_id)