(input_dict: dict, api_customization: bool=False, tools_root: str="data.toolenv.tools", schema_root: str="data/toolenv/response_examples")
| 127 | |
| 128 | |
| 129 | def get_rapidapi_response(input_dict: dict, api_customization: bool=False, tools_root: str="data.toolenv.tools", schema_root: str="data/toolenv/response_examples"): |
| 130 | info = Info |
| 131 | info.category = input_dict['category'] |
| 132 | info.tool_name = input_dict['tool_name'] |
| 133 | info.api_name = input_dict['api_name'] |
| 134 | info.tool_input = input_dict['tool_input'] |
| 135 | info.strip = input_dict['strip'] |
| 136 | rapidapi_key = input_dict['rapidapi_key'] |
| 137 | |
| 138 | tool_name, standard_category, api_name, code_string = prepare_tool_name_and_url(tools_root, info) |
| 139 | tool_input = info.tool_input |
| 140 | |
| 141 | strip_method = info.strip |
| 142 | |
| 143 | try: |
| 144 | tool_input = json.loads(tool_input) |
| 145 | except Exception as e: |
| 146 | if tool_input == "": |
| 147 | tool_input = {} |
| 148 | else: |
| 149 | print(f"Can not parse tool input into json: {tool_input}") |
| 150 | response_dict = {"error": f"Tool input parse error...\n", "response": ""} |
| 151 | return response_dict |
| 152 | |
| 153 | input_params_str = "" |
| 154 | if len(tool_input) > 0: |
| 155 | for key, value in tool_input.items(): |
| 156 | if isinstance(value, str): |
| 157 | input_params_str += f'{key}="{value}", ' |
| 158 | else: |
| 159 | input_params_str += f'{key}={value}, ' |
| 160 | if not api_customization: |
| 161 | input_params_str += f"toolbench_rapidapi_key='{rapidapi_key}'" |
| 162 | success_flag, switch_flag, response_dict, save_cache = run(code_string, api_name, input_params_str) |
| 163 | observation = observation_shorten(schema_root, response_dict, standard_category, tool_name.replace(f"_for_{standard_category}", ""), api_name, strip_method) |
| 164 | result = str(observation)[:2048] |
| 165 | return {"error": response_dict['error'], "response": result} |
| 166 | |
| 167 | |
| 168 | if __name__ == "__main__": |
no test coverage detected