Prompt text optimization method, device and electronic device based on corpus
By extracting operation instructions and object text from the prompt text of the large language model and obtaining matching positive samples from the corpus for optimization, the problem of traditional prompt design lacks guidance and improves the performance of the model in complex tasks.
Patent Information
- Application Number
- CN202411081509.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-08
- Publication Date
- 2025-02-25
- Estimated Expiration
- 2044-08-08
AI Technical Summary
The lack of guidance in traditional cues designs leads to large language models that do not perform well in the face of complex or novel tasks and fail to fully realize their potential abilities.
After detecting the first prompt text, the operation instruction text and the operation object text are extracted from it, and based on these texts, a positive sample matching the first prompt text matches from the preset corpus, and optimize the first prompt text, the first prompt text is obtained A more instructive second prompt text.
Dynamically generate positive sample use cases, enhance the guiding nature of prompt text, enable large language models to understand user intentions more accurately, and thus improve model inference effect.
Smart Images

Figure CN118606727B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field of natural language processing, and in particular to a corpus-based prompt text optimization method, device and electronic device. Background Art
[0002] In recent years, with the rapid development of deep learning technology, large language models have attracted widespread attention in academia and industry for their powerful natural language processing capabilities.
[0003] By pre-training on massive text data, large language models can capture complex language structures and semantic information, and thus demonstrate excellent performance in a variety of natural language processing tasks such as question answering, text generation, translation, and summarization. At present, the efficient application of large language models depends on carefully designed prompts. Prompts are the bridge for humans to interact with large models. They not only determine the model's understanding of the task, but also directly affect the final output quality.
[0004] However, traditional prompt designs are often too straightforward or lack guidance, causing the model to perform poorly when faced with complex or novel tasks and failing to fully utilize its potential. Summary of the invention
[0005] The main purpose of this application is to provide a corpus-based prompt text optimization method, device and electronic device, aiming to solve the technical problem that traditional prompt design lacks guidance and leads to poor model reasoning effect.
[0006] To achieve the above objectives, the present application provides a corpus-based prompt text optimization method, comprising:
[0007] After detecting the first prompt text, extracting the operation instruction text and the operation object text from the first prompt text;
[0008] Based on the operation instruction text and the operation object text, a positive sample matching the first prompt text is obtained from the preset corpus;
[0009] Based on the positive sample, the first prompt text is optimized to obtain a second prompt text.
[0010] In one embodiment, the step of acquiring a positive sample matching the first prompt text from the preset corpus based on the operation instruction text and the operation object text includes:
[0011] Based on the operation instruction text, determining a target corpus from a plurality of preset corpora;
[0012] Based on the operation object text, a positive sample matching the first prompt text is obtained from the target corpus.
[0013] In one embodiment, the step of determining a target corpus from a plurality of preset corpora based on the operation instruction text includes:
[0014] Performing domain type recognition and task type recognition on the operation instruction text to determine the domain type and task type of the first prompt text;
[0015] From the preset multiple corpora, a corpus matching the domain type and the task type is determined as a target corpus.
[0016] In one embodiment, the target corpus stores a plurality of sample examples, and the step of acquiring a positive sample matching the first prompt text from the target corpus based on the operation object text includes:
[0017] Calculate the sample similarity between each sample example and the operation object text by using a preset similarity algorithm;
[0018] Based on the sample similarity, sample examples that meet a preset similarity condition are determined as target sample examples;
[0019] According to the target sample example, a positive sample matching the first prompt text is determined.
[0020] In one embodiment, the sample examples are composed of operation object examples and output result examples, and the step of calculating the sample similarity between each of the sample examples and the operation object text by using a preset similarity algorithm includes:
[0021] Calculating the object similarity between the operation object example of each sample example and the operation object text by using a preset similarity algorithm;
[0022] Based on the object similarity, determining the sample similarity between each of the sample examples and the operation object text;
[0023] The step of determining a positive sample matching the first prompt text according to the target sample example comprises:
[0024] The output result example of the target sample example is determined as a positive sample matching the first prompt text.
[0025] In one embodiment, the preset similarity algorithm is a cosine similarity algorithm, and the step of calculating the object similarity between the operation object example of each sample example and the operation object text by using the preset similarity algorithm includes:
[0026] Calculating the cosine similarity between the text vector of the operation object example of each sample example and the text vector of the operation object text by using the cosine similarity algorithm;
[0027] Based on the cosine similarity, the object similarity between the operation object example of each sample example and the operation object text is determined.
[0028] In one embodiment, the step of optimizing the first prompt text based on the positive sample to obtain the second prompt text includes:
[0029] The positive sample is concatenated with the first prompt text to obtain a second prompt text.
[0030] In addition, to achieve the above-mentioned purpose, the present application also provides a corpus-based prompt text optimization device, the device comprising:
[0031] A text extraction module, configured to extract an operation instruction text and an operation object text from the first prompt text after detecting the first prompt text;
[0032] A sample matching module, used for acquiring a positive sample matching the first prompt text from the preset corpus based on the operation instruction text and the operation object text;
[0033] The prompt optimization module is used to optimize the first prompt text based on the positive sample to obtain a second prompt text.
[0034] In addition, to achieve the above-mentioned purpose, the present application also provides an electronic device, which includes: a memory, a processor, and a corpus-based prompt text optimization program stored on the memory and executable on the processor, wherein the corpus-based prompt text optimization program, when executed by the processor, implements the corpus-based prompt text optimization method as described above.
[0035] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, including a computer program, which implements the steps of the above-mentioned corpus-based prompt text optimization method when executed by a processor.
[0036] The embodiment of the present application provides a corpus-based prompt text optimization method, device and electronic device. The technical solution of the embodiment of the present application is to extract an operation instruction text and an operation object text from the first prompt text after detecting the first prompt text, and then obtain a positive sample matching the first prompt text from a preset corpus based on the operation instruction text and the operation object text, and finally optimize the first prompt text based on the positive sample to obtain a more instructive second prompt text, so that the embodiment of the present application can enhance the guidance of the prompt text by dynamically generating positive few-sample use cases, so that the large language model can more accurately understand the user's intention, thereby improving the model reasoning effect, and solving the technical problem that the traditional prompt design lacks guidance and leads to poor model reasoning effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0038] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on the structures shown in these drawings without paying any creative work.
[0039] Figure 1 A flowchart diagram of the first embodiment of the corpus-based prompt text optimization method of the present application is provided;
[0040] Figure 2 A flowchart diagram of the second embodiment of the corpus-based prompt text optimization method of the present application is provided;
[0041] Figure 3 A flowchart diagram of a third embodiment of the corpus-based prompt text optimization method of the present application;
[0042] Figure 4 This is a schematic diagram of the module structure of the corpus-based prompt text optimization device according to an embodiment of the present application;
[0043] Figure 5 Schematic diagram of the device structure of the hardware operating environment involved in the corpus-based prompt text optimization method in the embodiment of the present application.
[0044] The purpose, features and advantages of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0045] Exemplary embodiments will be described in detail herein, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The implementations described in the following exemplary embodiments do not represent all implementations consistent with the present application. Instead, they are merely examples of devices and methods consistent with some aspects of the present application as detailed in the appended claims.
[0046] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.
[0047] Currently, the efficient application of large language models relies on carefully designed prompts. Prompts are the bridge for humans to interact with large models. They not only determine the model's understanding of the task, but also directly affect the final output quality.
[0048] However, traditional prompt designs are often too straightforward or lack guidance, causing the model to perform poorly when faced with complex or novel tasks and failing to fully utilize its potential.
[0049] The main solution of the embodiment of the present application is a corpus-based prompt text optimization method, comprising: after detecting a first prompt text, extracting an operation instruction text and an operation object text from the first prompt text; based on the operation instruction text and the operation object text, obtaining a positive sample matching the first prompt text from the preset corpus; based on the positive sample, optimizing the first prompt text to obtain a second prompt text.
[0050] The embodiments of the present application can enhance the guidance of the prompt text by dynamically generating positive few-sample use cases, so that the large language model can more accurately understand the user's intention, thereby improving the model reasoning effect, and solving the technical problem that the traditional prompt design lacks guidance and leads to poor model reasoning effect.
[0051] It should be noted that the execution subject of the embodiments of the present application is an electronic device, which may include but is not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs (Televisions), desktop computers, etc., or any electronic device that can achieve the above functions, and the embodiments of the present application do not specifically limit this. The following uses electronic devices as the execution subject as an example to illustrate the following embodiments of the present application.
[0052] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0053] Please refer to Figure 1 , Figure 1 A flowchart diagram of the first embodiment of the corpus-based prompt text optimization method of the present application is provided.
[0054] The present application proposes a first embodiment of a corpus-based prompt text optimization method, comprising steps S100 to S300:
[0055] Step S100, after detecting the first prompt text, extracting the operation instruction text and the operation object text from the first prompt text;
[0056] Those skilled in the art will know that in a large language model (LLM), a prompt is input information provided by a user or a system to a model, the purpose of which is to guide the model to generate expected output, and the prompt text is the specific text content that constitutes the prompt.
[0057] It should be noted that, in this embodiment, the first prompt text is a prompt text that has not been optimized or still has room for optimization, and the first prompt text is composed of at least two parts: an operation instruction text and an operation object text. Among them, the operation object text is the direct object for the large language model to analyze, process or generate content, and the operation instruction text is used to guide the large language model on how to understand and perform tasks, and may include but is not limited to instruction information for clearly telling the large language model the specific tasks or actions to be performed, output indication information for specifying the format, type or expected response style of the large language model output, role information for defining the role played or attitude taken by the large language model when performing tasks, and constraint information for setting rules that must be followed or content to be avoided when the large language model generates output, etc.
[0058] Exemplarily, the operation object text may be a sentence to be translated in the first prompt text, a concept to be explained, a theme for which a story is required to be generated, and the like.
[0059] After detecting the input first prompt text, this embodiment uses natural language processing technology, keyword extraction, topic model application or deep learning classifier to identify and analyze the first prompt text, and extracts the operation instruction text and the operation object text therefrom, so as to facilitate the subsequent acquisition of positive samples for optimizing the first prompt text from a preset corpus by clearly distinguishing the operation instruction text and the operation object text.
[0060] Step S200, based on the operation instruction text and the operation object text, obtaining a positive sample matching the first prompt text from the preset corpus;
[0061] It should be noted that, in this embodiment, the corpus is a database that stores sample examples. Sample examples refer to a set of input-output pairs used to guide the model on how to understand and perform specific tasks. Positive samples refer to high-quality, correct or reasonable text data that matches the expected output, and are used to guide model generation and improve output quality.
[0062] It is worth mentioning that, in this embodiment, the positive sample can be a complete sample example or an output part of the sample example.
[0063] In the related art, after receiving the input prompt text, the large language model analyzes, modifies, expands or generates new content for the operation object text in the prompt text according to the guidance of the operation instruction text in the prompt text. For example, in entity extraction tasks, the operation instruction text may be "extract location nouns from the target text", and the operation object text may be "the target text is: how long does it take to walk from location A to location B", and the large language model should extract location A and location B from the operation object text according to the operation instruction text, thereby generating the output result "location A, location B".
[0064] However, such prompt text only contains operation instruction text and operation object text. In some scenarios, the model may find it difficult to achieve optimal performance by relying solely on operation instruction text and operation object text, especially when the task complexity is high or the model lacks sufficient relevant experience.
[0065] Therefore, based on the first prompt text, this embodiment obtains positive samples that match the first prompt text from a preset corpus, thereby fusing the positive samples with the first prompt text to optimize the prompt text, so that the model does not only rely on the operation instruction text and the operation object text when performing tasks.
[0066] It is not difficult to understand that there may be multiple positive samples obtained from the corpus that match the first prompt text.
[0067] Step S300: Based on the positive sample, the first prompt text is optimized to obtain a second prompt text.
[0068] In this embodiment, the second prompt text refers to the prompt text obtained by optimizing the first prompt text through the positive sample, and the second prompt text is more instructive than the first prompt text.
[0069] It should be noted that when optimizing the first prompt text based on the positive sample, the positive sample and the first prompt text can be directly spliced together to obtain the second prompt text. Alternatively, according to actual task requirements, the first prompt text can be optimized through grammatical reorganization, semantic enhancement, emotional color adjustment, etc., and then the positive sample can be spliced into it to obtain the second prompt text.
[0070] Exemplarily, in a feasible implementation manner, the step of optimizing the first prompt text based on the positive sample to obtain the second prompt text includes step S310:
[0071] Step S310: concatenate the positive sample with the first prompt text to obtain a second prompt text.
[0072] In this embodiment, a direct splicing method is used to splice the positive sample and the first prompt text into the second prompt text. Specifically, the positive sample can be added to the first prompt text as a prefix or suffix, or inserted between the operation instruction text and the operation object text of the first prompt text.
[0073] It is not difficult to understand that the direct splicing method of this embodiment is simple to operate, easy to implement, has strong scalability, and can effectively enhance the guidance of the prompt text, thereby improving the model reasoning effect.
[0074] After detecting the first prompt text, this embodiment obtains a positive sample matching the first prompt text from a preset corpus, and then optimizes the first prompt text based on the positive sample to obtain a second prompt text with stronger guidance. This enables the embodiment of the present application to dynamically generate positive few-sample use cases, enhance the guidance of the prompt text, enable the large language model to more accurately understand the user's intention, and thus improve the model reasoning effect, thereby solving the technical problem that the traditional prompt design lacks guidance and leads to poor model reasoning effect.
[0075] Based on the above first embodiment, a corpus-based prompt text optimization method of the second embodiment of the present application is proposed.
[0076] In the second embodiment of the present application, the same or similar contents as those in the above embodiments can be referred to the above introduction and will not be described in detail later.
[0077] Please refer to Figure 2 , Figure 2 A flowchart diagram of the second embodiment of the corpus-based prompt text optimization method of the present application is provided.
[0078] In this embodiment, the step of acquiring a positive sample matching the first prompt text from the preset corpus based on the operation instruction text and the operation object text may include steps S210 to S220:
[0079] Step S210, determining a target corpus from a plurality of preset corpora based on the operation instruction text;
[0080] It should be noted that in this embodiment, multiple corpora are preset, and the sample examples stored in each corpus are highly focused on a specific field (e.g., law, medicine, literature, etc.) or task type (e.g., classification, generation, translation, question-answering, etc.), or even on a specific task type in a specific field, such as entity extraction tasks in the medical field. The target corpus refers to a specific corpus that stores data that is closely related to the first prompt text and can provide effective positive samples in a targeted manner, that is, a specific field or specific task type that the sample examples in the target corpus are highly focused on, which is the same as or highly related to the field type or task type to which the first prompt text belongs.
[0081] Therefore, this embodiment first determines at least one of the domain type and task type to which the first prompt text belongs based on the operation instruction text in the first prompt text, so as to select a matching corpus from multiple preset corpora as the target corpus.
[0082] Exemplarily, in a feasible implementation manner, the step of determining a target corpus from a plurality of preset corpora based on the operation instruction text comprises steps S211 to S212:
[0083] Step S211, performing domain type recognition and task type recognition on the operation instruction text to determine the domain type and task type of the first prompt text;
[0084] It should be noted that when performing field type identification, it can be performed in conjunction with the operation object text in the first prompt text, so as to more accurately identify the field type corresponding to the first prompt text.
[0085] In addition, when the first prompt text contains sample examples in addition to the operation instruction text and the operation object text, the sample examples can also be extracted and used together with the operation instruction text to determine the domain type and task type of the first prompt text, or even used together with the operation object text to obtain positive samples that match the first prompt text from the target corpus.
[0086] Step S212: From the preset plurality of corpora, a corpus matching the domain type and the task type is determined as a target corpus.
[0087] This embodiment uses natural language processing technology, keyword extraction, topic model application or deep learning classifier to analyze the operation instruction text to identify the domain type and task type to which the first prompt text belongs. Through the dual identification of domain type and task type, the target corpus can be accurately located from a large number of preset corpora, avoiding blind search and greatly improving the optimization efficiency of the prompt text.
[0088] In addition, since the corpus is subdivided by domain and task, when faced with new domains or tasks, the present implementation can quickly adapt without large-scale reconstruction, and only requires increasing or adjusting the corpus accordingly.
[0089] Step S220: Based on the operation object text, obtain a positive sample matching the first prompt text from the target corpus.
[0090] This embodiment can extract keywords from the operation object text, and then search for positive samples matching the first prompt text from the target corpus based on the extracted keywords. It can also use an algorithm for calculating the similarity between texts to calculate the similarity between the operation object text and each sample example in the target corpus, and then determine the positive sample matching the first prompt text based on the calculated similarity.
[0091] This embodiment subdivides the corpus according to the technical field and task type, and sets a plurality of corpora of different technical fields and different task types. Therefore, when optimizing the first prompt text, according to the operation instruction text in the first prompt text, a corpus whose stored sample examples are highly relevant to the first prompt text (that is, a corpus matching the field type and task type to which the first prompt text belongs) is selected from the preset multiple corpora as the target corpus. Furthermore, according to the operation object text in the first prompt text, the most relevant and representative positive samples are selected in a targeted manner for the optimization of the first prompt text, thereby enhancing the guidance of the prompt text and improving the reasoning effect of the model.
[0092] Based on the above second embodiment, a corpus-based prompt text optimization method of the third embodiment of the present application is proposed.
[0093] In the third embodiment of the present application, the same or similar contents as those in the above embodiments can be referred to the above introduction and will not be repeated later.
[0094] Please refer to Figure 3 , Figure 3 A flowchart diagram of the third embodiment of the corpus-based prompt text optimization method of the present application is provided.
[0095] In this embodiment, the target corpus stores a plurality of sample examples, and the step of acquiring a positive sample matching the first prompt text from the target corpus based on the operation object text may include steps S221 to S223:
[0096] Step S221, calculating the sample similarity between each of the sample examples and the operation object text by using a preset similarity algorithm;
[0097] It should be noted that, in this embodiment, the similarity algorithm may include but is not limited to the cosine similarity algorithm, the semantic similarity algorithm, the edit distance method, etc. The sample similarity is used to measure the degree of association and similarity between the sample example and the operation object text, and the sample similarity can be calculated by a preset similarity algorithm.
[0098] Exemplarily, in a feasible implementation, the preset similarity algorithm is a semantic similarity algorithm, and the sample similarity between the sample example and the operation object text refers to the similarity between the semantic vector of the sample example and the semantic vector of the operation object text. Step S221 may include step S10:
[0099] Step S10, based on the semantic similarity algorithm, calculating the similarity between the semantic vector of each sample example and the semantic vector of the operation object text.
[0100] Those skilled in the art will know that semantic vectors refer to multidimensional vectors used to represent words or phrases in natural language processing, and these vectors can reflect the semantic information of words. In a semantic vector, each word is mapped to a high-dimensional real vector, and the coordinates of the vector reflect the position of the word in different semantic dimensions. Therefore, the semantic similarity between words (that is, the similarity between semantic vectors) can be measured by the distance or angle between vectors, that is, calculated by a semantic similarity algorithm.
[0101] It is not difficult to understand that in this embodiment, the target corpus can be a semantic vector library, that is, the sample examples stored in the target corpus are all stored in the form of semantic vectors. Therefore, the semantic vector of each sample example can be directly obtained from the target corpus, and the semantic vector of the operation object text can be obtained through specially designed word vector models, sentence vector models and other deep learning models.
[0102] It should be noted that in order to ensure the effectiveness and accuracy of the semantic similarity calculation, the semantic vector of the sample example and the semantic vector of the operation object text can be converted from the text format through the same deep learning model. That is, the deep learning model used to convert the sample examples into semantic vectors when constructing the target corpus is the same model as the deep learning model used to convert the operation object text into semantic vectors in this embodiment, or a model obtained using the same technical route and the same training set.
[0103] Step S222, based on the sample similarity, determining the target sample examples from the sample examples that meet the preset similarity condition;
[0104] It should be noted that, in this embodiment, the preset similarity condition is used to select sample examples that are highly relevant to the first prompt text from multiple sample examples in the target corpus, that is, the target sample examples.
[0105] Exemplarily, the preset similarity condition may be that the sample similarity is greater than a preset threshold, or the sample similarity ranks in the top five.
[0106] Step S223: Determine a positive sample matching the first prompt text according to the target sample example.
[0107] It should be noted that, in this embodiment, the target sample example may be directly determined as a positive sample matching the first prompt text, or the output result portion of the target sample example may be determined as a positive sample matching the first prompt text.
[0108] This embodiment uses a preset similarity algorithm to calculate the sample similarity between each sample example and the operation object text, thereby quantifying the degree of similarity between the sample example and the operation object text, and excludes irrelevant or low-quality sample examples through preset similarity conditions, ensuring that the final selected positive sample is highly relevant to the first prompt text, ensuring that the second prompt text obtained after optimizing the first prompt text using the positive sample is more instructive, so that the model can better understand the task, perform the task, and generate high-quality output.
[0109] Based on the third embodiment described above, a corpus-based prompt text optimization method of the fourth embodiment of the present application is proposed.
[0110] In the fourth embodiment of the present application, the same or similar contents as those in the above embodiments can be referred to the above introduction and will not be described in detail later.
[0111] In this embodiment, the sample examples are composed of operation object examples and output result examples. The step of calculating the sample similarity between each of the sample examples and the operation object text by using a preset similarity algorithm may further include steps S20 to S30:
[0112] Step S20, calculating the object similarity between the operation object example of each sample example and the operation object text by using a preset similarity algorithm;
[0113] Those skilled in the art will know that a sample example refers to a set of input-output pairs used to guide a model on how to understand and perform a specific task, and in this embodiment, the sample example consists of an operation object example and an output result example. The operation object example belongs to the input part of the input-output pair, and the output result example belongs to the output part.
[0114] It is not difficult to understand that the operation object example is an example of the operation object text under a specific task type in a specific field, and the output result example is an example of the output result generated by the expected model after the operation object example is input into the large language model under the specific task type in the specific field.
[0115] Exemplarily, in the entity extraction task type, an example of the operation object in the sample example may be a text to be extracted, and an example of the output result may be an entity label obtained after entity extraction is performed on the text to be extracted.
[0116] It should be noted that the sample examples in the corpus can be constructed through expert experience or generated through a large language model.
[0117] In addition, it should be noted that object similarity is used to measure the degree of association and similarity between the operation object example and the operation object text.
[0118] Exemplarily, in a feasible implementation manner, the preset similarity algorithm is a cosine similarity algorithm, and the step of calculating the object similarity between the operation object example of each sample example and the operation object text by using the preset similarity algorithm may include steps S21 to S22:
[0119] Step S21, calculating the cosine similarity between the text vector of the operation object example of each sample example and the text vector of the operation object text by using the cosine similarity algorithm;
[0120] Those skilled in the art will know that a text vector is a set of values obtained by vectorizing a text, which is mathematically represented as a multidimensional vector. A text vector can not only reflect the basic content of a text, but also the relationship between words and context information.
[0121] It should be noted that the cosine similarity algorithm is a commonly used method for evaluating the similarity between two vectors by measuring the cosine value of the angle between them. Cosine similarity is a measure of the similarity between two non-zero vectors by measuring the cosine value of the angle between them.
[0122] In addition, it should be noted that, in this embodiment, the target corpus can be a text vector library, that is, the sample examples stored in the target corpus are all stored in the form of text vectors. Therefore, the text vector of the operation object example of each sample example can be directly obtained from the target corpus, and the text vector of the operation object text can be obtained by text vectorizing the operation object text.
[0123] Step S22: determining the object similarity between the operation object example of each sample example and the operation object text based on the cosine similarity.
[0124] In this embodiment, the object similarity between the operation object example of the sample example and the operation object text can be calculated by the cosine similarity between the text vector of the operation object example of the sample example and the text vector of the operation object text.
[0125] In one example, cosine similarity can be directly used as object similarity. For example, if the cosine similarity between the text vector of the operation object example of sample example A and the text vector of the operation object text a is 0.9, then the object similarity between the operation object example of sample example A and the operation object text a is 0.9.
[0126] In another example, the value obtained by dividing the cosine similarity by the maximum value among the cosine similarities can be used as the object similarity. For example, in sample examples A, B, and C, the cosine similarity between the text vector of the operation object example of sample example A and the text vector of the operation object text D is 0.8, the cosine similarity between the text vector of the operation object example of sample example B and the text vector of the operation object text D is 0.6, the cosine similarity between the text vector of the operation object example of sample example C and the text vector of the operation object text D is 0.4, and the maximum value among the cosine similarities is 0.8, then the object similarity between the operation object example of sample example A and the operation object text D is 1, the object similarity between the operation object example of sample example B and the operation object text D is 0.75, and the object similarity between the operation object example of sample example C and the operation object text D is 0.5.
[0127] Step S30, determining the sample similarity between each of the sample examples and the operation object text based on the object similarity;
[0128] It should be noted that, in this embodiment, the object similarity between the operation object example of the sample example and the operation object can be directly used as the sample similarity between the sample example and the operation object, or it can be used as the sample similarity between the sample example and the operation object after certain transformation on the basis of the object similarity. For example, the object similarity is divided by the maximum value of each object similarity and then used as the sample similarity between the sample example and the operation object.
[0129] It is worth mentioning that the step of determining a positive sample matching the first prompt text according to the target sample example may include step S40:
[0130] Step S40: determining the output result example of the target sample example as a positive sample matching the first prompt text.
[0131] This embodiment determines the output result example in the target sample example that meets the preset similarity condition as a positive sample that matches the first prompt text, and then optimizes the first prompt text based on the output result example, which can effectively enhance the guidance of the prompt text, and then guide the model to learn the correct output mode, guide the model to generate answers that are more in line with user expectations, reduce the possibility of the model generating inappropriate or erroneous information, and improve the model reasoning effect.
[0132] In addition, please refer to Figure 4 , Figure 4 This is a schematic diagram of the module structure of the corpus-based prompt text optimization device according to an embodiment of the present application;
[0133] The present application also provides a corpus-based prompt text optimization device, the corpus-based prompt text optimization device comprising:
[0134] A text extraction module 10, for extracting an operation instruction text and an operation object text from the first prompt text after detecting the first prompt text;
[0135] A sample matching module 20, configured to obtain a positive sample matching the first prompt text from the preset corpus based on the operation instruction text and the operation object text;
[0136] The prompt optimization module 30 is used to optimize the first prompt text based on the positive sample to obtain a second prompt text.
[0137] In one embodiment, the first prompt text includes an operation instruction text, and the sample matching module 20 is further configured to:
[0138] Based on the operation instruction text, determining a target corpus from a plurality of preset corpora;
[0139] Based on the operation object text, a positive sample matching the first prompt text is obtained from the target corpus.
[0140] In one embodiment, the sample matching module 20 is further configured to:
[0141] Performing domain type recognition and task type recognition on the operation instruction text to determine the domain type and task type of the first prompt text;
[0142] From the preset multiple corpora, a corpus matching the domain type and the task type is determined as a target corpus.
[0143] In one embodiment, the target corpus stores a plurality of sample examples, and the sample matching module 20 is further configured to:
[0144] Calculate the sample similarity between each sample example and the operation object text by using a preset similarity algorithm;
[0145] Based on the sample similarity, sample examples that meet a preset similarity condition are determined as target sample examples;
[0146] According to the target sample example, a positive sample matching the first prompt text is determined.
[0147] In one embodiment, the sample example consists of an operation object example and an output result example, and the sample matching module 20 is further used to:
[0148] Calculating the object similarity between the operation object example of each sample example and the operation object text by using a preset similarity algorithm;
[0149] Based on the object similarity, determining the sample similarity between each of the sample examples and the operation object text;
[0150] The output result example of the target sample example is determined as a positive sample matching the first prompt text.
[0151] In one embodiment, the preset similarity algorithm is a cosine similarity algorithm, and the sample matching module 20 is further used for:
[0152] Calculating the cosine similarity between the text vector of the operation object example of each sample example and the text vector of the operation object text by using the cosine similarity algorithm;
[0153] Based on the cosine similarity, the object similarity between the operation object example of each sample example and the operation object text is determined.
[0154] In one embodiment, the prompt optimization module 30 is further used for:
[0155] The positive sample is concatenated with the first prompt text to obtain a second prompt text.
[0156] The corpus-based prompt text optimization device provided by the present application adopts the corpus-based prompt text optimization method in the above-mentioned embodiment, which can solve the technical problem that the traditional prompt design lacks guidance and leads to poor model reasoning effect. Compared with the prior art, the beneficial effects of the corpus-based prompt text optimization device provided by the present application are the same as the beneficial effects of the corpus-based prompt text optimization method provided by the above-mentioned embodiment, and the other technical features in the corpus-based prompt text optimization device are the same as the features disclosed in the corpus-based prompt text optimization method in the above-mentioned embodiment, which will not be repeated here.
[0157] In addition, please refer to Figure 5 , Figure 5 Schematic diagram of the device structure of the hardware operating environment involved in the corpus-based prompt text optimization method in the embodiment of the present application.
[0158] The present application also provides an electronic device, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the corpus-based prompt text optimization method in the above-mentioned embodiment.
[0159] Reference below Figure 5 , which shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiments of the present application. The electronic devices in the embodiments of the present application may include but are not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs (Televisions), desktop computers, etc., or any electronic device that can implement the above functions. Figure 5 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0160] like Figure 5As shown, the electronic device may include a processing device 1001 (e.g., a central processing unit, a graphics processor, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. In RAM1004, various programs and data required for the operation of the electronic device are also stored. The processing device 1001, ROM1002, and RAM1004 are connected to each other through a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Generally, the following systems can be connected to the I / O interface 1006: an input device 1007 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 1008 including, for example, a liquid crystal display (LCD: Liquid Crystal Display), a speaker, a vibrator, etc.; a storage device 1003 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 1009. The communication device 1009 can allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although the figure shows an electronic device with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems can be implemented or have alternatively.
[0161] In particular, according to the embodiments disclosed in the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present application include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.
[0162] The electronic device provided by the present application adopts the corpus-based prompt text optimization method in the above-mentioned embodiment, which can solve the technical problem that the traditional prompt design lacks guidance and leads to poor model reasoning effect. Compared with the prior art, the beneficial effects of the electronic device provided by the present application are the same as the beneficial effects of the corpus-based prompt text optimization method provided by the above-mentioned embodiment, and other technical features in the electronic device are the same as the features disclosed in the corpus-based prompt text optimization method in the above-mentioned embodiment, which will not be repeated here.
[0163] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0164] In addition, the present application also provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the corpus-based prompt text optimization method in the above-mentioned embodiment.
[0165] The computer-readable storage medium provided in the present application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, systems or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM: Random Access Memory), a read-only memory (ROM: Read Only Memory), an erasable programmable read-only memory (EPROM: Erasable Programmable Read Only Memory or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM: CD-Read Only Memory), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by or in combination with an instruction execution system, system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency: Radio Frequency), etc., or any suitable combination of the above.
[0166] The computer-readable storage medium may be included in the electronic device, or may exist independently without being installed in the electronic device.
[0167] The computer-readable storage medium carries one or more programs. When the one or more programs are executed by an electronic device, the electronic device: after detecting a first prompt text, extracts an operation instruction text and an operation object text from the first prompt text; based on the operation instruction text and the operation object text, obtains a positive sample matching the first prompt text from the preset corpus; based on the positive sample, optimizes the first prompt text to obtain a second prompt text.
[0168] Computer program code for performing the operations of the present application may be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0169] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present application. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0170] The modules involved in the embodiments described in this application may be implemented by software or hardware, wherein the name of the module does not constitute a limitation on the unit itself in some cases.
[0171] The computer-readable storage medium provided by the present application stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned corpus-based prompt text optimization method, which can solve the technical problem that the traditional prompt design lacks guidance and leads to poor model reasoning effect. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided by the present application are the same as the beneficial effects of the corpus-based prompt text optimization method provided by the above-mentioned embodiment, and will not be repeated here.
[0172] In addition, an embodiment of the present application also provides a computer program product, including a computer program, and when the computer program is executed by a processor, the corpus-based prompt text optimization method in the above embodiment is implemented.
[0173] The computer program product provided by the present application can solve the technical problem that the traditional prompt design lacks guidance, resulting in poor model reasoning effect. Compared with the prior art, the beneficial effects of the computer program product provided by the embodiment of the present application are the same as the beneficial effects of the corpus-based prompt text optimization method provided by the above embodiment, which will not be repeated here.
[0174] The above descriptions are only some embodiments of the present application, and are not intended to limit the patent scope of the present application. All equivalent structural changes made using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect applications in other related technical fields are included in the patent protection scope of the present application.
Claims
1. A corpus-based prompt text optimization method, characterized in that: The method comprises: After detecting a first prompt text consisting of at least an operation instruction text and an operation object text, extracting the operation instruction text and the operation object text from the first prompt text, wherein the operation instruction text is used to guide the large language model to understand and execute the task corresponding to the first prompt text, and the operation object text is a direct object for the large language model to understand and execute the task corresponding to the first prompt text; Based on the operation instruction text and the operation object text, a positive sample matching the first prompt text is obtained from the preset corpus; Optimizing the first prompt text by at least one of grammatical reorganization, semantic enhancement, and emotional color adjustment to obtain an optimized first prompt text; The positive sample is concatenated with the optimized first prompt text to obtain a second prompt text; The step of acquiring a positive sample matching the first prompt text from the preset corpus based on the operation instruction text and the operation object text comprises: Based on the operation instruction text, determining a target corpus from a plurality of preset corpora; Based on the operation object text, obtaining a positive sample matching the first prompt text from the target corpus; The step of determining a target corpus from a plurality of preset corpora based on the operation instruction text comprises: Performing domain type recognition and task type recognition on the operation instruction text to determine the domain type and task type of the first prompt text; From the preset multiple corpora, a corpus matching the domain type and the task type is determined as a target corpus; The target corpus stores a plurality of sample examples, and the step of acquiring a positive sample matching the first prompt text from the target corpus based on the operation object text includes: Calculate the sample similarity between each sample example and the operation object text by using a preset similarity algorithm; Based on the sample similarity, determining the sample examples that meet the preset similarity condition as target sample examples; Determine the target sample example as a positive sample matching the first prompt text; The preset similarity algorithm is a semantic similarity algorithm, and the sample similarity between the sample example and the operation object text is: the similarity between the semantic vector of the sample example and the semantic vector of the operation object text. The step of calculating the sample similarity between each sample example and the operation object text by using the preset similarity algorithm includes: Based on the semantic similarity algorithm, the similarity between the semantic vector of each sample example and the semantic vector of the operation object text is calculated.
2. A corpus-based prompt text optimization device, characterized in that: The device comprises: A text extraction module, configured to extract the operation instruction text and the operation object text from the first prompt text after detecting a first prompt text consisting of at least an operation instruction text and an operation object text, wherein the operation instruction text is used to guide the large language model to understand and execute the task corresponding to the first prompt text, and the operation object text is a direct object for the large language model to understand and execute the task corresponding to the first prompt text; A sample matching module, used for acquiring a positive sample matching the first prompt text from the preset corpus based on the operation instruction text and the operation object text; A prompt optimization module is used to optimize the first prompt text by at least one of grammatical reorganization, semantic enhancement, and emotional color adjustment to obtain an optimized first prompt text; and concatenate the positive sample with the optimized first prompt text to obtain a second prompt text; The sample matching module is also used for: Based on the operation instruction text, determining a target corpus from a plurality of preset corpora; Based on the operation object text, obtaining a positive sample matching the first prompt text from the target corpus; The sample matching module is also used for: The operation instruction text performs domain type recognition and task type recognition to determine the domain type and task type of the first prompt text; From the preset multiple corpora, a corpus matching the domain type and the task type is determined as a target corpus; The target corpus stores a plurality of sample examples, and the sample matching module is further used for: Calculate the sample similarity between each sample example and the operation object text by using a preset similarity algorithm; Based on the sample similarity, determining the sample examples that meet the preset similarity condition as target sample examples; Determine the target sample example as a positive sample matching the first prompt text; The preset similarity algorithm is a semantic similarity algorithm, and the sample similarity between the sample example and the operation object text is: the similarity between the semantic vector of the sample example and the semantic vector of the operation object text. The step of calculating the sample similarity between each sample example and the operation object text by using the preset similarity algorithm includes: Based on the semantic similarity algorithm, the similarity between the semantic vector of each sample example and the semantic vector of the operation object text is calculated.
3. An electronic device, characterized in that: include: A memory, a processor, and a corpus-based prompt text optimization program stored in the memory and executable on the processor, wherein the corpus-based prompt text optimization program implements the steps of the corpus-based prompt text optimization method according to claim 1 when executed by the processor.
4. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the corpus-based prompt text optimization method according to claim 1 are implemented.
Citation Information
Patent Citations
Text processing method and device based on artificial intelligence, equipment and storage medium
CN116861875A
Emotion analysis method and device and big language model training method and device
CN117436438A
Large model prompt data processing method and device, electronic equipment and storage medium
CN117633194A