Prompt word optimization method and device, electronic equipment and readable storage medium

By compressing and rewriting the prompt words of large language models at multiple levels, the problem of excessive token consumption is solved, resulting in cost savings, improved response speed, and improved output quality, making it suitable for various AI applications.

CN121833943APending Publication Date: 2026-04-10BEIJING KNOWNSEC INFORMATION TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
BEIJING KNOWNSEC INFORMATION TECHNOLOGY CO LTD
Filing Date
2025-12-23
Publication Date
2026-04-10

AI Technical Summary

Technical Problem

Large language models face the problem of excessive token consumption when processing long contexts, leading to increased demand for computing resources and high economic costs, and may cause truncation errors or response delays, especially under cloud service billing models.

Method used

By obtaining the context of the prompt, compression is performed if the compression conditions are met until the token reduction rate is within a preset range. Then, a multi-level rewriting strategy is executed to make the number of tokens in the new prompt digest less than or equal to the number of tokens in the original prompt digest. The semantic similarity and quality evaluation process is used to optimize the process and ensure semantic integrity and efficiency.

Benefits of technology

Significantly reduces token consumption, lowers computing costs, improves response speed and output quality, is suitable for various AI application scenarios, supports multiple model interfaces and platforms, provides adaptive optimization and cost monitoring, and enhances user experience.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121833943A_ABST
    Figure CN121833943A_ABST
Patent Text Reader

Abstract

The invention provides a cue word optimization method and device, electronic equipment and a readable storage medium. The method comprises the steps of obtaining a cue context; if the prompt context meets the compression condition, compressing the prompt context until the Token reduction rate is within a preset range, and obtaining an original prompt abstract; and executing a multi-level rewriting strategy on the original prompt abstract, so that the number of Tokens corresponding to the new prompt abstract obtained by rewriting is smaller than or equal to the number of Tokens of the original prompt abstract. According to the method, the problem that Token consumption is too high when a large language model processes a long context can be solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of artificial intelligence technology, and more specifically, to a method, apparatus, electronic device, and readable storage medium for optimizing prompt words. Background Technology

[0002] With the rapid development of artificial intelligence technology, large-scale language models have become a core technology in the field of natural language processing, and are widely used in generative AI, machine translation, sentiment analysis, and many other areas. These models, with their powerful computing capabilities and massive amounts of training data, are able to handle complex language tasks and provide high-quality output.

[0003] In current technical implementations, input prompts for large language models often contain lengthy contextual information, such as multi-turn dialogue history, long document summaries, or detailed background information. Because the model needs to process each token individually, this directly leads to a sharp increase in token consumption, significantly increasing computational resource requirements and economic costs. Furthermore, excessively long prompts may exceed the model's context window limits, causing truncation errors or response delays, severely impacting user experience. In cloud service billing models, the token-based billing mechanism makes cost issues particularly prominent, especially in high-frequency, large-scale deployment scenarios.

[0004] Therefore, considering that most existing systems lack effective optimization mechanisms to reduce unnecessary token expenditures, which not only leads to inefficiency but also wastes resources, there is an urgent need for an intelligent prompt word optimization method to balance cost control and performance improvement, and achieve more sustainable AI applications. Summary of the Invention

[0005] In view of this, the purpose of the present invention is to provide a method, apparatus, electronic device and readable storage medium for optimizing prompt words, which can solve the problem of excessive token consumption faced by large language models when processing long contexts, balance cost control and performance improvement, and realize more sustainable AI applications.

[0006] To achieve the above objectives, the technical solutions adopted in the embodiments of the present invention are as follows: In a first aspect, the present invention provides a method for optimizing prompt words, the method comprising: obtaining a prompt context; if the prompt context satisfies a compression condition, compressing the prompt context until the token reduction rate is within a preset range, and obtaining an original prompt summary; performing a multi-level rewriting strategy on the original prompt summary, such that the number of tokens corresponding to the rewritten new prompt summary is less than or equal to the number of tokens in the original prompt summary.

[0007] In an optional implementation, the method further includes: if the semantic similarity between the prompt context and the new prompt summary is less than a preset similarity threshold, then triggering an iterative optimization mechanism until the semantic similarity is greater than or equal to the similarity threshold.

[0008] In an optional implementation, the method further includes: if the semantic similarity between the prompt context and the new prompt summary is greater than or equal to a preset similarity threshold, then determining a quality assessment value for the new prompt summary; when the quality assessment value is less than the preset quality threshold, determining an adjustment amount for the similarity threshold; and updating the similarity threshold according to the adjustment amount.

[0009] In an optional implementation, when the quality assessment value is less than a preset quality threshold, determining the adjustment amount of the similarity threshold includes: determining the difference between the quality assessment value and the quality threshold; and using the product of the preset learning rate and the difference as the adjustment amount.

[0010] In an optional implementation, the method includes: obtaining the embedding vectors corresponding to the prompt context and the new prompt summary respectively; and using the similarity between the embedding vectors as the semantic similarity between the prompt context and the new prompt summary.

[0011] In an optional implementation, if the semantic similarity between the prompt context and the new prompt summary is greater than or equal to a preset similarity threshold, then determining the quality assessment value of the new prompt summary includes: converting the new prompt summary into a structured format; inputting the converted new prompt summary into a large model, and then having the large model determine the quality assessment value.

[0012] In optional implementations, the multi-level rewriting strategy includes any one or a combination of the following: lexical level rewriting; syntactic level rewriting; semantic level rewriting.

[0013] Secondly, the present invention provides a prompt word optimization device, comprising: an acquisition module for acquiring a prompt context; an optimization module for compressing the prompt context if the prompt context meets a compression condition until the token reduction rate is within a preset range, thereby obtaining an original prompt summary; the optimization module is further configured to perform a multi-level rewriting strategy on the original prompt summary, such that the number of tokens corresponding to the rewritten new prompt summary is less than or equal to the number of tokens in the original prompt summary.

[0014] Thirdly, the present invention provides an electronic device including a processor and a memory, the memory storing a computer program executable by the processor, the processor executing the computer program to implement the prompt word optimization method described in any of the foregoing embodiments.

[0015] Fourthly, the present invention provides a readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the prompt word optimization method as described in any of the foregoing embodiments.

[0016] The cue word optimization method, apparatus, electronic device, and readable storage medium provided in this invention first acquire the cue context; if the cue context meets compression conditions, it is compressed until the token reduction rate is within a preset range to obtain the original cue summary; subsequently, a multi-level rewriting strategy is performed on the original cue summary, such that the number of tokens corresponding to the rewritten cue summary is less than or equal to the number of tokens in the original cue summary. Through this series of steps, starting from acquiring the original context, the number of tokens is significantly reduced through compression, and then the number of tokens is further optimized through multi-level rewriting, gradually reducing the actual token consumption of large language models when processing long contexts, thereby effectively solving the problem of excessive token consumption.

[0017] To make the above-mentioned objects, features and advantages of the present invention more apparent and understandable, preferred embodiments are described below in detail with reference to the accompanying drawings. Attached Figure Description

[0018] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings used in the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present invention and should not be regarded as a limitation on the scope. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0019] Figure 1 This diagram illustrates an application scenario of the prompt word optimization method provided in an embodiment of the present invention. Figure 2 This diagram illustrates the functional block diagram of the gateway layer provided in an embodiment of the present invention. Figure 3 A schematic flowchart of the prompt word optimization method provided in an embodiment of the present invention is shown; Figure 4 The diagram shows the functional block diagram of the prompt word optimization device provided in an embodiment of the present invention; Figure 5 A structural block diagram of an electronic device provided in an embodiment of the present invention is shown. Detailed Implementation

[0020] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. The components of the embodiments of the present invention described and shown in the accompanying drawings can generally be arranged and designed in various different configurations.

[0021] Therefore, the following detailed description of the embodiments of the invention provided in the accompanying drawings is not intended to limit the scope of the claimed invention, but merely to illustrate selected embodiments of the invention. All other embodiments obtained by those skilled in the art based on the embodiments of the invention without inventive effort are within the scope of protection of the invention.

[0022] It should be noted that relational terms such as "first" and "second" are used merely to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0023] First, the relevant terms involved in the embodiments of the present invention will be explained and described.

[0024] 1. Prompt: In artificial intelligence models (such as large language models), the input text or instruction sequence used to guide the model in generating output.

[0025] 2. Context: The background information, historical dialogue, or relevant data contained in the cue words are used to help the model understand and generate responses.

[0026] 3. Token: The smallest unit processed by the model, usually corresponding to a word, subword, or character. The number of tokens directly affects the computational cost.

[0027] 4. Gateway: A middleware component in the system, responsible for handling input suggestions and interacting with the backend model.

[0028] During their research, the inventors discovered that existing technologies primarily employ direct input methods, where the user or system directly passes complete prompts and context to the AI ​​model for processing. For example, in chatbots or content generation systems, lengthy user-input dialogue histories or document content are used verbatim as input prompts for the model. The model then generates output based on the entire context. This approach relies on the model's built-in context window size and typically constructs prompts through simple concatenation or appending. However, this method also has the following drawbacks: 1. High token consumption: Long contexts directly lead to a surge in the number of tokens, which multiplies the model processing cost, especially in cloud service billing models (such as token-based billing).

[0029] 2. Inefficiency: Overly long prompts may exceed the model's context window limits, leading to truncation or incorrect responses. Additionally, unstructured input may degrade the model's output quality.

[0030] 3. Lack of optimization mechanism: Users need to manually edit the prompts, which increases the complexity of operation and the risk of potential errors.

[0031] 4. Uncontrollable costs: In large-scale applications, the cumulative token consumption may lead to high costs, and it is impossible to optimize from the source.

[0032] To address the aforementioned technical deficiencies, this invention provides a prompt word optimization method that can solve the problem of excessive token consumption faced by large language models when processing long contexts.

[0033] Please see Figure 1 , Figure 1 The illustration shows an application scenario of the prompt word optimization method provided in this embodiment of the invention. This application scenario includes a user terminal 101 and a server 102. The server 102 deploys a gateway layer 1021 and a backend AI model 1022.

[0034] The data interaction process between user terminal 101 and server 102 is as follows: user terminal 101 sends a request containing the original prompt words and context information to server 102; server 102 executes the prompt word optimization method provided in this embodiment of the invention on the received input through its internal gateway layer 1021; then forwards the optimized prompt words to the backend AI model 1022 to perform inference tasks and receives the output results returned by the model; finally, the result is encapsulated through gateway layer 1021 and sent back to user terminal 101 to complete the response loop.

[0035] Optionally, the user terminal 101 may include, but is not limited to, smartphones, tablets, desktop computers, laptops, intelligent customer service interfaces, etc., as long as they have the ability to send natural language requests to the server, they all fall within the scope of the applicable user terminals of this invention.

[0036] Optionally, server 102 may be, but is not limited to, a virtual machine instance in a cloud service platform, a physical server in a private data center, an edge computing node, or a containerized microservice cluster, on which an operating system, web service middleware, and AI inference runtime environment run, supporting high-concurrency request processing and elastic scaling.

[0037] It should be understood that Figure 1 Only a portion of the components of server 102 are shown in this paper, but this should not be construed as limiting the scope of protection of this invention. In practical applications, server 102 can also integrate other functional modules to realize complete production-grade AI service capabilities, which will not be described here.

[0038] Furthermore, to achieve the prompt word optimization objective provided in the embodiments of the present invention, please refer to... Figure 2 , Figure 2 The diagram shows a functional block diagram of the gateway layer provided in an embodiment of the present invention, including an input interface 1021-1 and a digest and rewrite module 1021-2.

[0039] In this embodiment of the invention, the input interface 1021-1 is used to receive the original prompts and context information submitted by the user or the system, including but not limited to multi-turn dialogue history, system instructions, knowledge base summaries, document fragments, and session metadata (such as user roles, timestamps, business tags, etc.), and organizes the information into an original token sequence and related context parameters as the input basis for subsequent optimization processing. The summarization and rewriting module 1021-2 includes a compression submodule and a rewriting submodule, which can work together to achieve intelligent phased optimization of the input prompts, thereby maximizing the saving of token consumption while ensuring the complete transmission of task intent, which will be described in detail in the subsequent prompt optimization method.

[0040] Figure 2 The modules shown can work in conjunction with the backend AI model 1022 to complete the prompt word optimization method provided in this embodiment of the invention, thereby solving the problem of excessive token consumption faced by large language models when processing long contexts, which will be described in detail in the following content.

[0041] In one embodiment of the present invention, except Figure 2In addition to the key functional modules shown, the gateway layer 1021 may also include a cost estimation module, a quality assessment and feedback module, and other monitoring components. The cost estimation module can be used to estimate the economic cost savings brought by each optimization; the quality assessment and feedback module is used to quantitatively analyze the semantic consistency of prompt words before and after rewriting, and dynamically adjust the optimization strategy based on the model's output quality indicators, forming an adaptive learning closed loop; other monitoring components may include functional units such as latency monitoring, throughput statistics, and error rate tracking, to ensure the stability and observability of the system.

[0042] In one embodiment of the present invention, the gateway layer 1021 may also deploy some auxiliary services to support the efficient operation of the core module. For example, an embedding service can be called to convert the original prompt words and optimized prompt words into high-dimensional vector representations; a caching service can store frequently occurring prompt word templates and their optimization results to achieve fast hits and reuse, reducing the overhead of repeated calculations; and a vector database can be used for long-term storage of embedding vectors.

[0043] See also Figure 2 The backend AI model 1022 is a large-scale language model that can communicate with the gateway layer 1021 through a standard API interface. The gateway layer 1021 sends optimized structured prompts to the backend AI model 1022; after the model completes inference, it returns the generated text or structured response result. Throughout the process, the gateway layer 1021 can also record key indicators such as token consumption, semantic similarity, and output quality score at each stage for subsequent cost accounting, performance analysis, and strategy iteration optimization.

[0044] exist Figure 1 and Figure 2 In the application scenario shown, this embodiment of the invention provides a prompt word optimization method, which can be achieved by... Figure 1 The server in the middle will execute it. Please see [link to relevant documentation]. Figure 3 , Figure 3 A schematic flowchart of the prompt word optimization method provided in an embodiment of the present invention is shown, which may include steps S301 to S303, as explained below: S301: Obtain the prompt context; In this embodiment of the invention, the prompt context includes, but is not limited to: system prompt, knowledge base search summary, document fragment, and historical multi-turn conversation.

[0045] S302: If the context of the prompt meets the compression condition, the context of the prompt is compressed until the token reduction rate is within the preset range, and the original prompt summary is obtained. In this embodiment of the invention, compression conditions are used to determine whether a compression process needs to be initiated, in order to avoid unnecessary computational overhead on short texts or low-complexity contexts, thereby improving the overall efficiency of the system. Token reduction rate refers to the relative reduction ratio of the number of tokens before and after the prompt word is compressed, used to quantify the compression effect and as a basis for subsequent cost estimation and quality control.

[0046] S303: Perform a multi-level rewriting strategy on the original hint digest, such that the number of tokens corresponding to the new hint digest obtained by rewriting is less than or equal to the number of tokens in the original hint digest.

[0047] Unlike existing technologies, the embodiments of the present invention start by obtaining the original context, then significantly reduce the number of tokens through compression processing, and further optimize the number of tokens through multi-level rewriting, thereby gradually reducing the actual token consumption of large language models when processing long contexts, thus effectively solving the problem of excessive token consumption.

[0048] Next, the embodiments of the present invention will be described in conjunction with the relevant accompanying drawings. Figure 1 The optimization process for the displayed prompt words is explained in detail.

[0049] In one embodiment of the present invention, the prompt context information in step S301 comes from user input or original request data automatically generated by the system, and is transmitted to the server gateway layer via API calls through the front-end application, client or business system. It may include highly redundant text such as multi-turn dialogue history, long document summary or knowledge retrieval results. In one embodiment of the present invention, while obtaining the prompt context information, the token sequence of the prompt context can also be obtained, denoted as... This token sequence will participate in subsequent optimization processes. Meanwhile, upon obtaining... At the same time, the number of tokens can also be recorded, denoted as ,in, This provides a basis for subsequently determining the compression effect.

[0050] In one embodiment of the present invention, the compression conditions in step S302 can be flexibly set by those skilled in the art. For example, the compression conditions may include, but are not limited to, the following: the number of context tokens exceeds a preset threshold (e.g., 300 tokens); or, the number of context tokens exceeds a predetermined proportion (e.g., 80%) of the target AI model context window size.

[0051] In this embodiment of the invention, the compression conditions can be determined by... Figure 1The gateway layer 1021 is dynamically configured and supports adaptive adjustment of threshold parameters according to different application scenarios (such as real-time customer service).

[0052] In one embodiment of the present invention, the compression method used in step S302 identifies redundant or secondary information in the prompt context, such as repetitive expressions, irrelevant details, or low-information-density paragraphs, and refines the prompt context into a more concise version, namely the original prompt summary in this embodiment of the present invention. This serves as the input basis for the subsequent rewriting process. For example, for a multi-turn dialogue history containing thousands of words, key dialogue points and core viewpoints can be extracted to generate a summary version with half the length, while preserving the integrity of the original meaning.

[0053] Optionally, the compression method can be, but is not limited to, intelligent summarization methods based on natural language processing, including two modes: extractive summarization and generative summarization. Extractive summarization can select several sentences with the highest information density in the context to form a summary through keyword extraction, sentence scoring, and dependency parsing; generative summarization can use a fine-tuned sequence-to-sequence model (such as T5-small) to perform semantic understanding and reconstruction of the original context, generating a more compact but semantically equivalent new text.

[0054] In one embodiment of the present invention, the compressed original hint digest can also be represented by a token sequence, denoted as . Correspondingly, the number of compressed tokens is denoted as... , So, the token reduction rate It can be obtained through the following formula:

[0055] In one embodiment of the present invention, to quantify the compression effect, a range can be preset, such as 30% to 70%. If the current compression result does not reach this range, the system will iteratively adjust the compression strategy. For example, it may enhance the digest strength or switch to a generative digest algorithm until the token reduction rate meets the preset range.

[0056] Next, based on the original hint digest obtained by compression in step S302, a rewriting strategy can be executed in step S303 to further reduce the number of tokens and achieve the goal of reducing token consumption.

[0057] In one embodiment of the present invention, the multi-level rewriting strategy in step S303 may include any one or a combination of the following: (1) Lexical layer rewriting: use synonym replacement and redundant word removal, calculate semantic similarity based on pre-trained word vectors (such as Word2Vec), and reduce the number of tokens while retaining the core semantics.

[0058] (2) Syntactic layer rewriting: Identify the main body of the sentence through dependency parsing, remove the modifying components, and break down the complex sentence into simple sentences.

[0059] (3) Semantic layer rewriting: The BERT encoder is used to extract semantic representations, and key information fragments are identified through the attention mechanism and reorganized into a more compact expression.

[0060] It should be understood that the various rewriting strategies mentioned above are merely examples. In real-world scenarios, other rewriting methods and techniques can be employed to further optimize the number of tokens while maintaining semantic integrity. These methods may include, but are not limited to, sentence simplification, synonym replacement, and key information extraction; the specific choice depends on the actual needs and the specific context.

[0061] In one embodiment of the present invention, the rewriting strategy in step S302 can be implemented by a rewriting engine. The rewriting engine can be, but is not limited to, a system centered on a finely tuned large model. This large model can transform the original prompts into a clearer, more effective, or task-specific form.

[0062] For example, a rewriting engine based on a fine-tuned T5 model can use a pre-trained T5-small model (with approximately 60 million parameters) and fine-tune it on a specially constructed prompt word rewriting dataset. This dataset contains 100,000 pairs of mappings between original and optimized prompt words, covering various scenarios such as dialogue, document summarization, and code generation. The fine-tuning process uses a sequence-to-sequence (Seq2Seq) learning paradigm, optimizing model parameters through a standard cross-entropy loss function to ensure that the generated rewriting results are consistent with the training data.

[0063] Finally, in step S303, the embodiment of the present invention can obtain a rewritten new hint summary, denoted as... The corresponding number of tokens is ,and .

[0064] In one embodiment of the present invention, in order to ensure semantic consistency between the rewritten prompt words and the original prompt words, the prompt word optimization process may further include: if the semantic similarity between the prompt context and the new prompt summary is less than a preset similarity threshold, an iterative optimization mechanism is triggered until the semantic similarity is greater than or equal to the similarity threshold.

[0065] In determining the semantic similarity between the cue context and the new cue summary, we can first obtain the embedding vectors corresponding to the cue context and the new cue summary respectively; then, the similarity between the embedding vectors is taken as the semantic similarity between the cue context and the new cue summary, denoted as Sim, which can be intuitively obtained through the following formula:

[0066] in and The embedding vector representations of the context and the new suggestion summary, " represents the dot product, " " indicates the vector magnitude.

[0067] In the above embodiments, the similarity threshold can be flexibly set by relevant technical personnel, but the setting process should refer to the Token Compression Rate Reduction mentioned above. That is, a higher Reduction should ensure that Sim is still higher than the similarity threshold (using...). (This is a representation) to balance compression and semantic integrity.

[0068] if If the similarity is 0.95, an iterative optimization mechanism is triggered until the semantic similarity is greater than or equal to the similarity threshold. If Sim is greater than or equal to... If so, no further processing is required and the subsequent procedures can be executed.

[0069] In one embodiment of the present invention, the foregoing described the handling method when the semantic similarity between the rewritten and unrewritten versions does not meet the requirements. For cases where the similarity threshold has already been reached, the following feedback process can be further executed: Step 1: If the semantic similarity between the context and the new suggestion summary is greater than or equal to the preset similarity threshold, then determine the quality assessment value of the new suggestion summary; In this embodiment of the invention, when Sim is greater than or equal to This indicates that the rewritten new suggestion summary is semantically complete. Based on this, embodiments of the present invention can further perform quality assessment on the new suggestion summary and adaptively optimize the semantic similarity assessment mechanism based on the quality assessment results.

[0070] In one embodiment of the present invention, for the new prompt summary, a structured format conversion can be performed first, and then the converted new prompt summary can be input into a large model, which will then determine the quality assessment value.

[0071] Optionally, embodiments of the present invention can utilize a template matching engine to perform formatted text conversion. This template matching engine can predefine various common prompt word templates (such as question-and-answer, command, dialogue, etc.), automatically identify the input type using regular expressions and NLP technology, and then convert the natural language into a structured format using a JSON / XML generator based on the identified template type. For example, the original input text is: "Please help me summarize this article about the development of artificial intelligence, focusing on technological breakthroughs and application scenarios." The rewritten text, converted into a structured format, is: {"task":"summarize", "topic":"AI_development", "focus":["tech_breakthrough", "applications"]}. This structured format is then input into a large model for quality evaluation.

[0072] Optionally, the quality assessment value in the embodiments of the present invention may be represented by, but is not limited to, the Flesch-Kincaid readability index.

[0073] Step 2: When the quality assessment value is less than the preset quality threshold, determine the adjustment amount of the similarity threshold; In this embodiment of the invention, the difference between the quality assessment value and the quality threshold can be determined first; then the product of the preset learning rate and the difference can be used as the adjustment amount.

[0074] Step 3: Update the similarity threshold based on the adjustment amount.

[0075] In this embodiment of the invention, the output quality is based on the model. Reverse the rewrite strategy. If (Quality threshold), then use the adjustment formula: ,in It's the learning rate. It is an adjustment amount used to update the semantic similarity threshold. To improve semantic preservation in the next optimization, the formula associates output quality with semantic mechanisms to achieve adaptive optimization.

[0076] In one embodiment of the invention, the number of tokens can be calculated before and after the rewrite to evaluate the optimization effect. Cost savings are calculated using a multi-stage formula: First, the cost savings in the compression stage are determined based on the token reduction rate obtained during the compression process, as shown in the following formula:

[0077] in, It is the cost of each token.

[0078] Then, the cost savings during the rewriting phase are determined based on the number of tokens before and after the rewriting, as shown in the following formula:

[0079] Therefore, the total cost savings are: SavingsTotal = SavingsCompressed + SavingsRewrite. Alternatively, the total cost savings can also be calculated directly using the following formula:

[0080] in, Both calculation methods demonstrate that the embodiments of the present invention can convert the effects of multi-stage optimization into economic benefits.

[0081] To facilitate a clear and intuitive understanding of the prompt word optimization process provided in this embodiment of the invention, a specific example will be used below to illustrate it.

[0082] Suppose the user enters a long suggestion word. "Please generate a summary based on the following historical dialogue: User A said: I like iPhones because of their elegant design. User B said: But Android is more flexible. User A said: Apple's ecosystem is better. User B said: Android has more customization options. (Continue with the long dialogue, assuming the total length results in a token count of 500)."

[0083] The prompt word optimization process provided by the embodiments of the present invention is as follows: Input interface: Receive Calculate the original number of tokens .

[0084] Summary and Rewrite Module: Compression submodule: Compresses long contexts and generates summary hints. To summarize the conversation between users A and B regarding their phone preferences: A prefers Apple's design and ecosystem, while B prefers Android's flexibility and customization, calculate the number of tokens: .

[0085] The token reduction rate is shown below:

[0086] That is, the embodiments of the present invention achieve an 80% token reduction rate during the compression stage.

[0087] Rewriting submodules: Applying a multi-level rewriting strategy to optimize the compression results: Lexical layer rewriting: Simplify "summarizing the conversation between users A and B about mobile phone preferences" to "comparing the mobile phone preferences of A and B", reducing redundant words; Syntactic rewriting: Extracting core ideas and removing descriptive phrases; The above method generates a new summary prompt. Calculate the number of tokens: (A further 65% reduction compared to the compression phase, ensuring) ).

[0088] Structure transformation: Using the T5 model to transform natural language The result is shown below when converted to JSON format: Json: {"task": "compare_preferences", "users": { "A": ["design", "ecosystem"], "B": ["flexibility", "customization"] }, "topic": "mobile_phones }

[0089] Semantic preservation mechanism: Similarity is calculated using the following formula.

[0090] Formula parameter description: : The dot product of the original and rewritten text embedding vectors, with a value of 0.768; : The magnitude of the embedding vector of the original cue context; The magnitude of the embedding vector of the rewritten new hint summary;

[0091] Assuming a threshold The value of 0.96 > 0.95 indicates that the rewrite passed the semantic integrity verification.

[0092] Cost estimator: Assuming a cost per token USD, ultimately .

[0093] Cost savings: USD Alternatively, use Reduction: USD Model interface: will The data is passed to the AI ​​model to generate an output summary: "User A prefers Apple's design and ecosystem, while User B prefers Android's flexibility and customization options." The output quality is recorded. (Assessed by BLEU score).

[0094] Feedback loop: Assuming a quality threshold Learning rate .because ; Detailed Explanation of Feedback Adjustment Calculation:

[0095] Parameter description: Quality threshold; : The current output quality assessment value; Learning rate, controlling the adjustment range; Adjustment amount for semantic similarity threshold; renew The result is 0.95 + 0.003 = 0.953, to improve the semantic requirements for the next optimization.

[0096] The above embodiments demonstrate how the system achieves optimization through the correlation of various components and formulas: Reduction affects the amount of cost savings, the semantic similarity threshold Sim ensures quality, and the feedback loop adjusts the semantic similarity threshold based on Q, ultimately reducing costs by about 90% and providing efficient and accurate output.

[0097] In summary, the prompt word optimization method provided in this embodiment of the invention has the following advantages: First, the embodiments of this invention propose that compression and rewriting mechanisms can significantly reduce token consumption, for example, achieving a reduction of 80% in the example, directly lowering AI service costs, especially suitable for high-frequency call scenarios such as enterprise chatbots or content generation platforms. Furthermore, estimating total cost savings (Savings) further helps users monitor and predict costs in real time, avoiding resource waste and promoting the sustainable development of AI applications.

[0098] Secondly, by compressing and rewriting the prompt words, the embodiments of this invention significantly reduce model processing time and improve overall response speed. For example, optimizing the original 500-token prompt words to 50-token reduces response latency from several seconds to milliseconds. This is particularly crucial in real-time interactive applications, such as online customer service systems, as it avoids user waiting and improves system throughput and concurrent processing capabilities.

[0099] Furthermore, embodiments of the present invention make the input clearer through structured format conversion, enabling the model to better understand the core intent, resulting in more accurate and relevant output. For example, a semantic preservation mechanism ensures Sim > 0.95, reducing ambiguity and noise, and improving BLEU scores or user satisfaction. In content generation or translation tasks, this can reduce the error rate by 20%-30% and produce higher quality results.

[0100] Furthermore, embodiments of the present invention provide that each module is designed as an independent component, which can be seamlessly integrated into existing AI systems without modifying the backend model or infrastructure. It supports multiple model interfaces (such as GPT and BERT), facilitating expansion across different platforms, such as from single-machine deployment to cloud clusters, adapting to application needs of various scales.

[0101] Finally, the prompt word optimization method provided in this embodiment of the invention is an automated process that requires no manual user intervention and is suitable for non-professional users. Through adaptive adjustments in a feedback loop, the system continuously optimizes performance, provides intuitive cost reports and quality feedback, lowers the barrier to entry, and improves the overall user experience.

[0102] In summary, the embodiments of this invention, by integrating summarizing and rewriting modules and an adaptive optimization mechanism, achieve significant cost savings, efficiency improvements (response time reduced to milliseconds), improved output quality (error rate reduced by 20%-30%), high scalability, and user-friendliness. These benefits not only reduce the actual cost of AI applications but also improve overall performance and usability, making it suitable for various scenarios and promoting the sustainable development of the technology.

[0103] In order to perform the above Figure 3 The corresponding steps are described below, along with an implementation of the prompt word optimization device 40. Please refer to [link / reference]. Figure 4 , Figure 4 A functional block diagram of the prompt word optimization device provided in an embodiment of the present invention is shown. The prompt word optimization device 40 includes: an acquisition module 401 and an optimization module 402.

[0104] Module 401 is used to obtain the prompt context; Optimization module 402 is used to compress the prompt context if the prompt context meets the compression conditions until the token reduction rate is within a preset range, and then obtain the original prompt summary. The optimization module 402 is also used to perform a multi-level rewriting strategy on the original hint digest, so that the number of tokens corresponding to the new hint digest obtained by rewriting is less than or equal to the number of tokens in the original hint digest.

[0105] It is understandable that the acquisition module 401 and the optimization module 402 can be executed collaboratively. Figure 3Each step in the process is used to achieve the corresponding technical effect.

[0106] It should be noted that the prompt word optimization device 40 provided in this embodiment of the invention can be specific hardware on the device or software or firmware installed on the device. The implementation principle and technical effects of the device provided in this embodiment of the invention are the same as those in the foregoing method embodiments. For the sake of brevity, any parts not mentioned in the device embodiments can be referred to the corresponding content in the foregoing method embodiments. Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the systems, devices, and units described above can all be referred to the corresponding processes in the above method embodiments, and will not be repeated here.

[0107] Optionally, the above modules can be stored in the form of software or firmware. Figure 5 The memory shown is either stored in or embedded in the operating system (OS) of the electronic device 50, and can be used by... Figure 5 The processor executes the commands. Meanwhile, the data and program code required to execute these modules can be stored in memory.

[0108] Please see Figure 5 , Figure 5 The diagram illustrates a structural block diagram of an electronic device provided in an embodiment of the present invention, including a memory 501, a processor 502, and a communication interface 503. The memory 501, processor 502, and communication interface 503 are electrically connected to each other directly or indirectly to achieve data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses or signal lines.

[0109] Optionally, the bus can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized into address buses, data buses, control buses, etc. For ease of representation, Figure 5 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.

[0110] In this embodiment of the invention, the processor 502 may be a general-purpose processor, a digital signal processor, an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components, capable of implementing or executing the methods, steps, and logic block diagrams disclosed in this embodiment of the invention. The general-purpose processor may be a microprocessor or any conventional processor. The steps of the methods disclosed in this embodiment of the invention can be directly manifested as being executed by a hardware processor, or executed by a combination of hardware and software modules within the processor. The software modules may be located in the memory 501, and the processor 502 reads the program instructions from the memory 501 and, in conjunction with its hardware, completes the steps of the aforementioned methods.

[0111] In this embodiment of the invention, the memory 501 can be a non-volatile memory, such as a hard disk drive (HDD) or a solid-state drive (SSD), or it can be volatile memory, such as RAM. The memory can also be any other medium capable of carrying or storing desired executable program code having an instruction or data structure form and accessible by a computer, but is not limited thereto. The memory in this embodiment of the invention can also be a circuit or any other device capable of implementing a storage function for storing instructions and / or data.

[0112] The memory 501 can be used to store software programs and modules, such as the instructions / modules of the prompt word optimization device 40 provided in this embodiment of the invention. These can be stored in the memory 501 in the form of software or firmware, or embedded in the operating system (OS) of the electronic device 50. The processor 502 executes various functional applications and data processing by executing the software programs and modules stored in the memory 501. The communication interface 503 can be used to communicate with other node devices for signaling or data.

[0113] Understandable. Figure 5 The structure shown is for illustrative purposes only; the electronic device 50 may also include components that are more advanced than those shown. Figure 5 The more or fewer components shown, or having the same Figure 5 The different configurations shown. Figure 5 The components shown can be implemented using hardware, software, or a combination thereof.

[0114] Based on the above embodiments, the present invention also provides a storage medium in which a computer program is stored. When the computer program is executed by a computer, the computer executes the prompt word optimization model method provided in the above embodiments. For specific implementation, please refer to the method embodiments, which will not be repeated here.

[0115] Based on the above embodiments, the present invention also provides a program product, which includes a computer program. The processor can execute the computer program to implement the prompt word optimization method provided in the embodiments of the present invention. For specific implementation, please refer to the method embodiments, which will not be repeated here.

[0116] In the embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. The apparatus embodiments described above are merely illustrative. For example, the division of units is only a logical functional division, and there may be other division methods in actual implementation. Furthermore, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Additionally, the coupling or direct coupling or communication connection shown or discussed may be through some communication interface; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0117] Furthermore, the units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the objectives of the embodiments of the present invention, depending on actual needs.

[0118] Furthermore, the functional modules in the various embodiments of this application can be integrated together to form an independent part, or each module can exist independently, or two or more modules can be integrated to form an independent part.

[0119] It should be noted that if the function is implemented as a software module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0120] The above description is merely a preferred embodiment of the present invention and is not intended to limit the invention. Various modifications and variations can be made to the present invention by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A method for optimizing prompt words, characterized in that, The method includes: Get the context of the prompt; If the prompt context meets the compression conditions, the prompt context is compressed until the token reduction rate is within a preset range, and the original prompt summary is obtained. A multi-level rewriting strategy is applied to the original hint digest, such that the number of tokens corresponding to the new hint digest obtained by rewriting is less than or equal to the number of tokens in the original hint digest.

2. The prompt word optimization method according to claim 1, characterized in that, The method further includes: If the semantic similarity between the prompt context and the new prompt summary is less than a preset similarity threshold, an iterative optimization mechanism is triggered until the semantic similarity is greater than or equal to the similarity threshold.

3. The prompt word optimization method according to claim 1, characterized in that, The method further includes: If the semantic similarity between the prompt context and the new prompt summary is greater than or equal to a preset similarity threshold, then the quality assessment value of the new prompt summary is determined. When the quality assessment value is less than the preset quality threshold, the adjustment amount of the similarity threshold is determined; The similarity threshold is updated based on the adjustment amount.

4. The prompt word optimization method according to claim 3, characterized in that, When the quality assessment value is less than a preset quality threshold, the adjustment amount of the similarity threshold is determined, including: Determine the difference between the quality assessment value and the quality threshold; The product of the preset learning rate and the difference is used as the adjustment amount.

5. The prompt word optimization method according to any one of claims 2 to 4, characterized in that, The method includes: Obtain the embedding vectors corresponding to the prompt context and the new prompt summary, respectively; The similarity between the embedded vectors is used as the semantic similarity between the cue context and the new cue summary.

6. The prompt word optimization method according to claim 3, characterized in that, If the semantic similarity between the prompt context and the new prompt summary is greater than or equal to a preset similarity threshold, then the quality assessment value of the new prompt summary is determined, including: The new prompt summary is converted into a structured format; After the transformed new suggestion summary is input into the large model, the large model determines the quality assessment value.

7. The prompt word optimization method according to claim 1, characterized in that, The multi-level rewriting strategy includes any one or a combination of the following: lexical level rewriting; syntactic level rewriting; semantic level rewriting.

8. A prompt word optimization device, characterized in that, include: The acquisition module is used to obtain the prompt context; The optimization module is used to compress the prompt context if the prompt context meets the compression conditions, until the token reduction rate is within a preset range, and then obtain the original prompt summary. The optimization module is further configured to perform a multi-level rewriting strategy on the original hint digest, such that the number of tokens corresponding to the new hint digest obtained by rewriting is less than or equal to the number of tokens in the original hint digest.

9. An electronic device, characterized in that, It includes a processor and a memory, the memory storing a computer program that can be executed by the processor to implement the prompt word optimization method according to any one of claims 1-7.

10. A readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the prompt word optimization method as described in any one of claims 1-7.