qninhdt commited on
Commit ·
9a9a2f5
1
Parent(s): 8b94669
cc
Browse files
scripts/generate_dataset.sh
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
-
mkdir -p
|
| 2 |
-
mkdir -p
|
| 3 |
-
mkdir -p
|
| 4 |
-
mkdir -p
|
| 5 |
|
| 6 |
# Download datasets
|
| 7 |
-
# wget -O
|
| 8 |
echo "APIGen cannot be downloaded directly due to permission issues. Please download it manually and place it in the datasets/apigen folder."
|
| 9 |
-
wget -O
|
| 10 |
-
wget -O
|
| 11 |
|
| 12 |
# Preprocess datasets
|
| 13 |
python ./scripts/preprocess_apigen_dataset.py
|
|
|
|
| 1 |
+
mkdir -p datasets
|
| 2 |
+
mkdir -p datasets/apigen
|
| 3 |
+
mkdir -p datasets/glaive
|
| 4 |
+
mkdir -p datasets/toolace
|
| 5 |
|
| 6 |
# Download datasets
|
| 7 |
+
# wget -O datasets/apigen/xlam_function_calling_60k.json https://huggingface.co/datasets/Salesforce/xlam-function-calling-60k/resolve/main/xlam_function_calling_60k.json
|
| 8 |
echo "APIGen cannot be downloaded directly due to permission issues. Please download it manually and place it in the datasets/apigen folder."
|
| 9 |
+
wget -O datasets/glaive/glaive-function-calling-v2.json https://huggingface.co/datasets/glaiveai/glaive-function-calling-v2/resolve/main/glaive-function-calling-v2.json
|
| 10 |
+
wget -O datasets/toolace/data.json https://huggingface.co/datasets/Team-ACE/ToolACE/resolve/main/data.json
|
| 11 |
|
| 12 |
# Preprocess datasets
|
| 13 |
python ./scripts/preprocess_apigen_dataset.py
|
scripts/mix_datasets.py
CHANGED
|
@@ -6,7 +6,7 @@ DATASETS = ["apigen", "glaive", "toolace"]
|
|
| 6 |
data = []
|
| 7 |
|
| 8 |
for dataset_name in DATASETS:
|
| 9 |
-
with open(f"./
|
| 10 |
subdata = json.load(f)
|
| 11 |
|
| 12 |
subdata = [{**item, "source": dataset_name} for item in subdata]
|
|
@@ -86,6 +86,12 @@ from random import shuffle, seed
|
|
| 86 |
one_two = [item for item in data if len(item["used_tools"]) in [1, 2]]
|
| 87 |
other = [item for item in data if len(item["used_tools"]) > 2]
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
train_samples = one_two[: int(len(one_two) * 0.8)]
|
| 90 |
test_samples = one_two[int(len(one_two) * 0.8) :] + other
|
| 91 |
|
|
@@ -118,10 +124,10 @@ print("Number of samples in test dataset:", len(test_dataset["samples"]))
|
|
| 118 |
|
| 119 |
import os
|
| 120 |
|
| 121 |
-
os.makedirs("./
|
| 122 |
|
| 123 |
-
with open("./
|
| 124 |
json.dump(train_dataset, f, indent=2)
|
| 125 |
|
| 126 |
-
with open("./
|
| 127 |
json.dump(test_dataset, f, indent=2)
|
|
|
|
| 6 |
data = []
|
| 7 |
|
| 8 |
for dataset_name in DATASETS:
|
| 9 |
+
with open(f"./datasets/{dataset_name}/output.json") as f:
|
| 10 |
subdata = json.load(f)
|
| 11 |
|
| 12 |
subdata = [{**item, "source": dataset_name} for item in subdata]
|
|
|
|
| 86 |
one_two = [item for item in data if len(item["used_tools"]) in [1, 2]]
|
| 87 |
other = [item for item in data if len(item["used_tools"]) > 2]
|
| 88 |
|
| 89 |
+
seed(42)
|
| 90 |
+
shuffle(one_two)
|
| 91 |
+
|
| 92 |
+
seed(42)
|
| 93 |
+
shuffle(other)
|
| 94 |
+
|
| 95 |
train_samples = one_two[: int(len(one_two) * 0.8)]
|
| 96 |
test_samples = one_two[int(len(one_two) * 0.8) :] + other
|
| 97 |
|
|
|
|
| 124 |
|
| 125 |
import os
|
| 126 |
|
| 127 |
+
os.makedirs("./datasets/mixed", exist_ok=True)
|
| 128 |
|
| 129 |
+
with open("./datasets/mixed/train.json", "w") as f:
|
| 130 |
json.dump(train_dataset, f, indent=2)
|
| 131 |
|
| 132 |
+
with open("./datasets/mixed/test.json", "w") as f:
|
| 133 |
json.dump(test_dataset, f, indent=2)
|
scripts/preprocess_apigen_dataset.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import json
|
| 2 |
|
| 3 |
-
with open("./
|
| 4 |
data = json.load(f)
|
| 5 |
|
| 6 |
results = []
|
|
@@ -23,5 +23,5 @@ for sample in tqdm(data, desc="Processing APIGen samples"):
|
|
| 23 |
|
| 24 |
results.append(result)
|
| 25 |
|
| 26 |
-
with open("./
|
| 27 |
json.dump(results, f, indent=4)
|
|
|
|
| 1 |
import json
|
| 2 |
|
| 3 |
+
with open("./datasets/apigen/xlam_function_calling_60k.json", "r") as f:
|
| 4 |
data = json.load(f)
|
| 5 |
|
| 6 |
results = []
|
|
|
|
| 23 |
|
| 24 |
results.append(result)
|
| 25 |
|
| 26 |
+
with open("./datasets/apigen/output.json", "w") as f:
|
| 27 |
json.dump(results, f, indent=4)
|
scripts/preprocess_glaive_dataset.py
CHANGED
|
@@ -2,7 +2,7 @@ import re
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
|
| 5 |
-
with open("./
|
| 6 |
data = json.load(f)
|
| 7 |
|
| 8 |
|
|
@@ -53,5 +53,5 @@ for sample in tqdm(data, desc="Processing GLAIVE samples"):
|
|
| 53 |
if processed:
|
| 54 |
results.extend(processed)
|
| 55 |
|
| 56 |
-
with open("./
|
| 57 |
json.dump(results, f, indent=2)
|
|
|
|
| 2 |
import json
|
| 3 |
|
| 4 |
|
| 5 |
+
with open("./datasets/glaive/glaive-function-calling-v2.json", "r") as f:
|
| 6 |
data = json.load(f)
|
| 7 |
|
| 8 |
|
|
|
|
| 53 |
if processed:
|
| 54 |
results.extend(processed)
|
| 55 |
|
| 56 |
+
with open("./datasets/glaive/output.json", "w") as f:
|
| 57 |
json.dump(results, f, indent=2)
|
scripts/preprocess_toolace_dataset.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import json
|
| 2 |
import re
|
| 3 |
|
| 4 |
-
with open("./
|
| 5 |
data = json.load(f)
|
| 6 |
|
| 7 |
|
|
@@ -58,5 +58,5 @@ for sample in tqdm(data, desc="Processing ToolACE samples"):
|
|
| 58 |
if processed:
|
| 59 |
results.extend(processed)
|
| 60 |
|
| 61 |
-
with open("./
|
| 62 |
json.dump(results, f, indent=2)
|
|
|
|
| 1 |
import json
|
| 2 |
import re
|
| 3 |
|
| 4 |
+
with open("./datasets/toolace/data.json") as f:
|
| 5 |
data = json.load(f)
|
| 6 |
|
| 7 |
|
|
|
|
| 58 |
if processed:
|
| 59 |
results.extend(processed)
|
| 60 |
|
| 61 |
+
with open("./datasets/toolace/output.json", "w") as f:
|
| 62 |
json.dump(results, f, indent=2)
|
src/models/miniagent_module.py
CHANGED
|
@@ -67,10 +67,6 @@ class MiniAgentModule(LightningModule):
|
|
| 67 |
pred = self.pred_model(inst_emb_r, tool_emb_r) # [BxB, 1]
|
| 68 |
pred = pred.view(B, B) # [B, B]
|
| 69 |
|
| 70 |
-
# target = torch.eye(B, device=pred.device).float()
|
| 71 |
-
|
| 72 |
-
# pos_weight = torch.tensor([B - 1], device=pred.device)
|
| 73 |
-
# loss = F.binary_cross_entropy_with_logits(pred, target, pos_weight=pos_weight)
|
| 74 |
labels = torch.arange(B, device=pred.device).long()
|
| 75 |
loss = (F.cross_entropy(pred, labels) + F.cross_entropy(pred.T, labels)) * 0.5
|
| 76 |
|
|
@@ -146,5 +142,5 @@ class MiniAgentModule(LightningModule):
|
|
| 146 |
pass
|
| 147 |
|
| 148 |
def configure_optimizers(self):
|
| 149 |
-
opt = torch.optim.AdamW(self.parameters(), lr=self.lr)
|
| 150 |
return opt
|
|
|
|
| 67 |
pred = self.pred_model(inst_emb_r, tool_emb_r) # [BxB, 1]
|
| 68 |
pred = pred.view(B, B) # [B, B]
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
labels = torch.arange(B, device=pred.device).long()
|
| 71 |
loss = (F.cross_entropy(pred, labels) + F.cross_entropy(pred.T, labels)) * 0.5
|
| 72 |
|
|
|
|
| 142 |
pass
|
| 143 |
|
| 144 |
def configure_optimizers(self):
|
| 145 |
+
opt = torch.optim.AdamW(self.parameters(), lr=self.lr, weight_decay=1e-4)
|
| 146 |
return opt
|
src/models/mlp_module.py
CHANGED
|
@@ -7,6 +7,7 @@ class MLPProjection(nn.Module):
|
|
| 7 |
def __init__(self, input_dim, hidden_dim, output_dim):
|
| 8 |
super().__init__()
|
| 9 |
self.linear1 = nn.Linear(input_dim, hidden_dim)
|
|
|
|
| 10 |
self.linear2 = nn.Linear(hidden_dim, output_dim)
|
| 11 |
|
| 12 |
def forward(self, x_output):
|
|
@@ -15,6 +16,7 @@ class MLPProjection(nn.Module):
|
|
| 15 |
|
| 16 |
x = self.linear1(x)
|
| 17 |
x = F.silu(x)
|
|
|
|
| 18 |
x = self.linear2(x)
|
| 19 |
|
| 20 |
return x
|
|
@@ -30,13 +32,15 @@ class MLPPrediction(nn.Module):
|
|
| 30 |
real_input_dim = input_dim * (2 + int(use_abs_diff) + int(use_mult))
|
| 31 |
|
| 32 |
self.mlp = nn.Sequential(
|
| 33 |
-
nn.Linear(real_input_dim,
|
| 34 |
-
nn.SiLU(),
|
| 35 |
-
nn.Linear(1024, 512),
|
| 36 |
nn.SiLU(),
|
|
|
|
| 37 |
nn.Linear(512, 256),
|
| 38 |
nn.SiLU(),
|
| 39 |
-
nn.
|
|
|
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
def forward(self, x1, x2):
|
|
|
|
| 7 |
def __init__(self, input_dim, hidden_dim, output_dim):
|
| 8 |
super().__init__()
|
| 9 |
self.linear1 = nn.Linear(input_dim, hidden_dim)
|
| 10 |
+
self.dropout = nn.Dropout(0.3)
|
| 11 |
self.linear2 = nn.Linear(hidden_dim, output_dim)
|
| 12 |
|
| 13 |
def forward(self, x_output):
|
|
|
|
| 16 |
|
| 17 |
x = self.linear1(x)
|
| 18 |
x = F.silu(x)
|
| 19 |
+
x = self.dropout(x)
|
| 20 |
x = self.linear2(x)
|
| 21 |
|
| 22 |
return x
|
|
|
|
| 32 |
real_input_dim = input_dim * (2 + int(use_abs_diff) + int(use_mult))
|
| 33 |
|
| 34 |
self.mlp = nn.Sequential(
|
| 35 |
+
nn.Linear(real_input_dim, 512),
|
|
|
|
|
|
|
| 36 |
nn.SiLU(),
|
| 37 |
+
nn.Dropout(0.3),
|
| 38 |
nn.Linear(512, 256),
|
| 39 |
nn.SiLU(),
|
| 40 |
+
nn.Dropout(0.3),
|
| 41 |
+
nn.Linear(256, 128),
|
| 42 |
+
nn.SiLU(),
|
| 43 |
+
nn.Linear(128, 1),
|
| 44 |
)
|
| 45 |
|
| 46 |
def forward(self, x1, x2):
|
test_bert.ipynb
CHANGED
|
@@ -154,7 +154,7 @@
|
|
| 154 |
"metadata": {},
|
| 155 |
"outputs": [],
|
| 156 |
"source": [
|
| 157 |
-
"datamodule = MixedDataModule(dataset_path=\"./
|
| 158 |
]
|
| 159 |
},
|
| 160 |
{
|
|
|
|
| 154 |
"metadata": {},
|
| 155 |
"outputs": [],
|
| 156 |
"source": [
|
| 157 |
+
"datamodule = MixedDataModule(dataset_path=\"./datasets/mixed\", batch_size=32, num_workers=4, bert_model=\"bert-base-uncased\", tool_capacity=16)"
|
| 158 |
]
|
| 159 |
},
|
| 160 |
{
|