Language model tool calling method and device, computer equipment and storage medium

By constructing training data containing prompt statements, function libraries, and answer sets, and ignoring the loss values ​​of users and tool roles in fine-tuning training, the problem of poor accuracy and generalization of language model tool call recognition is solved, and higher performance is achieved. Accuracy and adaptability.

CN118690853APending Publication Date: 2024-09-24NINGBO SHENQING INFORMATION TECH CO LTD +1
View PDF 0 Cites 8 Cited by

Patent Information

Application Number
CN202410892110.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-04
Publication Date
2024-09-24

AI Technical Summary

Technical Problem

Existing open source large-scale language models have low recognition accuracy and poor generalization in tool calls. Especially when facing unseen function interfaces or function name changes and parameter modifications, the recognition accuracy is very low and hallucinations are prone to occur.

Method used

The training data fine-tuned by building a language model includes prompt statements, local function libraries, question sets and answer sets. The input of the user role in the training data is the question set, the input of the assistant role is the labeled function name and parameters, and the input of the tool role Returns the annotated function call, and does not calculate the loss value of the user role and tool role during fine-tuning training to reduce the model's memory of existing functions and improve generalization.

Benefits of technology

It improves the recognition accuracy and generalization of language model tool calls. Even if existing functions are modified, the model can still accurately identify function tools, enhances the ability to identify unseen function interfaces, and reduces hallucinations.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118690853A
    Figure CN118690853A
Patent Text Reader

Abstract

The invention relates to a language model tool calling method and device, computer equipment and a storage medium. The method comprises the following steps: constructing training data for fine tuning of a language model; the training data comprises prompt statements, a local function library, a question set and an answer set; the question set serves as input of user roles in the training data, function names and parameters marked in the answer set, prompt statements and subsets of the local function library serve as input of assistant roles in the training data, and functions marked in the answer set are called and returned to serve as input of tool roles in the training data; performing fine tuning training on the language model by using the training data; wherein during fine tuning training, the language model does not calculate loss values of a user role and a tool role in back propagation; and reasoning according to a problem input by a user by adopting the language model subjected to fine tuning training to obtain a target function tool needing to be called. By adopting the method, the recognition accuracy and generalization of large language model tool calling can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a language model tool calling method, device, computer equipment and storage medium. Background Art

[0002] Tool call is a feature that allows large language models to call external functions or services during the text generation process. Large language models can determine when to call which functions based on user input, and can generate request parameters that meet the requirements based on the description of the target function. The model itself does not execute the function, but generates a function name and the parameters required to execute the function.

[0003] The existing open source large models already have certain tool calling capabilities, but their recognition accuracy is low and they are prone to hallucinations, generating non-existent function names or parameters. In addition, the generalization of tool calls is poor. In real business scenarios, there are a large number of function interfaces. The open source model cannot accurately recognize most of the function interfaces that have not been seen (not involved in training). At the same time, when the function name changes or the parameters are added, deleted, or modified, the recognition accuracy is also very low. Summary of the invention

[0004] Based on this, it is necessary to provide a language model tool calling method, apparatus, computer equipment and storage medium to address the above technical problems, so as to improve the recognition accuracy and generalization of language model tool calling.

[0005] A language model tool calling method, the method comprising:

[0006] Construct training data for fine-tuning the language model; the training data includes prompt statements, local function libraries, question sets, and answer sets; the question set is used as the input of the user role in the training data, the function names and parameters annotated in the answer set are used as the input of the assistant role in the training data, and the function call returns annotated in the answer set are used as the input of the tool role in the training data;

[0007] Fine-tune the language model using the training data. During fine-tuning, the language model does not calculate the loss values ​​of the user role and tool role in back propagation.

[0008] The fine-tuned language model is used to perform reasoning based on the questions input by the user to obtain the target function tool that needs to be called.

[0009] A language model tool calling device, the device comprising:

[0010] The training data construction module is used to construct training data for fine-tuning the language model. The training data includes prompt statements, local function libraries, question sets, and answer sets. The question set is used as the input of the user role in the training data, the function names and parameters annotated in the answer set are used as the input of the training data assistant role, and the function call returns annotated in the answer set are used as the input of the training data tool role.

[0011] The model fine-tuning training module is used to fine-tune the language model using the training data. During the fine-tuning training, the language model does not calculate the loss values ​​of the user role and the tool role in the back propagation.

[0012] The function tool calling module is used to use the language model that has completed fine-tuning training to perform reasoning based on the questions input by the user to obtain the target function tool that needs to be called.

[0013] A computer device comprises a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:

[0014] Construct training data for fine-tuning the language model; the training data includes prompt statements, local function libraries, question sets, and answer sets; the question set is used as the input of the user role in the training data, the function names and parameters annotated in the answer set are used as the input of the assistant role in the training data, and the function call returns annotated in the answer set are used as the input of the tool role in the training data;

[0015] Fine-tune the language model using the training data. During fine-tuning, the language model does not calculate the loss values ​​of the user role and tool role in back propagation.

[0016] The fine-tuned language model is used to perform reasoning based on the questions input by the user to obtain the target function tool that needs to be called.

[0017] A computer-readable storage medium stores a computer program, which, when executed by a processor, implements the following steps:

[0018] Construct training data for fine-tuning the language model; the training data includes prompt statements, local function libraries, question sets, and answer sets; the question set is used as the input of the user role in the training data, the function names and parameters annotated in the answer set are used as the input of the assistant role in the training data, and the function call returns annotated in the answer set are used as the input of the tool role in the training data;

[0019] Fine-tune the language model using the training data. During fine-tuning, the language model does not calculate the loss values ​​of the user role and tool role in back propagation.

[0020] The fine-tuned language model is used to perform reasoning based on the questions input by the user to obtain the target function tool that needs to be called.

[0021] The above-mentioned language model tool calling method, apparatus, computer equipment and storage medium first construct training data for fine-tuning the language model; the training data includes prompt statements, a local function library, a question set and an answer set; the question set is used as the input of the user role in the training data, the function name and parameters, prompt statements and a subset of the local function library annotated in the answer set are used as the input of the assistant role in the training data, and the function call return annotated in the answer set is used as the input of the tool role in the training data; then, when fine-tuning the language model, the loss value of the user role and the tool role is not calculated, that is, the loss value of the function information in the subset of the local function library input in the user role is not calculated, thereby reducing the language model's memory of existing functions in the training data, so that even if the existing functions are modified, the language model can still ensure the accuracy of the function tool call, that is, the generalization of the language model tool call is improved. BRIEF DESCRIPTION OF THE DRAWINGS

[0022] Figure 1 A flowchart of a language model tool calling method in one embodiment;

[0023] Figure 2 A schematic diagram of a large language model tool calling the reasoning phase in one embodiment;

[0024] Figure 3 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0025] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.

[0026] In one embodiment, Figure 1 As shown, a language model tool calling method is provided, comprising the following steps:

[0027] Step 102: construct training data for fine-tuning the language model.

[0028] The training data includes prompts, local function libraries, question sets, and answer sets. The local function library refers to a collection of existing functions that will be used in the language model fine-tuning process. The question set is used as the input of the user role in the training data. The annotated function names and parameters, prompts, and a subset of the local function library in the answer set are used as the input of the training data assistant role. The annotated function call returns in the answer set are used as the input of the training data tool role.

[0029] The training data of the above input language model can be constructed using the ChatML dialogue markup language. ChatML is a markup language released by OpenAI for describing dialogue structure. It can help make the dialogue between the language model and the AI ​​assistant more structured and clear. ChatML can describe various elements in the dialogue, such as the beginning and end of the dialogue, the speech of the user and the AI ​​assistant, the questions and answers in the dialogue, etc. Using ChatML can make the processing of dialogue more convenient and efficient, and also improve the readability and maintainability of the dialogue.

[0030] The general format of ChatML dialogue markup language is shown in Table 1.

[0031] Table 1 General format of ChatML dialogue markup language

[0032]

[0033] The data construction phase inserts the question set into the user role. Table 2 shows an input example of a user role constructed using ChatML dialog markup language.

[0034] Table 2 Input examples of user roles constructed using ChatML dialog markup language

[0035]

[0036] Step 104: fine-tune the language model using the training data.

[0037] During fine-tuning training, the language model does not calculate the loss values ​​of the user role and tool role in back propagation. That is, only the loss value of the assistant role is calculated.

[0038] In order to alleviate the illusion of large models during fine-tuning, based on the behavior during inference, the tokens inserted by the inference system do not need to calculate the loss value, but the tokens generated by the language model itself need to calculate the loss value. The token in the large model refers to the smallest unit of input data used by the model. A token can be a word, subword, character, punctuation mark, number, symbol, etc.

[0039] The input of the inference system is the tokens corresponding to the three roles of system, user, and function. The output is the token corresponding to the assistant role, which corresponds to the goal of fine-tuning the language model, so that the fine-tuned language model can output the information of the function tool that needs to be called according to the user's question. In other words, the tokens of the three roles of system, user, and function do not participate in the calculation of the loss value. System is a special role that can be used or ignored during training.

[0040] You can choose to set the label index (label value) of prompt templates, tool set lists, user questions and other information to -100, so that the language model does not calculate the loss values ​​of the system, user and function roles during the back propagation process. The prompt template and tool set list are also inputs of the user role. The tool set list contains information such as tool names and descriptions. It can be understood as a subset of the local function library, which is different from the input of the assistant role - the function names and parameters annotated in the answer set. This solution adjusts the calculation strategy of the loss function during the fine-tuning process so that the language model does not calculate the loss value of the tool set list in the user role during back propagation, thereby reducing the language model's memory of the functions in the training data (including names, descriptions, parameters, etc.). Even after the functions that have participated in the training are modified, the language model can still accurately identify them, thereby improving the generalization of the language model tool calls.

[0041] Step 106, using the language model that has completed fine-tuning training to perform reasoning based on the question input by the user, to obtain the target function tool that needs to be called.

[0042] In the inference stage, Figure 2 As shown, the following steps can be generally included:

[0043] 1) The function registration module registers the function definition list that needs to be used. In the actual reasoning result, the user question and the registered function definition will be combined according to the prompt template. The large language model returns the generated function name and the parameters required to execute the function, that is, the target function tool that needs to be called.

[0044] The registered functions may include local function libraries used in training and unused foreign function libraries.

[0045] 2) The function execution module executes the corresponding function according to the function name and parameter list, and returns the execution result to the large model calling module.

[0046] 3) The large model calling module outputs the answer according to the function return, and the reasoning module returns the output answer to the user as the final answer.

[0047] It should be understood that although Figure 1 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 1 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.

[0048] In one embodiment, the process of constructing the question set and the answer set includes:

[0049] Get user questions and local function libraries. The local function library contains information about functions involved in tool calls, and generally includes content as shown in Table 3. Function examples are shown in Table 4.

[0050] Table 3 Example of function information content

[0051]

[0052] Table 4 Function examples

[0053]

[0054]

[0055] Combined with the local function library, mark the function names and parameters involved in the user's question. Not only the parameter names should be marked, but also the parameter values ​​in the user's question. An example of the marking result is shown in Table 5.

[0056] Table 5 Annotation results

[0057]

[0058] The local function library interface is called according to the annotated function name and parameters to obtain the return value, and the natural language answer to the user's question is annotated according to the return value. The call result example is shown in Table 6.

[0059] Table 6 Call result example

[0060]

[0061]

[0062] The contents of Tables 3, 5, and 6 are combined into complete dialogue data. An example of a single multi-round dialogue data is shown in Table 7.

[0063] Table 7. Single multi-turn dialogue data sample

[0064]

[0065]

[0066]

[0067] Type indicates the data construction template format used. In this example, chatml is used. Functions stores a list of candidate functions, which is a subset of the local function library. Messages stores the content of multiple rounds of conversations, which are divided into three roles: user, assistant, and function. User stores user questions, and assistant stores the results of annotations. If function needs to be called, the input of the calling function will be stored in function_call. Function stores the return value of calling the local function library interface. The assistant role after the function role stores the annotated natural language answers. Source indicates the source of the data.

[0068] Repeat the above steps to obtain a question set according to the user's question, and obtain an answer set according to the annotated function name and parameters corresponding to each user's question, as well as the obtained return value and natural language answer. In one embodiment, the prompt statement includes a prompt template and instance data;

[0069] The prompt template called by the tool has a fixed format and follows the ReAct framework format; the instance data is converted from the local function library. Table 8 is an example of the prompt template format.

[0070] Table 8 Example of the format of the prompt template

[0071]

[0072] Among them, name, description, and parameters are placeholders used to fill External library The function in.

[0073] The function name and parameters annotated in the answer set will be used as the input of the training data assistant role, and the annotated function call return will be used as the input of the training data function role. It also needs to follow the ReAct framework format. The format of the answer set is shown in Table 9.

[0074] Table 9 Example of the format of the answer set

[0075]

[0076]

[0077] An example of a constructed complete large language model input is shown in Table 10.

[0078] Table 10. Complete large language model input example constructed

[0079]

[0080]

[0081] Among them, "You are a tool assistant" is the input of the system role, which can help set the context of the conversation so that AI can better understand its role in the conversation.

[0082] In one embodiment, the training data also includes negative rejection samples, that is, when there is no suitable function in the local function library, but the user's question needs to call a function, the language model is allowed to refuse to answer.

[0083] In order to alleviate the hallucination problem in the process of calling large model tools, we added negative rejection samples. Table 11 shows examples of negative rejection samples.

[0084] Table 11 Examples of rejected negative sample input

[0085]

[0086] In one embodiment, The training data also includes multi-round dialogue data and instruction fine-tuning data .

[0087] In order to alleviate the catastrophic forgetting problem caused by fine-tuning of large models, we mix a certain proportion of multi-round dialogue and instruction fine-tuning data. The format of data construction is the same as the original ChatML. The single multi-round dialogue in the annotation stage adds function sets and dialogue data of newly added function roles. The mixed multi-round dialogue data is additional general dialogue data (in the form of questions and answers between user and assistant). Table 12 is an example of multi-round dialogue data input.

[0088] Table 12 Example of multi-round dialogue data input

[0089]

[0090]

[0091] Table 12 Command fine-tuning data input example

[0092]

[0093] In one embodiment, fine-tuning the language model using the training data includes:

[0094] Full fine-tuning is used as the training fine-tuning strategy. The tiktoken tokenizer is used to tokenize the training data, load the language model, set the training parameters, start distributed fine-tuning training, and save the model weight with the lowest loss value during the training process.

[0095] In one embodiment, a language model tool calling device is provided, comprising: a training data building module, a model fine-tuning training module and a function tool calling module, wherein:

[0096] A training data construction module is used to construct training data for fine-tuning a language model; the training data includes prompt statements, a local function library, a question set, and an answer set; the question set is used as input for the user role in the training data, the function names and parameters annotated in the answer set, the prompt statements, and a subset of the local function library are used as input for the training data assistant role, and the function call returns annotated in the answer set are used as input for the training data tool role;

[0097] The model fine-tuning training module is used to use the training data to fine-tune the language model; wherein, during the fine-tuning training, the language model does not calculate the loss values ​​of the user role and the tool role in the back propagation.

[0098] The function tool calling module is used to use the language model that has completed fine-tuning training to perform reasoning based on the questions input by the user to obtain the target function tool that needs to be called.

[0099] For the specific limitations of the language model tool calling device, please refer to the limitations of the language model tool calling method above, which will not be repeated here. The various modules in the above-mentioned language model tool calling device can be implemented in whole or in part by software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.

[0100] In one embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as follows: Figure 3As shown. The computer device includes a processor, a memory, a network interface and a database connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store language model fine-tuning training data, etc. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a language model tool calling method is implemented.

[0101] Those skilled in the art will understand that Figure 3 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present invention, and does not constitute a limitation on the computer device to which the solution of the present invention is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0102] In one embodiment, a computer device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor implements the steps of the method in the above embodiment when executing the computer program.

[0103] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of the method in the above embodiment are implemented.

[0104] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided by the present invention can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).

[0105] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0106] The above-mentioned embodiments only express several implementation modes of the present invention, and the description thereof is relatively specific and detailed, but it cannot be understood as limiting the scope of the invention. It should be pointed out that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present invention, and these all belong to the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the attached claims.

Claims

1. A language model tool calling method, characterized in that: The method comprises: Constructing training data for fine-tuning a language model; the training data includes prompt statements, a local function library, a question set, and an answer set; the question set is used as input for the user role in the training data, the function names and parameters, prompt statements, and a subset of the local function library annotated in the answer set are used as input for the assistant role in the training data, and the function call returns annotated in the answer set are used as input for the tool role in the training data; Fine-tune the language model using the training data; wherein, during the fine-tune training, the language model does not calculate the loss values ​​of the user role and the tool role in the back propagation; The fine-tuned language model is used to perform reasoning based on the questions input by the user to obtain the target function tool that needs to be called.

2. The method according to claim 1, characterized in that The process of constructing the question set and answer set includes: Obtaining user questions and a local function library; the local function library contains information about functions involved in tool calls; Marking the function names and parameters involved in the user problem in combination with the local function library; Calling the interface of the local function library according to the marked function name and the parameters to obtain a return value, and marking a natural language answer to the user's question according to the return value; Repeat the above steps to obtain a question set according to the user's questions, and obtain an answer set according to the annotated function name and parameters corresponding to each user's question, as well as the obtained return value and natural language answer.

3. The method according to claim 2, characterized in that The local function library includes: the name of the function, a specific description of the function's function, the type definition of the parameter and its description, and whether the parameter is required.

4. The method according to claim 1, characterized in that: The prompt statement includes a prompt template and instance data; The prompt template called by the tool is in a fixed format and follows the ReAct framework format; The instance data is converted from the local function library.

5. The method according to claim 1, characterized in that The training data also includes negative rejection samples, that is, when there is no suitable function in the local function library, but the user's question needs to call a function, the language model is allowed to refuse to answer.

6. The method according to claim 1, characterized in that The training data also includes multi-round dialogue data and instruction fine-tuning data.

7. The method according to claim 1, characterized in that Fine-tune the language model using the training data, including: Full fine-tuning is used as the training fine-tuning strategy. The tiktoken tokenizer is used to tokenize the training data, load the language model, set the training parameters, start distributed fine-tuning training, and save the model weight with the lowest loss value during the training process.

8. A language model tool calling device, characterized in that: The device comprises: A training data construction module is used to construct training data for fine-tuning a language model; the training data includes prompt statements, a local function library, a question set, and an answer set; the question set is used as input for the user role in the training data, the function names and parameters annotated in the answer set, the prompt statements, and a subset of the local function library are used as input for the training data assistant role, and the function call returns annotated in the answer set are used as input for the training data tool role; A model fine-tuning training module, used to perform fine-tuning training on the language model using the training data; wherein, during the fine-tuning training, the language model does not calculate the loss values ​​of the user role and the tool role in the back propagation; The function tool calling module is used to use the language model that has completed fine-tuning training to perform reasoning based on the questions input by the user to obtain the target function tool that needs to be called.

9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.

Citation Information

Cited By

  • Financial question and answer method, system and equipment based on multi-agent interaction and medium

    CN119539095A

  • Financial question-answering method, system, device and medium based on multi-agent interaction

    CN119539095B

  • Data processing method and device, electronic equipment and computer readable storage medium

    CN119577453A

  • Data processing methods, apparatus, electronic devices and computer-readable storage media

    CN119577453B

  • Tool calling method and device based on large language model

    CN120104216A