Optimization method and device for cue words of large language model
By setting the initial prompt word template and using Q&A to adjust the data set, the prompt word template of the large language model is optimized, and the problem of users writing fuzzy prompt words is solved, which improves the accuracy and clarity of the model's answers and reduces labor costs.
Patent Information
- Application Number
- CN202510488260.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-18
- Publication Date
- 2025-07-18
AI Technical Summary
In the prior art, prompt words are usually written directly by users, and there are ambiguity or ambiguity, resulting in large language models being unable to provide accurate answers.
Set up the initial prompt word template, use the Q&A to adjust the template to the dataset, analyze the reasons for the correct and incorrect answers through a large language model, and optimize the prompt word template.
A more accurate and clear prompt word template is achieved, reducing labor costs, and improving the accuracy and pertinence of answers in large language models.
Smart Images

Figure CN120336515A_ABST
Abstract
Description
Technical Field
[0001] One or more embodiments of the present invention relate to network communication technologies, and in particular, to an optimization method and device for prompts for large language models. Background Art
[0002] A prompt, simply speaking, is some key information or guiding statements provided to a large language model to help the large language model better understand the user's question requirements and generate more accurate and targeted answers. It is like a guide for the large language model, telling it which direction to think and generate content. With the application of large language models in various industries, the importance of prompt engineering has become increasingly prominent. Optimized prompt design can effectively improve the performance of pre-trained language models, enhance the quality of generated content, and at the same time lower the technical threshold for model use, thus promoting the development and application of natural language processing technologies at multiple levels. For example, when a user inputs "What are the famous scenic spots in Paris?", the question itself is a simple prompt. But if we add some more specific prompts, such as "What are the famous scenic spots in Paris suitable for family outings?", then the large language model will answer more specifically about the famous scenic spots in Paris suitable for family outings, rather than just listing all the famous scenic spots in Paris.
[0003] However, currently, prompts are usually directly written and input by users, and users often use some vague or ambiguous description methods when inputting prompts. For example, when a user inputs "Please query the GDP data of City A this year", the "this year" here is a vague term, resulting in the large language model being unable to provide more accurate content required by the user.
[0004] Therefore, a more effective optimization method for prompts for large language models is needed. Summary of the Invention
[0005] One or more embodiments of the present invention describe an optimization method and device for prompts for large language models, which can obtain a more accurate prompt template, thus being more conducive to the large language model to understand questions and obtain more accurate answers.
[0006] According to a first aspect, an optimization method for prompts for large language models is provided.
[0007] Set an initial prompt template; the initial prompt template defines the basic structure of the prompt content for expressing the questioning method, and the initial prompt template includes input placeholders.
[0008] Obtain a question-and-answer pair data set; the question-and-answer pair data set includes at least two combinations of questions and correct answers.
[0009] The method further includes:
[0010] Performing the following input processing for each combination in the Q&A pair dataset:
[0011] Replacing the input placeholder in the initial prompt template with the question in the current combination to obtain the first version of the prompt template corresponding to the current combination; and
[0012] Inputting the first version of the prompt template corresponding to the current combination and the correct answer in the current combination into the large language model, and the large language model obtains and outputs the analysis answer corresponding to the current combination according to the first version of the prompt template corresponding to the current combination;
[0013] The large language model adjusts the initial prompt template according to the correct answers and the analysis answers output by itself in each combination in the Q&A pair dataset;
[0014] Taking the adjusted initial prompt template as the final prompt template for the large language model.
[0015] The initial prompt template includes at least one of the following:
[0016] Role: Used to define the role played by the large language model, and the content needs to be filled in by the user;
[0017] Explanation: The initial value is empty, and the subsequent value is the rule summarized by the large language model and filled in by the large language model; among them, the rule summarized by the large language model is: the rule summarized by the large language model according to the difference between the correct answer in each combination in the Q&A pair dataset and the analysis answer output by itself;
[0018] Task: Used to describe the task that the large language model needs to process, and the content needs to be filled in by the user;
[0019] Output format: Used to exemplify the data format that the large language model needs to return, and the content needs to be filled in by the user;
[0020] Example: Used to exemplify the input and output, and the content needs to be filled in by the user;
[0021] User question: The initial value is the input placeholder.
[0022] The large language model adjusts the initial prompt template according to the correct answers and the analysis answers output by itself in each combination in the Q&A pair dataset, including:
[0023] For each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination matches the correct answer in this combination, then construct a successful prompt template using this combination; wherein, the construction method of this successful prompt template includes: replacing the input placeholder in the initial prompt template with the question and correct answer in this combination;
[0024] For each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination does not match the correct answer in this combination, then construct a failed prompt template using this combination; wherein, the construction method of this failed prompt template includes: replacing the input placeholder in the initial prompt template with the analysis answer corresponding to this combination, as well as the question and correct answer in this combination;
[0025] Input each successful prompt template and each failed prompt template into the large language model, and let the large language model summarize the reasons for the correct analysis answer and the reasons for the incorrect / failed analysis answer, and let the large language model write the rules of various reasons summarized in the initial prompt template.
[0026] Execute at least two rounds of iterative processes for the Q&A pair dataset. In each round of iterative process, perform the above input processing and adjust the initial prompt template.
[0027] In the first round of iterative process, assign equal weight values to each combination in the Q&A pair dataset;
[0028] In each round of iterative process starting from the second round, for each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination in the previous round of iterative process does not match the correct answer in this combination, then increase the weight value assigned to this combination in this round of iterative process; if the analysis answer output by the large language model for this combination in the previous round of iterative process matches the correct answer in this combination, then maintain the weight value assigned to this combination in this round of iterative process.
[0029] For each round of iterative process, obtain a final prompt template, thus obtaining at least two final prompt templates.
[0030] After using the adjusted initial prompt template as the final prompt template for the large language model, the method further includes:
[0031] Receive the query question input by the user;
[0032] Use this query question to replace the input placeholder in each final prompt template respectively, thus obtaining at least two final prompt template contents for the query question;
[0033] Input the content of at least two final prompt templates for the query problem into the large language model respectively;
[0034] Based on the content of at least two final prompt templates for the query problem, the large language model obtains at least two answers for the current query problem;
[0035] The large language model votes on the at least two answers to obtain and output the final answer for the current query problem.
[0036] After using the adjusted initial prompt template as the final prompt template for the large language model, the method further includes:
[0037] Receive the query problem input by the user;
[0038] Use the query problem to replace the input placeholder in the final prompt template to obtain the content of the final prompt template for the query problem;
[0039] Input the content of the final prompt template for the query problem into the large language model;
[0040] Based on the content of the final prompt template for the query problem, the large language model obtains and outputs the final answer for the current query problem.
[0041] According to the second aspect, an optimization device for the prompt of the large language model is provided, and the device includes:
[0042] A template generation module for setting an initial prompt template; the initial prompt template defines the basic structure of the prompt content for expressing the questioning method, and the initial prompt template includes an input placeholder;
[0043] A training sample acquisition module for acquiring a question-and-answer pair data set; the question-and-answer pair data set includes at least two groups of combinations of questions and correct answers;
[0044] A training execution module for performing the following input processing for each group of combinations in the question-and-answer pair data set: using the question in the current combination to replace the input placeholder in the initial prompt template to obtain the first version of the prompt template corresponding to the current combination; and inputting the first version of the prompt template corresponding to the current combination and the correct answer in the current combination into the large language model, and the large language model obtains and outputs the analysis answer corresponding to the current combination according to the first version of the prompt template corresponding to the current combination;
[0045] A prompt template determination module is used to adjust the initial prompt template by the large language model according to the correct answers and the analyzed answers output by itself in each combination in the Q&A pair dataset; and use the adjusted initial prompt template as the final prompt template for the large language model.
[0046] According to a third aspect, a computing device is provided, including a memory and a processor. An executable code is stored in the memory. When the processor executes the executable code, the method according to any embodiment of the present invention is implemented.
[0047] Thus, each embodiment of the present invention has at least the following beneficial effects:
[0048] 1. In the embodiments of the present invention, instead of the user randomly inputting questions to the large language model according to their own habits, an initial prompt template is first set. The initial prompt template is used to standardize the content and structure that should be included in the questions of various types, that is, to provide a more general and clear way of asking questions, so as to be more conducive to obtaining more accurate and clear user questions, more conducive to the large language model understanding the user's question requirements, and generating more accurate and targeted answers;
[0049] 2. In the embodiments of the present invention, the initial prompt template is not fixed, but uses the combination of questions and correct answers in the Q&A pair dataset and the analyzed answers of the large language model to the questions in the Q&A pair dataset, to let the large language model explore and summarize the reasons for providing correct answers and wrong answers by itself, and let the large language model summarize the rules according to this reason by itself, and let the large language model adjust the initial prompt template according to the summarized rules, so as to make the initial prompt template more accurately reflect the user's questions, and make the initial prompt template provide a clearer index for the large language model to find more correct answers to the questions.
[0050] 3. The method of the embodiments of the present invention can realize automatic prompt optimization without manual adjustment of the prompt template, thus reducing the labor cost.
[0051] 4. In order to further enhance the optimization effect of the prompt template, in an embodiment of the present invention, at least two rounds of iterative processes can be performed on the Q&A pair dataset. In each round of iterative process, the input processing in steps 105 and 107 and adjusting the initial prompt template are performed, so as to continuously optimize the initial prompt template. For example, in the "explanation" element of the initial prompt template, continuously adjust or supplement the rules for the large language model to analyze correct and incorrect reasons, so that the finally obtained prompt template can be more general and can better inform the large language model of the successful rules that should be adopted and the failure errors that should be avoided when understanding questions. BRIEF DESCRIPTION OF THE DRAWINGS
[0052] In order to more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the following will briefly introduce the drawings required for the description of the embodiments or the prior art. Obviously, the drawings in the following description are some embodiments of the present invention. For those of ordinary skill in the art, without creative efforts, other drawings can also be obtained based on these drawings.
[0053] Figure 1 It is a flowchart of an optimization method for prompting words for large language models in an embodiment of the present invention.
[0054] Figure 2 It is a schematic diagram of the structure of an optimization device for prompting words for large language models in an embodiment of the present invention.
[0055] Figure 3 It is a schematic diagram of the structure of an optimization device for prompting words for large language models in another embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0056] The following will describe the solutions provided by the present invention in conjunction with the drawings.
[0057] First of all, it should be noted that 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 of "a", "the" and "said" used in the embodiments of the present invention and the appended claims are also intended to include the plural forms, unless the context clearly indicates otherwise.
[0058] It should be understood that the term " / and" used herein 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 simultaneously, and B exists alone. In addition, the character " / " in this article generally represents an "or" relationship between the associated objects before and after.
[0059] Figure 1 It is a flowchart of an optimization method for prompting words for large language models in an embodiment of the present invention. Refer to Figure 1 and the method includes:
[0060] Step 101: Set an initial prompt word template; the initial prompt word template defines the basic structure of the prompt word content for expressing the questioning method, and the initial prompt word template includes input placeholders;
[0061] Step 103: Obtain a question-and-answer pair data set; the question-and-answer pair data set includes at least two combinations of questions and correct answers;
[0062] Step 105: Perform the following input processing for each combination in the Q&A pair dataset:
[0063] Step 1051: Replace the input placeholder in the initial prompt template with the question in the current combination, and fill in the content related to the specific task in the initial prompt template, so as to obtain the first version of the prompt template corresponding to the current combination; and
[0064] Step 1053: Input the first version of the prompt template corresponding to the current combination and the correct answer in the current combination into the large language model, and the large language model obtains and outputs the analysis answer corresponding to the current combination according to the first version of the prompt template corresponding to the current combination;
[0065] Step 107: The large language model adjusts the initial prompt template according to the correct answers and the analysis answers output by itself in each combination in the Q&A pair dataset;
[0066] Step 109: Use the adjusted initial prompt template as the final prompt template for the large language model.
[0067] According to Figure 1 As can be seen from the shown process, the method of the embodiment of the present invention adopts the following ideas and achieves the following beneficial effects:
[0068] 1. In the embodiment of the present invention, instead of the user randomly inputting questions to the large language model according to their own habits, an initial prompt template is first set. The initial prompt template is used to standardize the content and structure that should be included in the questions of various types, that is, to provide a more general and clear way of asking questions, so that it is more conducive to obtaining more accurate and clear user questions, more conducive to the large language model understanding the question requirements of users, and generating more accurate and targeted answers;
[0069] 2. In the embodiment of the present invention, the initial prompt template is not fixed. Instead, by using the combination of questions and correct answers in the Q&A pair dataset and the analysis answers of the large language model for the questions in the Q&A pair dataset, the large language model is allowed to explore and summarize the reasons for providing correct answers and wrong answers by itself, and the large language model is allowed to summarize the rules according to the reasons by itself, and the large language model is allowed to adjust the initial prompt template according to the summarized rules, so that the initial prompt template can more accurately reflect the questions of users, and the initial prompt template can provide a clearer index for the large language model to find the answers to more correct questions.
[0070] 3. The method of the embodiment of the present invention can realize automatic prompt word optimization without manual adjustment of the prompt template, thus reducing the labor cost.
[0071] The following will describe the method of the embodiment of the present invention in conjunction with Figure 1 the flow shown and specific examples.
[0072] For the above step 101: Set an initial prompt word template; this initial prompt word template defines the basic structure of the prompt word content for expressing the questioning method, and this initial prompt word template contains an input placeholder.
[0073] It can be seen that the initial prompt word template can standardize the content and structure that should be included in the questions of various types, so as to prevent subsequent users from randomly inputting questions to the large language model according to their own habits.
[0074] The input placeholder in the initial prompt word template can be replaced with the content of the actual question according to the specific application scenario.
[0075] Therefore, in an embodiment of the present invention, the initial prompt word template may include at least one of the following:
[0076] Role: Used to define the role played by the large language model, and the user needs to fill in the content;
[0077] Explanation: The initial value is empty, and the subsequent value is the rule summarized by the large language model and filled in by the large language model; among them, the rule summarized by the large language model is: the rule summarized by the large language model according to the difference between the correct answer and the analysis answer output by itself in each combination of the Q&A pair dataset;
[0078] Task: Used to describe the task that the large language model needs to process, and the user needs to fill in the content;
[0079] Output format: Used to exemplify the data format that the large language model needs to return, and the user needs to fill in the content;
[0080] Example: Used to exemplify the input and output, and the user needs to fill in the content;
[0081] User question: The initial value is the input placeholder.
[0082] Next, for step 103: Obtain a Q&A pair dataset; this Q&A pair dataset includes at least two combinations of questions and correct answers.
[0083] In the embodiment of the present invention, the Q&A pair dataset is used to train the large language model, so that the large language model summarizes rules and discovers the parts to be adjusted in the initial prompt word template according to the Q&A pair dataset and the analysis answers output by itself.
[0084] The Q&A pair dataset can be expressed as Dataset = {q1:a1, q2:a2, …, q n :an}. Among them, q i is the i-th question, and a i is the correct answer to the i-th question. For example, q1 is "Please provide the gross domestic product (GDP) of City A as of the current date in 2025", and a1 is "The GDP of City A as of the current date in 2025 is the value XXX".
[0085] In the embodiments of the present invention, the Q&A pair dataset usually includes various types of Q&A pairs, such as questions and answers about asking the weather, questions and answers about historical scenic spots in a certain place, and questions and answers about government affairs, etc. The diversity of the Q&A pair types in the Q&A pair dataset can further ensure that the present invention obtains a more general final prompt word template applicable to various questions.
[0086] Next, for step 105: For each combination in the Q&A pair dataset, the following input processing is performed: Step 1051: Use the question in the current combination to replace the input placeholder in the initial prompt word template, so as to obtain the first version of the prompt word template corresponding to the current combination; and Step 1053: Input the first version of the prompt word template corresponding to the current combination and the correct answer in the current combination into the large language model, and the large language model obtains and outputs the analysis answer corresponding to the current combination according to the first version of the prompt word template corresponding to the current combination.
[0087] For example, the Q&A pair dataset includes 1000 combinations of questions and correct answers. For the first combination of questions and correct answers, use the question q1 in this combination to replace the input placeholder in the initial prompt word template, so as to obtain the first version of the prompt word template corresponding to the first combination; input the first version of the prompt word template corresponding to the first combination and the correct answer a1 in the first combination into the large language model, and the large language model obtains and outputs the analysis answer q1' corresponding to the first combination; similarly, for the second combination of questions and correct answers, use the question q2 in this combination to replace the input placeholder in the initial prompt word template, so as to obtain the first version of the prompt word template corresponding to the second combination; input the first version of the prompt word template corresponding to the second combination and the correct answer a2 in the second combination into the large language model, and the large language model obtains and outputs the analysis answer q2' corresponding to the second combination; and so on, until the analysis answer q i ' corresponding to each combination of questions and correct answers in all combinations has been obtained.
[0088] The following is an example to illustrate the implementation process of step 105.
[0089] For example, the user's current task is to implement a task of generating SQL statements from natural language. Correspondingly, the Q&A pair dataset includes the following combinations in the 3rd group (question: correct answer):
[0090] Question 3: The total GDP of Province A this year.
[0091] Answer 3: select gdp from xxx where date=2025 and province=Province A
[0092] Then, the processing process for this combination 3 includes:
[0093] After replacing the input placeholder in the initial prompt template with the above Question 3 in combination 3 and having the user fill in the content related to the specific task in the initial prompt template, the first version of the prompt template for combination 3 can be obtained, in the following form:
[0094] "
[0095] Role: You are an nl2sql assistant.
[0096] Instructions: Empty
[0097] Task: Based on the user's question and the provided table creation statement, convert the user's question into a database query statement, and the database type is mysql. Table creation statement: xxxxxx
[0098] Output format: {"sql": corresponding SQL query statement}
[0099] Example (number example)
[0100] Input: The GDP of Shandong Province
[0101] Output: {"sql": "select gdp from xxx"}
[0102] User question, only output the result, do not analyze the process:
[0103] Input: The total GDP of Province A this year.
[0104] Input the first version of the prompt template for the corresponding combination 3 and the Answer 3 in combination 3 into the large language model. The large language model obtains and outputs the analysis answer for combination 3 according to the first version of the prompt template for combination 3. The large language model may output the correct analysis answer or the wrong analysis answer. For example, since the prompt template is not perfect enough, the large language model generates the wrong analysis answer. The large language model interprets "this year" as 2024, and the analysis answer is:
[0105] {"sql": "select GDP from xxx where date=2024"}
[0106] For each combination in the Q&A pair dataset, the large language model performs processing such as that in combination 3 to obtain an analysis answer for each combination. This analysis answer may be the correct answer (i.e., the gap from the correct answer in the combination is less than the preset value) or the wrong answer (i.e., the gap from the correct answer in the combination is greater than the preset value).
[0107] Next, for step 107: The large language model adjusts the initial prompt template according to the correct answers in each combination of the Q&A pair dataset and the analysis answers it outputs itself.
[0108] For each combination in the Q&A pair dataset, the large language model analyzes whether the analysis answer it outputs for this combination is the correct answer or the wrong answer. If it is the correct answer, it finds the reason for its correct analysis. If it is the wrong / failed answer, it finds the reason for its wrong analysis. Furthermore, the large language model can summarize the rules of the correct and wrong reasons. For example, the rule for the reason of the error is that "the large language model regards this year as 2024". In this way, in the subsequent process, the large language model can continue to use the reasons / methods for correct analysis and avoid using the reasons / methods for wrong analysis.
[0109] In an embodiment of the present invention, a specific implementation manner for the large language model to adjust the initial prompt template according to the correct answers in each combination of the Q&A pair dataset and the analysis answers it outputs itself in this step 107 may include:
[0110] Step 1071: For each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination conforms to the correct answer in this combination (such as the difference is less than or equal to the preset value), then use this combination to construct a successful prompt template; wherein, the construction method of this successful prompt template includes: using the question and the correct answer in this combination to replace the input placeholder in the initial prompt template;
[0111] Step 1073: For each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination does not conform to the correct answer in this combination (such as the difference is greater than the preset value), then use this combination to construct a failed prompt template; wherein, the construction method of this failed prompt template includes: using the analysis answer corresponding to this combination and the question and the correct answer in this combination to replace the input placeholder in the initial prompt template;
[0112] Step 1075: Input each successful prompt template and each failed prompt template into the large language model. The large language model summarizes the reasons for the correct analysis answers and the reasons for the incorrect / failed analysis answers, and writes the rules of various reasons summarized in the initial prompt template.
[0113] For example, a rule for a reason that causes the large language model to analyze incorrectly is that "the large language model regards this year as 2024". Therefore, the large language model can write the rule of this reason summarized in the initial prompt template. For example, write the summarized rule in the "Explanation" element of the initial prompt template. For example, the prompt template is modified to the following form:
[0114] Role: XXX
[0115] Explanation: This year is 2025, and the time calculation is based on 2025.
[0116] …….
[0117] Step 109: Use the adjusted initial prompt template as the final prompt template for the large language model.
[0118] In an embodiment of the present invention, after step 109 uses the adjusted initial prompt template as the final prompt template for the large language model, the method further includes the process in which the large language model uses the final prompt template to more accurately provide the answer to the user's query question, specifically including:
[0119] Receive the query question input by the user;
[0120] Use the query question to replace the input placeholder in the final prompt template, so as to obtain the content of the final prompt template for the query question;
[0121] Input the content of the final prompt template for the query question into the large language model;
[0122] The large language model obtains and outputs the final answer to the current query question according to the content of the final prompt template for the query question.
[0123] In order to further enhance the optimization effect of the prompt template, the following solution F can be adopted: In an embodiment of the present invention, at least two rounds of iterative processes can be performed on the question-and-answer pair data set. In each round of iterative process, the above-mentioned input processing and the adjustment of the initial prompt template in step 105 and step 107 are performed, so as to continuously optimize the initial prompt template. For example, continuously adjust or supplement the rules of the reasons for the correct and incorrect analysis of the large language model in the "Explanation" element of the initial prompt template.
[0124] In one embodiment of the present invention, when adopting Solution F and performing the above-mentioned iterative process for at least two rounds, in the first-round iterative process, equal weight values can be assigned to each combination in the Q&A pair dataset; that is, the same weight is given to the n samples in the Q&A pair dataset to ensure that at the beginning stage, each sample is treated equally. The formula is: W_i = 1 / n; where W_i represents the weight of the i-th sample in the first-round iteration, and n represents the total number of samples.
[0125] In each iterative process starting from the second round, for each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination in the previous-round iterative process does not match the correct answer in this combination, then in this-round iterative process, increase the weight value assigned to this combination; if the analysis answer output by the large language model for this combination in the previous-round iterative process matches the correct answer in this combination, then in this-round iterative process, maintain the weight value assigned to this combination.
[0126] In this way, the combination / data with a larger weight value is more likely to be extracted, and it is easier to determine the optimization direction, ensuring that the optimization process is biased towards the data with a high weight, that is: solving the problem presented by incorrect data.
[0127] In one embodiment of the present invention, when adopting Solution F and performing the above-mentioned iterative process for at least two rounds, one method is: in the iterative process of the subsequent round, continue to adjust the prompt template based on the prompt template obtained after adjustment in the previous round. In this way, the one obtained in step 109 is a final prompt template.
[0128] In one embodiment of the present invention, when adopting Solution F and performing the above-mentioned iterative process for at least two rounds, another method is: for each iterative process, adjust the initial prompt template. In the iterative process of the subsequent round, do not continue to adjust based on the prompt template obtained after adjustment in the previous round. In this way, a final prompt template is obtained in each iterative process, so that at least two final prompt templates are obtained in step 109.
[0129] In one embodiment of the present invention, if at least two final prompt templates are obtained in step 109, then, in the subsequent process, the large language model can adopt a voting method to use these at least two final prompt templates to more accurately provide the answer to the query question for the user, which specifically includes:
[0130] Receive the query question input by the user;
[0131] Use this query question to replace the input placeholder in each final prompt template respectively, so as to obtain at least two final prompt template contents for the query question;
[0132] Input the content of at least two final prompt templates for the query problem into the large language model respectively;
[0133] Based on the content of at least two final prompt templates for the query problem, the large language model obtains at least two answers for the current query problem;
[0134] The large language model votes on the at least two answers to obtain and output the final answer for the current query problem.
[0135] In another embodiment of the present invention, an optimization method for the prompts of the large language model includes the following steps:
[0136] S1: Initialize the weight distribution of the prompt template and the samples in the Q&A pair dataset (i.e., the combination of each question and the correct answer);
[0137] S2: Use the initialized prompt template to construct prompts and use the large language model for prediction. According to the generated results analysis, construct the prompt optimizer for the current iteration and perform multiple rounds of iterative optimization;
[0138] S3: Update the sample weight distribution, increase the weight of the wrong questions, and retrain the prompts under this weight distribution until the conditions are met.
[0139] In step S1, it specifically includes:
[0140] S11: When constructing the Q&A pair dataset, each sample contains a corresponding weight, which reflects the attention degree of the sample.
[0141] In step S2, it specifically includes:
[0142] S21: Use the large language model to summarize the rules for generating correct answers and wrong answers respectively.
[0143] S22: Use the rules of correct generation and wrong generation as the optimization direction to optimize the current prompt.
[0144] In step S3, it specifically includes:
[0145] S31: For the problems that cannot be solved by the optimized prompts, re-optimize new prompts to solve them.
[0146] S32: During the process of optimizing the new prompts, pay more attention to the wrong samples through the sample weights.
[0147] An embodiment of the present invention also proposes an optimization device for the prompts of the large language model. Refer to Figure 2 , the device includes:
[0148] The template generation module 201 is used to set an initial prompt template; the initial prompt template defines the basic structure of the prompt content for expressing the way of asking questions, and the initial prompt template includes input placeholders;
[0149] The training sample acquisition module 202 is used to acquire a question-and-answer pair dataset; the question-and-answer pair dataset includes at least two combinations of questions and correct answers;
[0150] The training execution module 203 is used to perform the following input processing for each combination in the question-and-answer pair dataset: replacing the input placeholder in the initial prompt template with the question in the current combination to obtain the first version of the prompt template corresponding to the current combination; and inputting the first version of the prompt template corresponding to the current combination and the correct answer in the current combination into the large language model, and the large language model obtains and outputs the analysis answer corresponding to the current combination according to the first version of the prompt template corresponding to the current combination;
[0151] The prompt template determination module 204 is used to adjust the initial prompt template by the large language model according to the correct answers and the analysis answers output by itself in each combination of the question-and-answer pair dataset; and use the adjusted initial prompt template as the final prompt template for the large language model.
[0152] In an embodiment of the device of the present invention, the initial prompt template includes at least one of the following:
[0153] Role: used to define the role played by the large language model, and content needs to be filled in by the user;
[0154] Explanation: The initial value is empty, and the subsequent value is the rule summarized by the large language model and filled in by the large language model; wherein, the rule summarized by the large language model is the rule summarized by the large language model according to the difference between the correct answer in each combination of the question-and-answer pair dataset and the analysis answer output by itself;
[0155] Task: used to describe the task that the large language model needs to process, and content needs to be filled in by the user;
[0156] Output format: used to exemplify the data format that the large language model needs to return, and content needs to be filled in by the user;
[0157] Example: used to exemplify the input and output, and content needs to be filled in by the user;
[0158] User question: The initial value is the input placeholder.
[0159] In an embodiment of the device of the present invention, the prompt template determination module 204 is configured to execute:
[0160] For each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination conforms to the correct answer in this combination, then use this combination to construct a successful prompt template; wherein, the construction method of this successful prompt template includes: using the question and the correct answer in this combination to replace the input placeholder in the initial prompt template;
[0161] For each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination does not conform to the correct answer in this combination, then use this combination to construct a failed prompt template; wherein, the construction method of this failed prompt template includes: using the analysis answer corresponding to this combination and the question and the correct answer in this combination to replace the input placeholder in the initial prompt template;
[0162] Input each of the successful prompt templates and each of the failed prompt templates into the large language model, and let the large language model summarize the reasons for the correct analysis answer and the reasons for the incorrect / failed analysis answer, and let the large language model write the rules of various reasons summarized in the initial prompt template.
[0163] In an embodiment of the device of the present invention, at least two rounds of iterative processes are performed on the Q&A pair dataset. In each round of the iterative process, the input processing is performed by the training execution module 203 and the initial prompt template is adjusted by the prompt template determination module 204.
[0164] In an embodiment of the device of the present invention, the training execution module 203 is configured to execute:
[0165] In the first round of the iterative process, equal weight values are assigned to each combination in the Q&A pair dataset;
[0166] In each round of the iterative process starting from the second round, for each combination in the Q&A pair dataset, if the analysis answer output by the large language model for this combination in the previous round of the iterative process does not conform to the correct answer in this combination, then increase the weight value assigned to this combination in this round of the iterative process; if the analysis answer output by the large language model for this combination in the previous round of the iterative process conforms to the correct answer in this combination, then maintain the weight value assigned to this combination in this round of the iterative process.
[0167] In an embodiment of the device of the present invention, for each round of the iterative process, a final prompt template is obtained, so as to obtain at least two final prompt templates.
[0168] In an embodiment of the device of the present invention, referring to Figure 3 , it further includes: an answer processing module 301, which is configured to execute:
[0169] Receive a query question input by the user;
[0170] Use the query question to replace the input placeholder in each final prompt word template respectively, so as to obtain at least two final prompt word template contents for the query question;
[0171] Input at least two final prompt word template contents for the query question into the large language model respectively;
[0172] The large language model obtains at least two answers for the current query question according to at least two final prompt word template contents for the query question;
[0173] The large language model votes on the at least two answers, so as to obtain and output the final answer for the current query question.
[0174] In an embodiment of the device of the present invention, refer to Figure 3 , and further includes: an answer processing module 301, configured to execute:
[0175] Receive a query question input by the user; use the query question to replace the input placeholder in the final prompt word template, so as to obtain the final prompt word template content for the query question; input the final prompt word template content for the query question into the large language model; the large language model obtains and outputs the final answer for the current query question according to the final prompt word template content for the query question.
[0176] An embodiment of the present invention provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed in a computer, the computer is made to execute the method in any one of the embodiments in the specification.
[0177] An embodiment of the present invention provides a computing device, including a memory and a processor. An executable code is stored in the memory. When the processor executes the executable code, the method in any one of the embodiments in the specification is implemented.
[0178] It can be understood that the structure schematically shown in the embodiments of the present invention does not constitute a specific limitation on the device of the embodiments of the present invention. In other embodiments of the specification, the above device may include more or fewer components than shown in the figure, or combine some components, or split some components, or have different component arrangements. The components shown in the figure can be implemented in hardware, software, or a combination of software and hardware.
[0179] Each embodiment in the present invention is described in a progressive manner. For the same or similar parts among the embodiments, reference can be made to each other, and the key point of each embodiment is to illustrate the differences from other embodiments. In particular, for the device embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and for the relevant parts, reference can be made to the corresponding descriptions in the method embodiments.
[0180] Those skilled in the art should be able to realize that in one or more of the above examples, the functions described in the present invention can be implemented by hardware, software, add-ons, or any combination thereof. When implemented using software, these functions can be stored in a computer-readable medium or transmitted as one or more instructions or codes on a computer-readable medium.
[0181] The specific embodiments described above further elaborate on the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above description is only the specific embodiments of the present invention and is not used to limit the protection scope of the present invention. Any modifications, equivalent replacements, improvements, etc. made on the basis of the technical solution of the present invention shall be included in the protection scope of the present invention.
Claims
1. Optimization method for prompting words of large language models, characterized in that: Set an initial prompting word template; The initial prompting word template defines the basic structure of the prompting word content for expressing the questioning method, and the initial prompting word template contains input placeholders; Obtain a question-and-answer pair dataset; The question-and-answer pair dataset includes at least two combinations of questions and correct answers; The method further includes: Perform the following input processing for each combination in the question-and-answer pair dataset: Use the question in the current combination to replace the input placeholder in the initial prompting word template, so as to obtain the first version of the prompting word template corresponding to the current combination; and Input the first version of the prompting word template corresponding to the current combination and the correct answer in the current combination into the large language model, and the large language model obtains and outputs the analysis answer corresponding to the current combination according to the first version of the prompting word template corresponding to the current combination; The large language model adjusts the initial prompting word template according to the correct answers and the analysis answers output by itself in each combination in the question-and-answer pair dataset; Use the adjusted initial prompting word template as the final prompting word template for the large language model.
2. The method according to claim 1, characterized in that, The initial prompting word template includes at least one of the following: Role: Used to define the role played by the large language model, and the content needs to be filled in by the user; Explanation: The initial value is empty, and the subsequent value is the rule summarized by the large language model and filled in by the large language model; among them, the rule summarized by the large language model is: the rule summarized by the large language model according to the difference between the correct answer in each combination in the question-and-answer pair dataset and the analysis answer output by itself; Task: Used to describe the task that the large language model needs to process, and the content needs to be filled in by the user; Output format: Used to exemplify the data format that the large language model needs to return, and the content needs to be filled in by the user; Example: Used to exemplify the input and output, and the content needs to be filled in by the user; User question: The initial value is the input placeholder.
3. The method according to claim 1, wherein The large language model adjusts the initial prompting word template according to the correct answers and the analysis answers output by itself in each combination in the question-and-answer pair dataset, including: For each combination in the question-and-answer pair dataset, if the analysis answer output by the large language model for this combination conforms to the correct answer in this combination, then use this combination to construct a successful prompting word template; among them, the construction method of the successful prompting word template includes: using the question and correct answer in this combination to replace the input placeholder in the initial prompting word template; For each combination in the question-and-answer pair dataset, if the analysis answer output by the large language model for this combination does not conform to the correct answer in this combination, then use this combination to construct a failed prompting word template; among them, the construction method of the failed prompting word template includes: using the analysis answer corresponding to this combination and the question and correct answer in this combination to replace the input placeholder in the initial prompting word template; Input each successful prompt template and each failed prompt template into the large language model. The large language model summarizes the reasons for the correct analysis answers and the reasons for the incorrect / failed analysis answers, and writes the rules of various reasons summarized into the initial prompt template.
4. The method according to claim 1, wherein Perform at least two rounds of iterative processes on the question-answer pair dataset. In each round of the iterative process, perform the above input processing and adjust the initial prompt template.
5. The method according to claim 4, characterized in that, In the first round of the iterative process, assign equal weight values to each combination in the question-answer pair dataset. In each round of the iterative process starting from the second round, for each combination in the question-answer pair dataset, if the analysis answer output by the large language model for this combination in the previous round of the iterative process does not match the correct answer in this combination, then increase the weight value assigned to this combination in this round of the iterative process. If the analysis answer output by the large language model for this combination in the previous round of the iterative process matches the correct answer in this combination, then maintain the weight value assigned to this combination in this round of the iterative process.
6. The method according to claim 4, wherein For each round of the iterative process, obtain a final prompt template, thereby obtaining at least two final prompt templates.
7. The method according to claim 6, wherein After using the adjusted initial prompt template as the final prompt template for the large language model, the method further includes: Receive a query question input by the user. Use the query question to replace the input placeholder in each final prompt template respectively, thereby obtaining at least two final prompt template contents for the query question. Input the at least two final prompt template contents for the query question into the large language model respectively. The large language model obtains at least two answers for the current query question according to the at least two final prompt template contents for the query question. The large language model votes on the at least two answers to obtain and output the final answer for the current query question.
8. The method according to claim 1, wherein After using the adjusted initial prompt template as the final prompt template for the large language model, the method further includes: Receive a query question input by the user. Use the query question to replace the input placeholder in the final prompt template, thereby obtaining the final prompt template content for the query question. Input the final prompt template content for the query question into the large language model. The large language model obtains and outputs the final answer for the current query question according to the final prompt template content for the query question.
9. An optimization device for prompts of large language models, characterized in that, The device includes: A template generation module for setting an initial prompt template. The initial prompt template defines the basic structure of the prompt word content for expressing the questioning method, and the initial prompt template contains an input placeholder. A training sample acquisition module for acquiring a question-answer pair dataset. The question-answer pair dataset includes at least two groups of combinations of questions and correct answers. A training execution module, configured to perform the following input processing for each combination in the Q&A pair dataset: replacing the input placeholder in the initial prompt template with the question in the current combination to obtain the first version of the prompt template corresponding to the current combination; and inputting the first version of the prompt template corresponding to the current combination and the correct answer in the current combination into a large language model, and the large language model obtains and outputs an analysis answer corresponding to the current combination according to the first version of the prompt template corresponding to the current combination. A prompt template determination module, configured to adjust the initial prompt template by the large language model according to the correct answers and the analysis answers output by itself in each combination in the Q&A pair dataset; and use the adjusted initial prompt template as the final prompt template for the large language model.
10. A computing device, comprising a memory and a processor, wherein executable code is stored in the memory, and when the processor executes the executable code, the method according to any one of claims 1-8 is implemented.
Citation Information
Patent Citations
Self-adaptive template reconstruction method and system based on few samples
CN119807421A
Cited By
Alarm prompt template optimization method, electronic equipment, medium and product
CN121882007A
Alarm prompt template optimization method, electronic device, medium and product
CN121882007B
Intelligent agent optimization method based on cue word engineering and test set management
CN122242503A
Agent optimization method based on prompt word engineering and test set management
CN122242503B