A method for optimizing toolsets and calling toolchains for native APIs based on a large language model
By decomposing, deduplicating, and reorganizing the native API, and combining it with a large language model and Monte Carlo tree search algorithm, a more adaptable toolchain was generated. This solved the problems of redundancy and coupling in the interaction between the large language model and the database, and improved the success rate of tasks and development efficiency.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- CHINA TELECOM DIGITAL INTELLIGENCE TECHNOLOGY CO LTD LIAONING BRANCH
- Filing Date
- 2025-12-29
- Publication Date
- 2026-05-26
Smart Images

Figure CN122086497A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of large language model application technology and heuristic optimization algorithms, specifically a method for optimizing toolsets and calling toolchains for native APIs based on large language models. Background Technology
[0002] In terms of interaction between large language models and databases, Text2sql technology is still insufficient for complex business analysis and querying. However, large language models have demonstrated outstanding reasoning, summarizing, inductive, and tool invocation capabilities in recent years. Tools provided by the data platform can be used to organize and categorize data according to business or function, and then the large language model can be used to select and appropriately apply tools to solve user problems.
[0003] However, the data analysis platform's backend contains numerous native APIs, which are redundant and tightly coupled. Interference may occur during large language model inference calls, causing duplicate or erroneous calls and leading to task failure. Prompt word engineers typically spend a significant amount of time and effort adjusting the tools and prompt words. Summary of the Invention
[0004] To address the aforementioned issues and improve development efficiency, this invention proposes a method for optimizing toolsets and toolchain calls for native APIs based on a large language model. This method can automatically orchestrate and organize native APIs to form toolsets. Furthermore, during task execution, the toolset combination can be dynamically adjusted based on the status, thereby enhancing the system's robustness.
[0005] The technical solution adopted by the present invention to achieve the above objectives is as follows: A method for optimizing toolsets and calling toolchains for native APIs based on a large language model includes the following core steps: Step 1: Constructing the sample dataset; Multiple sets of "task-conclusion" pairs formed by human analysis are extracted from historical reports to construct sample dataset D; sample dataset D is used for model performance evaluation and provides a basis for the initialization and optimization of subsequent toolsets; Step 2: Toolset Refinement; The native APIs of the data platform are structured and extracted into an initial toolset with higher information density through decomposition, deduplication, and reorganization. Step 3: Fine-tuning the toolset; The refined toolset is iteratively optimized using a greedy algorithm to obtain a fine-tuned toolset adapted for use with large language models. T opt ; Step 4: Invoke the Monte Carlo tree search toolchain; By using large language models as world models and reasoning agents, and combining them with planning algorithms based on Monte Carlo tree search, we can explore a vast reasoning space and generate a more robust toolchain.
[0006] Furthermore, the structure of the tool in step two includes "tool name, function description, input parameters, id, output result, and set of parent tool ids"; that is, {name, describe, parameter, id, return, from}. Step two, toolset refinement, specifically includes the following steps: S1 tool initialization: Treat each native API as an initial tool, assign a unique identifier id to each tool, and set the from field to {-1}; S2 Tool Decomposition: Utilizing the code generation capabilities of a large language model, a single tool is decomposed into at least two sub-tools. The "from" field of each sub-tool records the ID of the original tool, and the functionality of the sub-tool covers the core business logic of the original tool. S3 tool deduplication: It compares all the decomposed tools pairwise using a large language model, determines tools with the same function and keeps only one of them, and takes the union of the from fields of all duplicate tools for the retained tool.
[0007] Furthermore, the toolset fine-tuning in step two specifically includes the following steps:
[0008] in, This represents the score obtained by the large language model on dataset D using toolset T; The semantic similarity score between the task conclusion and the target conclusion can be obtained by calling the re-ranking model; The number of times the large language model is called is limited. Since tool calls may fail, retries are necessary after each failure. Each retry increases the number of additional calls to the large model. This situation should be punished; There is a limit to the number of times a tool call can be retried. When the limit is reached, the task fails. This can be resolved by directly assigning... A large negative value is used as a penalty for task failure; These are weight parameters; S3 Optimization Toolset; Optimization of the toolset can be abstracted as finding ways to improve the score. Maximum toolset .
[0009] Furthermore, the optimization toolset is solved iteratively in the following manner:
[0010] Furthermore, step four combines a large language model with the Monte Carlo tree search algorithm to plan a robust toolchain, specifically including the following steps: S1 Analysis Goal Definition: Clearly define the data analysis goal to be achieved;
[0011] Furthermore, in step S6, the reward function employs two methods for rewarding; the first is based on the predicted state. and the current state The first approach uses prompts (like "goal") to allow a large language model to determine whether an action contributes to achieving the goal; the second approach relies on the current state. With the goal, prompt words allow the large language model to determine whether the current information meets the conditions for calling the tool.
[0012] Furthermore, the Monte Carlo tree search algorithm iteratively constructs a reasoning tree, where each node represents a state, each edge represents an action, and the transition from the current state to the next state after applying the action; in order to guide the agent to expand and explore the most suitable node in the tree, a state-action value function is maintained for each node, which estimates the expected future reward for taking an action in the state; Step 1, Node Selection: Select nodes to be expanded in the tree based on the upper confidence limit (UCT) strategy, balancing node exploration (nodes with fewer visits) and development (nodes with high value scores). Step 2, Node Expansion; This stage expands the tree by adding new child nodes to the leaf nodes selected above; Given the state of the leaf nodes, through action generation, we use LLM to sample d possible actions, that is, select d possible tools from the toolset and add corresponding child nodes for each action. Step 3, Simulation Strategy; Starting from the leaf nodes, generate the strategy through action functions. Pa Obtain the predicted action, and then use the state prediction function. Ps Obtain the next state, loop until the terminal state is reached, and obtain the expected future reward. Q ; Step 4, Backpropagation: Backpropagate the reward value along the inference path from the root node to the terminal node, and update the state action value function of each node on the path. , Q(s,a) Used for estimating state s China takes action a Expected future rewards; Step 5, Toolchain Generation; After the algorithm reaches the predetermined number of iterations, starting from the root node in the inference tree, continue selecting... Q The maximum value indicates the toolchain's call path.
[0013] The beneficial effects of this invention are: 1. This invention utilizes the code generation capabilities of a large language model to process the native APIs of a data platform. Through decomposition, deduplication, and reorganization, it extracts a toolset with higher information density. It can automatically extract the required toolset from a large amount of redundant platform APIs in historical reports, automatically generating a toolset with higher information density.
[0014] 2. Based on the evaluation of the processing effect on the sample dataset, this invention designs an objective function, and obtains a fine-tuned toolset by optimizing the objective function. This method can form corresponding optimized toolsets for different large language models. Using the Softmax-greedy algorithm to fine-tune the toolset configuration makes the fine-tuned toolset more suitable for the current large language models.
[0015] 3. This invention uses a large language model as both a world model and a reasoning agent, combining it with a Monte Carlo tree search-based planning algorithm to generate a more efficient and robust toolchain. The use of Monte Carlo tree search technology improves the efficiency and stability of toolchain calls, ensuring high information density, personalized optimization, and strong robustness. Attached Figure Description
[0016] Figure 1 The main flowchart of a method for optimizing toolsets and calling toolchains for native APIs based on a large language model. Detailed Implementation
[0017] The following is in conjunction with the instruction manual appendix. Figure 1 The invention is further described in detail below with examples. A method for optimizing toolsets and calling toolchains based on a large language model for native APIs is provided. Tasks and conclusions are extracted from historical reports. These historical reports are generated by humans exploring data from different time ranges using the platform, containing numerous manually analyzed tasks and their corresponding conclusions. This can be further abstracted into multiple pairs of "task-conclusion". The dataset formed by these "task-conclusion" pairs, denoted as D, can be used to evaluate model performance. This sample set provides an important basis for the subsequent initial construction of the toolset.
[0018] The toolset extraction module addresses the issue that numerous APIs are generated during data platform development. Some APIs, due to their complex parameters and simplistic descriptions, require multiple trials and retries to be correctly invoked by the large language model. Furthermore, while some high-level APIs can directly perform advanced tasks, they have prerequisite dependencies. When selecting tools, the large language model may ignore these dependencies and directly choose the desired tool, leading to process crashes. To resolve these issues, the raw APIs need further processing, involving decomposition, deduplication, and reorganization to extract a more information-dense toolset.
[0019] The structure of a tool includes "tool name, function description, input parameters, id, output result, and a set of parent tool ids"; that is, {name, describe, parameter, id, return, from}. `from` is a set whose elements are the parent tool ids, indicating that it is derived from a parent tool. If no parent id exists, -1 is used. Example
[0020] First, initialize the toolset, i.e., the native API, which is a tool, assigning it a unique ID number, with `from` set to `{-1}`. Second, tool decomposition. Utilize the code generation capabilities of the large language model to decompose the tool. Each decomposition should generate at least two sub-tools. For example, "get event data based on time and event name" can be decomposed into "get event data based on time" and "filter event data based on event name".
[0021] Third, tool deduplication. After the second step, several tools with duplicate functions will be generated. To avoid interfering with subsequent calls, a large language model is used to compare them pairwise to determine if they are tools with the same function. If so, one of them is kept, and the from element is the union of the two.
[0022] The Softmax-greedy algorithm fine-tuning toolkit requires further processing to facilitate its use in large language models. Based on the performance evaluation of the sample dataset, an objective function is designed, and the fine-tuned toolkit is obtained by optimizing the objective function.
[0023]
[0024] in, This represents the score obtained by the large language model on dataset D using toolset T; The semantic similarity score between the task conclusion and the target conclusion can be obtained by calling the re-ranking model.
[0025] The number of times the large language model is called is limited. Since tool calls may fail, retries are necessary after each failure. Each retry increases the number of additional calls to the large model. This situation should be punished.
[0026] There is a limit to the number of times a tool call can be retried. When the limit is reached, the task fails. This can be resolved by directly assigning... A large negative value is used as a penalty for task failure.
[0027] These are the weight parameters.
[0028] 3. Optimize the toolset. Optimizing the toolset can be abstracted as finding ways to improve the scoring... Maximum toolset .
[0029] Solve iteratively using the following method:
[0030] Fourth, toolchain calls based on Monte Carlo tree search; By using a large language model as a world model and reasoning agent, and combining a planning algorithm based on Monte Carlo tree search, we can explore a vast reasoning space and plan a more robust toolchain.
[0031] Analysis objective: goal.
[0032] State definition: Where 'data' is a description of the current state, including fields and data types, and 'insight' is the current conclusion.
[0033] Action definition: The tool is invoked, and after the tool is invoked, it will output the corresponding data and description.
[0034] Action generation: Based on the current state and objective, select an action. That is, based on the current {data, insight} and the analysis objective, use a large language model to select the tool to be invoked next.
[0035] State prediction: Based on the current and the action generated in the current step The chosen tool T describes the prediction of the next state using a large language model. .
[0036] Reward function: To evaluate the quality of operation under a given state, this invention employs two methods for rewarding. The first is based on the predicted state. and the current state The first approach, based on the goal, uses cue words to allow a large language model to determine whether the action contributes to achieving the goal. The second approach is based on the current state. With the goal, prompt words allow the large language model to determine whether the current information meets the conditions for calling the tool.
[0037] Based on the above definition, the Monte Carlo tree search algorithm iteratively constructs a reasoning tree, where each node represents a state, each edge represents an action, and the transition from the current state to the next state after applying the action. To guide the agent in expanding and exploring the most suitable nodes in the tree, a state-action-value function is maintained for each node. This function estimates the state. China takes action Expected future rewards.
[0038] 1. Node Selection Strategy. The most promising parts of the existing tree for further expansion in the next stage. To strike a balance between exploration (visiting fewer nodes) and development (high-value nodes), this invention uses a confidence upper limit (UCT) applied to the tree to select each child node.
[0039] 2. Node Expansion. This stage expands the tree by adding new child nodes to the leaf nodes selected above. Given the state of a leaf node, through action generation, we use LLM to sample d possible actions, i.e., select d possible tools from the toolset.
[0040] 3. Simulation strategy. This is used to estimate expected future rewards. The model simulates the future state of the current node. Starting from the current node, at each node, actions are generated. Obtain the predicted action and use the state prediction Obtain the next state. This process continues until the final state is reached.
[0041] 4. Backpropagation. Once the terminal state is reached in the above stages, the inference path from the root node to the terminal node is obtained. The reward is then backpropagated along the inference path to update the Q value of each node.
[0042] 5. Toolchain generation. After the algorithm reaches the predetermined number of iterations, starting from the root node in the inference tree, the algorithm continuously selects the action with the largest Q value, thus obtaining the toolchain's call path.
Claims
1. A method for optimizing toolsets and calling toolchains for native APIs based on a large language model, characterized in that, The core steps include the following: Step 1: Constructing the sample dataset; Multiple sets of "task-conclusion" pairs formed by human analysis are extracted from historical reports to construct a sample dataset D; the sample dataset D is used for model performance evaluation and provides a basis for the initialization and optimization of the subsequent toolset. Step 2: Toolset Refinement; The native APIs of the data platform are structured and extracted into an initial toolset with higher information density through decomposition, deduplication, and reorganization. Step 3: Fine-tuning the toolset; The refined toolset is iteratively optimized using a greedy algorithm to obtain a fine-tuned toolset adapted for use with large language models. T opt ; Step 4: Invoke the Monte Carlo tree search toolchain; By using large language models as world models and reasoning agents, and combining them with planning algorithms based on Monte Carlo tree search, we can explore a vast reasoning space and generate a more robust toolchain.
2. The method for optimizing toolsets and calling toolchains for native APIs based on a large language model according to claim 1, characterized in that, The structure of the tool in step two includes "tool name, function description, input parameters, id, output result, and set of parent tool ids"; that is, {name, describe, parameter, id, return, from}. Step two, toolset refinement, specifically includes the following steps: S1 tool initialization: Treat each native API as an initial tool, assign a unique identifier id to each tool, and set the from field to {-1}; S2 Tool Decomposition: Utilizing the code generation capabilities of a large language model, a single tool is decomposed into at least two sub-tools. The "from" field of each sub-tool records the ID of the original tool, and the functionality of the sub-tool covers the core business logic of the original tool. S3 tool deduplication: It compares all the decomposed tools pairwise using a large language model, determines tools with the same function and keeps only one of them, and takes the union of the from fields of all duplicate tools for the retained tool.
3. The method for optimizing toolsets and calling toolchains for native APIs based on a large language model according to claim 1, characterized in that, Step three, toolset fine-tuning, specifically includes the following steps:
4. The method for optimizing toolsets and calling toolchains for native APIs based on a large language model according to claim 3, characterized in that, The optimization toolset is solved iteratively in the following manner:
5. The method for optimizing toolsets and calling toolchains for native APIs based on a large language model according to claim 1, characterized in that, Step four, which combines a large language model with the Monte Carlo tree search algorithm, plans a robust toolchain, specifically including the following steps:
6. The method for optimizing toolsets and calling toolchains for native APIs based on a large language model according to claim 5, characterized in that, In step S6, the reward function uses two methods to provide rewards; the first method is based on the predicted state. and the current state The first approach uses prompts (like "goal") to allow a large language model to determine whether an action contributes to achieving the goal; the second approach relies on the current state. With the goal, prompt words allow the large language model to determine whether the current information meets the conditions for calling the tool.
7. The method for optimizing toolsets and calling toolchains for native APIs based on a large language model according to claim 5, characterized in that, The Monte Carlo tree search algorithm iteratively constructs a reasoning tree, where each node represents a state, each edge represents an action, and the transition from the current state to the next state after applying the action. To guide the agent in expanding and exploring the most suitable node in the tree, a state-action-value function is maintained for each node, which estimates the expected future reward for taking an action in the state. Step 1, Node Selection: Select nodes to be expanded in the tree based on the upper confidence limit (UCT) strategy, balancing node exploration (nodes with fewer visits) and development (nodes with high value scores). Step 2, Node Expansion; This stage expands the tree by adding new child nodes to the leaf nodes selected above; Given the state of the leaf nodes, through action generation, we use LLM to sample d possible actions, that is, select d possible tools from the toolset and add corresponding child nodes for each action. Step 3, Simulation Strategy; Starting from the leaf nodes, generate the strategy through action functions. Pa Obtain the predicted action, and then use the state prediction function. Ps Obtain the next state, loop until the terminal state is reached, and obtain the expected future reward. Q ; Step 4, Backpropagation: Backpropagate the reward value along the inference path from the root node to the terminal node, and update the state action value function of each node on the path. , Q(s,a) Used for estimating state s China takes action a Expected future rewards; Step 5, Toolchain Generation; After the algorithm reaches the predetermined number of iterations, starting from the root node in the inference tree, continue selecting... Q The maximum value indicates the toolchain's call path.