ToolBench Documentation
repository·master·Indexed 26 days ago
https://github.com/openbmb/toolbenchToolBench is an open-source framework and dataset designed to improve the tool-use capabilities of Large Language Models (LLMs). It provides a large-scale instruction tuning dataset featuring over 126,000 instances and 16,464 real-world REST APIs from RapidAPI. The project includes the ToolLLaMA model family, a BERT-based tool retriever, and ToolEval, a machine evaluator for calculating pass rates and win rates of model predictions.
What's inside ToolBench
- ToolBench (ToolLLM) is an open-source project designed to construct large-scale, high-quality instruction tuning SFT data to improve the tool-use capabilities of Large Language Models (LLMs). It provides a dataset of over 126,000 instances involving 16,464 real-world REST APIs from RapidAPI. The project includes training and evaluation scripts, as well as the ToolLLaMA model, which is fine-tuned to master diverse API calls using a depth-first search based decision tree (DFSDT) for reasoning and planning.
Prepare model predictions for ToolEval
masterTo evaluate your own model, you must organize your predictions into a specific directory structure and then preprocess them.
Directory Structure: Create a directory named after your model/method (e.g.,
chatgpt_cot) containing subdirectories for the six test sets (G1_instruction,G1_category,G1_tool,G2_category,G2_instruction,G3_instruction). Each subdirectory should contain the prediction JSON files.Preprocessing: Run the
convert_to_answer_format.pyscript to convert raw predictions into the required internal format. You must set the following environment variables:RAW_ANSWER_PATH: Path to your raw model predictions.CONVERTED_ANSWER_PATH: Path where processed JSON files will be stored.MODEL_NAME: Name of your model.METHOD: The method used (e.g.,CoT).
Example preprocessing loop:
export RAW_ANSWER_PATH=../../data/reproduction_data/model_predictions/ export CONVERTED_ANSWER_PATH=../../data/reproduction_data/model_predictions_converted/ export MODEL_NAME=chatgpt_cot export METHOD=CoT mkdir ${CONVERTED_ANSWER_PATH}/${MODEL_NAME} for test_set in G1_instruction G1_category G1_tool G2_category G2_instruction G3_instruction do answer_dir=${RAW_ANSWER_PATH}/${MODEL_NAME}/${test_set} output_file=${CONVERTED_ANSWER_PATH}/${MODEL_NAME}/${test_set}.json python convert_to_answer_format.py\ --answer_dir ${answer_dir} \ --method ${METHOD} \ --output ${output_file} doneInference with ToolLLaMA
masterTo perform inference with ToolLLaMA, you must first initialize your
TOOLBENCH_KEY(obtained via questionnaire). Usetoolbench/inference/qa_pipeline.pyfor standard inference, LoRA-based inference, or open-domain inference.Standard ToolLLaMA Inference LoRA ToolLLaMA Inference (requires
--loraand--lora_path) Open-Domain LoRA Inference (requires--corpus_tsv_pathand--retrieval_model_path)export TOOLBENCH_KEY="your_toolbench_key" export PYTHONPATH=./ # Standard ToolLLaMA Inference python toolbench/inference/qa_pipeline.py \ --tool_root_dir data/toolenv/tools/ \ --backbone_model toolllama \ --model_path /path/to/your/toolllama \ --max_observation_length 1024 \ --observ_compress_method truncate \ --method DFS_woFilter_w2 \ --input_query_file data/test_instruction/G1_instruction.json \ --output_answer_file toolllama_dfs_inference_result \ --toolbench_key $TOOLBENCH_KEY # LoRA Open-Domain Inference python toolbench/inference/qa_pipeline_open_domain.py \ --tool_root_dir data/toolenv/tools/ \ --corpus_tsv_path data/retrieval/G1/corpus.tsv \ --retrieval_model_path /path/to/your/retrival_model \ --retrieved_api_nums 5 \ --backbone_model toolllama \ --model_path huggyllama/llama-7b \ --lora \ --lora_path /path/to/your/toolllama_lora \ --max_observation_length 1024 \ --observ_compress_method truncate \ --method DFS_woFilter_w2 \ --input_query_file data/test_instruction/G1_instruction.json \ --output_answer_file toolllama_lora_dfs_open_domain_inference_result \ --toolbench_key $TOOLBENCH_KEYUse RapidAPI Backend Service
masterTo avoid using your own RapidAPI key and subscribing to individual APIs, you can request access to the ToolBench RapidAPI backend service. Users must fill out a provided form for review. Once approved, a ToolBench key will be sent to you to start using the service.Set up the ToolBench Web UI
masterThe ToolBench Web UI is based on Chatbot UI and is split into a frontend and a backend server. To run the frontend, clone the
chatbot-ui-toolllamarepository, install dependencies, and start the development server.The application will be available at
http://localhost:3000/.git clone https://github.com/lilbillybiscuit/chatbot-ui-toolllama cd chatbot-ui-toolllama npm install npm run devDownload ToolBench Datasets
masterThe ToolBench dataset is available under the Apache License 2.0. You can download the latest version (containing over 120k reasoning paths and full reasoning thoughts) via Google Drive or Tsinghua Cloud Drive.
Note:
data_0801.ziprefers to the older version of the data.File Structure:
├── /data/ │ ├── /instruction/ │ ├── /answer/ │ ├── /toolenv/ │ ├── /retrieval/ │ ├── /test_instruction/ │ ├── /test_query_ids/ │ ├── /retrieval_test_query_ids/ │ ├── toolllama_G123_dfs_train.json │ └── toolllama_G123_dfs_eval.json ├── /reproduction_data/ │ ├── /chatgpt_cot/ │ ├── /chatgpt_dfs/ │ ├── ... │ └── /toolllama_dfs/Directory Descriptions:
instructionandanswer: Instruction data and solution path annotations.G1,G2, andG3refer to single-tool, intra-category multi-tool, and inter-category multi-tool data respectively.toolenv: Tool environment data, including API JSON, API code, and API example returns.retrieval: Data for tool retrieval.test_instructionandtest_query_ids: 200 instances sampled from each test set (queries and query IDs).retrieval_test_query_ids: Query IDs for retriever test instances.toolllama_G123_dfs_train.jsonandtoolllama_G123_dfs_eval.json: Preprocessed data for direct ToolLLaMA training and result reproduction.
Inference with OpenAI Models
masterYou can use ChatGPT or Text-Davinci-003 for inference by setting
TOOLBENCH_KEYandOPENAI_KEYenvironment variables. Use thebackbone_modelflag to specify the model type (chatgpt_functionordavinci).export TOOLBENCH_KEY="your_key" export OPENAI_KEY="your_openai_key" export PYTHONPATH=./ # ChatGPT Function Inference python toolbench/inference/qa_pipeline.py \ --tool_root_dir data/toolenv/tools/ \ --backbone_model chatgpt_function \ --openai_key $OPENAI_KEY \ --max_observation_length 1024 \ --method DFS_woFilter_w2 \ --input_query_file data/test_instruction/G1_instruction.json \ --output_answer_file chatgpt_dfs_inference_result \ --toolbench_key $TOOLBENCH_KEYCustomize APIs for inference
masterYou can add custom APIs for close-domain inference by following these steps:
- Create API Documentation: Create a JSON file (e.g.,
hello_world.json) following the required schema (includingtool_description,tool_name,api_list, etc.). - Place Documentation: Put the JSON file in a category folder under
data/toolenv/tools/(e.g.,data/toolenv/tools/Customized/hello_world.json). - Implement API Code: Create a folder named after the tool inside the category (e.g.,
data/toolenv/tools/Customized/hello_world/) and write the implementation inapi.py. - Update Query File: Modify your input query JSON to include the
category_name,tool_name, andapi_namefor the custom tool. - Run Inference: Use the
--api_customizationflag.
Note: Currently, customized API usage is only supported in close-domain settings.
# Example API implementation in api.py def get_hello_world(): """ To get hello world """ observation = "hello world" return observation # Running inference with customized API export PYTHONPATH=./ python toolbench/inference/qa_pipeline.py \ --tool_root_dir data/toolenv/tools/ \ --backbone_model toolllama \ --model_path ToolBench/ToolLLaMA-7b \ --max_observation_length 1024 \ --observ_compress_method truncate \ --method DFS_woFilter_w2 \ --input_query_file /path/to/your/query/file \ --output_answer_file /path/to/your/output/file \ --api_customization- Create API Documentation: Create a JSON file (e.g.,
Create a new Automatic Evaluator
masterTo implement a custom automatic evaluator:
- Create a config folder: Under
toolbench/tooleval/evaluators, create a folder named after your evaluator. Include aconfig.yaml(required) and an optionaltemplate.txt. - Implement the Evaluator Class: Create a class inheriting from
BaseEvaluatorand implement thefn_completionsmethod. Use the@register_evaluatordecorator to register it. - Register in Config: Set the
registered_cls_namein yourconfig.yamlto the name of your class. - Test: Run
evaluators_comparison.pyto verify performance.
Implementation Example:
from evaluators import register_evaluator,BaseEvaluator from typing import Dict,List @register_evaluator class MyEvaluator(BaseEvaluator): def __init__(self,config): super().__init__( fn_completions=self.fn_completions, ) # set your configures here def fn_completions(self,query:Dict,answers:List[Dict])->int: # implement your evaluator here # return the index of the preferred answer return 0- Create a config folder: Under
Inference with ToolLLaMA and OpenAI Models
masterYou can perform inference using ToolLLaMA (standard, LoRA, or open-domain) or OpenAI models (ChatGPT or Text-Davinci-003) via the
qa_pipeline.pyscript.For RapidAPI-based inference, you must set the
TOOLBENCH_KEYenvironment variable. If using your own RapidAPI account, provideRAPIDAPI_KEYand use the--use_rapidapi_keyflag.# Setup key export TOOLBENCH_KEY="your_toolbench_key" # ToolLLaMA Inference export PYTHONPATH=./ python toolbench/inference/qa_pipeline.py \ --tool_root_dir data/toolenv/tools/ \ --backbone_model toolllama \ --model_path ToolBench/ToolLLaMA-7b \ --max_observation_length 1024 \ --observ_compress_method truncate \ --method DFS_woFilter_w2 \ --input_query_file data/test_instruction/G1_instruction.json \ --output_answer_file toolllama_dfs_inference_result \ --toolbench_key $TOOLBENCH_KEY # OpenAI ChatGPT Inference export OPENAI_KEY="your_openai_key" export PYTHONPATH=./ python toolbench/inference/qa_pipeline.py \ --tool_root_dir data/toolenv/tools/ \ --backbone_model chatgpt_function \ --openai_key $OPENAI_KEY \ --max_observation_length 1024 \ --method DFS_woFilter_w2 \ --input_query_file data/test_instruction/G1_instruction.json \ --output_answer_file chatgpt_dfs_inference_result \ --toolbench_key $TOOLBENCH_KEY # Custom RapidAPI Account export RAPIDAPI_KEY="your_rapidapi_key" export OPENAI_KEY="your_openai_key" export PYTHONPATH=./ python toolbench/inference/qa_pipeline.py \ --tool_root_dir data/toolenv/tools/ \ --backbone_model chatgpt_function \ --openai_key $OPENAI_KEY \ --max_observation_length 1024 \ --method DFS_woFilter_w2 \ --input_query_file data/test_instruction/G1_instruction.json \ --output_answer_file chatgpt_dfs_inference_result \ --rapidapi_key $RAPIDAPI_KEY \ --use_rapidapi_keyInference with custom RapidAPI account
masterTo use your own RapidAPI account for inference, provide your
RAPIDAPI_KEYand set the--use_rapidapi_keyflag in theqa_pipeline.pyscript.export RAPIDAPI_KEY="your_rapidapi_key" export OPENAI_KEY="your_openai_key" export PYTHONPATH=./ python toolbench/inference/qa_pipeline.py \ --tool_root_dir data/toolenv/tools/ \ --backbone_model chatgpt_function \ --openai_key $OPENAI_KEY \ --max_observation_length 1024 \ --method DFS_woFilter_w2 \ --input_query_file data/test_instruction/G1_instruction.json \ --output_answer_file chatgpt_dfs_inference_result \ --rapidapi_key $RAPIDAPI_KEY \ --use_rapidapi_keyInstall ToolEval
masterTo use the ToolEval machine evaluator, install the required dependencies using pip. This requires Python 3.9 or higher.
pip install -r requirements.txt