A method and device for generating large model API sequence based on retrieval enhancement

By constructing the mapping relationship between the problem-code pair set and the API sequence set, the twin-tower model is trained to solve the incompleteness and hallucination problems of large language models when generating structured API sequences, and the large language model is combined to generate API sequences, which improves the generation results. The accuracy and reliability of the method solve the problem of insufficient accuracy in the existing technology.

CN118536607BActive Publication Date: 2024-10-01安徽思高智能科技有限公司
View PDF 8 Cites 0 Cited by

Patent Information

Application Number
CN202411006149.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-25
Publication Date
2024-10-01
Estimated Expiration
2044-07-25

AI Technical Summary

Technical Problem

Large language models have problems such as incomplete generation, hallucination problems and outdated knowledge when generating structured API sequences. The accuracy and reliability of existing retrieval enhancement generation technology are insufficient.

Method used

By constructing a mapping relationship between the problem-code pair set and the API sequence set, positive and negative sample sets are obtained, the twin-tower model is trained to enhance the accuracy of the generated results, and the API sequence is generated by combining a large language model.

Benefits of technology

Improves the accuracy and reliability of API sequence generation, reduces noise, and ensures high-precision API sequence output.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118536607B_ABST
    Figure CN118536607B_ABST
Patent Text Reader

Abstract

The present invention provides a method and device for generating a large model API sequence based on retrieval enhancement, which relates to the field of retrieval recommendation technology, including: constructing a mapping relationship set through the mapping relationship between questions and API sequences; obtaining a positive sample set and a negative sample set through the mapping relationship set, and training a dual-tower model through a "question-code" pair set, a positive sample set and a negative sample set; inputting a sentence to be queried into the trained dual-tower model to obtain a currently relevant API sequence set; constructing a prompt through the sentence to be queried and the currently relevant API sequence set, and inputting the prompt into a large language model for generation to obtain an API sequence that solves the query. The present invention constructs a positive sample set and a negative sample set through a mapping relationship set between questions and API sequences, trains the dual-tower model through the positive sample set and the negative sample set, and enhances the accuracy of the dual-tower model in calculating similarity, so as to obtain a high-precision currently relevant API sequence set.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of retrieval recommendation technology, and in particular to a method and device for generating a large model API sequence based on retrieval enhancement. Background Art

[0002] Large language models perform well in natural language processing tasks, but there are still challenges in generating structured API sequences. First, it is difficult for large language models to directly output fully structured API sequences, and the generated content usually needs to be post-processed to meet the task requirements. In addition, large language models also have the so-called "hallucination" problem, that is, they may generate APIs that do not exist, or even incorrectly piece together keywords in developer requirements into APIs. Due to the limited training data of large language models, when the query content exceeds the scope of its training data or involves the latest knowledge, the model may generate incorrect results. In order to solve the above problems, retrieval-enhanced generation technology is called an effective path. Retrieval-enhanced generation technology combines large language models with external retrieval systems, and enhances the generated results by retrieving relevant information from historical libraries, thereby alleviating the shortcomings of large language models in terms of insufficient structured generation capabilities, outdated knowledge, and hallucination problems. However, retrieval-enhanced generation technology also has its limitations. If the retrieved results are inaccurate, it may lead the large language model to generate incorrect results and increase noise. The quality and relevance of the retrieval system directly affect the accuracy and reliability of the final generated content. Summary of the invention

[0003] In view of this, the purpose of the present invention is to provide a large model API sequence generation method and device based on retrieval enhancement, which is used to solve the problem of low accuracy of existing retrieval recommendation methods.

[0004] The present invention provides a large model API sequence generation method based on retrieval enhancement, comprising:

[0005] S1: Obtain a set of "question-code" pairs and a set of API sequences through the history library, and construct a mapping relationship set based on the mapping relationship between the question and the API sequence;

[0006] S2: Obtain the positive sample set and the negative sample set through the mapping relationship set, build a dual-tower model, train the dual-tower model through the "question-code" pair set, the positive sample set and the negative sample set, and obtain the trained dual-tower model;

[0007] S3: Get the query statement, input the query statement into the trained dual-tower model, and obtain the current related API sequence set;

[0008] S4: Construct prompts through the query statement and the current related API sequence set, input the prompts into the large language model for generation, and obtain the API sequence that solves the query.

[0009] Preferably, step S1 specifically comprises:

[0010] S11: Obtain a history database, pre-process each question in the history database, and obtain a set of "question-code" pairs;

[0011] S12: Get the AST tree through the code in the history library, traverse the AST tree to get the API sequence set, API sequence set The expression is:

[0012] ,

[0013] Where n represents the total number of API sequences, Indicates the nth API sequence;

[0014] S13: Obtain the mapping relationship between the problem and the API sequence by using a key-value pair method, and construct a mapping relationship set.

[0015] Preferably, step S2 specifically comprises:

[0016] S21: Obtain the five least historically relevant API sequences and one most historically relevant API sequence for each problem through the mapping relationship set, use the least historically relevant API sequences as a negative sample set, and use the most historically relevant API sequences as a positive sample set;

[0017] S22: Build a dual-tower model, which includes a question encoder and an API sequence encoder;

[0018] S23: Construct a training vector set through the "question-code" pair set, the positive sample set and the negative sample set;

[0019] S24: Train the twin-tower model through the training vector set, and calculate the negative log-likelihood loss function of each problem;

[0020] S25: Repeat step S24 until the negative log-likelihood loss function of each problem is less than a preset value, and obtain a trained dual-tower model.

[0021] Preferred:

[0022] Question Encoder The expression is:

[0023] ,

[0024] API Sequence Encoder The expression is:

[0025] ,

[0026] in, represents a set of "question-code" pairs, Represents a collection of API sequences.

[0027] Preferred:

[0028] The expression of the training vector set D is:

[0029] ,

[0030] Where i is the number of the question, m is the number of questions in the history library, It is The vector representation of the problem is It is The vector representation of the positive sample corresponding to the question, It is The corresponding question The vector representation of negative samples is: .

[0031] Preferred:

[0032] The expression of the negative log-likelihood loss function is:

[0033] ,

[0034] Where i is the number of the question, It is The vector representation of the problem is It is The vector representation of the positive sample corresponding to the question, It is The corresponding question The vector representation of negative samples is: It is The negative log-likelihood loss function for this problem, sim() is the dot product similarity calculation function.

[0035] Preferably, the expression of the prompt in step S4 is:

[0036] {“Query”: [statement to be queried], “Currently related API sequence set”: [API1, API2,..., API5]}.

[0037] A storage medium stores instructions and data for implementing a large model API sequence generation method based on retrieval enhancement.

[0038] A device for generating a large model API sequence based on retrieval enhancement includes: a processor and a storage medium; the processor loads and executes instructions and data in the storage medium to implement the method for generating a large model API sequence based on retrieval enhancement.

[0039] The present invention has the following beneficial effects:

[0040] Positive sample sets and negative sample sets are constructed through a set of mapping relationships between questions and API sequences. The dual-tower model is trained through the positive sample sets and negative sample sets to enhance the accuracy of similarity calculation by the dual-tower model, so as to obtain a high-precision set of currently relevant API sequences. The currently relevant set of API sequences is input into the large language model, and finally a high-precision API sequence that solves the query is obtained. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] Figure 1 This is a flow chart of a method according to an embodiment of the present invention;

[0042] Figure 2 This is a diagram of the device structure of an embodiment of the present invention

[0043] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0044] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.

[0045] Reference Figure 1 The present application provides a method for generating a large model API sequence based on retrieval enhancement, comprising:

[0046] S1: Obtain a set of "question-code" pairs and a set of API sequences through the history library, and construct a mapping relationship set based on the mapping relationship between the question and the API sequence;

[0047] Furthermore, step S1 is specifically as follows:

[0048] S11: Obtain a history database, pre-process each question in the history database, and obtain a set of "question-code" pairs;

[0049] Specifically, we first sorted the projects in Github by star rating and mined out the projects with a star rating greater than 5. We also removed all branches of the projects to avoid duplication. For each project file, we parsed the functions in it, took the comments in the functions as the problems in the history library, and took the functions as the code in the history library to form a history library. We removed stop words and restored stems from the problems in the history library, removed comments and whitespace characters from the code in the history library, and performed lexical analysis to obtain a clean and formatted set of "problem-code" pairs;

[0050] S12: Get the AST tree through the code in the history library, traverse the AST tree to get the API sequence set, API sequence set The expression is:

[0051] ,

[0052] Where n represents the total number of API sequences, Indicates the nth API sequence;

[0053] Specifically, for the preprocessed code in the history library, we first processed the import statements in the code. When we encountered an import statement, we recorded the symbol defined by the import statement and extracted the module name from it. After extracting the module name, we searched for all function calls to the imported module in the body of each function. In particular, we did not consider relative imports (from x import y) and wildcard imports (from x import *). Each function call consists of one or more identifiers connected by dots, followed by a parameter list: for example, xyz(). If the function call matches a top-level function defined earlier in the file or a local function defined earlier in a function, the function call is replaced with a list of API calls extracted from the earlier function. Otherwise, we check whether the first identifier (for example, the identifier x in the function call xyz()) is defined by any target import statement of the file. If so, the first identifier is replaced according to the import rule, and then the function call is considered an API call. After that, we use the tree-sitter open source tool to build the AST tree of the code, traverse the AST tree, find nodes representing API calls (such as function calls and method calls), extract information such as API name, parameter list, and call context, and obtain detailed call information of the API sequence:

[0054] ,

[0055] S13: Obtain the mapping relationship between the problem and the API sequence by using a key-value pair method, and construct a mapping relationship set.

[0056] S2: Obtain the positive sample set and the negative sample set through the mapping relationship set, build a dual-tower model, train the dual-tower model through the "question-code" pair set, the positive sample set and the negative sample set, and obtain the trained dual-tower model;

[0057] Furthermore, step S2 is specifically as follows:

[0058] S21: Obtain the five least historically relevant API sequences and one most historically relevant API sequence for each problem through the mapping relationship set, use the least historically relevant API sequences as a negative sample set, and use the most historically relevant API sequences as a positive sample set;

[0059] Specifically, the BM25 technology is used to search in the history library. By calculating and sorting the similarity between the current question and all the questions in the history library, the five least relevant API sequences and one most relevant API sequence are obtained for each question.

[0060] S22: Build a dual-tower model, which includes a question encoder and an API sequence encoder;

[0061] Furthermore, the present invention uses the open-source CodeBERT pre-trained model in HuggingFace as the question encoder;

[0062] Question Encoder The expression is:

[0063] ,

[0064] Similarly, the present invention uses the CodeBERT pre-trained model as the API encoder;

[0065] API Sequence Encoder The expression is:

[0066] ,

[0067] in, represents a set of "question-code" pairs, Represents a collection of API sequences.

[0068] Specifically, CodeBERT represents the CodeBERT pre-trained model. In particular, the parameters of the two CodeBERT pre-trained models are not shared.

[0069] S23: Construct a training vector set through the "question-code" pair set, the positive sample set and the negative sample set;

[0070] Furthermore,

[0071] The expression of the training vector set D is:

[0072] ,

[0073] Where i is the number of the question, m is the number of questions in the history library, It is The vector representation of the problem is It is The vector representation of the positive sample corresponding to the question, It is The corresponding question The vector representation of negative samples is: .

[0074] S24: Train the twin-tower model through the training vector set, and calculate the negative log-likelihood loss function of each problem;

[0075] Furthermore, the negative log-likelihood loss function is used to encourage the model to move the query vector closer to the positive sample text vector and away from the negative sample text vector.

[0076] The expression of the negative log-likelihood loss function is:

[0077] ,

[0078] Where i is the number of the question, It is The vector representation of the problem is It is The vector representation of the positive sample corresponding to the question, It is The corresponding question The vector representation of negative samples is: It is The negative log-likelihood loss function for this problem, sim() is the dot product similarity calculation function.

[0079] S25: Repeat step S24 until the negative log-likelihood loss function of each problem is less than a preset value, and obtain a trained dual-tower model.

[0080] Specifically, the present invention uses the AdamW algorithm to optimize the model parameters and sets the learning rate to α1e-4. After every 1000 batches, the model is evaluated with the test data set: the actual apiseq in the test set is compared with the apiseq predicted by the model. We use the BLEU-4 score, that is, the BLEU score considering nα-grams as 4 as the comparison benchmark, and α is the alpha symbol.

[0081] S3: Get the query statement, input the query statement into the trained dual-tower model, and obtain the current related API sequence set;

[0082] Specifically, the trained dual-tower model is used to generate and store vector representations for all API sequences in the historical library, and FAISS is used to build an index to speed up retrieval.

[0083] Generate a vector representation of the query statement, use dot product similarity to calculate its similarity with all API sequences in the history library, sort them in descending order according to the similarity score, select the top 5 API sequences as the 5 most relevant API sequences to the query statement, and use the 5 most relevant API sequences as the current relevant API sequence set.

[0084] S4: Construct prompts through the query statement and the current related API sequence set, input the prompts into the large language model for generation, and obtain the API sequence that solves the query.

[0085] Specifically, heuristic algorithms such as regular expressions are used to extract the API sequence that solves the query from the results given by the large language model.

[0086] Furthermore, the expression of the prompt in step S4 is:

[0087] {“Query”: [statement to be queried], “Currently related API sequence set”: [API1, API2,..., API5]}.

[0088] See also Figure 2 , Figure 2 It is a schematic diagram of the working of the hardware device of an embodiment of the present invention, and the hardware device specifically includes: a large model API sequence generation device 401 based on retrieval enhancement, a processor 402 and a storage medium 403.

[0089] A large model API sequence generation device 401 based on retrieval enhancement: The large model API sequence generation device 401 based on retrieval enhancement implements the large model API sequence generation method based on retrieval enhancement.

[0090] Processor 402: The processor 402 loads and executes the instructions and data in the storage medium 403 to implement the large model API sequence generation method based on retrieval enhancement.

[0091] Storage medium 403: The storage medium 403 stores instructions and data; the storage medium 403 is used to implement the large model API sequence generation method based on retrieval enhancement.

[0092] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or system including the element.

[0093] The serial numbers of the embodiments of the present invention are for descriptive purposes only and do not represent the superiority or inferiority of the embodiments. In a unit claim that lists several means, several of these means may be embodied by the same hardware item. The use of the words first, second, and third, etc. does not indicate any order and these words may be interpreted as identifiers.

[0094] The above are only preferred embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.

Claims

1. A method for generating large model API sequences based on retrieval enhancement, characterized in that: include: S1: Obtain a set of "question-code" pairs and a set of API sequences from the history library, and construct a mapping relationship set based on the mapping relationship between the question and the API sequence; S2: Obtain the positive sample set and the negative sample set through the mapping relationship set, build a double-tower model, train the double-tower model through the "question-code" pair set, the positive sample set and the negative sample set, and obtain the trained double-tower model; S3: Get the query statement, input the query statement into the trained dual-tower model, and obtain the current related API sequence set; S4: construct prompts through the query statement and the current related API sequence set, input the prompts into the large language model for generation, and obtain the API sequence that solves the query; Step S2 is specifically as follows: S21: Obtain the five least historically relevant API sequences and one most historically relevant API sequence for each problem through the mapping relationship set, use the least historically relevant API sequences as a negative sample set, and use the most historically relevant API sequences as a positive sample set; S22: Build a dual-tower model, which includes a question encoder and an API sequence encoder; S23: Construct a training vector set through the "question-code" pair set, the positive sample set and the negative sample set; S24: Train the twin-tower model through the training vector set, and calculate the negative log-likelihood loss function of each problem; S25: Repeat step S24 until the negative log-likelihood loss function of each problem is less than a preset value, and a trained double-tower model is obtained; Question Encoder The expression is: , API Sequence Encoder The expression is: , in, represents a set of "question-code" pairs, Represents a collection of API sequences.

2. The method for generating a large model API sequence based on retrieval enhancement according to claim 1 is characterized in that: Step S1 is specifically as follows: S11: Obtain a history database, pre-process each question in the history database, and obtain a set of "question-code" pairs; S12: Get the AST tree through the code in the history library, traverse the AST tree to get the API sequence set, API sequence set The expression is: , Where n represents the total number of API sequences, Indicates the nth API sequence; S13: Obtain the mapping relationship between the problem and the API sequence by using a key-value pair method, and construct a mapping relationship set.

3. The method for generating a large model API sequence based on retrieval enhancement according to claim 1, characterized in that: The expression of the training vector set D is: , Where i is the number of the question, m is the number of questions in the history library, It is The vector representation of the problem is It is The vector representation of the positive sample corresponding to the question, It is The corresponding question The vector representation of negative samples is: .

4. The method for generating a large model API sequence based on retrieval enhancement according to claim 1, characterized in that: The expression of the negative log-likelihood loss function is: , Where i is the number of the question, It is The vector representation of the problem is It is The vector representation of the positive sample corresponding to the question, It is The corresponding question The vector representation of negative samples is: It is The negative log-likelihood loss function for this problem, sim() is the dot product similarity calculation function.

5. The method for generating a large model API sequence based on retrieval enhancement according to claim 1 is characterized in that: The expression of the prompt in step S4 is: {"Query": [statement to be queried], "Currently related API sequence set": [API1, API2,..., API5]}.

6. A storage medium, characterized in that: The storage medium stores instructions and data for implementing a large model API sequence generation method based on retrieval enhancement as described in any one of claims 1 to 5.

7. A large model API sequence generation device based on retrieval enhancement, characterized in that: include: Processor and storage medium; the processor loads and executes instructions and data in the storage medium to implement a retrieval-enhanced large model API sequence generation method as described in any one of claims 1 to 5.

Citation Information

Patent Citations

  • Cross-language or cross-library application program interface mapping method based on representation learning

    CN116483437A

  • Retrieval method, system and equipment based on double-tower recall and storage medium

    CN116501837A

  • Reply method and device based on local knowledge base and large language model

    CN117349420A

  • Multi-task code retrieval method based on modal-independent comparative learning

    CN117634615A

  • Dynamic adaptation question answering system and method based on hierarchical structure and retrieval enhancement

    CN118193714A