Agent-FLAN: Designing Data and Methods of Effective Agent Tuning for Large Language Models (2024)

Abstract: Open-sourced Large Language Models (LLMs) have achieved great success in various NLP tasks, however, they are still far inferior to APIbased models when acting as agents. How to integrate agent ability into general LLMs becomes a crucial and urgent problem. This paper first delivers three key observations: (1) the current agent training corpus is entangled with both formats following and agent reasoning, which significantly shifts from the distribution of its pre-training data; (2) LLMs exhibit different learning speeds on the capabilities required by agent tasks; and (3) current approaches have side-effects when improving agent abilities by introducing hallucinations. Based on the above findings, we propose Agent-FLAN to effectively Fine-tune LANguage models for Agents. Through careful decomposition and redesign of the training corpus, Agent-FLAN enables Llama2-7B to outperform prior best works by 3.5% across various agent evaluation datasets. With comprehensively constructed negative samples, Agent-FLAN greatly alleviates the hallucination issues based on our established evaluation benchmark. Besides, it consistently improves the agent capability of LLMs when scaling model sizes while slightly enhancing the general capability of LLMs. The code and model are available at https://github.com/InternLM/Agent-FLAN.

heading: 1 Introduction
text: Language agent (Mialon et al., 2023; Sumers et al., 2023), which leverages the powerful abilities of LLMs to perceive environments, make decisions, and take actions, has emerged as an effective solution to complicated real-world problems (Song et al., 2023; Huang et al., 2023; Chen et al., 2024). Existing studies mainly focus on either prompt engineering (Wei et al., 2022; Yao et al., 2022b; Shinn
∗ Corresponding author
et al., 2023) or framework scheduling of one/multiple close-sourced LLM (GPT-4) (Wu et al., 2023; Talebirad and Nadiri, 2023; Hong et al., 2023) to accomplish agent tasks. Despite the impressive results and flexibility of these works, close-sourced LLMs suffer from steep financial cost and safety issues (Li et al., 2023; Yuan et al., 2023), impeding them from further deployment.
Recently, open-sourced LLMs emerged as effective alternatives and show promising results in various applications (Chiang et al., 2023; Touvron et al., 2023; Jiang et al., 2024). However, a lingering challenge persists: while these LLMs excel in tasks necessitating linguistic finesse, they fall short when deployed as agents, especially when compared to API-based models (Zeng et al., 2023; Liu et al., 2023a). Efforts have been made to finetune LLMs for specific vertical fields (Qin et al., 2023; Gou et al., 2023), but this approach risks compromising their general capabilities. Thus, integrating effective agent abilities into general LLMs becomes a critical and pressing challenge in the field, aiming to bridge the gap between specialized and versatile language models.
In this paper, we embark on a journey to mitigate this disparity between the prowess of open-sourced LLMs and the superior performance exhibited by API-based models in agent-centric tasks. We first unravel three critical observations that serve as the foundation for our exploration: (1) most agent training data is entangled with both format following and general reasoning, deviating from the original pretrain domain of the model, i.e., natural conversation, leading to inadequate agent learning, (2) by explicitly decomposing the training data along the basic capabilities, each loss exhibits different convergence curves, indicating varied learning speeds on the abilities requisite for agent tasks, and (3) the existing approaches that predominantly concentrate on specific agent abilities, overlooking the prevalence and significance of hallucinations in the
9354
model’s output. Building upon these key insights, we present Agent-FLAN, which investigates the designing choice of effective Agent Finetuning for large LANguage models. Specifically, we tease apart format following and common reasoning in the agent training corpus, aligning the fine-tuning process to the pretrained domain of the language model. This elicits the pure agent abilities in LLMs without overfitting to specific format protocols. By further disentangling the agent tasks into distinct facets along the fundamental competencies of LLMs, Agent-FLAN affords training flexibilities depending on the varying learning rates of respective competencies. To comprehensively resolve the hallucination issues in agent tasks, we construct the AgentH benchmark, which assesses the hallucination issues of LLMs from various aspects. Subsequently, we meticulously curate diverse ‘negative’ training samples to mitigate this problem effectively.
We employ Agent-FLAN on the open-sourced Llama2-series, which surpasses prior works by a substantial 3.5% margin across a spectrum of agent evaluation benchmarks, including general agent tasks and tool utilization. Furthermore, we provide a deeper understanding of the dynamics involved in agent tuning: the scaling laws governing data and model dimensions, and the intricate relationships between general and agent-specific tasks. Our major contributions are as follows:
• We identify three critical observations that hinder open-sourced LLMs from achieving competitive performance in the agent domain, offering valuable insights into the complex
landscape of agent tuning.
• Based on above findings, we introduce AgentFLAN, an innovative approach aiming to integrate effective agent abilities into general LLMs: aligning agent tuning to chat format (§4.1), capabilities decomposition and data balancing (§4.2), and negative sample constructions for hallucination elimination (§4.3).
• Agent-FLAN outperforms prior works by a substantial 3.5% margin on Llama2-series across a spectrum of agent evaluation benchmarks. Besides, we further study the dynamics of agent tuning, including scaling laws in terms of data and model scales, and intricate relationships between general and agentspecific tasks.

heading: 2 Related Work
text: LLM as Agent. The emergence of large language models (LLMs) represents a significant stride towards Artificial General Intelligence (AGI). With the advancement of LLMs, language agents built upon LLMs engage with the world to accomplish a diverse array of tasks, having become a focal point of research (Wang et al., 2023a; Xi et al., 2023). LLMs have been utilized in various studies for specific agent tasks, including web browsing(Deng et al., 2023; Zhou et al., 2023), online shopping (Yao et al., 2022a), database operations(Liu et al., 2023a), science experiments (Wang et al., 2022a), observation free reasoning (Xu et al., 2023), Wikipedia Q&A (Yang et al., 2018), daily computer tasks (Kim et al., 2023) and
household exploration (Shridhar et al., 2020). In addition to research focused on specific tasks, there are ongoing studies concerning AI agents based on LLMs. Some studies, such as ReAct(Yao et al., 2022b), emphasize actions during thinking, leading to significant improvements over various methods. While other works focus primarily on human and social property inside agents (Mao et al., 2023; Park et al., 2023; Zhang et al., 2023a), intelligence collaboration within multiple agents (Chen et al., 2023b; Liu et al., 2023b; Liang et al., 2023). Different from above, Agent-FLAN facilitates the integration of effective agent capabilities into general LLMs, enabling models to better understand and tackle complex problems in the real world.
Language Model Fine-Tuning. Language Model Fine-Tuning is also a research hotspot, involving the adjustment of pre-trained models to adapt to specific tasks, aiming to align the output with expectations (Zhang et al., 2023b). Various research studies have been conducted on fine-tuning to optimize the model’s reasoning capabilities (Liu et al., 2021; Fu et al., 2023), proficiency in tools (Patil et al., 2023; Qin et al., 2023; Qiao et al., 2023), planning capability (Chen et al., 2023a), retrieval-augmented (Wang et al., 2023b), etc. Additionally, there are various studies conducted on fine-tuning methods (Hu et al., 2021; Ivison et al., 2022; Dettmers et al., 2023; Lv et al., 2023), data selection principles (Gunasekar et al., 2023) and fine-tuning datasets (Sanh et al., 2021; Wang et al., 2022b; Honovich et al., 2022; Longpre et al., 2023; Luo et al., 2023).

heading: 3 Pilot Observations
text: In this section, we delve into three pivotal observations on agent tuning that serve as the foundation
of our subsequent investigation. Observation 1. Most agent training data is entangled with both format following and general reasoning, causing a significant departure from the model’s original pretraining language domain, namely, natural conversation. Recent agent tuning works (Zeng et al., 2023; Qin et al., 2023) endorse the adoption of specific formats, exemplified by ReAct (Thought-ActionObservation), for fine-tuning the language model. Moreover, it’s noteworthy that action arguments are frequently presented in JSON format. Encoding both format and reasoning knowledge into the training corpus shifts the tuning process from the original chat domain, presenting it as an out-ofdomain task for language models. As shown in Figure 2, we compare the training curve of formatted data and normal data. It can be clearly seen that the loss associated with formatted data descends more rapidly to a low value, while keeping content loss still high (0.54 vs 0.04), indicating that the former leads to an inadequate learning process. This phenomenon can be probably attributed to the existence of a fixed structure (ReAct, JSON), where the model quickly gets overfitted to the format itself. Consequently, it fails to grasp the underlying reasoning abilities embedded within the training data, resulting in unsatisfied performance.
Observation 2. By explicitly decomposing the training data along the basic capability aspects, each loss exhibits different convergence curves, indicating varied learning speeds on the capabilities requisite for agent tasks of LLMs. Inspired by (Chen et al., 2023c), we explicitly disentangle the model’s capabilities into distinct components: instruction following, reasoning, retrieval, and understanding. In this context, instruction fol-
lowing corresponds to format generation, reasoning corresponds to the thought quality at each step, retrieval involves selecting the appropriate function name to execute the task, and the understanding encompasses the parameter inputs for the selected functions. By visualizing the loss based on respective aspects in Figure 3, we discern that LLM tends to exhibit varying learning speeds for the capabilities essential for proficient agents. To elaborate, retrieval and understanding emerge as relatively more manageable tasks compared to reasoning, with instruction following being the simplest in the learning process. This observation serves as a compelling motivation to further disentangle the training data along these model capabilities and subsequently balance these data based on the model’s varying learning rates.
Observation 3. Existing approaches predominantly concentrate on specialized agent abilities, overlooking the prevalence and significance of hallucinations in the model’s output. AgentTuning (Zeng et al., 2023) introduces mixture training, where both a general dataset and an agent dataset are simultaneously provided to the model during the tuning process. While this strategy indeed leads to steady performance improvements, we observe that it has limited impact on addressing hallucination issues, i.e., a crucial concern often overlooked in recent agent studies. This concern
becomes particularly significant when deploying language models in real-world applications, as illustrated in Figure 4. Hallucination manifests in two primary aspects: (1) When the model is required to invoke a response, it strictly adheres to the training format, disregarding the user-generated query, and (2) The model is susceptible to being triggered with non-existent functions when presented with induced questions. This underscores the necessity of directing more attention toward refining agent tuning mechanisms and the establishment of appropriate benchmarks to assess and mitigate agent hallucination effectively.

heading: 4 Agent-FLAN
text: Recent works start to explore the effectiveness of finetuning language models on various agent tasks, with varied data quality, model sizes, and tuning approaches. Building on the pilot observations, we dive deeper into the data and method designing choice of effective agent tuning and discuss three key improvements to the language agent models. Experimental Setup We finetune the language model Llama2-series (Touvron et al., 2023), and use 7B size for ablations for efficiency unless otherwise stated. We construct the dataset in accordance with the data and settings established by AgentTuning (Zeng et al., 2023). Specifically, we pick a suite of training data sources as held-in tasks: ALF-
World (Shridhar et al., 2020), WebShop (Yao et al., 2022a), Mind2Web (Deng et al., 2023), Knowledge Graph (Liu et al., 2023a), Operating System (Liu et al., 2023a), Database (Liu et al., 2023a), and ToolBench (Qin et al., 2023), covering both general agents and tool utilization domains. Our heldout evaluation encompasses a list of complex interactive tasks, including complex QA (HotpotQA (Yang et al., 2018)), web browsing (WebArena (Zhou et al., 2023)), science experiments (SciWorld (Wang et al., 2022a)), and tool utilization (T-Eval (Chen et al., 2023c)). Details adopted in AgentFLAN and hyper-parameters during training can be found in Appendix A.

heading: 4.1 Aligning Agent Tuning to Chat Domain
text: LLMs are first pretrained on natural language corpus, which comprises a massive number of human conversations. However, agent data are often presented in specific formats (ReAct, JSON), leading to out-of-distribution learning during the tuning process. Such a misalignment further results in an inadequate learning process. Besides, LLMs are more likely to get overfitted to these specific formats after fine-tuning, deteriorating their instruction-following abilities. To mitigate
this problem, we propose to transform the formatted data into natural conversations. Concretely, we first replace the classical ‘THOUGHT-ACTIONACTIONINPUT’ templates with multi-turn dialogues. After that, we further decompose JSON arguments by inserting several elicit statements. An example has been shown in Figure 5. Since loss will be only applied to the ‘assistant’ clause, the introduction of formatted elicit statements has little overfitting issue on models. By explicitly aligning the agent corpus into chat domain, we are able to fully boost the learning on pure agent ability, without focusing on strict format protocols. In order to keep the ability to output various requested formats, we additionally construct instruction following pairs which request the model to respond with ReAct and JSON formats. Our latter experiments also show that only a small portion of instruct following data is enough to achieve satisfying results. From Table 2, we can observe steady improvements by aligning training corpus into chat domain, i.e., 3.1% improvements on T-Eval, and 2.5% on HotpotQA. This further validates the correctness and effectiveness of the alignment of training corpus to the chat format.

heading: 4.2 Capabilities Decomposition and Data Balancing
text: Prior works have shown that a proper mixture of training data sources leads to better performance (Longpre et al., 2023). In this work, instead of simply exploring the balanced composition of each dataset, we investigate the mixture of training corpus from the perspective of capabilities. Inspired by (Chen et al., 2023c), we explicitly decompose the agent data along the capabilities required by each task, including reasoning, retrieval, understanding, and instruction following. As demonstrated in Section 3, LLM exhibits varied learning speeds on each capability, indicating that properly composing these data sources also matters in optimizing final results. To validate this assumption, we conduct experiments with vanilla decomposed aligned agent corpus, where each ability data is identical to the original data, and then ablates each sub-mixture with half of the data on T-Eval and HotpotQA.
As shown in Table 2, reasoning and understanding are among the most beneficial mixtures, then comes retrieval and instruction following: when reducing the portion of reasoning and understanding data to 50%, the final performance declines 1.1/0.3
Table 1: Main results of Agent-FLAN. Agent-FLAN significantly outperforms previous agent-tuning approaches by a large margin on both held-in and held-out tasks. * denotes our re-implementation with the same amount of training data for a fair comparison. Since FireAct does not train on AgentInstruct dataset, we omit its performance on the HELD-IN set. Bold: the best in API-based and open-sourced models.
MODEL HELD-IN HELD-OUTHotpotQA SciWorld WebArena T-Eval Agent-H Overall
GPT-3.5 (OpenAI, 2022) 1.59 37.4 21.2 4.56 84.0 92.1 47.8 GPT-4 (OpenAI, 2023) 2.75 52.1 36.4 6.28 86.4 94.2 55.1
Llama2-7B (Touvron et al., 2023) 0.19 22.6 5.9 1.2 27.4 78.7 27.2 FireAct-7B (Chen et al., 2023a) - 26.2 6.8 0.25 9.3 40.4 16.6 AgentLM-7B (Zeng et al., 2023) 1.96 22.3 13.7 0.74 41.4 80.6 31.7
AgentTuning* (Zeng et al., 2023) 1.89 25.4 16.8 2.71 61.8 84.5 38.2 Agent-FLAN (Ours) 2.01 28.5 20.0 4.68 66.0 89.1 41.7
points, respectively. By reducing the amount of retrieval and instruction following, the performance incurs little influence and even improves. These findings are also consistent with the observations in Figure 3, where the loss of retrieval and instruction following drops much quicker than reasoning and understanding. Such a phenomenon also inspires us to narrow the mixture weights search space and greatly reduce the training tokens based on the loss curves on each capability.

heading: 4.3 Negative Sample Learning for Hallucination Elimination
text: Hallucination is a crucial issue of current LLMs, which refers to “generating unfaithful or nonsensical text” (Ji et al., 2023, 2024). In agent tasks, we also observe such phenomenons as illustrated in Figure 4. We summarize agent hallucination into two main categories: format hallucination and action hallucination. The former plays an important role when deploying LLMs into specific agent systems, and the latter matters when acting as a general chat assistant. Therefore, how to effectively eliminate the agent hallucination issues is an essential path in developing agent LLMs.
Figure 6: Demonstration of 4 different circumstances commonly existed in agent tasks, by splitting them through user query and system prompt.
However, most prior work mainly focuses on the general agent’s abilities, while omitting the hallucination issues. To comprehensively gauge the LLMs on agent hallucination, we first establish Agent-H benchmark, which accesses such issues from two aspects: (1) format-level: requests with various response formats, and check if the model follows the instructions, and (2) action-level: we curate the questions from 4 different perspectives as illustrated in Figure 6, covering most agent circumstances for LLMs. Detailed evaluation protocols for Agent-H can refer to Appendix C.
Table 3 reports the experimental results on AgentH, as well as the scores on T-Eval, which provides a comprehensive demonstration of both agent abilities and hallucination issues. Ideally, a general language model should obtain high scores on both benchmarks. From the table, we can see that Llama2-7B obtains low scores on both Agent-H and T-Eval. This is possibly due to the lack of agent data in its pretraining corpus, which further proves the necessity of agent tuning. We also follow the implementation of AgentTuning (Zeng et al., 2023) to finetune the model from Llama2-7B. Despite the huge improvements in T-Eval score, the hallucination issue is quite severe gauged by Agent-H,
pinpointing the inner defects of current agent tuning approaches.
To address this problem, we first examine the current agent corpus according to Figure 6. It is easy to find that in most cases, the training data only covers normal conversation without tools (a) and agent tasks with provided tools (d) while omitting the rest (b,c). Since the model has never seen these negative samples during training, it can hardly generalize to these requests, leading to unexpected responses. To this end, we introduce negative sample learning, by meticulously curating diverse negative training samples covering various conditions mentioned above. Specifically, we insert two different types of negative samples: (1) no tools provided, user query requests for tools (2) tool provided, user query requests for normal conversations. Through explicit supervision, we teach the model not only how but when to act as an agent. In Table 3, the negative sampling learning strategy significantly mitigates the hallucination issues, meanwhile maintaining the high performance on T-Eval.

heading: 5 Analysis

heading: 5.1 Scaling Law for Agent Tuning
text: Scaling law for language models is quite important and provides valuable insights in further development. In this section, we explore such phenomenons on agent tuning from both the data and model scales on the general HotpotQA task. Data Scaling Law for Agent Tuning. Recent work (Chung et al., 2022) has demonstrated language models can benefit from vast and diverse training corpus in general abilities. We investigate how the amount of training data influences the agent’s abilities. By evenly dividing Agent-FLAN data into 25%, 50%, 75%, and 100%, we report results in Figure 7. It can be seen that with only 25% training samples, the agent ability gains the most. This further verifies that the vanilla Llama-2 model exhibits
weak agent abilities and requires specific training, and only a small portion of agent corpus can elicit most abilities required by agent tasks. When further increasing the data amount (50%, 75%), the improvements persist, however with slower speeds, which indicates that simply enlarging the scale of the training corpus of agents does not contribute much to the model capabilities. Therefore, enriching the diversity or improving the quality of the training corpus may be the necessary path to better language agents. Model Scaling Law for Agent Tuning. As previous work (Longpre et al., 2023) proves that zero/few-shot ability of language models is substantially improved with a larger model scale, we next explore if this rule is also applied in the agent domain. By evaluating our approach on models of size 7B, 13B, and 70B on Llama2, the final results are shown in Figure 9. We can observe that as the model scale increases, the performance continuously improves without any saturation, proving that larger parameters do guarantee better performances. When taking a close look at enhancement across different model scales, it can be found that specific agent tuning brings steady improvements compared to vanilla ReAct tuning as the model scales up. We infer the reason that larger models have already possessed basic abilities required by agent tasks, such as reasoning and retrieval. Therefore, it is more important to elicit agent capability with a certain amount of agent tuning corpus in an appropriate way.

heading: 5.2 General Ability v.s Agent Ability
text: As we have seen that specific tuning improves the ability of a model to act as agent, it is still unclear what is the relationship between general ability
and agent ability. AgentTuning has verified the necessity of general data on agent training, but does agent tuning further promote the general capability of the model? We evaluate our model on three general capabilities that are widely adopted in the field: MMLU (linguistic knowledge), GSM8K (mathematical ability), and HumanEval (code capability). The results are shown in Table 4. From the table, we can see that introducing agent training corpus not only enhances the ability on agent tasks but also brings extra benefits to general capabilities. We speculate the reason that agent corpus contains basic capabilities such as reasoning, and instruction following, which are also amenable to other domains. This further indicates that properly inte-
grating agent data into the current LLM training corpus can lead to better performance.

heading: 6 Conclusion
text: In this work, we investigate the design choice of data and methods in agent tuning for LLMs. By first carrying out three critical observations, we pinpoint the current bottleneck in bridging the gap between the open-sourced LLMs and API-based models. Based on this, we present Agent-FLAN to effectively fine-tune the language models for agent tasks. Through careful decomposition and redesign of the existing training corpus, Agent-FLAN em-
powers Llama2-7B to outperform previous works significantly on a wide spectrum of agent tasks.

heading: 7 Limitations
text: In this paper, we focus on constructing the agent training corpus. Despite our best efforts, this paper may still have some remaining limitations. 1) The training and validation dataset only encompass part of agent tasks. There are many other interactive scenarios beyond this. We will conduct further research on applying Agent-FLAN to a wider range of benchmarks in the future. 2) In order to keep the quality of training data, we only choose roughly 20,000 valid samples from ToolBench, which is 10% of the whole dataset. It would be better to fully utilize them to further enhance the performance of the model. We leave it for future work.

heading: 8 Ethical Considerations
text: We used publicly available reference documents/APIs for our experiments, effectively circumventing any possible harm toward individuals or groups. The generated data by LLMs were carefully selected and processed by humans to secure privacy and confidentiality. No personal identification information was involved, and all data were made anonymous before any analysis was conducted. Besides, We use ChatGPT and Grammarly to polish the writing.

heading: 9 Acknowledgments
text: This work was supported by the JKW Research Funds under Grant 20-163-14-LZ-001-004-01, and the Anhui Provincial Natural Science Foundation under Grant 2108085UD12. We acknowledge the support of GPU cluster built by MCC Lab of Information Science and Technology Institution, USTC.

heading: A Experimental Details
text: For Agent-FLAN experiments, we follow the practice in AgentTuning, where ShareGPT and agent corpus are mixed for training with a mix ratio of 1:1. Besides, we empirically find that a small amount of ReAct format data improves the performance. Therefore, we incorporate 10% data with ReAct format and the rest follows the format in AgentFLAN. The models are fine-tuned on the training corpus with 1 epoch. We list the hyper-parameters of models with different scales in Table 5. The balancing weight for reasoning, retrieval, and understanding is set to 1:0.25:0.75. We additionally curate 2000 samples of instruction following data for training.

heading: B Dataset Details
text: In this section, we provide details on the detailed construction of our constructed Agent-FLAN dataset. In order to keep the generality of our approach, we adopt open-sourced datasets AgentInstruct (Zeng et al., 2023) (which comprises 6 different agent tasks), ToolBench (Qin et al., 2023) as our basic datasets. We filter the data that does not follow the format of ‘Thought-Action-ActionInput’, and remove the few-shot examples in the AgentInstruct. As for ToolBench dataset, we only keep samples that have FINALANSWER and remove those that directly begin with ‘This is not the first time you try this task’ (due to the DFSDT introduced in (Qin et al., 2023)). By default, Agent-FLAN remixes 10% ReAct format and 90% conversation format data for better performance. The overview of our dataset is shown in Table 6.

heading: C Agent-H Benchmark
text: In this section, we introduce the detailed implementation of Agent-H. We select glaive-functioncalling-v2 (GlaiveAI, 2023) as our base dataset. By explicitly checking if the response contains

heading: D Negative Samples Construction Details
text: In order to construct negative samples for hallucination elimination, we first extract a small portion (761 samples) of user queries from ToolBench dataset. Then we prompt the gpt-3.5-turbo to directly reply to these queries without providing any tool information. To construct normal conversation with the tool information provided, we randomly append half of the queries with irrelevant tools, which generate the case (b) in Figure 6. Illustrations of our constructed negative samples are shown in Figure 10, 11.

heading: Future Work
text: We will conduct further research on applying Agent-FLAN to a wider range of benchmarks in the future. 2) In order to keep the quality of training data, we only choose roughly 20,000 valid samples from ToolBench, which is 10% of the whole dataset. It would be better to fully utilize them to further enhance the performance of the model. We leave it for future work