ToolBench初探 - 郑瀚Andrew
2023-6-8 16:31:0 Author: www.cnblogs.com(查看原文) 阅读量:16 收藏

人工智能基础模型也类似,如果仅靠训练阶段得到的权重,使用场景就会非常受限。因此工具学习(tool learning)技术被提出,将特定领域的专用工具与大规模基础模型相结合,可以实现更高的效率、性能。

另外一方面,人们逐渐认识到,无论是直接基于Base Model进行zero/few-shot prompt,还是类似langchain风格的多步骤链式推理(Multi Steps CoT Chains),将一个具体的任务分解为一些列子步骤,逐步解决后再汇总,是一种更有效的大模型推理过程。

清华大学自然语言处理实验室等支持的开源社区OpenBMB (Open Lab for Big Model Base)发布了ToolBench项目,可以帮助开发者构建开源、大规模、高质量的指令调优数据,促进构建具有通用工具使用能力的大型语言模型。 

https://mp.weixin.qq.com/s/U0XeFDycMNILqajENvJEGg
https://github.com/OpenBMB/ToolBench/tree/master

ToolBench的数据集是研发人员通过prompt engining,按照工具调用和Cot Chains的格式构造的prompt templates,并通过GPT API接口进行打标,将获取的completions和prompt进行融合整理,得到的一个由dict数组组成的JSON数据集。

如果想要扩展这个数据集,开发者需要从prompt engining开始,将自己的领域任务转换为对应的prompt templates,然后通过GPT API接口进行打标,通过同样的方式融合整理得到新的数据集。

Tool Descrition:
BMTools Tool_name: translation
Tool action: get_translation
action_input: {"text": target texts, "tgt_lang": target language}

Generated Data:
{
    "prompt": "Answer the following questions as best you can. Specifically, you have access to the following APIs:\n\nget_translation: . Your input should be a json (args json schema): {{\"text\" : string, \"tgt_lang\" : string, }} The Action to trigger this API should be get_translation and the input parameters should be a json dict string. Pay attention to the type of parameters.\n\nUse the following format:\n\nQuestion: the input question you must answer\nThought: you should always think about what to do\nAction: the action to take, should be one of [get_translation]\nAction Input: the input to the action\nObservation: the result of the action\n... (this Thought/Action/Action Input/Observation can repeat N times, max 7 times)\nThought: I now know the final answer\nFinal Answer: the final answer to the original input question\n\nBegin! Remember: (1) Follow the format, i.e,\nThought:\nAction:\nAction Input:\nObservation:\nFinal Answer:\n (2) Provide as much as useful information in your Final Answer. (3) Do not make up anything, and if your Observation has no link, DO NOT hallucihate one. (4) If you have enough information and want to stop the process, please use \nThought: I have got enough information\nFinal Answer: **your response. \n The Action: MUST be one of the following:get_translation\nQuestion: {input}\n Agent scratchpad (history actions):\n {agent_scratchpad}",
    "query": "My intention is to convert the data provided in ما هي الأقسام الثلاثة للقوات المسلحة؟ into Arabic(ara).\n",
    "chains": [
        {
            "thought": "I need to use the get_translation API to convert the text into Arabic.",
            "action": "get_translation",
            "action_input": "{\"text\": \"What are the three branches of the military?\", \"tgt_lang\": \"ara\"}",
            "observation": "\"ما هي الفروع الثلاثة للجيش ؟\""
        }
    ],
    "answer": "The translation of \"What are the three branches of the military?\" into Arabic is \"ما هي الفروع الثلاثة للجيش ؟\"."
}
{
        "query": "Can you suggest some travel insurance options for a backpacking trip to Southeast Asia?\n",
        "steps": [
            {
                "assistant_reply": "{\n    \"thoughts\": {\n        \"text\": \"I think I can use the Bing_search command to find travel insurance options for a backpacking trip to Southeast Asia.\",\n        \"reasoning\": \"Bing_search can help me find information on travel insurance options for backpacking trips in Southeast Asia by searching for relevant websites and articles.\"\n    },\n    \"command\": {\n        \"name\": \"Bing_search\",\n        \"args\": {\n            \"goal\": \"Find travel insurance options for a backpacking trip to Southeast Asia.\"\n        }\n    }\n}",
                "chains": [
                    {
                        "thought": "I need to search for travel insurance options for backpacking in Southeast Asia.",
                        "action": "search_top3",
                        "action_input": "{\"key_words\": \"backpacking travel insurance Southeast Asia\"}",
                        "observation": "\"page: 1\\ntitle: Backpacker Travel Insurance - The Essential Guide\\nsummary: Popular Backpacker Travel Insurance Providers: Quick Answers! Do I Need Travel Insurance for Backpacking? Is Backpacker Insurance Different From Travel Insurance? What Travel Insurance SHOULD Cover; What Most Travel Insurers DON\u2019T Cover; TOP 3 Most Popular Backpacking Insurance Companies With Our Readers! 1. World Nomads; 2.\\npage: 2\\ntitle: Asia Travel Insurance | Asia Backpacker Travel Insurance\\nsummary: More than just being a cheap travel destination, South East Asia has a wealth of history, some stunning architecture & extremely friendly locals. Our Asia travel insurance provides you with all the cover you need if you are heading to Asia - whether that be to Vietnam or Thailand, China or even Mongolia!\\npage: 3\\ntitle: Backpacking Southeast Asia Travel Guide (Budget Tips \u2022 2023)\\nsummary: Travel Insurance for Southeast Asia World Nomads \u2019 mission is to support and encourage travellers to explore their boundaries. They offer simple & flexible travel insurance, and safety advice to help you travel confidently.\\n\""
                    },
                    {
                        "thought": "I need more detailed information about the travel insurance providers listed in the search results.",
                        "action": "load_page_index",
                        "action_input": "{\"idx\": 0}",
                        "observation": "\"Toms AsiaToms AsiaAre you looking to hit the road and explore the magnificent sites of Southeast Asia for 1 month? Backpacking is the ideal way to traverse the idyllic forests, beaches and cities that the region has to offer. With a range of activities to try, friendly locals and a wonderful variety of culture, Southeast Asia is the perfect destination for an adventure-packed one month trip. This article will provide valuable tips and advice to ensure you make the most of your one month journey around Southeast Asia. So lets get into backpacking Southeast Asia for 1 Month.BudgetMake sure you have an appropriate budget for your trip that allows for backpacking Southeast Asia for 1 month and all the necessary travel expenses such as accommodation, transport, food and activities.As someone who loves to travel and explore new cultures, I am very excited to embark on my next journey- a backpacking trip through Southeast Asia. However, planning and executing a successful budget-friendly trip is no small feat. To ensure my trip is enjoyable, memorable, and actually cost-effective, I will need to plan meticulously and really research my costs in depth.Before I even consider travelling to Southeast Asia, I must first determine my budget backpacking Southeast Asia for 1 month. This will help me prioritize my spending, as well as narrow down my travel destinations. By having a good understanding of my financial limits, I can ensure that my trip is not only within my budget, but that I a\""
                    },
                    {
                        "thought": "This page is not related to the travel insurance providers, I should try loading the second or third page.",
                        "action": "load_page_index",
                        "action_input": "{\"idx\":1}",
                        "observation": "\"Sure to sweep you off your feet in any season, Napa Valley is the ultimate destination for romanceVisit Napa Valley for the perfect conference, incentive trip, social group event, corporate event or team building meeting. Unique meeting spaces create an inspiring atmosphere.Give Your Clients the Ultimate Napa Valley Experience -\u00a0information for lodging, wineries, restaurants, arts, transportation, special offers, events and moreLearn all about Visit Napa Valley the organization, our mission, our strategy, partners and much more while promoting our area as the premier destination it isSure to sweep you off your feet in any season, Napa Valley is the ultimate destination for romanceVisit Napa Valley for the perfect conference, incentive trip, social group event, corporate event or team building meeting. Unique meeting spaces create an inspiring atmosphere.Give Your Clients the Ultimate Napa Valley Experience -\u00a0information for lodging, wineries, restaurants, arts, transportation, special offers, events and moreLearn all about Visit Napa Valley the organization, our mission, our strategy, partners and much more while promoting our area as the premier destination it isFrom intimate boutique inns to full-service luxury resorts, and everything in between, Napa Valley properties are ready to welcome you with world-class hospitality.Let\u2019s raise a glass, because our legendary Napa Valley wine country has only grown better with time. Offering world-class wine, winemakers, and wineries, \""
                    },
                    {
                        "thought": "The second page is not related to travel insurance either, I should try the third one.",
                        "action": "load_page_index",
                        "action_input": "{\"idx\":2}",
                        "observation": "\"Timeout for loading this page, Please try to load another one or search again.\""
                    },
                    {
                        "thought": "",
                        "action": "",
                        "action_input": "",
                        "observation": "I was not able to load the third page for more information, but World Nomads was listed as the top recommended travel insurance provider for backpacking in Southeast Asia in the search results.\nFinal Answer: World Nomads is the top recommended travel insurance provider for backpacking in Southeast Asia."
                    }
                ],
                "answer": "Tool Bing_search returned: {'input': '{\"goal\": \"Find travel insurance options for a backpacking trip to Southeast Asia.\", \"history context\": \"[]\"}', 'output': 'World Nomads is the top recommended travel insurance provider for backpacking in Southeast Asia.'}"
            },
            {
                "assistant_reply": "{\n    \"thoughts\": {\n        \"text\": \"Based on the previous search results, I think I can use the Wikipedia command to learn more about World Nomads and confirm that it is a reliable travel insurance provider for backpacking in Southeast Asia.\",\n        \"reasoning\": \"Wikipedia can provide reliable information on various topics, including travel insurance providers and their reputations. I can use this command to verify the information found in the previous search.\"\n    },\n    \"command\": {\n        \"name\": \"Wikipedia\",\n        \"args\": {\n            \"goal\": \"Learn more about World Nomads and confirm its reliability as a travel insurance provider for backpacking in Southeast Asia.\"\n        }\n    }\n}",
                "chains": [
                    {
                        "thought": "I need to search for more information on World Nomads",
                        "action": "search",
                        "action_input": "{\"entity\": \"World Nomads\"}",
                        "observation": "\"Could not find World Nomads. Try some similar entities: ['World Nomad Games', 'Nomad', 'Worldnomads.com', 'Digital nomad', 'Eurasian nomads'].\""
                    },
                    {
                        "thought": "I should try searching for a related entity from the list of similar entities provided in the previous search result",
                        "action": "search",
                        "action_input": "{\"entity\": \"World Nomad Games\"}",
                        "observation": "\"World Nomad Games (Kyrgyz: \u0414\u04af\u0439\u043d\u04e9\u043b\u04af\u043a \u043a\u04e9\u0447\u043c\u04e9\u043d\u0434\u04e9\u0440 \u043e\u044e\u043d\u0434\u0430\u0440\u044b) are an international sport competition dedicated to ethnic sports practiced in Central Asia. The first three World Nomad Games were held in Cholpon-Ata, Kyrgyzstan. The fourth games were held in Iznik, Turkey between September 29th and October 2nd 2022.[1] More than 3000 athletes from 102 countries participated in the events.[2]. The Games are surrounded by a cultural and ethnical program. During the first edition of the Games, a yurt village was installed, and cultural events not linked with the Games' disciplines occurred, as well as other entertaining activities..\""
                    },
                    {
                        "thought": "The previous search didn't give me useful information about World Nomads. I should try disambiguating the name to find related entities.",
                        "action": "disambiguation",
                        "action_input": "{\"entity\": \"World Nomads\"}",
                        "observation": "\"Related entities to World Nomads: []\""
                    },
                    {
                        "thought": "Since the disambiguation API did not yield any related entities, I should try refining my search and try again",
                        "action": "search",
                        "action_input": "{\"entity\": \"World Nomads travel insurance\"}",
                        "observation": "\"Could not find World Nomads travel insurance. Try some similar entities: ['Digital nomad', 'Worldnomads.com (category Online insurance companies)', 'Travel visa', 'Nib Health Funds (category Health insurance in Australia)', 'Bleisure travel'].\""
                    },
                    {
                        "thought": "I should try searching for \"World Nomads travel insurance\" as a full entity name to get better results",
                        "action": "search",
                        "action_input": "{\"entity\": \"World Nomads travel insurance\"}",
                        "observation": "\"Could not find World Nomads travel insurance. Try some similar entities: ['Digital nomad', 'Worldnomads.com (category Online insurance companies)', 'Travel visa', 'Nib Health Funds (category Health insurance in Australia)', 'Rock Machine Motorcycle Club (category Articles with WorldCat identifiers)'].\""
                    },
                    {
                        "thought": "I still cannot find any information on World Nomads travel insurance. I should try looking up related keywords to see if I can find any relevant information.",
                        "action": "lookup",
                        "action_input": "{\"keyword\": \"backpacking travel insurance\"}",
                        "observation": "\"No more results.\""
                    },
                    {
                        "thought": "",
                        "action": "",
                        "action_input": "",
                        "observation": "I cannot find any relevant information on World Nomads or backpacking travel insurance. I should try using another tool or source to gather more information about travel insurance providers for backpacking in Southeast Asia.\nFinal Answer: No information found on World Nomads or any other travel insurance providers for backpacking in Southeast Asia through the given API tool. Further research is needed using other sources."
                    }
                ],
                "answer": "Tool Wikipedia returned: {'input': '{\"goal\": \"Learn more about World Nomads and confirm its reliability as a travel insurance provider for backpacking in Southeast Asia.\", \"history context\": \"[\\\\\"Tool Bing_search returned: {\\\\\\\\\\\\\"input\\\\\\\\\\\\\": \\\\\\\\\\\\\"{\"goal\": \"Find travel insurance options for a backpacking trip to Southeast Asia.\", \"history context\": \"[]\"}\\\\\\\\\\\\\", \\\\\\\\\\\\\"output\\\\\\\\\\\\\": \\\\\\\\\\\\\"World Nomads is the top recommended travel insurance provider for backpacking in Southeast Asia.\\\\\\\\\\\\\"}\\\\\"]\"}', 'output': 'No information found on World Nomads or any other travel insurance providers for backpacking in Southeast Asia through the given API tool. Further research is needed using other sources.'}"
            },
            {
                "assistant_reply": "{\n    \"thoughts\": {\n        \"text\": \"Since Wikipedia did not provide any information on World Nomads or other travel insurance providers for backpacking in Southeast Asia, I can use the google_places command to search for recommendations from backpacker communities and blogs.\",\n        \"reasoning\": \"Google Places API allows me to query for addresses and related information based on search queries, including backpacking communities and blogs that may have relevant information on travel insurance providers for Southeast Asia.\"\n    },\n    \"command\": {\n        \"name\": \"google_places\",\n        \"args\": {\n            \"goal\": \"Search for recommendations from backpacking communities and blogs on travel insurance providers for backpacking trips in Southeast Asia.\"\n        }\n    }\n}",
                "chains": [
                    {
                        "thought": "I need to use the search_places API to query the Google Places API with the appropriate input to search for recommendations on travel insurance providers for backpacking trips in Southeast Asia.",
                        "action": "search_places",
                        "action_input": "{\"query\": \"backpacking travel insurance Southeast Asia recommendations\"}",
                        "observation": "\"Google Places did not find any places that match the description\""
                    },
                    {
                        "thought": "",
                        "action": "",
                        "action_input": "",
                        "observation": "The Google Places API did not find any places that match the given query. Further research is needed using other sources.\nFinal Answer: No information found on travel insurance providers for backpacking in Southeast Asia through the given API tool. Further research is needed using other sources."
                    }
                ],
                "answer": "Tool google_places returned: {'input': '{\"goal\": \"Search for recommendations from backpacking communities and blogs on travel insurance providers for backpacking trips in Southeast Asia.\", \"history context\": \"\\\\\\\\\\\\\\\\\"{\"goal\": \"Find travel insurance options for a backpacking trip to Southeast Asia.\", \"history context\": \"[]\"}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\", \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"output\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\": \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"World Nomads is the top recommended travel insurance provider for backpacking in Southeast Asia.\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"}\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"]\"}\\\\\\\\\\\\\", \\\\\\\\\\\\\"output\\\\\\\\\\\\\": \\\\\\\\\\\\\"No information found on World Nomads or any other travel insurance providers for backpacking in Southeast Asia through the given API tool. Further research is needed using other sources.\\\\\\\\\\\\\"}\\\\\"]\"}', 'output': 'No information found on travel insurance providers for backpacking in Southeast Asia through the given API tool. Further research is needed using other sources.'}"
            }
        ]
    },

笔者认为,这有点感觉是面向prompt编程的感觉,把prompt-completions当成一段用自然语言描述的过程调用程序逻辑,构造过程却比程序开发还灵活,语言的语法还可以自己定义

git clone https://github.com/OpenBMB/ToolBench.git
cd ToolBench
pip install -r requirements.txt
python3 data/preprocess.py \
    --tool_mode single
    --tool_data_path data/original/weather_demo.json \
    --output_path data/processed/weather_demo.json
python data/preprocess.py \
    --tool_mode multi
    --tool_data_path data/original/meta_file_demo.json \
    --output_path data/processed/meta_file_demo.json
export PYTHONPATH=./
torchrun --nproc_per_node=8 --master_port=20001 toolbench/train/train_mem.py \
    --model_name_or_path huggyllama/llama-7b  \
    --data_path  data/processed/weather_processed.json \
    --bf16 True \
    --output_dir output \
    --num_train_epochs 3 \
    --per_device_train_batch_size 2 \
    --per_device_eval_batch_size 2 \
    --gradient_accumulation_steps 8 \
    --evaluation_strategy "steps" \
    --eval_steps 1500 \
    --save_strategy "steps" \
    --save_steps 1500 \
    --save_total_limit 8 \
    --learning_rate 5e-5 \
    --weight_decay 0. \
    --warmup_ratio 0.04 \
    --lr_scheduler_type "cosine" \
    --logging_steps 1 \
    --fsdp "full_shard auto_wrap" \
    --fsdp_transformer_layer_cls_to_wrap 'LlamaDecoderLayer' \
    --tf32 True \
    --model_max_length 2048 \
    --gradient_checkpointing True \
    --lazy_preprocess True
source BMTools/secret_keys.sh
export PYTHONPATH=BMTools

下面的命令需要大约 14GB 的 GPU 内存用于 ToolLLaMA-7B。将 /path/to/ToolLLaMA/weights 替换为您转换后的 ToolLLaMA 权重路径。

# or single tool inference:
python toolbench/inference/inference_single_tool.py \
    --tool_name weather \
    --model_path /path/to/ToolLLaMA/weights

# for lora:
python toolbench/inference/inference_single_tool.py \
    --tool_name weather \
    --model_path /path/to/llama/weights \
    --lora_path /path/to/lora/weights

# For multi tools inference:
python toolbench/inference/inference_multi_tools.py \
    --model_path /path/to/ToolLLaMA/weights

https://huggingface.co/datasets/rewoo/planner_instruction_tuning_2k/viewer/rewoo--planner_instruction_tuning_2k/train?row=0

在上述元素中,“prompt-completions orient programer“的变化理论上是无限的,它是一个极度开放的搜索空间。在GPT-3.5/GPT-4开放后,以及“SFT base on task-orient prompt-completions”技术被提出后,如雨后春笋冒出的各种开源大模型项目就可以印证这一点。

最令人兴奋的还不是“prompt-completions orient programer“的无限开放性,而是基于“prompt-completions orient programer“得到的“SFT-LLM on task-orient prompt-completions”本身也具备远超传统机器学习模型或者传统编程软件的泛化性能。


文章来源: https://www.cnblogs.com/LittleHann/p/17461789.html
如有侵权请联系:admin#unsafe.sh