Question rewriting method, question and answer processing method and corresponding devices
By matching the natural language question text and key information in the code base, and using a large language model to rewrite the question text, the problem of inaccuracy in code retrieval caused by unstructured questions in user questions is solved, and the accuracy of code retrieval and user experience are improved. .
Patent Information
- Application Number
- CN202410875471.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-01
- Publication Date
- 2024-10-01
AI Technical Summary
Natural language questions raised by users are often unstructured, vague or unspecific, making it difficult for traditional code retrieval methods to directly use natural language to accurately locate the most relevant code fragments, reducing the quality of retrieval results and user experience.
By obtaining the natural language question text, matching the key information in the code base, constructing the prompt text and inputting the large language model to obtain the rewritten question text, which is used to retrieve code snippets in the code base. The method includes static analysis tools to extract file names, function names and contextual information, encoding processing to calculate similarities, filtering and ranking candidate code snippets, and generating responses to natural language questions.
It improves the accuracy and user experience of code retrieval, optimizes the efficiency and cost of code retrieval, ensures that the rewritten questions are more suitable for code retrieval needs, and improves the accuracy of answers and the quality of target code snippets.
Smart Images

Figure CN118733735A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a problem rewriting method, a problem processing method and a corresponding device. Background Art
[0002] With the development of programming assistance tools, especially the progress in deep learning and natural language processing, automatic code retrieval tools based on user queries have become more and more common. With the help of these tools, developers can quickly find corresponding code examples by asking questions, which improves programming efficiency.
[0003] However, in reality, the questions asked by users are often natural language texts, which are usually unstructured, random, and even contain vague or non-specific words. The inaccuracy of user questions makes it difficult for traditional code retrieval methods that directly use natural language questions to accurately locate the most relevant code snippets, thereby reducing the quality of retrieval results and user experience. Summary of the invention
[0004] The present application provides a question rewriting method, a question processing method and a corresponding device to rewrite a user's natural language question to be more in line with code retrieval requirements.
[0005] This application provides the following solutions:
[0006] In a first aspect, a method for question rewriting is provided, the method comprising:
[0007] Get natural language question text;
[0008] Matching the natural language question text in a key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from code snippets included in a code library;
[0009] A prompt text is constructed using the natural language question text and the candidate key information, and the prompt text is input into a large language model to obtain one or more rewritten question texts for the natural language question text. The rewritten question text is used to retrieve code snippets in the code library.
[0010] According to an achievable manner in the embodiment of the present application, the method further includes:
[0011] Using a static analysis tool to parse the code snippets contained in the code library to extract key information to form the key information set;
[0012] The key information includes at least one of a file name, a function name, a keyword, and context information.
[0013] According to an achievable manner in an embodiment of the present application, the method further includes: encoding each key information in the key information set to obtain a vector representation of each key information;
[0014] Matching the natural language question text in a key information set to obtain one or more candidate key information includes: encoding the natural language question text to obtain a vector representation of the natural language question text; calculating the similarity between the vector representation of the natural language question text and the vector representation of each key information, and taking the key information whose similarity meets the similarity requirement as the candidate key information.
[0015] According to an achievable method in an embodiment of the present application, constructing a prompt text using the natural language question text and the candidate key information includes:
[0016] Filling the natural language question text and the candidate key information into a preset prompt template to obtain the prompt text;
[0017] The prompt template includes: an instruction to prompt the large language model to select key information from the candidate key information to rewrite the natural language question text.
[0018] According to an achievable manner in the embodiment of the present application, the prompt template further includes: an instruction to prompt the large language model to output key information of the selection and the reason for the selection;
[0019] The method further includes: obtaining key information of the selection and the reason for the selection output by the large language model.
[0020] According to an achievable manner in the embodiment of the present application, the method further includes:
[0021] Using the natural language question text and the rewritten question text respectively, matching them in the code library to obtain a target code snippet;
[0022] A response to the natural language question text is generated using the target code snippet.
[0023] According to an achievable method in an embodiment of the present application, the natural language question text and the rewritten question text are respectively used to match in the code library to obtain a target code fragment including:
[0024] Using the natural language question text and the rewritten question text respectively, matching them in the code library to obtain candidate code snippets;
[0025] Perform at least one of filtering and sorting the candidate code snippets;
[0026] A target code segment is determined from the candidate code segments based on the processing result.
[0027] According to an achievable method in an embodiment of the present application, filtering the candidate code snippets includes:
[0028] Performing intent recognition on the natural language question text to obtain an intent category corresponding to the natural language question text;
[0029] Filter out candidate code snippets that do not match the intent category among the candidate code snippets.
[0030] According to an achievable method in an embodiment of the present application, sorting the candidate code snippets includes:
[0031] Determining a first ranking score for each candidate code snippet based on the semantic similarity between each candidate code snippet and the natural language question text;
[0032] Determining a second ranking score for each candidate code snippet based on a hit status of each candidate code snippet on a keyword feature, wherein the keyword feature is obtained by using keywords that meet preset requirements extracted from the natural language question text and the rewritten question text;
[0033] The candidate code snippets are comprehensively ranked using the first ranking score and the second ranking score of each candidate code snippet.
[0034] In a second aspect, a question-answer processing method is provided, which is applied to a cloud server, and the method includes:
[0035] Obtaining a natural language question text from a user terminal;
[0036] Matching the natural language question text in a key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from code snippets included in a code library;
[0037] Constructing a prompt text using the natural language question text and the candidate key information, and inputting the prompt text into a large language model to obtain one or more rewritten question texts for the natural language question text;
[0038] Using the natural language question text and the rewritten question text respectively, matching them in the code library to obtain a target code snippet;
[0039] generating a response to the natural language question text using the target code snippet;
[0040] The reply is returned to the user terminal.
[0041] In a third aspect, a question rewriting device is provided, the device comprising:
[0042] A question acquisition unit configured to acquire a natural language question text;
[0043] A first matching unit is configured to match the natural language question text in a key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from code snippets included in a code library;
[0044] The rewriting processing unit is configured to construct a prompt text using the natural language question text and the candidate key information, and input the prompt text into a large language model to obtain one or more rewritten question texts for the natural language question text, wherein the rewritten question text is used to retrieve code snippets in the code library.
[0045] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored, and when the program is executed by a processor, the steps of the method described in any one of the first and second aspects are implemented.
[0046] In a fifth aspect, an electronic device is provided, including:
[0047] one or more processors; and
[0048] A memory associated with the one or more processors, the memory being used to store program instructions, wherein when the program instructions are read and executed by the one or more processors, the steps of the method described in any one of the first and second aspects are executed.
[0049] In a sixth aspect, a computer program product is provided, comprising a computer program, characterized in that when the computer program is executed by a processor, the steps of the method described in any one of the first and second aspects are implemented.
[0050] According to the specific embodiments provided in this application, this application discloses the following technical effects:
[0051] 1) This application extracts key information from the code snippets contained in the code library to form a key information set, and obtains candidate key information by matching the natural language question text in the key information set, thereby establishing a relationship between the natural language question text and the key information. The large language model can use the candidate key information to accurately rewrite the question, making the rewritten question more easily fit the needs of code retrieval, thereby improving the accuracy of code retrieval and user experience.
[0052] 2) This application uses static analysis tools to parse the code snippets contained in the code library, extracts file names, function names, etc. as key information from the code snippets to construct a key information set, so that the large language model can refer to the information of the code snippet itself to rewrite the natural language question text, so that the rewritten question text can meet the code retrieval requirements.
[0053] 3) This application encodes both the key information and the natural language question text to obtain vector representations, and selects candidate key information by calculating the similarity between the vector representation of the natural language question text and the vector representation of each key information, thereby improving the accuracy of screening candidate key information, and making the rewritten question text more in line with code retrieval requirements.
[0054] 4) This application obtains the prompt text by presetting the prompt template, and by limiting the content of the prompt template, prompts the large language model to perform corresponding operations, thereby improving the efficiency and accuracy of prompt text generation, thereby providing more precise guidance for the work of the large language model, and the rewritten question text essentially narrows the scope of code retrieval, and combined with the accurate rewriting of the large language model, optimizes the efficiency and cost of code retrieval.
[0055] 5) This application matches the natural language question text and the rewritten question text in the code library to obtain the target code fragment, and uses the target code fragment to generate a response to the natural language question text, thereby improving the accuracy of the response.
[0056] 6) The present application obtains a target code snippet by filtering and sorting the candidate code snippets, thereby improving the quality of the target code snippet and thus improving the accuracy of the reply.
[0057] 7) When filtering candidate code snippets, the present application filters out candidate code snippets that do not match the intent category of the natural language question text, thereby improving the matching degree between the candidate code snippets and the natural language question text, and further improving the matching degree between the retrieved target code snippets and the user's true intent, thereby improving the accuracy of the answer.
[0058] 8) When sorting the candidate code segments, in addition to considering the semantic similarity between the candidate code segments and the natural language question text, the present application also considers the similarity in expression between each candidate code segment and the keyword features extracted from the natural language question text and the rewritten question (i.e., the hit status), thereby improving the accuracy of sorting, improving the quality of the target code segments, and thus improving the accuracy of the answers.
[0059] Of course, any product implementing the present application does not necessarily need to achieve all of the advantages described above at the same time. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0061] Figure 1 A system architecture diagram applicable to the embodiments of the present application;
[0062] Figure 2 A flowchart of a method for rewriting a question provided in an embodiment of the present application;
[0063] Figure 3 A schematic diagram of the principle of the question rewriting method provided in an embodiment of the present application;
[0064] Figure 4 A schematic diagram of the principle of a method for generating a reply using a rewritten question text provided in an embodiment of the present application;
[0065] Figure 5 A schematic block diagram of a question rewriting device provided in an embodiment of the present application;
[0066] Figure 6 A schematic block diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0067] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. All other embodiments obtained by ordinary technicians in this field based on the embodiments in the present application belong to the scope of protection of this application.
[0068] The terms used in the embodiments of the present invention are only for the purpose of describing specific embodiments, and are not intended to limit the present invention. The singular forms "a", "said" and "the" used in the embodiments of the present invention and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings.
[0069] It should be understood that the term "and / or" used in this article is only a description of the association relationship of associated objects, indicating that there can be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / " in this article generally indicates that the associated objects before and after are in an "or" relationship.
[0070] The word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining" or "in response to detecting", depending on the context. Similarly, the phrases "if it is determined" or "if (stated condition or event) is detected" may be interpreted as "when it is determined" or "in response to determining" or "when detecting (stated condition or event)" or "in response to detecting (stated condition or event)", depending on the context.
[0071] At present, there are some technologies that first rewrite the user's natural language question text, and then perform code retrieval based on the rewritten question text. However, the rewriting method used is a rule-based rewriting method, such as performing simple rewriting such as synonym expansion, person conversion, and part of speech conversion on some words in the user's natural language question text. This rewriting method satisfies the capture of the real content in the code base, but still cannot achieve accurate code retrieval.
[0072] In view of this, the present application provides a new idea. In order to facilitate the understanding of the present application, the system architecture on which the present application is based is first described. Figure 1 An exemplary system architecture to which the embodiments of the present application can be applied is shown. Figure 1 As shown in , the system architecture may include: a user terminal and a question rewriting device located on the server side, a code library, and a large language model.
[0073] The user can send the user question to the server through the user terminal, and the server uses the user question to obtain the natural language question text corresponding to the user question, wherein the user terminal may include but is not limited to: smart mobile terminal, smart home device, wearable device, PC (Personal Computer), etc. Among them, smart mobile devices may include such as mobile phones, tablet computers, laptops, PDAs (Personal Digital Assistants), Internet cars, etc. Smart home devices may include smart TVs, smart refrigerators, etc. Wearable devices may include such as smart watches, smart glasses, virtual reality devices, augmented reality devices, mixed reality devices (i.e., devices that can support virtual reality and augmented reality), etc.
[0074] The question rewriting device can use the method provided in the embodiment of the present application to generate a rewritten question text. The question rewriting device can be set as an independent server, or can be set in a server group, or can be set in a cloud server. A cloud server, also known as a cloud computing server or cloud host, is a host product in a cloud computing service system to solve the problems of difficult management and weak service scalability in traditional physical hosts and virtual private servers (VPS). Figure 1 In addition to the illustrated architecture, the question rewriting device may also be disposed on a computer terminal with relatively strong computing capabilities.
[0075] As one of the achievable methods, a user may input a natural language question text through a user terminal, and the user terminal sends the natural language question text to a question rewriting device on a server through a network. The question rewriting device rewrites the natural language question text to obtain one or more rewritten question texts. The question rewriting device utilizes a large language model in the process of question rewriting.
[0076] After obtaining the rewritten question text, a question-answering processing device (not shown in the figure) can use the natural language question text and the rewritten question text to match in the code library to obtain a target code snippet. The target code snippet and the natural language question text are provided to the large language model, and the large language model generates a reply to the natural language question text; the reply is returned to the user terminal.
[0077] In another achievable manner, the question rewriting device can be used as a part of the question and answer processing device.
[0078] It should be understood that Figure 1 The user terminals, question rewriting devices, code bases, and large language models are merely illustrative. Depending on the implementation requirements, there may be any number of user terminals, question rewriting devices, code bases, and large language models.
[0079] Figure 2 A flowchart of a method for rewriting a question provided in an embodiment of the present application. The method can be performed by Figure 1 The problem rewriting device in the system shown is executed. Figure 2 As shown in , the method may include the following steps:
[0080] Step 201, obtaining natural language question text.
[0081] Step 202 , matching the natural language question text with the key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from the code snippets included in the code library.
[0082] Step 203, construct a prompt text using the natural language question text and the candidate key information, and input the prompt text into a large language model to obtain one or more rewritten question texts for the natural language question text.
[0083] As can be seen from the above process, this application forms a key information set by extracting key information from the code snippets contained in the code library, and obtains candidate key information by matching the natural language question text in the key information set, thereby establishing a relationship between the natural language question text and the key information. The large language model can use the candidate key information to accurately rewrite the question, making the rewritten question more easily fit the needs of code retrieval, thereby improving the accuracy of code retrieval and user experience.
[0084] In addition, this method does not require manual writing and updating of rewriting rules, and can automatically adapt to new code data, which is more efficient and less costly.
[0085] The following is a detailed description of each step in the above process and the effects that can be further produced in conjunction with the embodiments. It should be noted that the "first" and "second" limitations involved in the present disclosure do not have limitations in terms of size, order, and quantity, and are only used to distinguish them in name, for example, "first ranking score" and "second ranking score" are used to distinguish two scores in name.
[0086] First, the above step 201, namely "obtaining natural language question text", is described in detail in conjunction with the embodiment.
[0087] Users send natural language question texts from their user terminals to the server according to their own needs. For example, when users want to quickly understand the basic information of a code-based project, the natural language question texts they raise may be: "What does this project do?", "What modules does this project have?", "What is the content of the unit test for XX function in this project?", etc.
[0088] The natural language question text can be obtained in a variety of ways. For example, a dialog box can be provided to the user by the user terminal, and the user terminal obtains the natural language question text by obtaining the content entered by the user in the dialog box. The user terminal can also push a voice input box to the user, and the user inputs a voice signal indicating his or her needs. The user terminal parses the voice signal and converts it into a text format to obtain the natural language question text. The user terminal can also provide the user with multiple page components, each of which corresponds to a different natural language question text, and the user inputs the natural language question text corresponding to the page component by triggering the page component.
[0089] The above step 202, namely "matching the natural language question text in the key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from the code snippets included in the code library", is described in detail below in conjunction with an embodiment.
[0090] In the present application, the key information set includes key information, and the key information is extracted from the code snippets contained in the code library. The code library includes code snippets obtained after parsing the code and extracting the snippets. For example, the code snippets extracted from a project can be stored in a code library. When the user asks questions about the project, the code library corresponding to the project can be retrieved and matched. For example, Tree-sitter is a lightweight parsing tool for parsing code. It uses context-free grammar to build syntax trees. These syntax trees can be used for editor extensions, code analysis tools, and any scenarios that require code parsing. Tree-sitter can be used to parse the syntax tree of the project file, extract code snippets at the class and method levels, and obtain a code library.
[0091] Key information refers to information indicating characteristics of the code snippet such as type and content, and generally includes file names, function names, keywords, version information, test information, etc. involved in the code snippet.
[0092] Among them, the key information set can be constructed in many ways. For example, write regular expressions to match and extract specific patterns in code snippets, such as specific function names, variable names, comment formats, etc., use string processing functions to parse and extract comments, document strings or other text information in code snippets, and use the extracted information to form a key information set.
[0093] As a preferred implementation, a static analysis tool is used to parse the code snippets contained in the code base to extract key information to form a key information set, wherein the key information includes at least one of a file name, a function name, a keyword, and context information.
[0094] A static analysis tool is a computer program that analyzes source code or binary code without executing the code to check for potential errors, code quality issues, security vulnerabilities, code style issues, etc. Static analysis tools are usually used in the software development process to help developers find and solve problems during the code writing stage, thereby improving software quality and development efficiency. Common static analysis tools include Coverity, ESLint (Error-to-Lint), SonarQube, and AST (Abstract Syntax Tree). Taking AST as an example, AST is a tree data structure that represents the structure of the code. AST is traversed to extract key information, such as function parameters, return value type, control flow structure, etc., and a key information set is constructed from the extracted key information. This application can use AST to conduct in-depth analysis of code snippets in the code base, extracting the file names, function names, keywords and their context information involved.
[0095] Figure 3A schematic diagram of the principle of the question rewriting method provided in the embodiment of the present application, such as Figure 3 As shown, the present application matches the natural language question text in the key information set on the basis of obtaining the key information set to obtain more than one candidate key information. It should be noted here that since the key information set usually corresponds to the code library, before matching, it is necessary to first determine the target code library for the user's natural language question text, and then match it in the key information set corresponding to the target code library. Among them, specific input components can be provided to users for different code libraries. After the user enters the natural language question text in a specific input component, the code library corresponding to the input component is determined to be the target code library. Alternatively, the default code library can also be used as the target code library. If the user does not explicitly specify the target code library, the default code library is used as the target code library. Alternatively, the target code library can also be determined from the semantics expressed in the natural language question text input by the user. For example, if the user specifies a project or a code library in the natural language question text, the code library corresponding to the specified project can be used as the target code library or the specified code library can be used as the target code library.
[0096] As some feasible ways, we can use TF-IDF (Term Frequency-Inverse Document Frequency), TextRank and other algorithms to extract keywords from natural language question texts and match them with keywords of key information in the key information set. The matching process can be simple string matching or fuzzy matching algorithms (such as Levenshtein distance, Jaccard similarity, etc.) to handle incomplete matching.
[0097] As a more preferred implementation, each key information in the key information set can be encoded separately to obtain a vector representation of each key information; the natural language question text can be encoded to obtain a vector representation of the natural language question text; the vector representation of the natural language question text and the vector representation of each key information are similarity calculated, and the key information whose corresponding similarity meets the similarity requirement is used as candidate key information. Specifically, deep learning models such as BERT (Bidirectional Encoder Representation from Transformers) and GPT (Generative Pre-Training) can be used to vectorize natural language question text and key information and convert them into high-dimensional semantic vectors.
[0098] The number of candidate key information is more than one, and the similarity requirement can be, for example: the similarity exceeds a preset similarity threshold, or the key information ranked in the top N positions in similarity is used as the candidate key information, where N is a preset positive integer. In other words, the key information whose similarity exceeds the threshold is used as the candidate key information; or the required candidate key information is limited to N, the key information is ranked according to the similarity, and the key information ranked in the top N positions in similarity is used as the candidate key information.
[0099] The above step 203, namely "using the natural language question text and the candidate key information to construct a prompt text, and inputting the prompt text into the large language model to obtain one or more rewritten question texts for the natural language question text" is described in detail below in conjunction with the embodiments.
[0100] This application uses a large language model to rewrite natural language question text. The prompt text (Prompt) input by the large language model is constructed according to the natural language question text and candidate key information. The prompt text is input into the large language model to obtain one or more rewritten question texts for the natural language question text.
[0101] Large language model, or LLM, refers to a deep learning model trained with a large amount of text data that can generate natural language text or understand the meaning of language text. It is characterized by its large scale and huge number of parameters (usually reaching more than 10 billion), and is usually based on deep learning architectures such as Transformer architectures. The difference between LLM and ordinary pre-trained language models lies in the parameter scale. When the parameter scale exceeds a certain level, the model achieves significant performance improvements and exhibits capabilities that do not exist in small models, such as in-context learning capabilities, which can learn complex patterns in language and perform a wide range of tasks, including text summarization, translation, sentiment analysis, multi-round dialogue, etc. Therefore, in order to distinguish it from traditional pre-trained language models, such models with a parameter scale exceeding a certain level are called LLMs. In general, language models with a parameter scale of more than 10 billion based on deep learning architectures can be considered large language models. Common LLMs include: GTP-3 (Generative Pre-trained Transformer 3), T5 (Text-to-Text Transfer Transformer), GTP-4, PaLM (a large language model proposed by Google), LLaMA (Large Language Model Meta AI, a large language model released by Meta AI), and so on.
[0102] In a large language model, prompt text is input text used to guide the model to generate specific types of text or perform specific tasks. Prompt text can be generated in a variety of ways. It can be directly concatenated from natural language question text and candidate key information, or it can be generated using predefined templates or rules.
[0103] As a preferred implementation, the natural language question text and the candidate key information are filled into a preset prompt template to obtain the prompt text. The prompt template describes the structure and content of the required prompt text. The content of the prompt template may include: instructions for prompting the large language model to select key information from the candidate key information to rewrite the natural language question text.
[0104] Furthermore, the prompt template may also include: an instruction for prompting the large language model to output selected key information and selection reasons. The key information and selection reasons of the large language model output are obtained.
[0105] For example, the prompt template content is as follows:
[0106] "From now on, you are a code analyst. I will give you a problem and a module table for a project. You need to rewrite the user's non-standard questions with standardized words in the module table to facilitate subsequent retrieval.
[0107] You need to first analyze which modules in these module tables are relevant to the user's questions, and then select the words that are most suitable for translating and rewriting the user's questions.
[0108] Then you need to translate the question into English and rewrite it without changing the meaning of the question so that more search engine results can be recalled. You need to follow the following rules:
[0109] 1. You need to select words related to user questions from the module table I provide, and then use these words for subsequent translation and rewriting.
[0110] 2. In the rewriting stage, you need to rewrite the question without changing the meaning of the sentence, such as using different synonyms and antonyms. You should rewrite meaningful verbs and noun entities as much as possible, such as: search->query, module->Component. However, when rewriting, you cannot use words that have nothing to do with the original meaning. You can only choose synonyms or antonyms.
[0111] 3. Your results must be in English.
[0112] 4. Your results need to be returned in JSON format. Your returned results are divided into three parts:
[0113] -First, explain why you chose the words you want to use (when explaining, all quotation marks must be ", not ')
[0114] -Then return to the vocabulary you choose for translation and rewriting (the vocabulary must be in the module table, not other words. You cannot add - to the words, change the case, tense, or part of speech. Please be sure to strictly follow this, otherwise the consequences will be serious.)
[0115] -Return a new question with the vocabulary of your choice. (You must use all the vocabulary of your choice to rewrite this question. Please be sure to strictly follow this, otherwise the consequences will be serious.)
[0116] The returned format is as follows:
[0117] ```json
[0118] {"reason":"","choose_words":[""],"rewrite":""}
[0119] ```"'+f"'
[0120] Here is the module:
[0121] ```Module table
[0122] {module_list}
[0123] ```
[0124] Here are the problems:
[0125] ```Problem
[0126] {question}
[0127] ```
[0128] I would like to emphasize again that the vocabulary must be from the module table and not from other places. You cannot add - to words, capitalize them, or change tense or part of speech. Please strictly follow this, otherwise the consequences will be serious. "
[0129] In the above prompt template, the first six lines indicate the task content of the large language model, and the latter part restricts the rules that the large language model needs to follow when performing tasks. {question} and {module_list} are fillable slots. The natural language question text can be filled into the {question} part of the template. The module table refers to the key information library, and the candidate key information is filled into the {module_list} part. The above prompt template also limits the output content of the large language model to {"reason":"","choose_words":[""],"rewrite":""}. Among them, reason represents the reason for selecting specific key information among the candidate key information, choose_words represents the selected key information, and rewrite represents the rewritten question text.
[0130] The question rewriting method provided in this application is mainly used for code retrieval. Users can quickly find corresponding code snippets by asking questions, or users can obtain relevant answers to the target project code by asking questions. The process of obtaining relevant answers also involves the retrieval of code snippets.
[0131] Figure 4 A schematic diagram of the principle of the method for generating a reply using a rewritten question text provided in an embodiment of the present application, such as Figure 4 As shown, after obtaining the rewritten question text, the natural language question text and the rewritten question text can be used to match in the code base to obtain the target code snippet, and the target code snippet is used to generate a response to the natural language question text.
[0132] The natural language question text and the rewritten question text are used to match in the code base to obtain the target code snippet. Specifically, the natural language question text is matched in the code base to obtain a matching result, and the rewritten question text is also matched in the code base to obtain a matching result. The above two matching results are integrated, and the integrated result is the target code snippet. As an implementable method, the natural language text, the rewritten question text and the code snippet can all be encoded to obtain their corresponding vector representations. The vector similarity between the natural language question text and the code snippet, and between the rewritten question text and the code snippet is calculated using methods such as cosine similarity or Euclidean distance, and multiple code snippets that meet the similarity requirements are integrated to obtain the target code snippet.
[0133] As another feasible method, a pre-trained machine learning model may be used for matching, by respectively inputting the natural language question text and the rewritten question text into the machine learning model, and having the model output the matched target code snippet.
[0134] As a more preferred implementation, the natural language question text and the rewritten question text can be used to match in the code library to obtain candidate code snippets; the candidate code snippets can be filtered and sorted; and the target code snippet can be determined from the candidate code snippets based on the processing results.
[0135] Among them, multiple rules can be used to filter the candidate code snippets, such as filtering out code snippets with too low version information, or filtering out code snippets with potential security vulnerabilities (such as unsafe function calls, inappropriate error handling).
[0136] As a more preferred embodiment, filtering the candidate code snippets includes performing intent recognition on the natural language question text. The intent category corresponding to the natural language question text is obtained through intent recognition, and the candidate code snippets that do not match the intent category in the candidate code snippets are filtered out. Intent recognition refers to identifying the intent of the question raised by the user to perform subsequent code snippet filtering operations. Intent recognition is to classify intent, and intent recognition can be implemented based on preset rules, for example, intent can be determined based on keywords, phrase matching or syntactic analysis. It can also be implemented using classification models in machine learning, such as support vector machines, decision trees, etc., to identify intent by extracting features and training models. Intent categories can include at least one of the following: writing or viewing unit test information; obtaining data structure or class definitions; querying configuration item information; obtaining query request and response processing code information; querying general questions; and so on. Intent categories mainly reflect the purpose of code retrieval, or what kind of answer the user wants to get.
[0137] As some feasible ways, multiple rules can be used to sort the candidate code snippets. For example, the code snippets can be sorted by usage frequency, with frequently used code snippets placed at the front; the code snippets can be sorted by semantic similarity between the code snippets and the natural language text, with the code snippets with high similarity placed at the front; the code snippets can also be sorted by access level, such as public at the front and private at the back, etc.
[0138] However, as a more preferred implementation, the candidate code snippets may be ranked as follows: based on the semantic similarity between each candidate code snippet and the natural language question text, a first ranking score of each candidate code snippet is determined. Based on the hit status of each candidate code snippet on the keyword feature, a second ranking score of each candidate code snippet is determined. The candidate code snippets are ranked comprehensively using the first ranking score and the second ranking score of each candidate code snippet. Based on the result of the comprehensive ranking, a target code snippet is selected.
[0139] Among them, the first ranking score is obtained based on the semantic similarity between each candidate code snippet and the natural language question text. It can be obtained by directly calculating the similarity between the vectors according to the vector representation of the natural language text, the rewritten question text and the code snippet. For example, a mapping relationship between the similarity and the first ranking score is set, and the first ranking score is obtained according to the similarity. A rerank model can also be used to score and sort the result set again in a more complex and computationally intensive manner based on the above similarity, in order to obtain a more accurate and personalized final result. The rerank model is a model used in a variety of machine learning tasks, especially in the field of natural language processing. The core purpose of the rerank model is to rerank a set of candidate outputs in order to find outputs that meet specific criteria. The core principles of the rerank model include calculating the relevance score between the natural language text, the rewritten question text and each candidate code snippet, and then returning a list of candidate code snippets sorted from high to low in terms of relevance.
[0140] The second ranking score is determined based on the hit status of each candidate code snippet on the keyword feature, wherein the keyword feature is obtained by using keywords that meet preset requirements extracted from the natural language question text and the rewritten question text.
[0141] Keywords can be extracted by means of a pre-trained machine learning model, or the part of speech of each word in the natural language question text and the rewritten question text can be marked, and several types of nouns, verbs, adjectives, etc. can be selected as keywords as needed. As a preferred embodiment, keywords can be extracted from the natural language question text and the rewritten question text, and the words whose occurrence frequency is greater than or equal to the preset frequency threshold are used as keywords. For example, after the natural language question text and the rewritten question text are subjected to word segmentation processing, keywords with an occurrence frequency exceeding 2 are extracted.
[0142] After extracting the keywords, keyword features can be directly extracted from the keywords. The extracted keywords can also be rewritten to obtain rewritten keywords, and keyword features can be obtained using the extracted keywords and the rewritten keywords. Keyword features are some representative keywords among all keywords.
[0143] There are many ways to rewrite the extracted keywords. For example, the large language model can be used to perform some extended and inferred rewrites on the keywords. For example, when the keyword is 'search', the keyword rewritten by the large language model is 'query'; the traditional natural language processing method can also be used to rewrite the keywords in terms of part of speech, morphology, person, and tense conversion. For example, when the keyword is 'running', the rewritten keyword is 'ran'; the keywords can also be rewritten according to the vocabulary. This method rewrites the keywords in terms of some expressions that are commonly used by programmers during the development process. For example, when the keyword is 'document', the rewritten keyword is 'doc'.
[0144] The second ranking score is determined based on the hit status of each candidate code segment to the keyword feature. For example, the higher the probability that the candidate code segment hits the keyword feature, the higher the second ranking score.
[0145] As a preferred implementation, the second ranking score of the candidate code snippet can be determined based on the position of the candidate code snippet hitting the keyword feature and the weight of the hit keyword feature relative to the natural language question text. Specifically, a keyword feature may appear in multiple positions in the code block, including but not limited to: file name, package reference, class naming, function naming, variable naming, call, etc. The position where the keyword feature appears to some extent indicates its relevance to the candidate code snippet. At the same time, the weight of the keyword feature relative to the natural language question text can also reflect the importance of the keyword to the question text. A linear weighted scoring formula can be calculated based on experimental experience, regression test results, etc., and the second ranking score is determined by comprehensively considering factors such as the weight and hit position of the keyword feature relative to the natural language question text. For the calculation of weights, the Rerank model can be used to calculate the word weight of the keyword feature relative to the user's original question. It is also possible to eliminate a portion of low-scoring keyword features according to certain rules, such as by average score, to ensure the quality of the keyword features.
[0146] After obtaining the first ranking score and the second ranking score, the candidate code fragments are comprehensively ranked using the first ranking score and the second ranking score of each candidate code fragment. The comprehensive ranking can be ranked according to the score directly added together with the first ranking score and the second ranking score, or the first ranking score and the second ranking score can be ranked after obtaining the comprehensive score using a certain calculation method, such as weighted summation or defining a nonlinear function.
[0147] Based on the comprehensive ranking results, the target code snippet is selected. A quantity threshold M can be set to select the top M candidate code snippets in the comprehensive ranking as the target code snippet, where M is a preset positive integer.
[0148] After obtaining the target code snippet, the target code snippet is used to generate a response to the natural language question text. The target code snippet, the natural language question text, and the rewritten question text can be input into the large language model, and the large language model generates a response to the natural language question text. At this time, the large language model can refer to the rewritten question text and the target code snippet when generating the response, and generate a response with a higher accuracy rate.
[0149] In addition, due to the "black box" nature of large models, they often lack explainability. During the code retrieval stage, this application first uses candidate key information to rewrite questions, which can provide clear rewriting directions and prompts, thereby helping users and developers to better understand the rewriting process and response results.
[0150] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0151] According to an embodiment of another aspect, a question rewriting device is provided. Figure 5 A schematic block diagram of a question rewriting device according to an embodiment is shown, wherein the device is arranged at Figure 1 The server side of the architecture shown in Figure 1. Figure 5 As shown, the device 500 includes: a question acquisition unit 501, a first matching unit 502 and a rewriting processing unit 503, and may further include: a second matching unit 504 and a reply generation unit 505. The main functions of each component unit are as follows:
[0152] The question acquisition unit 501 is configured to acquire a natural language question text.
[0153] The first matching unit 502 is configured to match the natural language question text in the key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from the code snippets included in the code library.
[0154] The rewriting processing unit 503 is configured to construct a prompt text using the natural language question text and the candidate key information, and input the prompt text into the large language model to obtain one or more rewritten question texts for the natural language question text.
[0155] As one of the achievable methods, the first matching unit 502 can be configured to use a static analysis tool to parse the code snippets contained in the code library to extract key information to form a key information set; wherein the key information includes at least one of a file name, a function name, a keyword, and context information.
[0156] As one of the achievable methods, the first matching unit 502 can be configured to encode each key information in the key information set respectively to obtain a vector representation of each key information; match the natural language question text in the key information set to obtain one or more candidate key information including: encoding the natural language question text to obtain a vector representation of the natural language question text; calculating the similarity between the vector representation of the natural language question text and the vector representation of each key information, and taking the key information whose corresponding similarity meets the similarity requirement as the candidate key information.
[0157] As one of the feasible ways, when constructing prompt text using natural language question text and candidate key information, the rewriting processing unit 503 can be configured as follows: fill the natural language question text and candidate key information into a preset prompt template to obtain prompt text; wherein the prompt template includes: instructions for prompting the large language model to select key information from the candidate key information to rewrite the natural language question text.
[0158] As one of the implementable ways, the prompt template also includes: instructions for prompting the large language model to output key information and selection reasons; the rewriting processing unit 503 can be configured to obtain the key information and selection reasons of the large language model output.
[0159] As one achievable manner, the second matching unit 504 may be configured to: use the natural language question text and the rewritten question text to perform matching in the code base to obtain the target code snippet.
[0160] The answer generation unit 505 may be configured to generate an answer to the natural language question text using the target code snippet.
[0161] As one of the feasible ways, the second matching unit 504 can be specifically configured as follows: using the natural language question text and the rewritten question text respectively to match in the code library to obtain candidate code snippets; performing at least one of filtering and sorting on the candidate code snippets; and determining the target code snippet from the candidate code snippets based on the processing results.
[0162] As one of the achievable methods, the second matching unit 504 can be configured to perform intent recognition on the natural language question text when filtering the candidate code snippets, and obtain the intent category corresponding to the natural language question text; and filter out the candidate code snippets that do not match the intent category.
[0163] As one of the feasible ways, when sorting the candidate code fragments, the second matching unit 504 can be configured as follows: based on the semantic similarity between each candidate code fragment and the natural language question text, determine the first sorting score of each candidate code fragment; based on the hit status of each candidate code fragment for the keyword feature, determine the second sorting score of each candidate code fragment, wherein the keyword feature is obtained by using the keywords that meet the preset requirements extracted from the natural language question text and the rewritten question text; and comprehensively sort each candidate code fragment by using the first sorting score and the second sorting score of each candidate code fragment.
[0164] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment. The device embodiment described above is only schematic, wherein the unit described as a separate component may or may not be physically separated, and the component displayed as a unit may or may not be a physical unit, that is, it may be located in one place, or it may be distributed on multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative work.
[0165] 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 used 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 must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0166] In addition, an embodiment of the present application further provides a computer-readable storage medium on which a computer program is stored, and when the program is executed by a processor, the steps of any one of the methods in the aforementioned method embodiments are implemented.
[0167] And an electronic device, comprising:
[0168] one or more processors; and
[0169] A memory associated with the one or more processors, the memory being used to store program instructions, wherein the program instructions, when read and executed by the one or more processors, execute the steps of the method described in any one of the aforementioned method embodiments.
[0170] The present application also provides a computer program product, including a computer program, which implements the steps of any one of the methods in the aforementioned method embodiments when executed by a processor.
[0171] in, Figure 6 The architecture of the electronic device is shown as an example, which may include a processor 610, a video display adapter 611, a disk drive 612, an input / output interface 613, a network interface 614, and a memory 620. The processor 610, the video display adapter 611, the disk drive 612, the input / output interface 613, the network interface 614, and the memory 620 may be communicatively connected via a communication bus 630.
[0172] Among them, the processor 610 can be implemented by a general-purpose CPU, a microprocessor, an application-specific integrated circuit (ASIC), or one or more integrated circuits, etc., to execute relevant programs to implement the technical solutions provided in this application.
[0173] The memory 620 can be implemented in the form of ROM (Read Only Memory), RAM (Random Access Memory), static storage device, dynamic storage device, etc. The memory 620 can store an operating system 621 for controlling the operation of the electronic device 600, and a basic input and output system (BIOS) 622 for controlling the low-level operation of the electronic device 600. In addition, a web browser 623, a data storage management system 624, and a question rewriting device 625, etc. can also be stored. The above-mentioned question rewriting device 625 can be an application program that specifically implements the operations of the aforementioned steps in the embodiment of the present application. In short, when the technical solution provided by the present application is implemented by software or firmware, the relevant program code is stored in the memory 620 and is called and executed by the processor 610.
[0174] The input / output interface 613 is used to connect the input / output module to realize information input and output. The input / output module can be configured in the device as a component (not shown in the figure), or it can be externally connected to the device to provide corresponding functions. The input device may include a keyboard, a mouse, a touch screen, a microphone, various sensors, etc., and the output device may include a display, a speaker, a vibrator, an indicator light, etc.
[0175] The network interface 614 is used to connect to a communication module (not shown) to realize communication interaction between the device and other devices. The communication module can realize communication through a wired mode (such as USB, network cable, etc.) or a wireless mode (such as mobile network, WIFI, Bluetooth, etc.).
[0176] The bus 630 comprises a pathway for transmitting information between the various components of the device (eg, the processor 610, the video display adapter 611, the disk drive 612, the input / output interface 613, the network interface 614, and the memory 620).
[0177] It should be noted that, although the above device only shows a processor 610, a video display adapter 611, a disk drive 612, an input / output interface 613, a network interface 614, a memory 620, a bus 630, etc., in the specific implementation process, the device may also include other components necessary for normal operation. In addition, it can be understood by those skilled in the art that the above device may also only include components necessary for implementing the solution of the present application, and does not necessarily include all the components shown in the figure.
[0178] It can be known from the description of the above implementation methods that those skilled in the art can clearly understand that the present application can be implemented by means of software plus a necessary general hardware platform. Based on such an understanding, the technical solution of the present application can be essentially or partly contributed to the prior art in the form of a computer program product, which can be stored in a storage medium such as ROM / RAM, a magnetic disk, an optical disk, etc., and includes several instructions for enabling a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in the various embodiments of the present application or certain parts of the embodiments.
[0179] The technical solution provided by the present application is described in detail above. The principle and implementation method of the present application are described in detail using specific examples. The description of the above embodiments is only used to help understand the method and core idea of the present application. At the same time, for those skilled in the art, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as limiting the present application.
Claims
1. A method for question rewriting, characterized in that: The method comprises: Get natural language question text; Matching the natural language question text in a key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from code snippets included in a code library; A prompt text is constructed using the natural language question text and the candidate key information, and the prompt text is input into a large language model to obtain one or more rewritten question texts for the natural language question text. The rewritten question text is used to retrieve code snippets in the code library.
2. The method according to claim 1, characterized in that The method further comprises: Using a static analysis tool to parse the code snippets contained in the code library to extract key information to form the key information set; The key information includes at least one of a file name, a function name, a keyword, and context information.
3. The method according to claim 1, characterized in that: The method further includes: encoding each key information in the key information set to obtain a vector representation of each key information; Matching the natural language question text in a key information set to obtain one or more candidate key information includes: encoding the natural language question text to obtain a vector representation of the natural language question text; calculating the similarity between the vector representation of the natural language question text and the vector representation of each key information, and taking the key information whose similarity meets the similarity requirement as the candidate key information.
4. The method according to claim 1, characterized in that: Using the natural language question text and the candidate key information to construct a prompt text includes: Filling the natural language question text and the candidate key information into a preset prompt template to obtain the prompt text; The prompt template includes: an instruction to prompt the large language model to select key information from the candidate key information to rewrite the natural language question text.
5. The method according to claim 4, characterized in that The prompt template also includes: instructions for prompting the large language model to output selected key information and selection reasons; The method further includes: obtaining key information of the selection and the reason for the selection output by the large language model.
6. The method according to any one of claims 1 to 5, characterized in that The method further comprises: Using the natural language question text and the rewritten question text respectively, matching them in the code library to obtain a target code snippet; A response to the natural language question text is generated using the target code snippet.
7. The method according to claim 6, characterized in that Using the natural language question text and the rewritten question text respectively, matching in the code library to obtain a target code snippet includes: Using the natural language question text and the rewritten question text respectively, matching them in the code library to obtain candidate code snippets; Perform at least one of filtering and sorting the candidate code snippets; A target code segment is determined from the candidate code segments based on the processing result.
8. The method according to claim 7, characterized in that Filtering the candidate code snippets includes: Performing intent recognition on the natural language question text to obtain an intent category corresponding to the natural language question text; Filter out candidate code snippets that do not match the intent category among the candidate code snippets.
9. The method according to claim 7, characterized in that: Sorting the candidate code snippets includes: Determining a first ranking score for each candidate code snippet based on the semantic similarity between each candidate code snippet and the natural language question text; Determining a second ranking score for each candidate code snippet based on a hit status of each candidate code snippet on a keyword feature, wherein the keyword feature is obtained by using keywords that meet preset requirements extracted from the natural language question text and the rewritten question text; The candidate code snippets are comprehensively ranked using the first ranking score and the second ranking score of each candidate code snippet.
10. A question-answering processing method, applied to a cloud server, characterized in that: The method comprises: Obtaining a natural language question text from a user terminal; Matching the natural language question text in a key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from code snippets included in a code library; Constructing a prompt text using the natural language question text and the candidate key information, and inputting the prompt text into a large language model to obtain one or more rewritten question texts for the natural language question text; Using the natural language question text and the rewritten question text respectively, matching them in the code library to obtain a target code snippet; generating a response to the natural language question text using the target code snippet; The reply is returned to the user terminal.
11. A question rewriting device, characterized in that: The device comprises: A question acquisition unit configured to acquire a natural language question text; A first matching unit is configured to match the natural language question text in a key information set to obtain one or more candidate key information, wherein the key information included in the key information set is extracted from code snippets included in a code library; The rewriting processing unit is configured to construct a prompt text using the natural language question text and the candidate key information, and input the prompt text into a large language model to obtain one or more rewritten question texts for the natural language question text, wherein the rewritten question text is used to retrieve code snippets in the code library.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps of the method according to any one of claims 1 to 10 are implemented.
13. An electronic device, characterized in that: include: one or more processors; as well as A memory associated with the one or more processors, the memory being used to store program instructions, wherein the program instructions, when read and executed by the one or more processors, execute the steps of the method described in any one of claims 1 to 10.
14. A computer program product comprising a computer program, 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 10 are implemented.
Citation Information
Cited By
Data construction method, code question-answering method, task platform and code question-answering system
CN119149712A
Method and device for performing context retrieval based on code repository
CN119883276A
A method and apparatus for context retrieval based on code repositories
CN119883276B
Code generation method and device, storage medium and electronic equipment
CN120447880A
Code generation method and device, storage medium and electronic device
CN120447880B
