Code generation method, device, electronic device and storage medium
By obtaining the basic function library and large-scale language model, using prompt words to generate an extended function library and optimize the model, the problem of low code generation accuracy of large-scale language models in the vertical field is solved, and the accuracy and efficiency of code generation are improved.
Patent Information
- Application Number
- CN202411436610.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-15
- Publication Date
- 2025-02-14
- Estimated Expiration
- 2044-10-15
AI Technical Summary
Large-scale language models are less accurate when generating code in vertical fields, especially when dealing with complex mathematical problems, making it difficult to generate accurate code.
By obtaining the basic function library and large-scale language model, the first prompt word is used to generate an extended function library, and the second prompt word is optimized to output the target code.
Improve the accuracy and efficiency of code generation of large-scale language models in vertical fields, and enhance their understanding of complex problems and deep logic.
Smart Images

Figure CN118963739B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a code generation method, device, electronic equipment and storage medium. Background Art
[0002] In-context learning (ICL) refers to large-scale language models (LLMs) that understand and generate corresponding answers as outputs by simply observing input and output sample sentences (shots) in context, without prior training. The accuracy of the answer depends on the design of prompts. When the prompts can provide enough information and examples, the model can understand the task and generate accurate output by learning from the examples. When it comes to programming tasks in vertical fields, large-scale language models need to output corresponding programming results. For example, function programming involving complex mathematical problems requires outputting programming results, but because the function itself has deep and complex logic, it is difficult for large-scale language models to learn enough based on examples to generate accurate code as output. Summary of the invention
[0003] The purpose of the embodiments of the present invention is to provide a code generation method, device, electronic device and storage medium to improve the accuracy of code generation by large-scale language models. The specific technical solution is as follows:
[0004] In a first aspect, an embodiment of the present invention provides a code generation method, comprising:
[0005] Acquire a basic function library and a large-scale language model, wherein the basic function library includes functions in the target mathematical field;
[0006] Inputting a first prompt word and the basic function library into a large-scale language model to generate an extended function library for solving problems in the target mathematical field, wherein the first prompt word is used to prompt the large-scale language model to generate an agent corresponding to the target mathematical field;
[0007] The extended function library and the second prompt word are input into the large-scale language model so that when a user inputs a target problem in the target mathematical field, the large-scale language model outputs a target code for solving the target problem, wherein the second prompt word is used to prompt the large-scale language model to output a response in the form of code.
[0008] In one embodiment of the present invention, the step of inputting the first prompt word and the basic function library into a large-scale language model to generate an extended function library for solving problems in the target mathematical field includes:
[0009] Inputting the first prompt word into a large-scale language model so that the large-scale language model generates an agent corresponding to the target mathematical field;
[0010] When the agent runs on the large-scale language model, the basic function library is input into the large-scale language model to generate a sample question library in the target mathematics field;
[0011] An extended function library for solving the sample question library is generated based on the sample question library and the knowledge base carried by the large-scale language model.
[0012] In one embodiment of the present invention, the inputting the extended function library and the second prompt word into the large-scale language model so that when the user inputs a target problem in the target mathematical field, the large-scale language model outputs a target code for solving the target problem, including:
[0013] Encapsulating the extended function library to obtain an interface document, and generating a second prompt word corresponding to the interface document;
[0014] The interface document and the second prompt word are input into the large-scale language model, so that when a user inputs a target problem in the target mathematical field, the large-scale language model outputs a target code for solving the target problem.
[0015] In one embodiment of the present invention, encapsulating the extended function library, obtaining an interface document, and generating a second prompt word corresponding to the interface document includes:
[0016] Obtaining parameters of an original function of a first function and a second function in the extended function library, wherein the first function is a function of a first preset ratio in the extended function library, and the second function is a function of a second preset ratio in the extended function library;
[0017] The original function of the first function and the parameter of the second function are encapsulated to obtain the interface document, and the second prompt word corresponding to the interface document is generated.
[0018] In a second aspect, an embodiment of the present invention provides a code generation device, including:
[0019] A model acquisition module, used to acquire a basic function library and a large-scale language model, wherein the basic function library includes functions in the target mathematical field;
[0020] A function library generation module, used to input a first prompt word and the basic function library into a large-scale language model to generate an extended function library for solving problems in the target mathematical field, wherein the first prompt word is used to prompt the large-scale language model to generate an agent corresponding to the target mathematical field;
[0021] A code output module is used to input the extended function library and the second prompt word into the large-scale language model, so that when the user inputs a target problem in the target mathematical field, the large-scale language model outputs a target code for solving the target problem, wherein the second prompt word is used to prompt the large-scale language model to output a response in the form of code.
[0022] In one embodiment of the present invention, the function library generation module is specifically used to:
[0023] Inputting the first prompt word into a large-scale language model so that the large-scale language model generates an agent corresponding to the target mathematical field;
[0024] When the agent runs on the large-scale language model, the basic function library is input into the large-scale language model to generate a sample question library in the target mathematics field;
[0025] An extended function library for solving the sample question library is generated based on the sample question library and the knowledge base carried by the large-scale language model.
[0026] In one embodiment of the present invention, the code output module includes:
[0027] A function encapsulation submodule, used to encapsulate the extended function library, obtain an interface document, and generate a second prompt word corresponding to the interface document;
[0028] The code output submodule is used to input the interface document and the second prompt word into the large-scale language model, so that when the user inputs the target problem in the target mathematical field, the large-scale language model outputs the target code for solving the target problem.
[0029] In one embodiment of the present invention, the function encapsulation submodule is specifically used for:
[0030] Obtaining parameters of an original function of a first function and a second function in the extended function library, wherein the first function is a function of a first preset ratio in the extended function library, and the second function is a function of a second preset ratio in the extended function library;
[0031] The original function of the first function and the parameter of the second function are encapsulated to obtain the interface document, and the second prompt word corresponding to the interface document is generated.
[0032] In a third aspect, an embodiment of the present invention provides an electronic device, including a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0033] Memory, used to store computer programs;
[0034] The processor is used to implement any of the above-mentioned code generation methods when executing the program stored in the memory.
[0035] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the code generation method described above is implemented.
[0036] An embodiment of the present invention further provides a computer program product comprising instructions, which, when executed on a computer, enables the computer to execute any of the above-mentioned code generation methods.
[0037] Beneficial effects of the embodiments of the present invention:
[0038] The code generation method provided by the embodiment of the present invention obtains a basic function library and a large-scale language model, inputs a first prompt word and the basic function library into the large-scale language model, generates an extended function library for solving problems in a target mathematical field, generates a high-quality extended function library on the basis of the basic function library by constructing the first prompt word, and enriches the diversity of the function library in the knowledge reserve of the large-scale language model; inputs the extended function library and the second prompt word into the large-scale language model, and optimizes the large-scale language model by context learning according to the extended function library without manual re-labeling and adjustment training, thereby improving its ability to understand complex problem prompts and deep-level logic in the target mathematical field, so that the large-scale language model outputs a target code for solving the target problem when the user inputs the target problem in the target mathematical field. The programming efficiency and code quality of the large-scale language model are improved, and the adaptability, generalization and scalability of the large-scale language model in vertical fields are enhanced.
[0039] Of course, it is not necessary to achieve all of the advantages described above at the same time to implement any product or method of the present invention. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present invention, and for ordinary technicians in this field, other embodiments can also be obtained based on these drawings.
[0041] Figure 1 A flowchart of a code generation method provided by an embodiment of the present invention;
[0042] Figure 2 A possible implementation of step S102 provided in an embodiment of the present invention;
[0043] Figure 3 A possible implementation of step S103 provided in an embodiment of the present invention;
[0044] Figure 4 A possible implementation of step S301 provided in an embodiment of the present invention;
[0045] Figure 5-1 An example diagram comparing the accuracy and error rates of direct calculations and function library calculations on matrix problems provided by an embodiment of the present invention;
[0046] Figure 5-2 An example diagram comparing the accuracy and error rates of direct calculation and function library calculation on a polyhedron problem provided by an embodiment of the present invention;
[0047] Figure 5-3 An example diagram comparing the accuracy, error rate and hallucination rate of a function library operation and an interface library operation on matrix and polyhedron problems provided by an embodiment of the present invention;
[0048] Figure 5-4 An example diagram comparing the accuracy, error rate and hallucination rate of function library operations and interface library operations on vector problems provided by an embodiment of the present invention;
[0049] Figure 5-5 An overall example diagram of code generation provided by an embodiment of the present invention;
[0050] Figure 6 A schematic diagram of the structure of a code generation device provided by an embodiment of the present invention;
[0051] Figure 7 A schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0052] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field based on this application belong to the scope of protection of the present invention.
[0053] Since large-scale language models in related technologies have low accuracy when generating codes in vertical fields, in order to solve this problem, the embodiments of the present application provide a code generation method, device, electronic device and storage medium.
[0054] The following is a detailed description of the code generation method provided in the embodiment of the present application. Figure 1 , Figure 1 A flowchart of a code generation method is provided for an embodiment of the present application, including:
[0055] Step S101, obtaining a basic function library and a large-scale language model.
[0056] The basic function library includes functions in the target mathematical field.
[0057] The target mathematical domain refers to the vertical domain in which the generated code is expected to be located, and can be, for example, the field of mathematics (algebra, geometry, probability statistics, practice, etc.), the field of physics, etc. The basic function library includes multiple commonly used functions in the target mathematical domain, such as the Euler function in the field of algebra.
[0058] Large-scale language model refers to GPT (Generative Pre-Trained Transformer), which is a deep learning model for text generation that is trained based on available data on the Internet. Large-scale language models have been pre-trained on massive general data, and their own knowledge base includes knowledge from massive general training data. However, the general data corresponding to the vertical field is relatively simple basic knowledge in the field, while the deep knowledge structure and logical relationship of the vertical field are relatively complex and profound. The corresponding programming tasks often require deep and superimposed reasoning. Even if the large-scale language model has a knowledge base, it is difficult to generate code with expected generalization ability based on contextual prompts. Although GPT4 has expanded the context window to 128K (storage unit), for logically complex codes in vertical fields, even if a large number of code examples are input into the model, it will not be able to understand the relevant complex problems well due to its insufficient understanding of the vertical field, and even cause hallucinations in the model. When it comes to function problems in vertical fields, the answers output by large-scale language models are less accurate.
[0059] Exemplarily, the large-scale language model framework applied in the present invention is implemented based on OpenAI (a public artificial intelligence toolkit), the machine graphics card configuration is NVIDIA GeForce RTX 4090 (24GB) (graphics card configuration model), and the Agent (large-scale language model intelligent agent) is designed using GPT-4o (a version of GPT) as the basic Agent (intelligent agent).
[0060] Step S102: input the first prompt word and the basic function library into a large-scale language model to generate an extended function library for solving problems in the target mathematical field.
[0061] The first prompt word is used to prompt the large-scale language model to generate an agent corresponding to the target mathematical field.
[0062] The first prompt word is input into the large-scale language model so that the large-scale language model generates an agent FuncAgent corresponding to the target mathematical domain. The agent is used to make the large-scale language model currently in the context learning scenario of the target mathematical domain.
[0063] The basic function library is input into the current large-scale language model so that the agent uses the basic function library as a benchmark and, under the influence of the large-scale language model's own knowledge base, generates an extended function library for solving problems in the target mathematical field. The functions in the extended function library have the ability to solve complex logical problems in the target mathematical field.
[0064] Step S103: input the extended function library and the second prompt word into the large-scale language model, so that when the user inputs a target problem in the target mathematical field, the large-scale language model outputs a target code for solving the target problem.
[0065] The second prompt word is used to prompt the large-scale language model to output a response in code form.
[0066] The extended function library is input into the large-scale language model so that the current large-scale language model has the ability to process complex logical problems in the target mathematical field. A second prompt word is input for prompting the large-scale language model to output a code, so that when a user inputs a target problem in the target mathematical field, a target code for solving the target problem is generated as an output with the knowledge support of the extended function library.
[0067] As can be seen from the above, the code generation method provided by the embodiment of the present invention obtains a basic function library and a large-scale language model, inputs the first prompt word and the basic function library into the large-scale language model, generates an extended function library for solving problems in the target mathematical field, and generates a high-quality extended function library on the basis of the basic function library by constructing the first prompt word, enriching the diversity of the function library in the knowledge reserve of the large-scale language model; inputs the extended function library and the second prompt word into the large-scale language model, and optimizes the large-scale language model by context learning according to the extended function library, without manual re-labeling and adjustment training, so as to improve its ability to understand complex problem prompts and deep logic in the target mathematical field, so that the large-scale language model outputs the target code for solving the target problem when the user inputs the target problem in the target mathematical field. The programming efficiency and code quality of the large-scale language model are improved, and the adaptability, generalization and scalability of the large-scale language model in vertical fields are enhanced.
[0068] In one embodiment of the present invention, Figure 2 As shown, the above step S102 inputs the first prompt word and the basic function library into the large-scale language model to generate an extended function library for solving problems in the target mathematical field, including:
[0069] Step S201, inputting the first prompt word into a large-scale language model, so that the large-scale language model generates an agent corresponding to the target mathematics field;
[0070] Step S202, when the agent runs on the large-scale language model, inputting the basic function library into the large-scale language model to generate a sample question library in the target mathematics field;
[0071] Step S203: generating an extended function library for solving the sample question library according to the sample question library and the knowledge base carried by the large-scale language model.
[0072] Exemplarily, the first prompt word may be as follows:
[0073] You are a professional math function question setter (Prompt symbol for contextual learning)
[0074] Your goal is to generate a specific complex problem based on a given function that requires the combination of this function with other related functions to solve.
[0075] The generated problems must have specific numerical values and solutions, and generate executable Python (a common programming language) code based on this method.
[0076] Try to use the interfaces or functions mentioned in the context to solve the problem. If the problem cannot be solved, you can define your own function.
[0077] The format of the answer must be: instruction: question, output: code
[0078] Please note that 'problem' and 'code' do not appear in the generated results.
[0079] When the agent runs on a large-scale language model, it indicates that the large-scale language model is currently in the context of the target mathematical domain. The basic function library f(n)0 is input into the large-scale language model, so that the large-scale language model analyzes the knowledge domain type (specific type of the target mathematical domain) where the basic function library is located, and generates a sample problem library q(n) belonging to the target mathematical domain. The problems included in the sample problem library need to be solved through reasoning through a large amount of deep knowledge such as knowledge structure and logical relationship in this field. Combined with the general data knowledge base and basic function library of the large-scale language model itself, an extended function library f(n) for solving the sample problem library is generated. m .
[0080] As can be seen from the above, the code generation method provided by the embodiment of the present invention uses the basic function library to analyze the target mathematical field and generate a sample problem library in the target mathematical field. The problems included in the sample problem library need to be solved by reasoning through a large amount of deep knowledge such as knowledge structure and logical relationship in this field. An extended function library for solving the sample problem library is generated by combining its own knowledge base and the basic function library, thereby achieving an effective expansion of the function library knowledge of the large-scale language model and improving the ability of the large-scale language model to solve problems in vertical fields. A large-scale language model function agent is constructed through pre-designed prompt words to generate problems in the target mathematical field and extended functions, thereby achieving the purpose of low-cost supplementation of function diversity.
[0081] In one embodiment of the present invention, Figure 3 As shown, the above step S103 inputs the extended function library and the second prompt word into the large-scale language model, so that when the user inputs the target problem in the target mathematical field, the large-scale language model outputs a target code for solving the target problem, including:
[0082] Step S301, encapsulating the extended function library, obtaining an interface document, and generating a second prompt word corresponding to the interface document;
[0083] Step S302: input the interface document and the second prompt word into the large-scale language model, so that when the user inputs a target problem in the target mathematics field, the large-scale language model outputs a target code for solving the target problem.
[0084] As mentioned above, although GPT4 has expanded the context window to 128K (storage unit), for logically complex codes in vertical fields, even if a large number of code examples are input into the model, it will not be able to understand the relevant complex problems well due to its insufficient understanding of the vertical field, and may even cause the model to hallucinate. In the case where the extended function library can improve the model's understanding of the vertical field, in order to minimize the length of the context window of the input model as much as possible, the extended function library is not directly input as a prompt word, but is encapsulated into an interface document, and the second prompt word corresponding to the interface document is generated.
[0085] The interface document and the corresponding second prompt word are input into the large-scale language model, so that the model has the ability to understand complex problems in the target mathematical field, and outputs the target code for solving the target problem when the user inputs the target problem in the target mathematical field.
[0086] As can be seen from the above, the code generation method provided by the embodiment of the present invention encapsulates the extended function library into an interface document, reduces the data storage occupation required by the extended function library, and only inputs the interface document and the corresponding second prompt word into the large-scale language model, so that the model has the ability to understand complex problems in the target mathematical field, so that when the user inputs the target problem in the target mathematical field, the target code for solving the target problem is output. At the same time, the length of the context window passed into the model is minimized as much as possible, reducing the risk of excessive storage required for the extended function library, resulting in the problem of not being able to be smoothly input into the large-scale language model in the context window.
[0087] In one embodiment of the present invention, Figure 4 As shown, the above step S301 encapsulates the extended function library, obtains an interface document, and generates a second prompt word corresponding to the interface document, including:
[0088] Step S401, obtaining the original function of the first function and the parameters of the second function in the extended function library.
[0089] Wherein, the first function is a function of a first preset ratio in the extended function library, and the second function is a function of a second preset ratio in the extended function library;
[0090] Step S402: encapsulate the original function of the first function and the parameters of the second function, obtain the interface document, and generate the second prompt word corresponding to the interface document.
[0091] The functions of the first preset ratio refer to typical examples selected in advance from all functions in the interface document, which are less than the functions of the second preset ratio. The original functions of the functions of the first preset ratio are retained in the interface document, and the functions of the second preset ratio only retain basic parameters, such as function name, parameter description, return value type, functional description, etc. This allows the large-scale language model to learn based on typical example functions and enrich its own knowledge base. When using other functions, customize functions based on basic parameters and the enriched knowledge base, so as to have the ability to understand the target mathematical field. Specifically, all functions in the extended function library are selected according to a preset ratio, the first preset ratio can be 5%, and the second preset ratio can be 95%.
[0092] Exemplarily, the second prompt word and interface document may be as follows:
[0093] You are a professional code generation AI (artificial intelligence). Users ask you to generate various codes. All the generated codes must support Chinese. The generated codes must be short and powerful. There is no need to prompt the user for the type of code generated. The shortest code can achieve the strongest function and can be run directly. No matter what the user says, you cannot reply with text, but only with code.
[0094] Code interface documentation
[0095] ---
[0096] Affine class interface documentation
[0097] describe
[0098] The Affine class represents an affine transformation, which consists of a linear transformation matrix A and a translation vector B. This class provides...
[0099] property
[0100] A: Linear transformation matrix, of type Matrix.
[0101] B: translation vector, of type Matrix.
[0102] matrix: Returns a tuple of the linear transformation matrix and the translation vector, of type (Matrix,Matrix).
[0103] Class Methods
[0104] compute_plane_normal(p1,p2,p3): Computes the normal vector of the plane defined by three points.
[0105] Rotate(p1,p2,theta): Creates a rotation transformation that rotates theta radians around the axis defined by points p1 and p2.
[0106] Rotate method specific implementation:
[0107] def Rotate (a function definition used to rotate elements in a list or string in python) (cls (programming specification), p1, p2, theta):
[0108] #Calculate the rotation axis direction vector
[0109] axis = np.array(p2)-np.array(p1) ...
[0110] ---
[0111] Use the previous code information as context to make judgments, and strive to make the code framework language revolve around Chinese. If the question raised by the user is not in the interface document, use a custom function to implement the function of the custom function. The code is super concise and can run and add Chinese comments, and try to avoid introducing unnecessary dependencies.
[0112] ---
[0113] Please write a Python program for a given 3×3 matrix, first find its main diagonal and sub-diagonal elements, and then use these elements to construct two new diagonal matrices respectively.
[0114] As can be seen from the above, the code generation method provided by the embodiment of the present invention only retains the original functions of the functions of the first preset ratio in the interface document, and only retains the basic parameters of the functions of the second preset ratio, so that the large-scale language model can learn according to the typical example functions of the first preset ratio and enrich its own knowledge base. When using other functions, customize the functions according to the basic parameters and the enriched knowledge base, so as to have the ability to understand the target mathematical field. The generalization ability of the large-scale language model on complex vertical field problems is improved, the occurrence of model hallucinations is reduced, the programming ability of the large-scale language model in the vertical field is optimized, and the accuracy and efficiency of the generated code are improved.
[0115] In one embodiment of the present invention, Figure 5-1 and Figure 5-2As shown in the figure, a verification comparison chart of the data expansion stage is provided. In order to verify the effectiveness of the extended function library, 50 matrix operation questions and 50 polyhedron operation questions in the target mathematics field are constructed using the intelligent agent. The third prompt word is designed to instruct the large-scale language model to calculate the questions through two methods: "direct calculation" (only directly give the answer based on the question) and "function library calculation" (call the extended function library to calculate and give the answer). Among them, Figure 5-1 The comparison of the accuracy and error rates of two different methods, direct operation and function library operation, on matrix problems is shown. Figure 5-2 The comparison of the accuracy and error rates of two different methods, direct calculation and function library calculation, on the polyhedron problem is shown.
[0116] Exemplarily, the third prompt word is as follows:
[0117] You are a professional programmer
[0118] Your goal is to answer the given questions and give the answer directly without giving the calculation process.
[0119] Please add verification code using the problem data to the given program and run it to get the result.
[0120] Do not output the program code before or after modification, just output the verification result. prompt=instructions+" "+"Question:"+item["instruction"]+" "+"Program"+" "+item["output"]
[0121] Example: ### Question generation and answers
[0122] Specific question: Given vector v1 = (1,2,0) and vector v2 = (2,4,0), are they parallel? What is the distance from point (3,4,5) to the Z axis?
[0123] Answer: Vectors v1 and v2 are parallel; the distance from point (3,4,5) to the Z axis is 5.
[0124] ###Verification code and results
[0125] ```Python
[0126] # Create vectors v1 and v2
[0127] v1 = Vector3(1,2,0,'v1')
[0128] v2 = Vector3(2,4,0,'v2')
[0129] # Determine whether vectors v1 and v2 are parallel
[0130] parallel_result = is_parallel(v1,v2)
[0131] print(f"Are vectors v1 and v2 parallel: {parallel_result}")
[0132] #Create point (3,4,5)
[0133] point=Vector3(3,4,5,'point')
[0134] #Calculate the distance from point (3,4,5) to the Z axis
[0135] distance_result = point_to_z_axis_distance(point)
[0136] print(f"The distance from point (3,4,5) to the Z axis: {distance_result}")
[0137] ```
[0138] ### Verify the results
[0139] ```
[0140] Are vectors v1 and v2 parallel: True
[0141] The distance from point (3,4,5) to the Z axis: 5.0
[0142] ```
[0143] like Figure 5-1 and Figure 5-2 As shown in the figure, "direct operation" got 35 and 30 out of 50 matrix problems and 50 polyhedron problems right, respectively, while "library operation" got 42 and 37 out of them right, respectively. That is, the accuracy of "library operation" is significantly higher than that of "direct operation", which proves the effectiveness of the extended function library.
[0144] In order to verify the influence of the interface document and the corresponding second prompt word used in the present invention as the prompt word, and the direct use of the extended function library as the prompt word on the large-scale language model, the model output under the two prompts was evaluated from the three indicators of accuracy, generalization and hallucination.
[0145] The evaluation of three indicators is defined:
[0146] Accuracy: Determine whether the generated code can be executed correctly and output correct results.
[0147] Generalization: Based on the above 100 target mathematics problems, 50 more vector calculation problems were added using the intelligent agent FuncAgent to test the generalization of the two methods: "interface document" (marked as interface library in the figure) and "extended function library".
[0148] Hallucination: Tests whether the code generated by the large-scale language model correctly understands the given extended function library and ensures that the generated code does not contain irrelevant or erroneous content.
[0149] Evaluation results such as Figure 5-3 and Figure 5-4 As shown, a verification comparison chart of the model understanding stage is provided. When the "interface document" method of the present invention is used to generate vertical field code for large-scale language models, it is significantly better than the "function library" method in terms of accuracy, generalization, and ability to handle hallucinations. Among them, Figure 5-3 The comparison of the accuracy, error rate and hallucination rate of two different methods, function library operation and interface library operation (interface document operation), on matrix and polyhedron problems is shown. Figure 5-4 The comparison of the accuracy, error rate and hallucination rate of two different methods, function library operation and interface library operation (interface document operation), on vector problems is shown.
[0150] In one embodiment of the present invention, Figure 5-5 As shown, an overall example diagram of code generation is also provided, including the data expansion stage and the large-scale language model data understanding stage.
[0151] See also Figure 6 The embodiment of the present invention further provides a schematic diagram of the structure of a code generating device, including:
[0152] A model acquisition module 601 is used to acquire a basic function library and a large-scale language model, wherein the basic function library includes functions in the target mathematical field;
[0153] A function library generation module 602 is used to input a first prompt word and the basic function library into a large-scale language model to generate an extended function library for solving problems in the target mathematical field, wherein the first prompt word is used to prompt the large-scale language model to generate an agent corresponding to the target mathematical field;
[0154] The code output module 603 is used to input the extended function library and the second prompt word into the large-scale language model, so that when the user inputs the target problem in the target mathematical field, the large-scale language model outputs the target code for solving the target problem, wherein the second prompt word is used to prompt the large-scale language model to output a response in the form of code.
[0155] As can be seen from the above, the code generation device provided by the embodiment of the present invention obtains a basic function library and a large-scale language model, inputs the first prompt word and the basic function library into the large-scale language model, generates an extended function library for solving problems in the target mathematical field, and generates a high-quality extended function library on the basis of the basic function library by constructing the first prompt word, enriching the diversity of the function library in the knowledge reserve of the large-scale language model; inputs the extended function library and the second prompt word into the large-scale language model, and optimizes the large-scale language model by context learning according to the extended function library, without manual re-labeling and adjustment training, so as to improve its ability to understand complex problem prompts and deep-level logic in the target mathematical field, so that the large-scale language model outputs the target code for solving the target problem when the user inputs the target problem in the target mathematical field. The programming efficiency and code quality of the large-scale language model are improved, and the adaptability, generalization and scalability of the large-scale language model in vertical fields are enhanced.
[0156] In one embodiment of the present invention, the function library generation module 602 is specifically used for:
[0157] Inputting the first prompt word into a large-scale language model so that the large-scale language model generates an agent corresponding to the target mathematical field;
[0158] When the agent runs on the large-scale language model, the basic function library is input into the large-scale language model to generate a sample question library in the target mathematics field;
[0159] An extended function library for solving the sample question library is generated based on the sample question library and the knowledge base carried by the large-scale language model.
[0160] As can be seen from the above, the code generation device provided by the embodiment of the present invention uses the basic function library to analyze the target mathematical field and generate a sample problem library in the target mathematical field. The problems included in the sample problem library need to be solved by reasoning through a large amount of deep knowledge such as knowledge structure and logical relationship in this field. An extended function library for solving the sample problem library is generated by combining its own knowledge base and the basic function library, thereby achieving an effective expansion of the function library knowledge of the large-scale language model and improving the ability of the large-scale language model to solve problems in vertical fields. A large-scale language model function agent is constructed through pre-designed prompt words to generate problems in the target mathematical field and extended functions, thereby achieving the purpose of low-cost supplementation of function diversity.
[0161] In one embodiment of the present invention, the code output module 603 includes:
[0162] A function encapsulation submodule, used to encapsulate the extended function library, obtain an interface document, and generate a second prompt word corresponding to the interface document;
[0163] The code output submodule is used to input the interface document and the second prompt word into the large-scale language model, so that when the user inputs the target problem in the target mathematical field, the large-scale language model outputs the target code for solving the target problem.
[0164] As can be seen from the above, the code generation device provided by the embodiment of the present invention encapsulates the extended function library into an interface document, reduces the data storage occupation required by the extended function library, and only inputs the interface document and the corresponding second prompt word into the large-scale language model, so that the model has the ability to understand complex problems in the target mathematical field, so that when the user inputs the target problem in the target mathematical field, the target code for solving the target problem is output. At the same time, the length of the context window passed into the model is minimized as much as possible, reducing the risk of excessive storage required for the extended function library, resulting in the problem of not being able to be smoothly input into the large-scale language model in the context window.
[0165] In one embodiment of the present invention, the function encapsulation submodule is specifically used for:
[0166] Obtaining parameters of an original function of a first function and a second function in the extended function library, wherein the first function is a function of a first preset ratio in the extended function library, and the second function is a function of a second preset ratio in the extended function library;
[0167] The original function of the first function and the parameter of the second function are encapsulated to obtain the interface document, and the second prompt word corresponding to the interface document is generated.
[0168] As can be seen from the above, the code generation device provided by the embodiment of the present invention only retains the original functions of the functions of the first preset ratio in the interface document, and only retains the basic parameters of the functions of the second preset ratio, so that the large-scale language model can learn according to the typical example functions of the first preset ratio and enrich its own knowledge base. When using other functions, customize the functions according to the basic parameters and the enriched knowledge base, so as to have the ability to understand the target mathematical field. The generalization ability of the large-scale language model on complex vertical field problems is improved, the occurrence of model hallucinations is reduced, the programming ability of the large-scale language model in the vertical field is optimized, and the accuracy and efficiency of the generated code are improved.
[0169] The embodiment of the present invention further provides an electronic device, such as Figure 7 As shown, it includes a processor 701 , a communication interface 702 , a memory 703 and a communication bus 704 , wherein the processor 701 , the communication interface 702 , and the memory 703 communicate with each other via the communication bus 704 .
[0170] Memory 703, used for storing computer programs;
[0171] The processor 701 is used to implement any of the above-mentioned code generation methods when executing the program stored in the memory 703.
[0172] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0173] The communication interface is used for communication between the above electronic device and other devices.
[0174] The memory may include a random access memory (RAM) or a non-volatile memory (NVM), such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0175] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0176] In another embodiment of the present invention, a computer-readable storage medium is provided, in which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above code generation methods are implemented.
[0177] In another embodiment of the present invention, a computer program product including instructions is provided, which, when executed on a computer, enables the computer to execute any code generation method in the above embodiments.
[0178] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from one website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive Solid State Disk (SSD)), etc.
[0179] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0180] Each embodiment in this specification is described in a related 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.
[0181] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. A code generation method, characterized in that: include: Acquire a basic function library and a large-scale language model, wherein the basic function library includes functions in the target mathematical field; Inputting a first prompt word and the basic function library into a large-scale language model to generate an extended function library for solving problems in a target mathematical field, wherein the first prompt word is used to prompt the large-scale language model to generate an agent corresponding to the target mathematical field; Obtaining parameters of an original function of a first function and a second function in the extended function library, wherein the first function is a function of a first preset ratio in the extended function library, and the second function is a function of a second preset ratio in the extended function library; Encapsulating the original function of the first function and the parameters of the second function to obtain an interface document, and generating a second prompt word corresponding to the interface document, wherein the second prompt word is used to prompt the large-scale language model to output a reply in code form; The interface document and the second prompt word are input into the large-scale language model, so that when a user inputs a target problem in the target mathematical field, the large-scale language model outputs a target code for solving the target problem.
2. The method according to claim 1, characterized in that The step of inputting the first prompt word and the basic function library into a large-scale language model to generate an extended function library for solving problems in the target mathematical field includes: Inputting the first prompt word into a large-scale language model so that the large-scale language model generates an agent corresponding to the target mathematical field; When the agent runs on the large-scale language model, the basic function library is input into the large-scale language model to generate a sample question library in the target mathematics field; An extended function library for solving the sample question library is generated based on the sample question library and the knowledge base carried by the large-scale language model.
3. A code generating device, characterized in that: include: A model acquisition module, used to acquire a basic function library and a large-scale language model, wherein the basic function library includes functions in the target mathematical field; A function library generation module, used to input a first prompt word and the basic function library into a large-scale language model to generate an extended function library for solving problems in a target mathematical field, wherein the first prompt word is used to prompt the large-scale language model to generate an agent corresponding to the target mathematical field; Code output module, including: A function encapsulation submodule, used to obtain the original function of the first function and the parameters of the second function in the extended function library, wherein the first function is a function of a first preset proportion in the extended function library, and the second function is a function of a second preset proportion in the extended function library; Encapsulating the original function of the first function and the parameters of the second function to obtain an interface document, and generating a second prompt word corresponding to the interface document, wherein the second prompt word is used to prompt the large-scale language model to output a reply in code form; The code output submodule is used to input the interface document and the second prompt word into the large-scale language model, so that when the user inputs the target problem in the target mathematical field, the large-scale language model outputs the target code for solving the target problem.
4. The device according to claim 3, characterized in that The function library generation module is specifically used for: Inputting the first prompt word into a large-scale language model so that the large-scale language model generates an agent corresponding to the target mathematical field; When the agent runs on the large-scale language model, the basic function library is input into the large-scale language model to generate a sample question library in the target mathematics field; An extended function library for solving the sample question library is generated based on the sample question library and the knowledge base carried by the large-scale language model.
5. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, used to implement the method described in any one of claims 1-2 when executing a program stored in a memory.
6. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 2 is implemented.
Citation Information
Patent Citations
Autonomous task processing method and device, storage medium and electronic equipment
CN118567626A