Method, apparatus, device, and storage medium for generating question-and-answer pairs based on API documentation
By structurally processing API document function blocks and using logical reasoning with prompt templates, the method addresses inefficiencies in generating question-answer pairs, ensuring relevance to enterprise scenarios and improving the quality and efficiency of data generation for enterprise-specific language models.
Patent Information
- Application Number
- CN202510252098.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-05
- Publication Date
- 2025-07-15
- Estimated Expiration
- 2045-03-05
AI Technical Summary
The number of internal API documents in the enterprise is huge and complex. The existing Q&A is inefficient in generating methods and cannot simulate practical problems.
By structuring the API document, structured function blocks are generated, logical reasoning is used to use preset prompt templates and language models to generate high-quality Q&A pair data, including questions to be entered, intermediate questions, question answers and code generation requests, ensuring that the generated Q&A pairs are suitable for different application scenarios of enterprise self-use models.
It improves the data generation efficiency of Q&A pairs, ensures that the generated Q&A pairs can simulate practical problems that enterprises may encounter, and improves the practicality and versatility of the enterprise's own models.
Smart Images

Figure CN119739755B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical fields of natural language processing and artificial intelligence, and particularly to a method, apparatus, device, and storage medium for generating question-and-answer pairs based on API documents. Background Art
[0002] Currently, there are various open-source large language models on the market, such as Llama. Open-source large language models have a large number of parameters and high model performance. Enterprises have their own language models set up to solve industry problems. Compared with open-source large language models, the number of parameters of self-owned language models is smaller and their model performance is lower. In order to make the performance of self-owned language models as close as possible to that of open-source language models, enterprises often need to fine-tune self-owned language models with question-and-answer pair data in different scenarios. Therefore, it is necessary to first generate this question-and-answer pair data.
[0003] It should be noted that with the wide popularization of enterprise software development processes and the application of APIs (Application Programming Interfaces), enterprises often have a large number of API documents for developers to use. Currently, the method for generating question-and-answer pair data is as follows: Manually design questions that need to be input into an open-source language model based on the API documents within the enterprise, then input these questions into the open-source language model for processing, and output the answers corresponding to these questions. Based on these questions and the corresponding answers, question-and-answer pair data is obtained.
[0004] However, due to the large number and complex structure of API documents within the enterprise, designing questions based on API documents manually not only results in low efficiency in generating question-and-answer pair data, but also the final obtained question-and-answer pair data cannot truly simulate the actual problems that enterprises may encounter because the API call methods in different application scenarios are not considered. Summary of the Invention
[0005] Based on this, in view of the above technical problems, it is necessary to provide a method, apparatus, computer device, computer-readable storage medium, and computer program product for generating question-and-answer pairs based on API documents, which can improve the efficiency of generating question-and-answer pairs and enable the generated question-and-answer pairs to truly simulate the actual problems that enterprises may encounter.
[0006] In a first aspect, this application provides a method for generating question-and-answer pairs based on API documents, including:
[0007] Structurally process the document function blocks in the obtained API document to obtain structured function blocks;
[0008] Based on the structured function block and a preset first prompt template, determine the question to be input; input the question to be input into a preset language model for processing, and output an intermediate question;
[0009] Based on the intermediate question, the language model, and a preset second prompt template, determine the question answer and the code generation request; input the code generation request into the language model for processing, and output the code answer;
[0010] In response to the code answer passing the verification, use the question to be input, the intermediate question, the question answer, the code generation request, and the code answer corresponding to the code answer as the target Q&A pair.
[0011] In a second aspect, the present application provides a Q&A pair generation device based on an API document, including:
[0012] A structured module for performing structured processing on the document function blocks in the obtained API document to obtain structured function blocks;
[0013] An intermediate processing module for determining the question to be input based on the structured function block and a preset first prompt template; inputting the question to be input into a preset language model for processing, and outputting an intermediate question;
[0014] A code output module for determining the question answer and the code generation request based on the intermediate question, the language model, and a preset second prompt template; inputting the code generation request into the language model for processing, and outputting the code answer;
[0015] A Q&A pair generation module for, in response to the code answer passing the verification, using the question to be input, the intermediate question, the question answer, the code generation request, and the code answer corresponding to the code answer as the target Q&A pair.
[0016] In a third aspect, the present application provides a computer device. The computer device includes a memory and a processor. The memory stores a computer program, and when the processor executes the computer program, the steps in the above method are implemented.
[0017] In a fourth aspect, the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps in the above method are implemented.
[0018] In a fifth aspect, the present application provides a computer program product. The computer program product includes a computer program. When the computer program is executed by a processor, the steps in the above method are implemented.
[0019] The above-mentioned method, device, computer device, computer-readable storage medium, and computer program product for generating question-and-answer pairs based on API documents generate a to-be-input question by using the structured function blocks obtained after structuring the API documents and the first prompt template, and input the to-be-input question into the language model for processing, which can realize guiding the language model to perform the initial logical reasoning in the scenario corresponding to the structured function blocks to obtain an intermediate question; then, guide the language model to perform the secondary logical reasoning through the intermediate question to obtain the question answer; afterwards, generate a code generation request by using the question answer and the second prompt template, and input the code generation request into the language model for processing, and once again guide the language model to perform logical reasoning to obtain the code answer. In this way, the language model can be guided to continuously perform logical reasoning in the form of a thought tree to obtain the required code answer; finally, after the code answer passes the verification, it indicates that all the to-be-input questions, intermediate questions, question answers, code generation requests, and code answers obtained through the entire logical reasoning are suitable as the question-and-answer pair data required for training the enterprise's self-owned language model. Since this question-and-answer pair data is targeted at specific scenarios, it can ensure that the obtained question-and-answer pair data is data considering the API call methods in different application scenarios, thereby simulating the actual problems that the enterprise may encounter; and in the process of generating the question-and-answer pairs, it avoids manually designing questions based on the large number and complex structure of API documents. In this way, it is convenient to improve the generation efficiency of the question-and-answer pair data. Description of the Drawings
[0020] Figure 1 FIG. is an application environment diagram of a method for generating question-and-answer pairs based on API documents provided by an embodiment of the present application;
[0021] Figure 2 FIG. is a schematic flowchart of a method for generating question-and-answer pairs based on API documents provided by an embodiment of the present application;
[0022] Figure 3 FIG. is a schematic flowchart of another method for generating question-and-answer pairs based on API documents provided by an embodiment of the present application;
[0023] Figure 4 FIG. is a structural block diagram of a device for generating question-and-answer pairs based on API documents provided by an embodiment of the present application;
[0024] Figure 5 FIG. is an internal structure diagram of a computer device provided by an embodiment of the present application;
[0025] Figure 6 FIG. is an internal structure diagram of another computer device provided by an embodiment of the present application;
[0026] Figure 7 FIG. is an internal structure diagram of a computer-readable storage medium provided by an embodiment of the present application. Detailed implementation manners
[0027] In order to make the objectives, technical solutions and advantages of the present application more clear and understandable, the present application will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0028] The method for generating question-and-answer pairs based on API documents provided by the embodiments of the present application can be applied to an application environment as Figure 1 shown. Among them, the terminal 102 communicates with the server 104 through a communication network. The data storage system can store the data that the server 104 needs to process. The data storage system can be integrated on the server 104, or can be placed in the cloud or on other network servers. Among them, the terminal 102 can be, but is not limited to, various personal computers, laptop computers, smart phones, tablet computers, Internet of Things devices and portable wearable devices. The Internet of Things devices can be smart speakers, smart TVs, smart air conditioners, smart in-vehicle devices, etc. The portable wearable devices can be smart watches, smart bracelets, head-mounted devices, etc. The server 104 can be implemented by an independent server or a server cluster composed of multiple servers.
[0029] As Figure 2 shown, the embodiments of the present application provide a method for generating question-and-answer pairs based on API documents. Taking the case where this method is applied to the Figure 1 terminal 102 or the server 104 as an example for description. It can be understood that the computer device can include at least one of a terminal and a server. The method includes the following steps:
[0030] S210. Perform structured processing on the document function blocks in the obtained API document to obtain structured function blocks.
[0031] Among them, when an enterprise develops software related to API (Application Programming Interface) applications, a large number of API documents will be generated. The API document is a technical document used to describe in detail the application programming interfaces provided by a software system (such as a website, an application program, an operating system, etc.); the API document is similar to a user manual for developers, and is used to guide developers on how to correctly use the various interfaces provided by the software system to build software or achieve interactions between systems. The API document contains multiple function blocks, and the function blocks in the API document are denoted as document function blocks; subsequently, a specific question is needed to guide the language model to output question-and-answer pairs, and the specific question is generated based on the document function blocks. In order to facilitate obtaining the specific question, it is necessary to perform structured processing on the document function blocks to obtain structured function blocks, and extract some keywords from the structured function blocks to generate the specific question.
[0032] Among them, the structured processing is to label different parts in the document function block to determine the corresponding labels for each part, and then determine the content corresponding to different labels, so as to obtain a structured function block; Exemplarily, please refer to Figure 3 , the labels corresponding to different parts in the document function block include: function name, function description, function example, parameter description, etc.; Taking a document function block as an example, after determining the labels corresponding to different parts of the document function block, the content corresponding to different labels can also be determined. In this way, the structured processing of the document function block can be realized, and the structured function block corresponding to the document function block can be obtained. The structured function blocks corresponding to multiple document function blocks together form a structured document.
[0033] Exemplarily, please refer to Figure 3 , the structured function block corresponding to a document function block in the API document is as shown in the content of the first dashed box in the structured document. The function name of this structured function block is: CvtColor, the function description is: change the arrangement order of image channels, the function example is: CvtColor(…), and the parameter description is: input: input image, ……. Another exemplarily, please also refer to Figure 3 , the structured function block corresponding to another document function block in the API document is as shown in the content of the last dashed box in the structured document. The function name of this structured function block is: Rescale, the function description is: change the image size, the function example is: Rescale(…), and the parameter description is: input: input image, …….
[0034] S220. Determine the question to be input based on the structured function block and a preset first prompt template; input the question to be input into a preset language model for processing, and output an intermediate question.
[0035] Among them, this embodiment presets multiple first prompt templates, such as Figure 3 prompt1.1, prompt2.1, and prompt3.1 shown in , the prompt template includes a fixed part and a part to be filled. The part to be filled is used to fill in the content in the structured function block; Taking a first prompt template as an example, after filling in the content in the structured function block in the first prompt template, a question for subsequent input into the language model can be formed, and this question is recorded as the question to be input.
[0036] Exemplarily, a first prompt template is: "When you need to execute {function name} in {scenario}, what problems might you encounter?" Among them, "{scenario}" and "{function name}" are parts to be filled. Subsequently, "scenario" and "function name" can be extracted from the structured function block and filled into the parts to be filled, that is, filled into the curly braces corresponding to the first prompt template; the part other than the parts to be filled in the first prompt template is the fixed part. After filling, the question to be input can be obtained. Exemplarily, the question to be input is "When you need to execute {Add function} in {product production}, what problems might you encounter?".
[0037] Another exemplarily, another first prompt template is: "If you use {function name} in {scenario}, what error situations might occur?" Among them, "{scenario}" and "{function name}" are parts to be filled. Subsequently, "scenario" and "function name" can be extracted from the structured function block and filled into the parts to be filled. After filling, the question to be input can be obtained. Exemplarily, the question to be input is "If you use {Add function} in {product production}, what error situations might occur?".
[0038] Among them, the language model is the large language model that has been open-sourced in the market currently. Exemplarily, the language model can be the Llama model, and there is no specific limitation; the question to be input is input into the language model for processing to initially guide the language model to perform logical reasoning, output the question corresponding to the question to be input, and record this question as the intermediate question. This intermediate question needs to be further input into the language model for processing later to guide the language model to perform logical reasoning again.
[0039] S230. Based on the intermediate question, the language model, and a preset second prompt template, determine the question answer and the code generation request; input the code generation request into the language model for processing, and output the code answer.
[0040] Among them, after the intermediate question is input into the language model for processing, the question answer corresponding to the intermediate question is output; this embodiment also presets multiple second prompt templates, such as Figure 3 shown, prompt1.2, prompt2.2, and prompt3.2. The second prompt template also includes a fixed part and a part to be filled. This part to be filled is used to fill the question answer corresponding to the intermediate question.
[0041] Exemplarily, a second prompt template is "If {question answer}, please give the corresponding code example", where "{question answer}" is the part to be filled, and the rest of the second prompt template except "{question answer}" is the fixed part.
[0042] After filling in the corresponding question answers in the second prompt template, a code generation request is obtained; the language model outputs the corresponding code, and this code is recorded as the code answer, that is, Figure 3 The code answers 1, code answer 2, and code answer 3 shown in the figure. It should be noted that the code generation request input into the language model can once again guide the language model to perform logical reasoning. By guiding the language model to perform logical reasoning, the language model can output question-and-answer pairs that meet expectations. The question-and-answer pairs that meet expectations are also the question-and-answer pairs required for training the enterprise-owned model to achieve fine-tuning of the enterprise-owned model.
[0043] S240. In response to the code answer passing the verification, the to-be-input question, intermediate question, question answer, code generation request, and code answer corresponding to the code answer are used as the target question-and-answer pair.
[0044] It should be noted that the code answer output by the language model is not necessarily correct. Therefore, it is necessary to verify the code answer.
[0045] If it is determined through verification that the code answer is correct, it means that the to-be-input question, intermediate question, question answer, code generation request, and the code answer that guide the language model to perform logical reasoning during the generation of the code answer can all be used as question-and-answer pairs that meet expectations, and the to-be-input question, intermediate question, question answer, code generation request, and the code answer corresponding to the code answer are recorded as the target question-and-answer pair; In this embodiment, there is also a pre-set question-and-answer pair database, such as Figure 3 As shown, this question-and-answer pair database is used to store the target question-and-answer pair. The target question-and-answer pair is a high-quality question-and-answer pair, and the target question-and-answer pair can provide reliable training data for subsequent fine-tuning of the enterprise-owned model.
[0046] If it is determined through verification that the code answer is incorrect, it means that the to-be-input question, intermediate question, question answer, code generation request, and the code answer that guide the language model to perform logical reasoning during the generation of the code answer cannot be used as question-and-answer pairs that meet expectations.
[0047] It can be seen that in the embodiment of the present application, by generating the to-be-input question from the structured function block obtained by structuring the API document and the first prompt template, and inputting the to-be-input question into the language model for processing, it is possible to guide the language model to perform primary logical reasoning in the scenario corresponding to the structured function block to obtain the intermediate question; then, through the intermediate question, guide the language model to perform secondary logical reasoning to obtain the question answer; after that, generate a code generation request from the question answer and the second prompt template, and input the code generation request into the language model for processing, once again guiding the language model to perform logical reasoning to obtain the code answer. In this way, a thought tree (such as Figure 3In the form shown), guide the language model to continuously perform logical reasoning to obtain the required code answer; finally, after the code answer passes the verification, it is explained that the question to be input, intermediate questions, question answers, code generation requests, and code answers obtained through the entire logical reasoning are all suitable as the Q&A pair data required for training the enterprise's self - used language model. Since this Q&A pair data is targeted at specific scenarios, it can ensure that the obtained Q&A pair data takes into account the API call methods under different application scenarios, thus simulating the actual problems that the enterprise may encounter; and during the process of generating the Q&A pairs, it avoids manually designing questions based on a large number of API documents with complex structures. In this way, it is convenient to improve the generation efficiency of the Q&A pair data.
[0048] It should also be noted that in this embodiment, through an automated code verification mechanism, it is ensured that the code in the generated Q&A pairs can be correctly executed, avoiding the misguidance caused by incorrect codes; in addition, a verification and generation mechanism based on the thought tree is also used to make the generated Q&A pairs closer to the actual scenario, ensuring high - quality and practical answers; furthermore, after fine - tuning the enterprise's self - used model with the target Q&A pairs, the enterprise's self - used model can more accurately meet the needs of different users for API document queries, improving the practicality and generality of the model.
[0049] In some embodiments, the document function blocks in the obtained API document are structurally processed to obtain structured function blocks, including:
[0050] S211. Perform text parsing on the obtained API document to obtain document function blocks.
[0051] Among them, the API document is composed of multiple document function blocks. In this embodiment, a text parsing algorithm is preset, and this text parsing algorithm is used to automatically identify the document function blocks in the API document according to the chapter structure of the API document.
[0052] Specifically, use the preset text parsing algorithm to process the API document and output the document function blocks.
[0053] S212. Perform named - entity recognition on the document function blocks to obtain entities corresponding one - by - one to each key part of the document function blocks.
[0054] Among them, taking a document function block as an example, the document function block consists of multiple key parts. Exemplarily, the key parts of the document function block include: function name part, function description part, parameter description part, function example part, return value description part, and error handling part. To facilitate obtaining a structured function block, it is necessary to tag each key part to determine the corresponding tags for each key part, that is, to determine the corresponding entities for each key part. Here, the entity can be understood as a tag. Among them, the function description part usually includes the function's function and applicable scenarios, etc. The parameter description part includes parameter names, parameter types, default values, and detailed parameter descriptions, etc. The function example part is the function call example code attached in the API document.
[0055] In this embodiment, a named entity recognition algorithm is preset. This named entity recognition algorithm is used to perform named entity recognition on each key part in the document function block one by one. Named entity recognition is also to determine the entities corresponding to each key part of the document function block. Exemplarily, by performing named entity recognition on each key part in the document function block one by one, the entity of the function name part can be obtained as "Function Name (Entity: Function Name)", the entity of the function description part as "Function Description (Entity: Function Description)", the entity of the parameter description part as "Parameter Description (Entity: Parameters)", the entity of the function example part as "Example Code (Entity: Example Code)", the entity of the return value description part as "Return Value Description (Entity: Return Value)", and the entity of the error handling part as "Error Handling (Entity: Error Handling)".
[0056] S213. Determine a structured function block based on the key part and its corresponding entity.
[0057] Among them, taking one of the document function blocks as an example, the structured function block includes the entities determined based on this document function block, and the key parts corresponding to each entity one by one; the structured function block is as Figure 3 shown in the content of each dashed box in the structured document.
[0058] It can be seen that in this embodiment, each key part in the structured function block has its corresponding entity. Subsequently, when initially guiding the language model to perform logical reasoning, it is necessary to input a problem to be input into the language model, and the problem to be input is generated based on the structured function block and a preset first prompt template. The entities or key parts in the structured function block and the first prompt template can form the problem to be input. Therefore, obtaining the structured function block through the above steps can prepare the data in advance for generating the problem to be input later.
[0059] In some embodiments, determining the problem to be input based on the structured function block and the preset first prompt template includes:
[0060] S221. Determine the target function block based on the structured function block and the regular expressions corresponding to its respective structures.
[0061] The structured function block is composed of multiple structures; each structure consists of an entity and the key part corresponding to the entity; for example, one structure is "function name: CvtColor"; where "function name" is the entity and "CvtColor" is the key part corresponding to the entity.
[0062] Each structure has its preset regular expression, and the regular expression is used to represent the document format of the corresponding structure in the corresponding structured function block; for example, the regular expression corresponding to the structure related to the entity "function description" is "the structure contains the characters 'function description', and this structure is located in the second position in the corresponding structured function block"; the document format of the structure can be judged through the preset regular expression of the structure to determine whether the document format of the structure conforms to the corresponding regular expression.
[0063] For example, if the document format of the structure related to the entity "function description" conforms to the corresponding regular expression "the structure contains the characters 'function description', and this structure is located in the second position in the corresponding structured function block", it indicates that the document format of this structure conforms to the corresponding regular expression; if the document format of the structure related to the entity "function description" does not conform to the corresponding regular expression "the structure contains the characters 'function description', and this structure is located in the second position in the corresponding structured function block", it indicates that the entity in this structure may be incorrect, and the entity corresponding to this structure needs to be corrected subsequently to obtain a new structure, and then it is determined again whether the document format of the new structure conforms to the corresponding regular expression; if it is determined that each structure in the structured function block conforms to its corresponding regular expression, it indicates that the entities in each structure are all correct, and at this time, this kind of structured function block is recorded as the target function block.
[0064] S222. Determine the problem to be input based on the target function block and the preset first prompt template.
[0065] Among them, the structured function block contains entities and the key parts corresponding to the entities. The function description of the key parts corresponding to this entity contains the description of "scenario", that is, the above-mentioned "applicable scenario"; the preset first prompt template contains parts to be filled, and this part to be filled is used to fill in the "entity" in the structured function block and / or the "scenario" in the key parts; Exemplarily, a preset first prompt template is "When you need to execute {function name} in the {scenario}, what problems may you encounter?" After filling the "entity" in the structured function block and the "scenario" in the key parts into the preset first prompt template, the question to be input is obtained, and this question to be input is used to be input into the language model for processing.
[0066] It can be seen that in this embodiment, by judging whether the document format of the corresponding structure is correct according to the regular expression corresponding to each structure, it can be judged whether the entity of each structure is correct. In this way, it is convenient to ensure that the entities in the obtained target function block are all correct, because the entities in the target function block will be used later to generate the corresponding question to be input in combination with the corresponding first prompt template. In this way, it is convenient to ensure the accuracy of the question to be input, so as to ensure the correct guidance of the language model, and then ensure the accuracy of the subsequent generated question-and-answer pairs.
[0067] In some embodiments, based on the structured function block and the regular expressions corresponding to its respective structures, determining the target function block includes:
[0068] S221A. Determine the regular expressions corresponding to the respective structures in the structured function block.
[0069] Among them, the structured function block contains multiple structures. In this embodiment, a one-to-one corresponding regular expression is preset for each structure, and the regular expression is used to verify whether the document format of the corresponding structure meets the expectation.
[0070] S221B. In response to the document format of the structure conforming to the regular expression, use the structure as the target structure; otherwise, correct the entity of the structure to obtain the target structure.
[0071] Among them, if the document format of the structure conforms to the regular expression, it means that the entity in this structure is correct, and this type of structure is recorded as the target structure; if the document format of the structure does not conform to the regular expression, it means that the entity in this structure may be incorrect. At this time, it is necessary to correct the entity in this structure to obtain a new structure, and then judge again whether the document format of the new structure conforms to the regular expression. If so, use the new structure as the target structure; otherwise, continue to correct the entity in the new structure.
[0072] S221C. Based on the target structure, determine the target function block.
[0073] Among them, the target function block is composed of target structures corresponding to different entities.
[0074] It can be seen that in this embodiment, by validating the document format of the structure through the regular expressions corresponding to each structure, the accuracy of the entities in the structure can be guaranteed.
[0075] In some embodiments, after determining the target function block based on the structured function block and the regular expressions corresponding to its respective structures, the method further includes:
[0076] In response to the absence of a scenario in the target function block, at least one scenario is added to the target function block to obtain a new target function block.
[0077] It should be noted that the key part of some target function blocks may not contain a scenario. The scenario is the above-mentioned "applicable scenario". However, the part to be filled in the first prompt template corresponding to the target function block may need to fill in "scenario". Therefore, after obtaining the target function block, it is also necessary to determine whether the target function block contains a scenario, that is, to determine whether the key part corresponding to the entity of function description in the target function block contains "applicable scenario"; if not, at least one "applicable scenario" needs to be added to this key part, and the target function block with "applicable scenario" added is denoted as the new target function block. Among them, the scenario can be "product production", etc., and is not specifically limited.
[0078] It can be seen that in this embodiment, after generating the target function block, it is further determined whether the target function block contains a scenario, and when there is no scenario, a scenario is added to the target function block, so as to ensure that when the first prompt template needs to fill in "scenario", the corresponding "scenario" can be extracted from the target function block, thereby facilitating the generation of the problem to be input.
[0079] In some embodiments, based on the intermediate problem, the language model, and the preset second prompt template, determining the problem answer and the code generation request includes:
[0080] S231. Input the intermediate problem into the language model for processing, and output the problem answer.
[0081] Among them, the language model can generate a problem answer corresponding to the intermediate problem by processing the intermediate problem.
[0082] Exemplarily, the intermediate problem is "When you need to execute the {Add function} in {product production}, what problems may you encounter?" Inputting this intermediate problem into the language model for processing, the output problem answer is "When you need to execute the {Add function} in {product production}, you need to determine the parameters for the addition operation".
[0083] S232. Determine a code generation request based on the question answer and a preset second prompt template.
[0084] Among them, the part to be filled in the preset second prompt template is used to fill in the obtained question answer above. After completion, a code generation request is obtained.
[0085] Exemplarily, the preset second prompt template is "If {question answer}, please give the corresponding code example". After filling in the question answer into the part to be filled in the second prompt template, the code generation request "If {when you need to execute the {Add function} in {product production}, you need to determine the parameters for the addition operation}, please give the corresponding code example" is obtained.
[0086] It can be seen that in this embodiment, by inputting intermediate questions into the language model, the language model can be guided to perform logical reasoning to obtain the question answer. Further, through the question answer and the preset second prompt template, a code generation request can be generated. The code generation request is used to guide the language model to further perform logical reasoning to output the corresponding code answer. Through the intermediate questions and the code generation request, the language model can be guided to perform logical reasoning, so that it is convenient for the language model to generate expected question-and-answer pairs.
[0087] In some embodiments, in response to the code answer passing the verification, it includes:
[0088] S241. Input the code answer into a preset static analysis tool for code standardization checking and / or error checking.
[0089] S242. If the code answer passes the code standardization checking and / or error checking, confirm that the code answer passes the verification.
[0090] Among them, the static analysis tool is used to perform code standardization checking and / or error checking on the code answer. Among them, the code standardization checking is used to determine whether the code answer conforms to the code writing specification, and the error checking is used to determine whether there are programming errors in the code answer; both the code standardization checking and / or error checking are used to determine whether the code answer output by the language model (that is, a piece of code corresponding to the code generation request) can run normally. The static analysis tool adopted in this embodiment includes but is not limited to Pylint or Flake8. If the code answer passes the code standardization checking and / or error checking, it means that the code answer passes the verification, that is, it means that the input question, intermediate question, question answer, code generation request, and the code answer generated during the generation of the code answer can be used as the target question-and-answer pair.
[0091] It can be seen that in this embodiment, by verifying the correctness of the generated code answer, it is convenient to determine whether the to-be-input question, intermediate question, question answer, code generation request, and the code answer generated during the process of generating the code answer are suitable as the target Q&A pair based on the verification result.
[0092] It should be understood that although each step in the flowchart involved in the above embodiments is shown in sequence according to the indication of the arrow, these steps are not necessarily executed in the order indicated by the arrow. Unless there is a clear description in this article, the execution of these steps has no strict order limit, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowchart involved in the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily executed at the same moment, but can be executed at different moments. The execution order of these steps or stages is not necessarily sequential, but can be executed alternately or in turn with at least a part of other steps or steps or stages in other steps.
[0093] Based on the same inventive concept, an embodiment of the present application also provides a Q&A pair generation device based on an API document. The implementation solution for solving problems provided by this device is similar to the implementation solution described in the above method. Therefore, the specific limitations in one or more embodiments of the Q&A pair generation device based on an API document provided below can refer to the limitations on the Q&A pair generation method based on an API document in the above text, and will not be repeated here.
[0094] As Figure 4 shown, an embodiment of the present application provides a Q&A pair generation device 400 based on an API document, including:
[0095] A structuring module 410, configured to perform structuring processing on the document function blocks in the obtained API document to obtain structured function blocks;
[0096] An intermediate processing module 420, configured to determine a to-be-input question based on the structured function block and a preset first prompt template; input the to-be-input question into a preset language model for processing, and output an intermediate question;
[0097] A code output module 430, configured to determine a question answer and a code generation request based on the intermediate question, the language model, and a preset second prompt template; input the code generation request into the language model for processing, and output a code answer;
[0098] A Q&A pair generation module 440, configured to, in response to the code answer passing the verification, use the to-be-input question, intermediate question, question answer, code generation request, and code answer corresponding to the code answer as the target Q&A pair.
[0099] In some embodiments, when structuring the document function blocks in the obtained API document to obtain structured function blocks, the structuring module 410 is specifically configured to:
[0100] Perform text parsing on the obtained API document to obtain document function blocks;
[0101] Perform named entity recognition on the document function blocks to obtain entities corresponding to each key part of the document function blocks;
[0102] Determine structured function blocks based on the key parts and their corresponding entities.
[0103] In some embodiments, when determining the problem to be input based on the structured function blocks and a preset first prompt template, the intermediate processing module 420 is specifically configured to:
[0104] Determine target function blocks based on the structured function blocks and regular expressions corresponding to their respective structures;
[0105] Determine the problem to be input based on the target function blocks and a preset first prompt template.
[0106] In some embodiments, when determining target function blocks based on the structured function blocks and regular expressions corresponding to their respective structures, the intermediate processing module 420 is specifically configured to:
[0107] Determine regular expressions corresponding to the respective structures in the structured function blocks;
[0108] In response to the document format of the structure conforming to the regular expression, regard the structure as the target structure; otherwise, correct the entity of the structure to obtain the target structure;
[0109] Determine target function blocks based on the target structures.
[0110] In some embodiments, the intermediate processing module 420 is further configured to:
[0111] In response to the target function blocks not containing scenarios, add at least one scenario to the target function blocks to obtain new target function blocks.
[0112] In some embodiments, when determining the question answer and code generation request based on the intermediate question, the language model, and a preset second prompt template, the code output module 430 is specifically configured to:
[0113] Input the intermediate question into the language model for processing and output the question answer;
[0114] Determine the code generation request based on the question answer and a preset second prompt template.
[0115] In some embodiments, in response to the code answer passing the verification, the Q&A pair generation module 440 is specifically configured to:
[0116] Input the code answer into a preset static analysis tool for code standardization checking and / or error checking;
[0117] If the code answer passes the code standardization checking and / or error checking, it is confirmed that the code answer passes the verification.
[0118] Each module in the above Q&A pair generation device based on API documentation can be implemented in whole or in part by software, hardware, and their combination. The above modules can be embedded in the processor in the computer device in hardware form or be independent of it, or can be stored in the memory in the computer device in software form, so that the processor can call and execute the operations corresponding to the above modules.
[0119] In some embodiments, a computer device is provided. The computer device can be a server, and its internal structure diagram can be as Figure 5 shown. The computer device includes a processor, a memory, an input / output interface (Input / Output, abbreviated as I / O), and a communication interface. Among them, the processor, the memory, and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. 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 data related to the Q&A pair generation method based on API documentation. The input / output interface of the computer device is used to exchange information between the processor and external devices. The communication 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, it implements the steps in the above Q&A pair generation method based on API documentation.
[0120] In some embodiments, a computer device is provided. The computer device can be a terminal, and its internal structure diagram can be as Figure 6As shown in the figure. The computer device includes a processor, a memory, an input / output interface, a communication interface, a display unit, and an input device. Among them, the processor, the memory, and the input / output interface are connected through a system bus, and the communication interface, the display unit, and the input device are connected to the system bus through the input / output interface. 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 and computer programs. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage medium. The input / output interface of the computer device is used to exchange information between the processor and external devices. The communication interface of the computer device is used to communicate with external terminals in a wired or wireless manner, and the wireless manner can be implemented through WIFI, a mobile cellular network, NFC (Near Field Communication), or other technologies. When the computer program is executed by the processor, it implements the steps in the above-mentioned method for generating question-and-answer pairs based on API documents. The display unit of the computer device is used to form a visually visible picture, which can be a display screen, a projection device, or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen; the input device of the computer device can be a touch layer covering the display screen, or a button, a trackball, or a touchpad provided on the computer device housing, or an external keyboard, touchpad, or mouse, etc.
[0121] Those skilled in the art can understand that Figure 5 or Figure 6 the structure shown in the figure is only a block diagram of some structures related to the solution of this application, and does not constitute a limitation on the computer device to which the solution of this application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine some components, or have different component arrangements.
[0122] In some embodiments, a computer device is provided. The computer device includes a memory and a processor. The memory stores a computer program. When the processor executes the computer program, it implements the steps in the above-mentioned method embodiments.
[0123] In some embodiments, as Figure 7 shown, an internal structure diagram of a computer-readable storage medium is provided. The computer-readable storage medium stores a computer program. When the computer program is executed by the processor, it implements the steps in the above-mentioned method embodiments.
[0124] In some embodiments, a computer program product is provided. The computer program product includes a computer program. When the computer program is executed by the processor, it implements the steps in the above-mentioned method embodiments.
[0125] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data for analysis, stored data, displayed data, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use, and processing of relevant data need to comply with the relevant laws, regulations, and standards of relevant countries and regions.
[0126] Those of ordinary skill in the art can understand that all or part of the processes in the methods of the above embodiments can be completed by instructing relevant hardware through a computer program. 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 methods. Among them, any reference to a memory, database, or other medium used in the embodiments provided in this application can include at least one of non-volatile and volatile memories. Non-volatile memory can include Read-Only Memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetoresistive random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM), etc. The databases involved in the embodiments provided in this application can include at least one of relational databases and non-relational databases. Non-relational databases can include distributed databases based on blockchain, etc., and are not limited thereto. The processors involved in the embodiments provided in this application can be general-purpose processors, central processors, graphics processors, digital signal processors, programmable logic devices, data processing logics based on quantum computing, etc., and are not limited thereto.
[0127] The technical features of the above embodiments can be combined arbitrarily. For the sake of brevity of description, 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, it should be considered to be within the scope described in this specification.
[0128] The above embodiments merely represent several implementation manners of the present application. The description thereof is relatively specific and detailed, but it should not be construed as a limitation on the patent scope of the present application. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present application, several modifications and improvements can still be made, and these all fall within the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the appended claims.
Claims
1. A method for generating question-answer pairs based on API documentation, characterized in that, including: structuring the document function blocks in the obtained API documentation to obtain structured function blocks; determining regular expressions corresponding to each structure in the structured function blocks; in response to the document format of the structure conforming to the regular expression, regarding the structure as a target structure; otherwise, correcting the entity of the structure to obtain a target structure; determining a target function block based on the target structure; determining a question to be input based on the target function block and a preset first prompt template; inputting the question to be input into a preset language model for processing to output an intermediate question; wherein, the intermediate question is obtained by guiding the language model to perform logical reasoning based on the question to be input; inputting the intermediate question into the language model for processing to output a question answer; determining a code generation request based on the question answer and a preset second prompt template; inputting the code generation request into the language model for processing to output a code answer; in response to the code answer passing the verification, regarding the question to be input, the intermediate question, the question answer, the code generation request, and the code answer corresponding to the code answer as a target Q&A pair.
2. The method according to claim 1, wherein The structuring the document function blocks in the obtained API documentation to obtain structured function blocks includes: performing text parsing on the obtained API documentation to obtain document function blocks; performing named entity recognition on the document function blocks to obtain entities corresponding one by one to each key part of the document function blocks; determining structured function blocks based on the key parts and their corresponding entities.
3. The method according to claim 1, wherein After determining the target function block based on the structured function blocks and the regular expressions corresponding to their respective structures, the method further includes: in response to the target function block not containing a scenario, adding at least one scenario to the target function block to obtain a new target function block.
4. The method according to claim 1, characterized in that, The in response to the code answer passing the verification includes: inputting the code answer into a preset static analysis tool for code standardization checking and / or error checking; if the code answer passes the code standardization checking and / or the error checking, confirming that the code answer passes the verification.
5. A question-and-answer pair generation device based on an API document, characterized in that including: a structuring module for structuring the document function blocks in the obtained API documentation to obtain structured function blocks; an intermediate processing module for determining regular expressions corresponding to each structure in the structured function blocks; in response to the document format of the structure conforming to the regular expression, regarding the structure as a target structure; otherwise, correcting the entity of the structure to obtain a target structure; determining a target function block based on the target structure; determining a question to be input based on the target function block and a preset first prompt template; inputting the question to be input into a preset language model for processing to output an intermediate question; wherein, the intermediate question is obtained by guiding the language model to perform logical reasoning based on the question to be input; A code output module, configured to input the intermediate question into the language model for processing and output a question answer; determine a code generation request based on the question answer and a preset second prompt template; input the code generation request into the language model for processing and output a code answer. A Q&A pair generation module, configured to, in response to the code answer passing the verification, use the to-be-input question, the intermediate question, the question answer, the code generation request, and the code answer corresponding to the code answer as a target Q&A pair.
6. The device according to claim 5, characterized in that, In terms of structuring the document function blocks in the obtained API document to obtain structured function blocks, the structuring module is specifically configured to: Perform text parsing on the obtained API document to obtain document function blocks; Perform named entity recognition on the document function blocks to obtain entities corresponding to each key part of the document function blocks; Determine structured function blocks based on the key parts and their corresponding entities.
7. The device according to claim 5, characterized in that, The intermediate processing module is further configured to: In response to the target function block not containing a scenario, add at least one scenario to the target function block to obtain a new target function block.
8. A computer device, the computer device comprising a memory and a processor, the memory storing a computer program, characterized in that, When the processor executes the computer program, the steps of the method according to any one of claims 1 to 4 are implemented.
9. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, the steps of the method according to any one of claims 1 to 4 are implemented.
Citation Information
Patent Citations
Front-end API (Application Program Interface) service code generation method and device, equipment and storage medium
CN119415100A