A method and system for streaming local document replacement for generative models
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- CHINA ELECTRONICS CLOUD DIGITAL INTELLIGENCE TECH CO LTD
- Filing Date
- 2026-07-09
- Publication Date
- 2026-08-07
AI Technical Summary
本发明提供一种面向生成式模型的流式局部文档替换方法,旨在解决现有技术在生成式文档编辑过程中存在的重复锚点导致替换提前、模型锚点不稳定、中文偏移错误、控制标签泄漏、尾部内容残留及缺乏替换前安全校验等问题
[0067] (1) Eliminate premature replacement caused by repeated anchor points: Through the combination of "reverse disambiguation" and "context expansion", the problem of premature termination of replacement range and content residue caused by repeated anchor points in the output of generative models is completely solved, ensuring the accuracy of replacement range.
Smart Images

Figure CN122528883A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the fields of natural language processing (NLP), generative artificial intelligence (AI), streaming data processing and electronic document editing, and specifically to a streaming local document replacement method, system, computer-readable storage medium and electronic device for generative models. Background Technology
[0002] With the rapid development of artificial intelligence technology, generative models have demonstrated powerful capabilities in areas such as article rewriting, expansion, polishing, content revision, and code generation, becoming core tools for intelligent content creation and editing. In document editing scenarios, users typically send the original document and specific modification requests to the generative model, expecting the model to return the modified content. These modifications can be full-text or partial.
[0003] Existing partial document replacement solutions mainly include the following implementation forms:
[0004] (1) Specifying the replacement range based on character subscripts: The client predetermines the starting and ending character subscripts that need to be replaced in the original document and sends this range information to the model, or uses this information to perform the replacement after receiving the model results.
[0005] (2) Precise matching based on user-selected text: The user selects a piece of text in the editor as the identifier of the replacement area, and the system searches for the area in the original document that completely matches the selected text.
[0006] (3) Locating the replacement range by the first and last text fragments: The model or the client agrees in advance that the model returns the starting text fragment and the ending text fragment (i.e., the anchor point) to locate the replacement area in the original document.
[0007] (4) The model returns the complete document and then overwrites the original document: The model directly returns the entire modified document, and the client completely overwrites the original document with the new document.
[0008] (5) Use structured tags to identify the original anchor and the modified text: The model explicitly indicates the starting anchor, ending anchor and the actual modified text in the output through specific structured tags (such as XML or JSON format).
[0009] However, in scenarios where content is output in a streaming manner for generative models and precise local replacement of documents is required, the existing solutions mentioned above, even when combined with conventional engineering techniques (such as streaming caching, XML / JSON tag validation, text hashing, atomic replacement, etc.), still face the following challenges:
[0010] (1) Repeated anchor matching leads to incorrect replacement range: The end anchor (end_text) returned by the generative model may appear multiple times in the original document. Existing systems usually default to using the first matching result as the replacement endpoint. This strategy of simply changing the search direction (e.g., searching from back to front) or relying solely on the first matching is very likely to cause the replacement region to end prematurely when faced with repeated, generic, or ambiguous anchors that the model may output. This results in the original text not being deleted correctly, causing duplicate old and new content or content that should not remain in the document.
[0011] (2) Insufficient anchor point stability leads to location failure or inaccuracy: Generative models are unstable in generating anchor points, and may return anchor points that are too short or not unique, or use middle paragraphs, chapter titles, or even modified text fragments as anchor points instead of accurate identifiers in the original document. Even with text matching context expansion technology, if it is not combined with the characteristics of the output of the generative model, it is difficult to accurately and uniquely identify anchor points within complex document structures and task boundaries, thus increasing the difficulty of location, and there is still a risk of original residue after replacement.
[0012] (3) Inaccurate calculation of Chinese character offset leads to misplacement replacement: When processing documents containing multi-byte characters (such as Chinese, Japanese, Korean, etc. encoded in UTF-8), some systems calculate the offset by bytes. Since a Chinese character usually occupies multiple bytes, the byte offset is inconsistent with the character position (or number of Unicode code points) displayed in the front-end editor, resulting in misplacement of the replacement position.
[0013] (4) Markdown tail content interferes with positioning, resulting in incomplete modification: The original document may contain blank lines, lines containing only spaces or tabs, or isolated Markdown control characters (such as "##", "---", "***", etc.). Existing systems may mistakenly regard them as valid text, or the end anchor points generated by the model may fail to cover these non-semantic contents, resulting in tail markup residue when modifying the whole text or chapters.
[0014] (5) Streaming tag transmission across data blocks causes parsing problems and information leakage: During streaming transmission, the structured control tags output by the model (such as...)<start_text> ,<end_text> Tags (such as data packets) may be split into multiple data blocks for transmission due to network packet splitting, buffer limitations, or other reasons. If the system parses the data block by block, it may result in incomplete or missing tags, or the main content may be sent to the client prematurely before the tags are fully parsed, or even the unclosed parts of the control tags may leak into the final main content.
[0015] (6) Lack of pre-replacement verification mechanism poses security risks: Existing systems often directly perform replacement after obtaining model results, lacking necessary pre-verification, including not verifying whether the anchor points originate from the original document, whether they are unique, whether the order of the start and end anchor points is correct, and whether the replacement endpoint covers the expected modification range. In addition, in concurrent editing scenarios, failure to verify the document version (e.g., through hash values) may lead to misaligned overwriting.
[0016] In summary, existing technologies struggle to simultaneously guarantee the accuracy, stability, and security of local document replacement in streaming scenarios oriented towards generative models. There is an urgent need for a local document replacement method that can systematically address these issues. Summary of the Invention
[0017] To overcome the aforementioned deficiencies in the existing technology, this application proposes a novel streaming local document replacement method and system oriented towards generative models.
[0018] Given that existing technologies for streaming partial document replacement in generative model-oriented scenarios require improvement in user experience, replacement accuracy, and system stability, this invention provides a streaming partial document replacement method for generative models. This method aims to address issues encountered in existing technologies during generative document editing, such as premature replacement due to repeated anchor points, unstable model anchor points, Chinese character offset errors, control tag leakage, residual tail content, and lack of pre-replacement security checks. Addressing the unique challenges of streaming output from generative models, this invention combines core technologies such as reverse disambiguation and context expansion of end anchor points, semantic endpoint recognition and automatic correction of erroneous endpoints, and robust parsing and text isolation of streaming control tags. This results in high-precision and high-stability partial document replacement in complex streaming interaction scenarios.
[0019] To achieve the above objectives, the present invention employs the following technical strategies:
[0020] 1. A combined mechanism for ending anchor point "reverse disambiguation" and "context expansion": Addressing the problem of repeated matching of output anchor points in generative models, leading to premature replacement of the endpoint and potentially unstable or non-unique anchor points generated by the model, this invention innovatively proposes:
[0021] After the starting anchor point is determined, the end anchor point is searched from the end of the document backward within the task boundary, and the last valid match is selected (to resolve premature replacement caused by duplicate anchor points).
[0022] When there are multiple candidates for the ending anchor point, expand the context character by character forward from the target ending anchor point until the expanded anchor point is unique within the target range (to solve the problem of insufficient anchor point stability and improve uniqueness).
[0023] This combined mechanism fundamentally solves the problem of the accuracy and uniqueness of output anchor points in complex texts by generative models, overcoming the limitations of traditional solutions.
[0024] 2. A combined mechanism of "semantic endpoint recognition" and "automatic correction of erroneous endpoints": Addressing the issue that generative models may fail to fully cover the modified scope during full-text or chapter revisions due to inaccurate anchor point generation, resulting in residual content at the end (including non-semantic Markdown tags), this invention innovatively proposes:
[0025] By scanning backwards from the end of the document, it intelligently skips blank lines, whitespace lines, and specific Markdown markup lines to accurately determine the semantic endpoint of the document.
[0026] When the starting anchor point represents a full-text or chapter modification, and the model returns an ending anchor point position earlier than the semantic endpoint, the system automatically generates a unique ending anchor point from the vicinity of the semantic endpoint and corrects the replacement endpoint to the semantic endpoint or physical end.
[0027] This combined mechanism ensures the thoroughness of full-text or chapter modifications, avoids unnecessary residues, and improves the integrity and consistency of document editing.
[0028] 3. A combined mechanism of "robust parsing of streaming control tags" and "text isolation to prevent leakage": To address the issues of control tags potentially being truncated, incompletely parsed, or accidentally leaked into the final text during streaming transmission, this invention innovatively designs:
[0029] A cross-block streaming tag caching and parsing mechanism ensures complete identification and verification even when tags are split (solving the robustness problem of streaming tag parsing).
[0030] After the control tag is fully parsed, the control area is immediately isolated from the subsequent text stream, and any similar control tags entering the text stream are detected, filtered, or blocked in real time (to solve the control tag leakage problem).
[0031] This combination ensures the structural integrity of the streaming output and the purity of the final text, and improves the system's compatibility and security for diverse outputs from generative models.
[0032] 4. "Precise Unicode Character Offset Conversion": Addressing the issue of inaccurate Chinese character offsets under multi-byte character encoding, leading to misalignment between front-end display and back-end replacement, this invention innovatively enforces Unicode code point counting for the final character-level offset conversion during the replacement process after determining the precise byte position. This is not a simple Unicode counting process, but rather embeds it as a crucial step to ensure front-end / back-end consistency, integrating it into the entire complex streaming replacement workflow.
[0033] 5. "Multi-dimensional integrity verification before replacement": This invention integrates multi-dimensional verification such as document version consistency verification (through summary value comparison), anchor point existence, uniqueness, order, range, and control tag existence into the final execution stage of replacement, as a final security guarantee to avoid data corruption caused by unstable model output and concurrent editing.
[0034] Specifically, this application provides the following technical solutions:
[0035] The first aspect of this application provides a streaming local document replacement method for generative models, such as Figure 1 As shown, it includes the following steps:
[0036] S1. Stream receive data blocks output by the generative model. The data blocks contain structured control tags and subsequent text content. Incrementally cache the data blocks to splice control tags across data blocks, and continuously parse and verify the integrity, legality, order, and singleness of the control tags.
[0037] S2. Search for the starting anchor point in the original document, and determine the unique target starting position of the starting anchor point in the original document in combination with the preset task scope;
[0038] S3. After the target start position, search for the end anchor point from the end of the original document or the task boundary forward, and select the last matching position that satisfies the task boundary as the initial replacement endpoint of the end anchor point; if there are still multiple matches of the end anchor point within the target range, expand the context character by character forward from the start position of the initial replacement endpoint to generate new candidate anchor points until a unique end anchor point in the original document is found.
[0039] S4. Scan backwards from the end of the original document, identify and skip blank lines, lines containing only whitespace characters, and lines with specific Markdown tags to determine the semantic endpoint of the original document; if the starting anchor point is located at the beginning of the original document and the ending anchor point is earlier than the semantic endpoint, then truncate and expand from the vicinity of the semantic endpoint to generate a new unique ending anchor point, and correct the replacement endpoint to the semantic endpoint or the physical end.
[0040] S5. Based on the finally determined start byte position and end byte position, calculate the character-level start offset OffsetFrom and end offset OffsetTo using the Unicode code point counting method;
[0041] S6. Perform a multi-dimensional replacement integrity check. After the check passes, replace the range defined by [OffsetFrom, OffsetTo) in the original document with the generated text using atomic operations.
[0042] Furthermore, in the method of this application, in step S1, the control tag includes<attachment_type> ,<start_text> and<end_text> ;
[0043] The verification includes: verification<attachment_type> Check whether the value is within the predefined type set.<start_text> and<end_text> The system checks the complete closure of the tags, whether the three types of tags appear in the protocol order, and whether the entire header control area contains only one set of beginning and end anchor points.
[0044] Furthermore, in the method of this application, in step S1, when the control tag is not completely closed, subsequent data blocks continue to be cached; if a complete and valid tag is still not formed after exceeding the preset cache threshold, it is determined to be a structural anomaly.
[0045] Before the control tag is fully parsed, the main text content is prevented from entering subsequent processing; after the control tag is fully parsed, the subsequently received content is treated as the main text stream, and control tags that unexpectedly appear in the main text stream are detected and processed, including deletion, escaping, blocking, or triggering regeneration.
[0046] Furthermore, in the method of this application, in step S2, when there are multiple candidate positions for the starting anchor point, a unique target starting position is determined by combining the task scope, document chapter boundary information, and user modification instructions: the full-text modification task selects the matching position closest to the beginning of the document; the specified chapter modification task selects the matching position closest to the boundary of the target chapter; and the user selection modification task selects the matching position closest to the starting point of the user selection area.
[0047] Furthermore, in the method of this application, in step S3, the process of expanding the context character by character forward is limited to the same natural paragraph or the same text line. If the preset maximum expansion length still cannot uniquely locate the position, the final positioning is completed by combining the absolute offset or document chapter structure information.
[0048] Furthermore, in the method of this application, in step S4, the specific Markdown mark line includes an isolated heading character "#", a list or emphasis character "*", a list or separator "-", a backtick, and a tilde;
[0049] In step S4, for chapter modification tasks, the correction process is limited by the current chapter boundary, and the actual end boundary of the chapter is determined based on the next same or higher-level Markdown heading.
[0050] Furthermore, in the method of this application, in step S5, the Unicode code point counting method is implemented by the RuneCount function to calculate the number of Unicode code points contained from the beginning of the document to the target byte position.
[0051] Furthermore, in the method of this application, step S6, the multi-dimensional integrity verification before replacement includes: existence of the starting anchor point, existence of the ending anchor point, the starting position being earlier than the ending position, the replacement interval being within the allowed range of the task, the generated text not containing unprocessed control tags, and consistency verification of the original document version summary;
[0052] The original document version summary consistency check is performed by recalculating the summary value of the current original document and comparing it with the initial summary value saved at the start of the replacement process. If they are inconsistent, the automatic replacement is stopped.
[0053] Furthermore, in the method of this application, after the atomic operation is executed in step S6, the metadata of the replacement operation is recorded, including the original anchor point, the corrected anchor point, the original offset, the corrected offset, the replacement text, the document version information, and the abnormal correction record.
[0054] A second aspect of this application provides a streaming local document replacement system for generative models, wherein the system implements the steps of the aforementioned streaming local document replacement method for generative models during runtime, such as... Figure 2 As shown, the system includes:
[0055] A streaming receiving module is used to receive data blocks output by a generative model, wherein the data blocks contain structured control tags and subsequent text content;
[0056] The tag caching and protocol parsing module is used to incrementally cache the data blocks to concatenate control tags across data blocks, and to parse and verify the integrity, legality, order, and single-group nature of the control tags;
[0057] Anchor point positioning and verification module is used to search for the starting anchor point in the original document and determine the unique target starting position of the starting anchor point in the original document in combination with the preset task range;
[0058] The reverse disambiguation and anchor point expansion module is used to search for an end anchor point backward from the end of the original document or the task boundary after the target start position, and select the last matching position that satisfies the task boundary as the initial replacement endpoint of the end anchor point; if there are still multiple matches of the end anchor point within the target range, the context is expanded character by character backward from the start position of the initial replacement endpoint to generate new candidate anchor points until a unique end anchor point in the original document is found.
[0059] The document structure analysis and endpoint correction module is used to scan backward from the end of the original document, identify and skip blank lines, lines containing only whitespace characters, and lines with specific Markdown tags to determine the semantic endpoint of the original document; if the starting anchor point is located at the beginning of the original document and the ending anchor point is earlier than the semantic endpoint, then a new unique ending anchor point is generated by truncating and expanding from the vicinity of the semantic endpoint, and the endpoint is corrected to the semantic endpoint or the physical end.
[0060] The Unicode offset module is used to calculate the character-level start offset OffsetFrom and end offset OffsetTo based on the finally determined start and end byte positions using Unicode code point counting.
[0061] The text isolation and atomic replacement module is used to isolate the subsequent text content after the control tags are fully parsed, and to detect and process control tags that appear unexpectedly in the text stream; and to replace the range defined by [OffsetFrom, OffsetTo) in the original document with the processed text content after the multi-dimensional integrity check passes, and to execute it in an atomic manner.
[0062] A third aspect of this application provides an electronic device, including: a memory and a processor;
[0063] Memory: Used to store computer programs;
[0064] Processor: Used to execute the computer program to implement the steps of the aforementioned streaming local document replacement method for generative models.
[0065] A fourth aspect of this application provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the aforementioned streaming partial document replacement method for a generative model.
[0066] In summary, compared with the prior art, the solution of the present invention has the following advantages:
[0067] (1) Eliminate premature replacement caused by repeated anchor points: Through the combination of "reverse disambiguation" and "context expansion", the problem of premature termination of replacement range and content residue caused by repeated anchor points in the output of generative models is completely solved, ensuring the accuracy of replacement range.
[0068] (2) Enhance the determinism of unstable anchor points: It solves the problem that the output anchor points of generative models may be too short, not unique or unstable. The "anchor point expansion" mechanism generates unique anchor points within the target range, which greatly improves the robustness and determinism of the positioning.
[0069] (3) Eliminate tail residues of full text / chapter modifications: Through the synergistic effect of "semantic end recognition" and "automatic error end correction", it is ensured that the full text or chapter modification task can completely cover the expected scope, avoiding the residues of non-semantic content and isolated Markdown tags.
[0070] (4) Completely solve Chinese character offset errors: Innovatively embed "Unicode code point counting" in the replacement process and combine it with precise anchor point positioning to ensure the absolute accuracy of character-level offset in multi-byte character environment and avoid the problem of inconsistent positioning between the front end and the back end.
[0071] (5) Robust handling of streaming control tags and prevention of leakage: Through the "streaming tag caching and parsing" and "text isolation" mechanisms, the problems of control tags being split, incompletely parsed or accidentally leaked to the final text during streaming transmission are solved, improving the system's compatibility and security for diverse outputs of generative models.
[0072] (6) Improve the overall security and stability of replacement operations: It integrates multi-dimensional "integrity verification before replacement" (including document version summary consistency), which significantly reduces the risk of misalignment and data corruption caused by model output errors, concurrent editing or system anomalies, making intelligent document editing more reliable.
[0073] (7) Good compatibility and high integration: No need to modify the internal structure of the generative model, it can be adapted to different models and workflow platforms and is easy to integrate into existing document editing systems.
[0074] (8) Significantly reduce operation and maintenance costs: By automating the solution of common and difficult-to-locate replacement errors in traditional solutions, the cost of manual inspection and repair is greatly reduced, and the efficiency and automation of intelligent document editing are improved. Attached Figure Description
[0075] To more clearly illustrate the technical solution of this application, the accompanying drawings involved in the description of this invention will be briefly introduced below. It should be noted that the drawings only show some embodiments of the invention. For those skilled in the art, other related drawings can be derived from these drawings without creative effort.
[0076] Figure 1 This is a schematic diagram illustrating the operational steps of the streaming local document replacement method for generative models according to the present invention.
[0077] Figure 2 This is a structural diagram of the streaming local document replacement system for generative models according to the present invention.
[0078] Figure 3 This is a schematic diagram of the overall implementation process of the streaming local document replacement method of the present invention.
[0079] Figure 4 This is a schematic diagram of the overall architecture of the streaming local document replacement system of the present invention.
[0080] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention.
[0081] Caption: Processor-310, Communication Interface-320, Memory-330, Communication Bus-340. Detailed Implementation
[0082] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. It should be understood that the described embodiments are only some embodiments of this application, and not all embodiments. All other embodiments obtained by those skilled in the art based on the embodiments of this application without creative effort are within the protection scope of this application.
[0083] In this document, the term "comprising" and any variations thereof (such as "including," "including," etc.) are open-ended expressions and should be understood as "including but not limited to," meaning that the listed content is not exhaustive and may include other content not explicitly mentioned. The term "based on" should be understood as "at least partially based on," meaning that the basis or condition referred to may not be the only factor and may involve other relevant factors. The term "one embodiment" should be understood as "at least one embodiment," meaning that the described embodiment is not the only possible implementation, and other similar embodiments may exist.
[0084] This invention provides the following technical solution:
[0085] A streaming local document replacement method for generative models includes the following main steps:
[0086] 1. Streaming Reception and Robust Tag Parsing: The output data blocks of the generative model are received in a streaming manner, incrementally cached, and continuously parsed for the structured control tags. This step ensures that even if tags are split into multiple data blocks, their legality, order, and singularity can be fully identified and verified, and subsequent text is isolated beforehand to prevent tag leakage.
[0087] 2. Starting Anchor Point Location: Search for the parsed starting anchor point in the original document, and combine it with the task scope, document structure and user instructions to determine the unique starting position of the target.
[0088] 3. End Anchor Point Reverse Disambiguation and Context Expansion: After determining the starting position, the end anchor point output by the model is searched. Unlike conventional first matching or simple reverse search, this invention searches backward from the end of the document within the task boundary, prioritizing the last matching position that satisfies the task boundary constraints as the initial replacement endpoint, effectively solving the problem of premature termination of replacement due to duplicate anchor points. If multiple matches still exist for the end anchor point within the target range, the context is expanded character by character backward from its starting position until a unique end anchor point in the original text is generated, addressing the issue of unstable anchor points generated by the model.
[0089] 4. Semantic Endpoint Recognition and Automatic Correction of Erroneous Endpoints: The system scans backwards from the end of the original document, intelligently identifying and skipping blank lines, lines containing only whitespace characters, and lines marked with specific Markdown tags to determine the semantic endpoints of the original document. If the starting anchor point is determined to be at the beginning of the original document (or representing a full-text modification) and the ending anchor point is earlier than the semantic endpoint, the system automatically corrects the ending anchor point, expanding and replacing the endpoint from the vicinity of the semantic endpoint (by trunculating and expanding to generate a new unique anchor point). This ensures complete coverage of the entire document or chapter modification scope and avoids residual content at the end.
[0090] 5. Precise Unicode character offset conversion: Based on the final determined start and end byte positions, the corresponding character-level offset is precisely calculated using the Unicode code point counting method to solve the problem of inaccurate Chinese character offset under multi-byte character encoding, ensuring the precise consistency between front-end display and back-end replacement.
[0091] 6. Multi-dimensional Integrity Verification and Atomic Replacement: Before performing a replacement, a multi-dimensional integrity verification is conducted (including anchor point existence, uniqueness, order, range, control tag existence, and document version summary consistency) to avoid misaligned replacements caused by model generation errors or concurrent editing. After the verification passes, the replacement is performed atomically, and detailed operation metadata is recorded.
[0092] A streaming partial document replacement system includes: a task parsing module, a streaming receiving module, a tag caching module, a protocol parsing module, a text isolation module, an anchor point verification module, a reverse disambiguation module, an anchor point expansion module, a document structure analysis module, an endpoint correction module, a Unicode offset module, and an atomic replacement module. These modules work closely together to implement the aforementioned method.
[0093] To more clearly illustrate the technical solution of this application, the following will provide further explanation through specific scenario embodiments.
[0094] I. Streaming Local Document Replacement Method for Generative Models
[0095] This invention provides a streaming local document replacement method for generative models, the implementation process of which is as follows: Figure 3 As shown, the specific steps include:
[0096] S1: Obtain the original document and perform modification tasks
[0097] This step is the starting point of the entire replacement process. The system first obtains the original document to be modified (D) and the user-inputted modification instructions (I), which are typically in natural language and describe the desired changes in content or style. Simultaneously, the system also obtains the task scope parameter (R), which specifies the scope of this modification, for example:
[0098] Full rewrite: The model should modify the entire document.
[0099] Chapter rewrite: The model should modify one or more specific chapters in the document.
[0100] User selection rewrite: The model should modify the text area selected by the user in the editor.
[0101] Partial rewrite: The model modifies specific local content in the document, which may not be strictly limited to chapters or selections.
[0102] The system will follow a structured output protocol (containing specific control labels, such as) that includes the original document D, user modification instructions I, and the desired model output.<attachment_type> ,<start_text> ,<end_text> Together with the original document D, the data is sent to the generative model for processing. To handle concurrent editing scenarios, the digest value (e.g., hash value) of the original document D is calculated and saved at this time for subsequent document version consistency checks.
[0103] S2: Streaming the output data blocks of the generative model
[0104] After processing a request, the generative model returns its output in a streaming manner according to a predefined structured protocol. This invention assumes the logical structure of the model's output is as follows:
[0105] <attachment_type> full_rewrite< / attachment_type>
[0106] <start_text> Starting anchor point< / start_text>
[0107] <end_text> End Anchor Point< / end_text>
[0108] Revised text
[0109] The system continuously receives data blocks (C1, C2, ..., Cn) returned by the generative model over the network. During the reception process, the system maintains the following critical states to ensure the robustness of streaming processing:
[0110] Header buffer (H): Used to temporarily store received data blocks until the complete control tag is parsed, thus solving the problem of streaming tag transmission across data blocks.
[0111] Text waiting area (B): Used to store the generated text content received after the control tag has been parsed but before the actual replacement.
[0112] Starting anchor (S): Stores the resolved information.<start_text> Tag content.
[0113] End Anchor Point (E): Stores the resolved information<end_text> Tag content.
[0114] Tag parsing status: Records which tag is currently being parsed, whether it has been completely closed, etc.
[0115] S3: Incremental caching and robust resolution of control tags
[0116] Whenever the system receives a new data block, it appends it to the header buffer H. The system continuously scans the contents of H, attempting to identify and parse structured control tags. This step differs from simple streaming data parsing; it focuses on ensuring that control tags, which may be fragmented during streaming, can be completely and correctly identified. Only when a complete start tag (such as...) is detected...<start_text> ) and the corresponding closing label (e.g.< / start_text> Only when ) is its content extracted.
[0117] This step also includes the following important checks to enhance the robustness of the parsing:
[0118] attachment_type validity: Checks whether its value is within the predefined set of types (such as full_rewrite, chapter_rewrite, etc.).
[0119] Tag integrity: Ensure that both the start_text and end_text tags are fully closed.
[0120] Tag order: Verify whether the three types of tags (attachment_type, start_text, end_text) appear in the order specified by the protocol.
[0121] Single-setness: Ensure that only one set of head and tail anchor points is contained within the entire head control area. If multiple sets are present, it is considered a structural anomaly.
[0122] If the tag is not closed, the system will continue to cache subsequent data blocks and will not send incomplete tags or their content to the client. If a complete and valid tag cannot be formed after exceeding a preset caching threshold (e.g., 5KB or 10 seconds), the system will determine it as a structural anomaly and may trigger a regeneration request or issue an error message to the user to prevent unparsed content from being mistaken for text or causing downstream parsing errors.
[0123] S4: Isolate the control area and the main text area, and prevent label leakage.
[0124] when<end_text> The closing label (i.e.)< / end_text> After the closing tag is fully parsed, the system will use the end position of that closing tag as the starting point for generating the main text content. All content before this point, including attachment_type, start_text, end_text, and the tags themselves, is categorized as control information and used only for subsequent logical judgments and parameter extraction; its content will not be included in the final document. All content received after this point is considered as part of the generated main text stream.
[0125] To completely prevent the model from accidentally outputting control labels (such as <) again in the generated text.<start_text> If this leads to information leakage or rendering errors, the system will perform real-time detection of the content in the main text stream. Based on the preset configuration, the system can perform the following operations:
[0126] Delete: Directly remove control tags from the main text.
[0127] Escape: Escape special characters in control tags so that they no longer have control meaning (e.g., escape < to <).
[0128] Block: If a control tag is detected in the text, immediately interrupt the current streaming and report an error, or trigger a regeneration.
[0129] Regenerate: Discard the current text content and request the model to regenerate it.
[0130] This rigorous isolation and detection mechanism ensures the purity of the final document and solves the problem of streaming control tag leakage.
[0131] S5: Verify the starting anchor point
[0132] Search for the starting anchor point S parsed in step S3 in the original document D.
[0133] If no matching position exists, the starting anchor point is deemed invalid, the process terminates, or an exception is triggered.
[0134] If multiple candidate position sets PS = {p1, p2, ..., pm} exist, the system will combine the task scope R obtained in S1, the document's chapter boundary information, and the user's modification instructions to determine a unique target starting position, thereby avoiding ambiguity in the starting position:
[0135] (1) Full text rewrite task: Prioritize the candidate position at the first valid text character in the document, or the matching position closest to the beginning of the document.
[0136] (2) Specify chapter rewrite task: Select the closest matching position to the target chapter title or chapter start boundary.
[0137] (3) User selection modification task (selection_rewrite): Select the matching position closest to the starting point of the user selection.
[0138] S6: Perform reverse disambiguation on the ending anchor point.
[0139] After determining the starting anchor point in S5, the system continues searching for the ending anchor point E in the original document D. To address the issue that generative models may output duplicate anchor points, causing the replacement range to end prematurely, this invention innovatively adopts the following "reverse disambiguation" strategy:
[0140] Searching backward from the end of the original document or the task boundary: Unlike traditional solutions that default to selecting the first match, this invention starts from the end of the original document (or the end of the current task scope) and searches backward to end at anchor point E.
[0141] Prioritize the last match that satisfies the task boundary: Select the "last" match position that occurs within the current task boundary constraints as a candidate for replacement endpoint. For example:
[0142] (1) Full text modification task: Select the last valid match that appears in the original document.
[0143] (2) Chapter modification task: Select the last match that occurs before the end boundary of the current target chapter (but excluding subsequent chapters).
[0144] This "reverse disambiguation" strategy can effectively prevent the replacement range from ending prematurely due to duplicate anchor points, ensuring the integrity of the replacement. It is one of the core means to solve the "duplicate anchor point matching problem".
[0145] S7: Generate a unique ending anchor point through context expansion
[0146] Even after the reverse disambiguation of S6, if multiple matches still exist for the ending anchor point E within the target range (e.g., within a certain chapter) (e.g., the model-generated anchor point is too generic, with multiple instances within the target region), this reflects the problem of insufficient stability of the model-generated anchor points. To overcome this challenge, this invention performs an anchor point expansion operation to achieve uniqueness:
[0147] Expand the context character by character: Starting from the current ending anchor E, extract the context character by character forward (i.e., towards the beginning of the document), generating new candidate anchors. For example, if E is a "logical loop", then first generate E1 = x1 + E, then E2 = x2 + x1 + E, and so on. Here, x1 and x2 are one or more characters preceding E.
[0148] Count of occurrences: After each expansion, the system counts the number of times the new candidate anchor points (such as E1, E2) appear within the target range of the original document.
[0149] Determine a unique anchor point: When a candidate anchor point appears once within the target range after expansion, the candidate anchor point is determined as the new and unique ending anchor point.
[0150] Expansion restrictions: The expansion process is preferentially restricted to the same paragraph or the same line of text to avoid generating anchors that are semantically incoherent or span too large structural units, while maintaining the validity of the expanded anchors.
[0151] fallback strategy: If the preset maximum extension length still cannot uniquely locate the target, the system can combine absolute offset (such as the distance from the end of the chapter) or the chapter structure information of the document to complete the final location, for example, selecting the match closest to the end of the chapter within the chapter.
[0152] This "context expansion" mechanism, in conjunction with "reverse disambiguation," addresses the issues of "insufficient stability of anchor points generated by the model" and "duplicate matching of anchor points," ensuring the accuracy and uniqueness of anchor point location.
[0153] S8: Intelligent recognition of valid document endings
[0154] To more accurately handle full-text replacement and avoid unnecessary Markdown markup residue (i.e., the problem of "Markdown tail content interfering with localization"), this invention introduces a step of identifying the semantic endpoint T of the document. This is not a simple Markdown cleanup, but rather provides semantic boundaries for subsequent correction.
[0155] Reverse scan: Starting from the physical end of the original document, perform a reverse scan line by line.
[0156] Skip non-semantic content: During the scanning process, the system will intelligently skip the following types of text lines in sequence:
[0157] Blank line.
[0158] Lines containing only whitespace characters such as spaces or tabs.
[0159] Lines containing only specific Markdown tags, such as isolated "#" (heading symbol), "*" (list or emphasis symbol), "-" (list or separator), backticks (code block marker), or tildes (strikethrough), are often used as non-semantic formatting controls at the end of the document.
[0160] Determining the semantic end line and endpoint: The first text line identified containing actual text content (i.e., not the skippable content mentioned above) is determined as the semantic end line. The position of the last valid character (non-whitespace character) of this line is determined as the semantic endpoint T of the original document.
[0161] S9: Automatic error correction endpoint
[0162] This step is mainly used to handle situations where the generative model may not be able to fully cover the scope of the modification when the whole text or chapters are modified (i.e., "tail content residue"). This is not a simple cleaning of the tail Markdown, but an intelligent correction of the model's output characteristics.
[0163] Conditional judgment: If the starting anchor point determined in S5 is located at the beginning of the original text (or very close to the beginning, indicating that the task is a full-text modification), but if the ending anchor point determined in S6 or S7 is earlier than the semantic endpoint T identified in S8, the system judges that the current ending anchor point fails to cover the expected modification range of the entire document (or the entire chapter).
[0164] Correction strategy:
[0165] (1) The system extracts a continuous text of a preset length (e.g., 50-100 characters) from the semantic endpoint T as a new candidate end anchor point.
[0166] (2) Perform the forward expansion process described in S7 on this candidate anchor point to make it unique in the original text.
[0167] (3) For full-text replacement tasks, the corrected actual replacement offset endpoint will be extended to the physical length of the original document to ensure that all non-semantic content and Markdown tags, including those located after the semantic endpoint T, are removed.
[0168] (4) For chapter modification tasks, the correction process will be limited by the boundaries of the current chapter. The system will determine the actual end boundary of the chapter based on the next same or higher level Markdown heading, and limit the corrected end point to within that chapter to ensure that it does not cross the boundary and affect other chapters.
[0169] This combined mechanism of "semantic endpoint recognition and automatic correction of erroneous endpoints" completely solves the problem of residual tail content caused by incomplete coverage of model anchor points.
[0170] S10: Precisely calculate Unicode character offsets
[0171] In the preceding steps, we finally determined the starting byte position (Bs) and ending byte position (Be) of the replacement region in the original document. However, to ensure precise consistency with the character positions in the front-end editor (especially when processing UTF-8 encoded Chinese characters), this invention does not directly use the number of bytes as the offset, thus addressing the problem of "inaccurate calculation of Chinese character offsets".
[0172] The RuneCount function: The system calls a RuneCount function (or an equivalent Unicode code point counting method) to calculate the number of Unicode code points contained from the beginning of the document to position Bs, and obtains the character-level starting offset OffsetFrom = RuneCount(D[0:Bs]).
[0173] The RuneCount function: Similarly, it calculates the number of Unicode code points from the beginning of the document to position Be, and obtains the character-level end offset OffsetTo = RuneCount(D[0:Be]).
[0174] Left-closed, right-open interval: The replacement interval adopts the form of left-closed, right-open [OffsetFrom, OffsetTo].
[0175] This approach ensures that the positioning results on the front end and back end are accurate and consistent regardless of whether the document contains Chinese, English, numbers, or other Unicode characters, avoiding common Chinese offset errors and improving the user experience.
[0176] S11: Perform integrity verification before multi-dimensional replacement
[0177] Before the final replacement operation is executed, the system performs a series of multi-dimensional integrity checks to ensure the accuracy and security of the operation. This solves the problem of "lack of pre-replacement verification mechanism" and is not a simple hash check.
[0178] Existence of starting anchor points: Verify whether the identified starting anchor points actually exist in the original document.
[0179] Existence of ending anchors: Verify whether the identified ending anchors actually exist in the original document.
[0180] Anchor point order: Verify that the starting position is strictly earlier than the ending position.
[0181] Anchor range: Verify that the determined replacement range is within the task-allowed range specified in S1 (e.g., chapter modifications should not exceed chapter boundaries).
[0182] Full-text task endpoint coverage: For full-text modification tasks, verify whether the replacement endpoint covers the semantic tail or full length of the original document.
[0183] Chapter task out of bounds: For chapter modification tasks, verify whether the replacement scope has unexpectedly covered subsequent chapters.
[0184] No control tags in the main text: Verify whether the generated main text N no longer contains any unprocessed control tags.
[0185] Document version consistency: Recalculate the current original document's summary value (e.g., hash value) and compare it with the initial summary value saved in S1. If the document summary value changes, it indicates that the original document may have been modified by other concurrent editors during model generation. In this case, the system will stop automatic replacement and require the user to relocate or handle conflicts to avoid misalignment and overwriting.
[0186] S12: Atomic substitution and output results
[0187] After all verifications pass, the system performs the actual replacement operation.
[0188] Replacement formula: The interval defined by [OffsetFrom, OffsetTo) in the original document D is replaced by the generated text N, forming a new document D': D' = D[0:OffsetFrom] + N + D[OffsetTo:].
[0189] Atomicity: Replacement operations are executed in a transactional or atomic update manner, ensuring data consistency and avoiding intermediate states. In systems that support version control, a replacement operation can be committed as a new version.
[0190] Records: The system will record detailed metadata for this replacement operation, including but not limited to: original anchor points (S and E), corrected anchor points, original offset, corrected offset, replacement text, document version information, and records of any abnormal corrections (such as endpoint correction and anchor point extension), to facilitate auditing and problem tracing.
[0191] II. Streaming Local Document Replacement System
[0192] This invention also provides a streaming partial document replacement system, the architecture of which is as follows: Figure 4 As shown, it includes the following core modules, which work together to address the complex challenges of streaming replacement of generative models:
[0193] The task parsing module is responsible for receiving user-input modification instructions, the original document, and task scope parameters, and parsing them into structured task information that the system can process. It can identify and distinguish between full-text modification, specified chapter modification, user-selected area modification, and ordinary partial modification tasks.
[0194] Streaming receiving module: Responsible for establishing a connection with the generative model and receiving data chunks output by the model in a streaming manner.
[0195] Tag Buffer Module: Receives data blocks from the streaming receive module and appends them to its internal buffer. This module is not simply a buffer; its key function is to handle control tags transmitted across data blocks, ensuring tag integrity.
[0196] Protocol parsing module: Continuously scans the contents of the tag cache module, identifies and parses control tags in the structured output protocol, such as...<attachment_type> ,<start_text> and<end_text> This module not only extracts content but also performs tag validity, order, and single-group verification, which is crucial for ensuring the robustness of streaming tag parsing.
[0197] Text Isolation Module: After the protocol parsing module completes the parsing of all control tags, the received streaming data is divided into a control area and a text area. The core function of this module is to perform real-time detection of the streaming content entering the text area, and delete, escape, block, or trigger the regeneration of control tags that unexpectedly appear, based on the configuration, to completely prevent control tags from leaking into the final generated text N.
[0198] Anchor point verification module: Receives the starting anchor point S and ending anchor point E extracted by the protocol parsing module. It is responsible for searching for candidate positions of S and E in the original document and performing existence verification. For multiple matching cases, it determines a unique and accurate target position of the starting anchor point by combining the task scope, chapter boundaries, and user instructions.
[0199] Reverse disambiguation module: Receives the start position and end anchor point E determined by the anchor point verification module. This module is not a simple reverse search; its core function is to search backward from the end of the original text to all matches of the end anchor point E after the start position and within the task boundary, and prioritizes the last valid match that satisfies the task boundary as the initial replacement endpoint to resolve the duplicate anchor point problem.
[0200] Anchor point expansion module: This module is activated when the reverse disambiguation module cannot determine a unique ending anchor point. Its innovation lies in expanding the context character by character backward from the starting position of the ending anchor point, generating new candidate anchor points, and counting their occurrences in the original text until a unique ending anchor point is found, thus addressing the issue of unstable anchor point generation by the model. The expansion process is limited by preset length and text structure (such as paragraphs and lines).
[0201] The document structure analysis module is responsible for performing structured analysis on the original document, identifying Markdown chapter titles and boundaries. This module also innovatively implements reverse scanning from the end of the document, intelligently identifying and skipping blank lines, whitespace lines, and isolated Markdown markup lines to accurately determine the semantic endpoint T of the original document, providing a foundation for subsequent endpoint correction.
[0202] Endpoint Correction Module: This module combines the starting position determination (whether it's a full-text task) from the anchor point verification module with the semantic endpoint T provided by the document structure analysis module. The core of this module is that if the ending anchor point fails to cover the expected range, it automatically corrects the area near the semantic endpoint (by truncating and expanding to generate a new, unique anchor point), ensuring the integrity of the modification range and resolving the issue of residual content at the end.
[0203] Unicode Offset Module: Receives the start and end byte positions after all corrections. The key to this module is calling the Unicode code point counting function to accurately convert them into character-level start offsets OffsetFrom and end offsets OffsetTo, completely resolving the Chinese character offset problem under multi-byte character encoding.
[0204] Atomic Replacement Module: After all multi-dimensional checks (including anchor point existence, order, range, document version summary consistency, etc.) pass, it is responsible for performing atomic operations to replace the intervals defined by [OffsetFrom, OffsetTo) in the original document with the generated text N, and saving the operation record and any abnormal correction information. III. Specific Implementation Examples
[0206] The following specific scenarios will further illustrate the operation and beneficial effects of this invention.
[0207] Example 1: Solving the problem of premature replacement endpoint caused by duplicate anchor points
[0208] Suppose the original document contains the following:
[0209] # Chapter 1 ...
[0211] Content A: At the same time, accurately avoid core interest conflicts and form a highly self-consistent closed loop of action logic. ...
[0213] # Chapter Two ...
[0215] Content B: Through innovative collaborative mechanisms, a highly self-consistent closed loop of action logic is formed. ...
[0217] The user wants to modify the content between "# Chapter 1" and "Forming a highly self-consistent closed loop of action logic. (in Chapter 2)".
[0218] The generative model returns the following:
[0219] <attachment_type> chapter_rewrite< / attachment_type>
[0220] <start_text> # Chapter 1< / start_text>
[0221] <end_text> Form a highly self-consistent closed loop of action logic.< / end_text>
[0222] Revised text
[0223] Problems with traditional solutions:
[0224] Traditional methods typically employ a first-match strategy when locating the ending anchor point "forming a highly self-consistent action logic loop." This means it might target "forming a highly self-consistent action logic loop" within "Content A," incorrectly setting the replacement endpoint in the first chapter. This results in "Content B" and subsequent content in the second chapter not being properly deleted, ultimately leading to duplicate content in the document.
[0225] The solution of this invention:
[0226] (1) S5 check start anchor point: The system finds the unique position of "# Chapter 1" in the original document.
[0227] (2) S6 performs reverse disambiguation on the ending anchor point: The system identifies that the ending anchor point "forming a highly self-consistent action logic loop" appears twice in the original text. Unlike traditional solutions, this invention searches backward from the end of the original document, first matching "forming a highly self-consistent action logic loop" in "Content B" instead of in "Content A". Since the task scope may be set to cover two chapters, or it may detect that this is the last match in the document (within the target scope), this position will be selected as the initial endpoint for replacement.
[0228] (3) S7 Extended Repeated End Anchor Point (Optional): Suppose that under certain circumstances, after reverse disambiguation, it is found that "forming a highly self-consistent action logic closed loop" is still not unique within the target scope (e.g., there are multiple within Chapter 2). In this case, the present invention will extend forward from this anchor point. For example, an extended anchor point is generated: "Through innovative collaborative mechanisms, a highly self-consistent action logic closed loop is formed." This extended anchor point may be unique in the original text. The system will select this unique anchor point to determine the replacement endpoint.
[0229] Through this combined mechanism of "reverse disambiguation" and "anchor context extension", the present invention can accurately locate the replacement endpoint to the end of the second chapter expected by the user, avoiding the residue and repetition of the original text content, and solving the problem of premature replacement caused by repeated anchor points.
[0230] Example 2: Automatic correction of erroneous endpoints that do not cover the entire range
[0231] Suppose a user initiates a full-text rewriting task. The model returns the starting anchor point as the beginning of the document, but the ending anchor point as the middle of the document.
[0232] Original document:
[0233] # Document Title
[0234] The first paragraph.
[0235] The second paragraph.
[0236] Issues to be verified
[0237] Evidence supports
[0238] ## Document End Markup
[0239] Model returns:
[0240] <attachment_type> full_rewrite< / attachment_type>
[0241] <start_text> # Document Title< / start_text>
[0242] <end_text> The second paragraph.< / end_text>
[0243] Revised full text
[0244] Problems with traditional solutions:
[0245] Traditional solutions locate the end anchor point provided by the model to "second paragraph.", resulting in content such as "issues to be verified," "supporting evidence," and "## document end markers" being left in the final document, which is inconsistent with the user's intention to rewrite the entire document, i.e., residual end content.
[0246] The solution of this invention:
[0247] (1) S5 verifies the starting anchor point: The system confirms that the starting anchor point "# Document Title" is located at the beginning of the document, indicating that this is a full-text modification task.
[0248] (2) S6 / S7 positioning end anchor point: The system finds "Second paragraph of text." as the end anchor point provided by the model.
[0249] (3) S8 intelligent document end recognition: The system scans backwards from the end of the original text:
[0250] Intelligently skips "## document end mark" (recognized as Markdown markup lines).
[0251] The phrase "evidence supports" was identified as the semantic end line, with its last character being the semantic endpoint T.
[0252] (4) S9 Automatic Error Correction Endpoint: The system detects that the starting anchor point is at the beginning of the document, but the ending anchor point "Second paragraph text." provided by the model is earlier than the semantic endpoint T. This invention determines that the model does not cover the entire text.
[0253] The system will extract a continuous text segment (e.g., "Issuance to be verified\nEvidence support") from the semantic endpoint T (i.e., the end of "Evidence support") and perform S7 anchor expansion on it to ensure its uniqueness in the original text.
[0254] Ultimately, the corrected actual replacement offset endpoint will extend to the physical end of the original document, including the removal of the "## document end mark", ensuring the thoroughness of the entire document modification.
[0255] By combining the mechanisms of "semantic endpoint recognition" and "automatic correction of erroneous endpoints", this invention solves the problem of residual content at the end of the text / chapter after full-text / chapter modification.
[0256] Example 3: Handling Markdown Trailer Marks and Unicode Offsets
[0257] The end of the original document may contain the following:
[0258] The last paragraph is the main text.
[0259] ##
[0260] ---
[0261] If the model only modifies the main body of the document but does not touch these end tags, the returned end anchor point is positioned at "last paragraph."
[0262] The solution of this invention:
[0263] (1) S8 intelligent document end recognition: The system scans backwards from the end of the original text:
[0264] Intelligent skipping of blank lines.
[0265] Intelligently skips "##" (recognized as Markdown markup lines).
[0266] Intelligently skip "---" (recognized as Markdown markup lines).
[0267] The system identifies "the last paragraph of text." as the semantic end line, with its last character being the semantic endpoint T.
[0268] (2) S9 Automatic Correction of Error Endpoint: If it is a full text modification task and the model returns the end anchor point to "the last paragraph of the main text.", this invention will trigger automatic correction to extend the replacement endpoint to the actual physical end of the document, ensuring that the tail Markdown tags including "##" and "---" are also deleted, thus solving the problem of incomplete modification caused by interference from Markdown tail content positioning.
[0269] (3) S10 accurately calculates Unicode character offsets: Assuming the original document contains Chinese characters, such as "This is a Chinese sentence.", the number of bytes it occupies under UTF-8 encoding may be much greater than the number of characters.
[0270] Traditional systems, if calculating offsets by bytes, might result in: Bs=0, Be=15 (assuming a Chinese character is 3 bytes and a period is 1 byte). However, front-end editors expect character offsets of OffsetFrom=0, OffsetTo=6, which will lead to misalignment during replacement.
[0271] This invention uses the RuneCount() function to precisely calculate the number of characters in "This is a Chinese sentence." (offsetFrom=0 and OffsetTo=6), ensuring consistency with the front-end editor and achieving accurate replacement. This completely solves the problem of "inaccurate calculation of Chinese character offsets".
[0272] The flowcharts and block diagrams in the accompanying drawings illustrate possible implementations of systems, methods, and computer program products according to various embodiments of this application, including architecture, functionality, and operation. In these figures, each block may represent a module, program segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should be noted that each block in the block diagrams and / or flowcharts, and combinations thereof, can be implemented using either a dedicated hardware-based system or a combination of dedicated hardware and computer instructions to achieve the specified function or operation.
[0273] like Figure 5 As shown, embodiments of this application also disclose an electronic device, including: a processor 310, a communication interface 320, a memory 330 for storing a processor-executable computer program, and a communication bus 340. The processor 310, communication interface 320, and memory 330 communicate with each other via the communication bus 340. The processor 310 executes the executable computer program to implement the steps of the above-described streaming partial document replacement method for generative models.
[0274] Furthermore, this application also discloses a computer-readable storage medium, wherein when the instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device is able to perform the various steps of the streaming local document replacement method for generative models disclosed in this application.
[0275] Specifically, according to embodiments of this application, the processes described in the flowcharts can be implemented as computer software programs. For example, embodiments of this application relate to a computer program product comprising a computer program carried on a non-transitory computer-readable medium. This computer program contains program code for executing the streaming partial document replacement method for generative models disclosed in this application. When this computer program is executed by a processing system, it can achieve the functions defined in the embodiments of this application.
[0276] While the foregoing discussion contains several specific implementation details, these details should not be construed as limiting the scope of this application. The above description is merely a preferred embodiment of this application and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this application is not limited to technical solutions formed by specific combinations of the above-described technical features. Furthermore, this application should also cover other technical solutions formed by any combination of the above-described technical features or their equivalents without departing from the foregoing disclosed concept.
[0277] Those skilled in the art should also understand that they can modify the technical solutions described in the foregoing embodiments or make equivalent substitutions for some of the technical features without departing from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A streaming local document replacement method for generative models, characterized in that, Includes the following steps: S1. Stream receive data blocks output by the generative model. The data blocks contain structured control tags and subsequent text content. Incrementally cache the data blocks to splice control tags across data blocks, and continuously parse and verify the integrity, legality, order, and singleness of the control tags. S2. Search for the starting anchor point in the original document, and determine the unique target starting position of the starting anchor point in the original document in combination with the preset task scope; S3. After the target start position, search for the end anchor point from the end of the original document or the task boundary forward, and select the last matching position that satisfies the task boundary as the initial replacement endpoint of the end anchor point; if there are still multiple matches of the end anchor point within the target range, expand the context character by character forward from the start position of the initial replacement endpoint to generate new candidate anchor points until a unique end anchor point in the original document is found. S4. Scan backwards from the end of the original document, identify and skip blank lines, lines containing only whitespace characters, and lines with specific Markdown tags to determine the semantic endpoint of the original document; if the starting anchor point is located at the beginning of the original document and the ending anchor point is earlier than the semantic endpoint, then truncate and expand from the vicinity of the semantic endpoint to generate a new unique ending anchor point, and correct the replacement endpoint to the semantic endpoint or the physical end. S5. Based on the finally determined start byte position and end byte position, calculate the character-level start offset OffsetFrom and end offset OffsetTo using the Unicode code point counting method; S6. Perform a multi-dimensional replacement integrity check. After the check passes, replace the range defined by [OffsetFrom, OffsetTo) in the original document with the generated text using atomic operations.
2. The method according to claim 1, characterized in that, In step S1, the control tag includes<attachment_type> ,<start_text> and<end_text> ; The verification includes: verification<attachment_type> Check whether the value is within the predefined type set.<start_text> and<end_text> The system checks the complete closure of the tags, whether the three types of tags appear in the protocol order, and whether the entire header control area contains only one set of beginning and end anchor points.
3. The method according to claim 1, characterized in that, In step S1, if the control tag is not fully closed, the subsequent data blocks continue to be cached; if a complete and valid tag is still not formed after exceeding the preset cache threshold, it is determined to be a structural anomaly. Before the control tag is fully parsed, the main text content is prevented from entering subsequent processing; after the control tag is fully parsed, the subsequently received content is treated as the main text stream, and control tags that unexpectedly appear in the main text stream are detected and processed, including deletion, escaping, blocking, or triggering regeneration.
4. The method according to claim 1, characterized in that, In step S2, when there are multiple candidate positions for the starting anchor point, a unique target starting position is determined by combining the task scope, document chapter boundary information, and user modification instructions: the full-text modification task selects the matching position closest to the beginning of the document; the specified chapter modification task selects the matching position closest to the boundary of the target chapter; and the user selection modification task selects the matching position closest to the starting point of the user selection area.
5. The method according to claim 1, characterized in that, In step S3, the process of expanding the context character by character forward is limited to the same paragraph or the same text line. If the preset maximum expansion length still cannot be uniquely located, the final location is completed by combining the absolute offset or document chapter structure information.
6. The method according to claim 1, characterized in that, In step S4, the specific Markdown markup line includes an isolated heading symbol "#", a list or emphasis symbol "*", a list or separator "-", a backtick, and a tilde; In step S4, for chapter modification tasks, the correction process is limited by the current chapter boundary, and the actual end boundary of the chapter is determined based on the next same or higher-level Markdown heading.
7. The method according to claim 1, characterized in that, In step S5, the Unicode code point counting method is implemented by the RuneCount function to calculate the number of Unicode code points contained from the beginning of the document to the target byte position.
8. The method according to claim 1, characterized in that, In step S6, the multi-dimensional integrity verification before replacement includes: existence of the starting anchor point, existence of the ending anchor point, the starting position being earlier than the ending position, the replacement interval being within the allowed range of the task, the generated text not containing unprocessed control tags, and consistency verification of the original document version summary. The original document version summary consistency check is performed by recalculating the summary value of the current original document and comparing it with the initial summary value saved at the start of the replacement process. If they are inconsistent, the automatic replacement is stopped.
9. The method according to claim 1, characterized in that, In step S6, after the atomic operation is executed, the metadata of the replacement operation is recorded, including the original anchor point, the corrected anchor point, the original offset, the corrected offset, the replacement text, the document version information, and the abnormal correction record.
10. A streaming local document replacement system for generative models, characterized in that, The system runtime implements the steps of the streaming local document replacement method for generative models as described in any one of claims 1-9, including: A streaming receiving module is used to receive data blocks output by a generative model, wherein the data blocks contain structured control tags and subsequent text content; The tag caching and protocol parsing module is used to incrementally cache the data blocks to concatenate control tags across data blocks, and to parse and verify the integrity, legality, order, and single-group nature of the control tags; Anchor point positioning and verification module is used to search for the starting anchor point in the original document and determine the unique target starting position of the starting anchor point in the original document in combination with the preset task range; The reverse disambiguation and anchor point expansion module is used to search for an end anchor point backward from the end of the original document or the task boundary after the target start position, and select the last matching position that satisfies the task boundary as the initial replacement endpoint of the end anchor point; if there are still multiple matches of the end anchor point within the target range, the context is expanded character by character backward from the start position of the initial replacement endpoint to generate new candidate anchor points until a unique end anchor point in the original document is found. The document structure analysis and endpoint correction module is used to scan backward from the end of the original document, identify and skip blank lines, lines containing only whitespace characters, and lines with specific Markdown tags to determine the semantic endpoint of the original document; if the starting anchor point is located at the beginning of the original document and the ending anchor point is earlier than the semantic endpoint, then a new unique ending anchor point is generated by truncating and expanding from the vicinity of the semantic endpoint, and the endpoint is corrected to the semantic endpoint or the physical end. The Unicode offset module is used to calculate the character-level start offset OffsetFrom and end offset OffsetTo based on the finally determined start and end byte positions using Unicode code point counting. The text isolation and atomic replacement module is used to isolate the subsequent text content after the control tags are fully parsed, and to detect and process control tags that appear unexpectedly in the text stream; and to replace the range defined by [OffsetFrom, OffsetTo) in the original document with the processed text content after the multi-dimensional integrity check passes, and to execute it in an atomic manner.