MCPcopy Create free account
hub / github.com/OpenBMB/ToolBench / preprocess_rapidapi

Function preprocess_rapidapi

preprocess/preprocess_toolllama_data.py:16–112  ·  view source on GitHub ↗
(tool_data_dir, method, output_file)

Source from the content-addressed store, hash-verified

14
15
16def preprocess_rapidapi(tool_data_dir, method, output_file):
17 def process_assistant_reply(message_dict: dict) -> str:
18 content = message_dict["content"]
19 if "function_call" in message_dict:
20 function_call = message_dict["function_call"]
21 reply = function_call # the whole dict containing action name and action input as target.
22 elif content is not None:
23 reply = content
24 else:
25 print(f"Wrong assistant reply: {message_dict}")
26 return ""
27 return reply
28
29 def append_list(instances_list: list) -> list:
30 return_list = []
31 for instances in instances_list:
32 return_list.extend(instances)
33 return return_list
34
35 print(f"Preprocessing data from {tool_data_dir} into {output_file}")
36 out_list = []
37 for data_file in os.listdir(os.path.join(tool_data_dir)):
38 tmp_instances = []
39 if method not in data_file:
40 continue
41 data_dict = json.load(open(os.path.join(tool_data_dir, data_file), "r"))
42 answer_generation = data_dict["answer_generation"]
43 is_valid = answer_generation["valid_data"]
44 if not is_valid:
45 continue
46 train_messages = answer_generation["train_messages"]
47 query = answer_generation["query"]
48 functions = answer_generation["function"]
49 for train_message in train_messages:
50 conversations = []
51 cur_react = ""
52 for message_id, message_dict in enumerate(train_message):
53 role = message_dict["role"]
54 content = message_dict["content"]
55 if role == "assistant":
56 inputs = process_assistant_reply(message_dict)
57
58 # process the last assistant message as target
59 if message_id + 1 == len(train_message):
60 if "function_call" not in message_dict:
61 cur_react = ""
62 break
63 else:
64 if cur_react == "":
65 cur_react += "\nThought: "
66 action = inputs["name"]
67 action_input = inputs["arguments"]
68 cur_react += f"\nAction: {action}"
69 cur_react += f"\nAction Input: {action_input}"
70 conversations.append({
71 "from": role,
72 "value": cur_react
73 })

Callers 1

Calls 3

process_system_messageFunction · 0.90
process_assistant_replyFunction · 0.85
append_listFunction · 0.85

Tested by

no test coverage detected