Create CarTool-Instruct.py
Browse files- CarTool-Instruct.py +102 -0
CarTool-Instruct.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import datasets
|
| 3 |
+
|
| 4 |
+
class VehicleDataset(datasets.GeneratorBasedBuilder):
|
| 5 |
+
"""Dataset loading script for vehicle functions dataset."""
|
| 6 |
+
|
| 7 |
+
def _info(self):
|
| 8 |
+
# Define a strict schema to avoid type errors.
|
| 9 |
+
# We treat 'parameters' and 'arguments' as JSON strings to handle mixed types.
|
| 10 |
+
return datasets.DatasetInfo(
|
| 11 |
+
features=datasets.Features({
|
| 12 |
+
"metadata": datasets.Value("string"),
|
| 13 |
+
"tools": datasets.Sequence({
|
| 14 |
+
"function": {
|
| 15 |
+
"name": datasets.Value("string"),
|
| 16 |
+
"description": datasets.Value("string"),
|
| 17 |
+
# 'parameters' is complex and variable, so we store it as a string
|
| 18 |
+
"parameters": datasets.Value("string")
|
| 19 |
+
}
|
| 20 |
+
}),
|
| 21 |
+
"messages": datasets.Sequence({
|
| 22 |
+
"role": datasets.Value("string"),
|
| 23 |
+
"content": datasets.Value("string"),
|
| 24 |
+
"tool_calls": datasets.Sequence({
|
| 25 |
+
"function": {
|
| 26 |
+
"name": datasets.Value("string"),
|
| 27 |
+
# 'arguments' has mixed types (int/bool/array), so we store it as a string
|
| 28 |
+
"arguments": datasets.Value("string")
|
| 29 |
+
}
|
| 30 |
+
})
|
| 31 |
+
})
|
| 32 |
+
})
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
def _split_generators(self, dl_manager):
|
| 36 |
+
# This automatically downloads/finds the data files in your repo
|
| 37 |
+
# Assuming your data is in .json or .jsonl format
|
| 38 |
+
# You can adjust the pattern if your files have specific names
|
| 39 |
+
data_files = dl_manager.download_and_extract(self.config.data_files)
|
| 40 |
+
|
| 41 |
+
# If no data_files config is passed, try to find default files (simplest fallback)
|
| 42 |
+
if not data_files:
|
| 43 |
+
# Fallback: looks for common filenames or you can hardcode the URL/filename here
|
| 44 |
+
# For a simple repo, we typically rely on the user passing data_files in README
|
| 45 |
+
# or we assume a file exists. Let's assume standard "train.jsonl" or search the dir.
|
| 46 |
+
return [
|
| 47 |
+
datasets.SplitGenerator(
|
| 48 |
+
name=datasets.Split.TRAIN,
|
| 49 |
+
# NOTE: Update 'train.jsonl' to your actual filename if needed
|
| 50 |
+
gen_kwargs={"filepath": dl_manager.download("train.jsonl")},
|
| 51 |
+
),
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
return [
|
| 55 |
+
datasets.SplitGenerator(
|
| 56 |
+
name=split,
|
| 57 |
+
gen_kwargs={"filepath": files[0] if isinstance(files, list) else files},
|
| 58 |
+
)
|
| 59 |
+
for split, files in data_files.items()
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
def _generate_examples(self, filepath):
|
| 63 |
+
"""Yields examples as (key, example) tuples."""
|
| 64 |
+
with open(filepath, encoding="utf-8") as f:
|
| 65 |
+
for id_, line in enumerate(f):
|
| 66 |
+
if not line.strip():
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
try:
|
| 70 |
+
data = json.loads(line)
|
| 71 |
+
|
| 72 |
+
# 1. Process 'tools' to stringify 'parameters'
|
| 73 |
+
if "tools" in data and isinstance(data["tools"], list):
|
| 74 |
+
for tool in data["tools"]:
|
| 75 |
+
if "function" in tool:
|
| 76 |
+
func = tool["function"]
|
| 77 |
+
if "parameters" in func and isinstance(func["parameters"], dict):
|
| 78 |
+
func["parameters"] = json.dumps(func["parameters"])
|
| 79 |
+
|
| 80 |
+
# 2. Process 'messages' to stringify 'arguments' inside 'tool_calls'
|
| 81 |
+
if "messages" in data and isinstance(data["messages"], list):
|
| 82 |
+
for msg in data["messages"]:
|
| 83 |
+
# Ensure tool_calls exists and is a list
|
| 84 |
+
tool_calls = msg.get("tool_calls", [])
|
| 85 |
+
if tool_calls is None:
|
| 86 |
+
tool_calls = []
|
| 87 |
+
|
| 88 |
+
processed_tool_calls = []
|
| 89 |
+
for tc in tool_calls:
|
| 90 |
+
if "function" in tc:
|
| 91 |
+
func = tc["function"]
|
| 92 |
+
# Convert the problematic arguments dict to a JSON string
|
| 93 |
+
if "arguments" in func and isinstance(func["arguments"], dict):
|
| 94 |
+
func["arguments"] = json.dumps(func["arguments"])
|
| 95 |
+
processed_tool_calls.append(tc)
|
| 96 |
+
|
| 97 |
+
msg["tool_calls"] = processed_tool_calls
|
| 98 |
+
|
| 99 |
+
yield id_, data
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
print(f"Skipping row {id_} due to error: {e}")
|