A method and system for generating python postconditions based on fine-tuning open source large language models

By constructing a high-quality training dataset and fine-tuning an open-source large language model using low-rank adaptive techniques, combined with historical case retrieval and error feedback mechanisms, the problems of high cost of closed-source models and insufficient accuracy of open-source models are solved, achieving low-cost and high-efficiency generation of post-conditions and improving generation quality and efficiency.

CN122220196APending Publication Date: 2026-06-16SHANGHAI FORMAL TECH INFORMATION TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
SHANGHAI FORMAL TECH INFORMATION TECH CO LTD
Filing Date
2026-01-30
Publication Date
2026-06-16

Smart Images

  • Figure CN122220196A_ABST
    Figure CN122220196A_ABST
Patent Text Reader

Abstract

The application relates to the technical field of computers, and specifically discloses a Python postcondition generation method and system based on fine-tuning of an open-source large language model, which aims to solve the problems of high calling cost of an existing closed-source large language model, insufficient generation precision of an open-source large language model, lack of high-quality training data, and lack of a systematic generation framework. The method comprises the following steps: combining pre-defined parameter types, generating initial postconditions through a closed-source model, reversely synthesizing function document strings through semantic enhancement, rule variation, and effectiveness filtering, constructing a training data set, fine-tuning an open-source large language model, obtaining a task adaptation model, integrating a similar case retrieval module and an error feedback mechanism in the reasoning stage, generating candidate postconditions and iteratively repairing them, and updating a historical successful case knowledge base. The method can realize low-cost, high-accuracy, and high-defect-detection-completeness postcondition generation, effectively improves the automation degree and testing efficiency of software reliability verification, and reduces testing costs.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a Python postcondition generation method and system based on fine-tuning an open-source large language model. Background Technology

[0002] Closed-source large language models demonstrate strong performance in postcondition generation tasks, capable of handling unstructured function docstrings and generating postconditions containing multiple conditional clauses, thus improving input flexibility and expressiveness. However, closed-source large language models suffer from high costs associated with continuous API calls, which significantly increases the economic burden of software development and testing in the long run. In contrast, open-source large language models offer advantages such as convenient local deployment and low-cost support for domain-knowledge-based parameter fine-tuning iterations, making them more suitable for the practical needs of large-scale software development and testing. However, their accuracy in postcondition generation tasks is lower, making it difficult to meet the requirements of high accuracy and comprehensive defect detection.

[0003] In existing technologies, heuristic rule-based methods can extract program specifications from function docstrings. However, these methods heavily rely on highly structured inputs, typically requiring fixed templates and only generating simple logical assertions, failing to meet the logical expression needs of complex functions. Although recent methods based on large language models have overcome the limitations of input formats, these methods mostly directly call the model to generate results without constructing a systematic postcondition generation framework. Their isolated reasoning mechanisms prevent successfully generated postconditions from forming a reusable knowledge base, requiring repeated exploration in subsequent generation tasks, reducing generation efficiency. Furthermore, simple error filtering mechanisms lack systematic error correction strategies, only removing obviously erroneous results and failing to guide the model to learn from errors, causing the model to miss important optimization opportunities and further limiting the improvement of generation quality.

[0004] Furthermore, high-quality docstring-postconditioning training datasets are the core foundation for improving the fine-tuning effect of open-source large language models. However, such datasets are scarce in existing technologies and are difficult to cover the rich combinations of input and output parameter types and complex logical scenarios of Python functions. This limits the fine-tuning effect of open-source large language models and prevents them from fully realizing their potential in specific tasks.

[0005] Therefore, there is an urgent need for a Python postcondition generation method and system based on fine-tuning an open-source large language model to solve the above problems. Summary of the Invention

[0006] The purpose of this invention is to provide a Python postcondition generation method based on fine-tuning an open-source large language model, comprising the following steps: Based on the combination of input and output parameter types of Python functions, initial postconditions are generated using a closed-source large language model. After semantic enhancement, rule mutation, and validity filtering, the corresponding function docstrings are synthesized in reverse, thereby constructing a docstring-postcondition pair training dataset. The selected open-source large language model was fine-tuned using the training dataset to obtain a task-adapted model specifically for post-condition generation. During the inference phase, the document string of the target function is received, a knowledge base of historical success cases is constructed and maintained, and historical case postconditions similar to the target document string are retrieved as context. Combined with the context, the target document string is input into the task adaptation model to generate candidate postconditions. The candidate postconditions are evaluated for correctness and completeness of defect detection. If the evaluation fails, feedback prompts are generated based on the evaluation error information to guide the task adaptation model to repair and iterate the candidate postconditions. The candidate postconditions that have been successfully evaluated after iterative repair and their corresponding target document strings are used as new success case pairs and updated to the historical success case knowledge base.

[0007] Furthermore, the step of constructing the docstring-postcondition pair training dataset includes: A predefined system of input and output parameter type combinations covering basic Python data types and type conversions is provided. For each type combination, a prompt word template is constructed, and initial postconditions conforming to the Python assertion statement format are generated using a closed-source large language model to form a basic assertion set. The initial postconditions in the basic assertion set are semantically enhanced by combining multiple conditional clauses, using inferences, or calling a subset of preset built-in functions to generate enhanced postconditions with higher logical complexity. Predefined mutation rules are applied to the enhanced postconditions, including relational operator replacement and object inspection method replacement, to generate semantically diverse mutated postconditions; The postconditions obtained through generation, enhancement, and mutation are filtered for syntactic and semantic validity. Based on the valid postconditions retained after filtering, the corresponding natural language function document strings are generated in reverse, forming a document string-postcondition pair training dataset.

[0008] Furthermore, the step of obtaining a task adaptation model specifically for post-condition generation includes: The document string-postcondition pair training dataset is formatted into instruction-response pairs required for model fine-tuning, where the document string is the instruction and the postcondition is the response. Inject a trainable low-rank matrix into the selected open-source large language model and freeze the original parameters of the base model. The model is trained in a supervised manner using the formatted training dataset and the instruction-response method, with only the parameters of the low-rank matrix being updated via backpropagation. The parameters of the low-rank matrix after training are combined with the original parameters of the base model to obtain the task adaptation model.

[0009] Furthermore, the steps for building and maintaining a historical success case knowledge base include: Initialize a vector knowledge base for storing function docstrings and their success postcondition pairs. During system operation, new success case pairs are added to the knowledge base in real time. The semantic encoding model is used to convert the document string of each case in the knowledge base into a vector and store it in association with the corresponding postcondition text; A vector similarity algorithm is used to calculate the similarity value between the target document string vector and all case vectors in the knowledge base, and then sort them accordingly. Based on the preset K value, select the K cases with the highest similarity from the sorting results and extract their document strings and postcondition text.

[0010] Furthermore, the step of inputting the target document string into the task adaptation model to generate candidate postconditions includes: The received target function docstring is standardized and then converted into a dense vector representation using a semantic coding model; Calculate the semantic similarity between the target document string vector and all case document string vectors in the historical success case knowledge base, and retrieve the K most similar historical cases and their subsequent conditions based on the similarity ranking; The retrieved K historical cases and their postconditions are used as reference context and together with the target document string to construct an enhanced generation prompt; The enhanced generation prompts are input into the task adaptation model for inference, and candidate postconditions are output.

[0011] Furthermore, if the evaluation fails, the step of generating feedback based on the evaluation error information includes: The candidate postconditions are used to verify the correct implementation code of the target function. If it passes all the preset test cases, it is determined to be correct. The candidate postconditions are used to detect multiple preset defective code implementations of the objective function. If all preset defects can be identified, the function is judged to be complete. If the candidate postcondition fails the verification, the specific failed test cases or unidentified defects are analyzed, and a structured feedback prompt containing the error type and repair guidance is generated. The target document string, the failed candidate postconditions, and the structured feedback prompts are input into the task adaptation model to guide the model to generate the corrected postconditions. The verification steps are repeated until the evaluation is passed.

[0012] This application also provides a Python postcondition generation system based on fine-tuning an open-source large language model, including: The module is used to combine the input and output parameter types of Python functions, generate initial postconditions using a closed-source large language model, and after semantic enhancement, rule mutation and validity filtering, synthesize the corresponding function docstrings in reverse, thereby constructing the docstring-postcondition pair training dataset. The first generation module is used to fine-tune the selected open-source large language model using the training dataset to obtain a task-adapted model specifically for post-condition generation. The second generation module is used in the inference stage to receive the document string of the target function, build and maintain a knowledge base of historical successful cases, retrieve historical case postconditions similar to the target document string as context, and input the target document string into the task adaptation model in combination with the context to generate candidate postconditions. The repair module is used to evaluate the correctness and defect detection completeness of the candidate postconditions. If the evaluation fails, feedback prompts are generated based on the evaluation error information to guide the task adaptation model to repair the candidate postconditions iteratively. The update module is used to update the historical success case knowledge base by taking the candidate postconditions that have been evaluated as successful after iterative repair and their corresponding target document strings as new success case pairs.

[0013] Furthermore, the second generation module includes: An addition unit is used to initialize a vector knowledge base for storing function docstrings and their success postcondition pairs. During system operation, new success case pairs are added to the knowledge base in real time. The transformation unit is used to convert the document string of each case in the knowledge base into a vector using a semantic coding model, and store it in association with the corresponding postcondition text; The calculation unit is used to calculate the similarity value between the target document string vector and all case vectors in the knowledge base using a vector similarity algorithm, and then sort them accordingly. The extraction unit is used to select the K cases with the highest similarity from the sorting results based on a preset K value, and extract their document strings and postcondition text.

[0014] This application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the above-described Python postcondition generation method based on fine-tuning an open-source large language model.

[0015] This application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the above-described Python postcondition generation method based on fine-tuning an open-source large language model.

[0016] The beneficial effects of this application are as follows: Firstly, this invention generates high-quality docstring-postcondition training data through a multi-stage dataset construction process, effectively solving the problem of scarce training data during the fine-tuning of open-source large language models. This process utilizes a closed-source large language model to generate initial postconditions. After semantic enhancement to expand logical complexity, rule mutation to enrich semantic diversity, and validity filtering to ensure data quality, the corresponding function docstrings are synthesized in reverse, forming a training dataset covering various parameter type combinations and complex logical scenarios. This provides reliable support for the fine-tuning of open-source large language models, ensuring that the model can learn comprehensive docstring-postcondition mapping rules.

[0017] Secondly, this invention employs low-rank adaptive technology for efficient parameter fine-tuning of open-source large language models. This significantly improves the performance of the open-source model on post-condition generation tasks while reducing computational resource consumption. By injecting a trainable low-rank matrix into the Transformer layer of the open-source large language model, freezing the original parameters of the base model, and updating only the low-rank matrix parameters, this avoids the high computational demands and degradation of model versatility caused by full parameter fine-tuning. It also achieves precise adaptation of the model to post-condition generation tasks, enabling the open-source model to achieve generation accuracy similar to closed-source models while maintaining the low-cost advantage of local deployment.

[0018] Third, this invention constructs a self-optimizing generation framework integrating a similar case retrieval module and an error feedback mechanism, solving the problems of lack of a systematic generation framework, low generation efficiency, and insufficient error correction capabilities in existing technologies. The similar case retrieval module builds a knowledge base of historical successful cases, retrieving historical cases similar to the docstring of the objective function as contextual references. This helps the model quickly learn from existing experience, reduce redundant exploration, and improve generation efficiency and relevance. The error feedback mechanism evaluates the correctness and defect detection completeness of candidate postconditions from two dimensions. For results that fail the evaluation, it generates structured feedback containing error types and correction guidelines, guiding the model to iteratively correct and learn from errors, continuously optimizing generation quality. Attached Figure Description

[0019] Figure 1 This is a schematic diagram of a method flow proposed in an embodiment of this application.

[0020] Figure 2 This is a schematic diagram of the system structure proposed in an embodiment of the present invention.

[0021] The realization of the purpose, functional features and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. Detailed Implementation

[0022] It should be understood that the specific embodiments described herein are merely illustrative of the invention and are not intended to limit the invention.

[0023] like Figure 1 As shown, this application provides a Python postcondition generation method based on fine-tuning an open-source large language model, including the following steps: S1, based on the combination of input and output parameter types of Python functions, uses a closed-source large language model to generate initial postconditions, and after semantic enhancement, rule mutation and validity filtering, it back-synthesizes the corresponding function docstrings, thereby constructing a high-quality docstring-postcondition pair training dataset. S2, using low-rank adaptive technology, fine-tunes the selected open-source large language model using the training dataset to obtain a task-adaptive model specifically for post-condition generation; S3, In the inference phase, the document string of the target function is received, a knowledge base of historical success cases is constructed and maintained, and historical case postconditions similar to the target document string are retrieved as context. Combined with the context, the target document string is input into the task adaptation model to generate candidate postconditions. S4, evaluate the correctness and defect detection completeness of the candidate postconditions. If the evaluation fails, generate feedback prompts based on the evaluation error information to guide the task adaptation model to repair and iterate the candidate postconditions. S5. The candidate postconditions that have been successfully evaluated after iterative repair and their corresponding target document strings are used as new success case pairs and updated to the historical success case knowledge base.

[0024] As described in steps S1-S5 above, in the software development process, function docstrings are natural language descriptions of function functionality, and the code implementation must strictly adhere to these descriptions; otherwise, software defects are easily introduced. Converting docstrings into formalized postconditions is a key method for detecting such inconsistencies. While closed-source large language models exhibit good performance in postcondition generation tasks, they require continuous API call costs, making long-term application economically unsustainable. Open-source large language models support local deployment and can be iteratively optimized based on domain knowledge, offering a low-cost advantage, but their low generation accuracy is due to a lack of high-quality training data. Furthermore, existing technologies lack a systematic postcondition generation framework, cannot effectively reuse past successful experiences, and error handling is limited to simple filtering, lacking precise repair mechanisms. This makes it difficult for models to learn from errors, hindering the continuous improvement of generation quality. Therefore, a postcondition generation solution that balances cost, accuracy, and self-optimization capabilities is urgently needed.

[0025] Traditional solutions rely on fixed template inputs and can only generate simple logical assertions, which cannot adapt to unstructured document strings and complex functional logic. Solutions that directly use open-source large language models do not have targeted fine-tuning and generate low accuracy results. Solutions that rely on closed-source models have the problem of high ongoing costs. Furthermore, none of these solutions have established effective experience reuse and error correction mechanisms.

[0026] This invention achieves high accuracy and completeness in generating postconditions for Python function docstrings by constructing a high-quality training dataset and fine-tuning an open-source large language model. It also combines historical case retrieval to enhance the reasoning process and error feedback iterative repair mechanism. At the same time, it takes into account the low-cost advantages of local deployment and the system's continuous self-optimization capabilities.

[0027] Specifically, the core logic of this application's core technical solution is to construct a self-optimizing closed loop encompassing data support, model adaptation, inference optimization, and knowledge accumulation. Its principle addresses the technical pain points of high postcondition generation costs in existing closed-source large language models and insufficient accuracy and lack of a systematic generation framework in open-source large language models. First, based on combinations of Python function input and output parameter types, initial postconditions are generated using a closed-source large language model. After semantic enhancement to increase logical complexity, rule mutation to enrich semantic diversity, and effectiveness filtering to ensure data quality, the corresponding function docstrings are synthesized in reverse, constructing a high-quality docstring-postcondition pair training dataset. This solves the core problem of scarce fine-tuning data for open-source models. Then, low-rank adaptive technology is used to fine-tune the selected open-source large language model, updating only the low-rank matrix parameters injected into the Transformer layer and freezing the original parameters of the base model. This improves the model's adaptability to the postcondition generation task while controlling computational costs, resulting in an adapted model specifically for this task. In the inference phase, the target function is first received... The system generates docstrings and, by building and maintaining a knowledge base storing successful case pairs, retrieves historical case postconditions similar to the target docstring as contextual references. Leveraging historical experience improves the relevance and accuracy of the generated postconditions. The target docstring and context are then combined with the input task adaptation model to generate candidate postconditions. These candidate postconditions undergo a dual evaluation of correctness and defect detection completeness. If an evaluation fails, feedback prompts containing the error type and repair guidance are generated based on the specific error information, guiding the task adaptation model to iteratively repair the candidate postconditions until they pass the evaluation. Finally, the repaired and approved candidate postconditions and their corresponding target docstrings are updated to the knowledge base as new successful case pairs, continuously enriching historical experience. This forms a self-optimizing mechanism: data construction empowers model fine-tuning, case retrieval enhances reasoning generation, error feedback optimizes result quality, and knowledge accumulation feeds back into subsequent generation. Ultimately, this achieves the goal of generating high-accuracy and high-defect-detection-completeness postconditions at low cost and high efficiency, improving the automation of software reliability verification and reducing testing costs.

[0028] In one embodiment, the step of constructing a high-quality docstring-postconditioning training dataset includes: S11 predefines a system of input and output parameter type combinations that covers basic Python data types and type conversions. For each type combination, it constructs a prompt word template and uses a closed-source large language model to generate initial postconditions that conform to the Python assertion statement format, forming a basic assertion set. S12, perform semantic enhancement operation on the initial postconditions in the basic assertion set, and generate enhanced postconditions with higher logical complexity by combining multiple conditional clauses, using inferences or calling a preset built-in function subset. S13, apply predefined mutation rules to the enhanced postconditions, the mutation rules including relational operator replacement and object inspection method replacement, to generate semantically diverse mutated postconditions; S14 filters the postconditions obtained through generation, enhancement, and mutation based on their syntactic and semantic validity. Based on the valid postconditions retained after filtering, the corresponding natural language function document strings are generated in reverse, forming a document string-postcondition pair training dataset.

[0029] As described in steps S11-S14 above, initial postconditions are generated through a predefined input / output parameter type combination system. After semantic enhancement and rule mutation to expand data diversity, and then data quality is ensured through validity filtering, the corresponding function docstrings are finally generated in reverse. This constructs a high-quality and semantically comprehensive docstring-postcondition pair training dataset, providing reliable data support for fine-tuning open-source large language models.

[0030] Performance improvements of open-source large language models in postconditioning generation tasks heavily rely on high-quality training data, but existing training data has significant shortcomings. Python functions offer a rich variety of input and output parameter types, covering basic data types and various type conversions. Different combinations correspond to different function logic and postconditioning forms, and existing data struggles to fully cover these combined scenarios. Furthermore, simple postconditioning data prevents models from learning complex logical expressions, single-semantic datasets are prone to overfitting, and unfiltered, low-quality data directly impacts model fine-tuning. Therefore, there is an urgent need to construct a comprehensive, logically diverse, and reliable training dataset.

[0031] Traditional methods for constructing training datasets have many shortcomings. Manual annotation is inefficient and costly, making it difficult to generate large-scale datasets. Data generation based on fixed templates has narrow semantic coverage and cannot adapt to diverse parameter combinations and complex function logic. Some methods lack effective data filtering mechanisms, resulting in syntactic errors or semantic contradictions in the generated data, which can degrade model performance if used directly for training. To address these issues, a systematic process is adopted, including predefined combinations, initial data generation, augmentation and mutation, filtering and purification, and reverse pairing. This process comprehensively improves the quality of training data from three dimensions: data coverage, logical complexity, and quality reliability, providing a high-quality data foundation for model fine-tuning.

[0032] When predefining the input / output parameter type combination system, it is necessary to comprehensively cover basic Python data types, including integers, strings, lists, dictionaries, etc., and also include common type conversion combinations, such as list to integer, string to boolean, dictionary to list, etc., to ensure the completeness of parameter type combinations. For each type combination, a clear prompt word template should be constructed. The template should specify the input parameter type, output parameter type, and postcondition format requirements, limiting the postconditions to Python assertion statements, allowing only function input parameters and return values, and only calling a subset of predefined built-in functions. Utilizing the strong generation capabilities of the closed-source large language model, initial postconditions are generated based on the prompt word template, forming a basic assertion set. For example, for integer-to-integer type combinations, the prompt word template explicitly requires the generation of assertions reflecting the numerical relationship between input and output. The closed-source large language model generates initial postconditions such as "assertresult>=input_num", ensuring the relevance and basic quality of the initial data.

[0033] Semantic enhancement of initial postconditions in the basic assertion set aims to improve their logical complexity and expressive completeness. This can be achieved by combining multiple conditional clauses, expanding single logical judgments into multi-dimensional constraints, or enriching the expression using list comprehensions, dictionary comprehensions, and other structures. Functions from a pre-defined subset of built-in functions, such as `all`, `any`, and `len`, can also be invoked to enhance the logical coverage of postconditions. For example, the initial postcondition "assertresult>=input_num" can be enhanced to "assertresult>=input_numandresult%2==0". By adding a parity constraint, the logical complexity of the data is increased, allowing the model to learn more complex postcondition generation logic, thereby improving the model's ability to handle complex function docstrings.

[0034] Predefined mutation rules are applied to the enhanced postconditions to further expand the semantic diversity of the data. Mutation rules are divided into two categories: relational operator substitution and object checking method substitution. Relational operator substitution replaces the relational operators in the postcondition with functionally related or opposite operators, such as replacing greater than or equal to with less than or equal to, and equal to with not equal to. Object checking method substitution replaces the checking methods for objects such as strings and lists, such as replacing the `startswith` method for strings with the `endswith` method, and the `count` method for lists with the `index` method. For example, the enhanced postcondition "assertresult>=input_numandresult%2==0" is mutated to "assertresult<=input_numandresult%2!=0" through relational operator substitution, and the string checking-related "asserttinput_str.startswith(prefix)" is mutated to "asserttinput_str.endswith(prefix)" through method substitution. This generates a large number of semantically diverse postcondition variants, effectively expanding the semantic coverage of the dataset, avoiding overfitting during model training, and improving the model's generalization ability.

[0035] All postconditions generated, enhanced, and mutated are filtered for syntactic and semantic validity to ensure data quality. Syntactic filtering involves constructing an abstract syntax tree to perform lexical and syntactic analysis on the postconditions, checking for Python naming conventions, matching parentheses, and complete statement structures, eliminating postconditions with syntactic errors. Semantic filtering verifies that the types of operators and operands match, checks whether variables only reference function input parameters and return values, and confirms that function calls are within a preset subset of built-in functions, avoiding issues such as type incompatibility, referencing undefined variables, or calling illegal functions. For example, filtering out the postcondition "asserttemp>result" containing the undefined variable "temp", and eliminating "assertresult==numpy.sum(input_list)" which calls a non-preset function "numpy.sum", ensures that the retained postconditions are valid and provide high-quality data for model training.

[0036] Based on the valid postconditions retained after filtering, the closed-source large language model is used to reverse-generate the corresponding natural language function docstrings. During generation, the model is required to comprehensively describe the function's input parameter types, functional purpose, and return value characteristics according to the logical constraints of the postconditions, ensuring a high degree of semantic consistency between the docstring and the postconditions. For example, for the valid postcondition "assertresult>=input_numandresult%2==0", the docstring "The function receives an integer input and returns an even integer greater than or equal to that input" is generated. Each valid postcondition is paired with its corresponding docstring to form a complete docstring-postcondition pair, constructing the final training dataset. This provides accurate supervised learning samples for subsequent fine-tuning of the open-source large language model, helping the model establish a mapping relationship between natural language descriptions and formalized postconditions.

[0037] In one embodiment, the step of obtaining a task adaptation model specifically for post-condition generation includes: S21, the document string-postcondition pair training dataset is formatted into instruction-response pairs required for model fine-tuning, wherein the document string is the instruction and the postcondition is the response. S22, inject a trainable low-rank matrix into the Transformer layer of the selected open-source large language model and freeze the original parameters of the base model. S23, using the formatted training dataset, train the supervised model with the instruction-response method, updating only the parameters of the low-rank matrix using the backpropagation algorithm; S24. The parameters of the low-rank matrix after training are merged with the original parameters of the base model to obtain the task adaptation model.

[0038] As described in steps S21-S24 above, by formatting the high-quality document string-postcondition pair training dataset into instruction-response pairs, a trainable low-rank matrix is ​​injected into the Transformer layer of the open-source large language model using low-rank adaptive technology, the original parameters of the base model are frozen and only the parameters of the low-rank matrix are updated, and finally the two types of parameters are merged to obtain a task-adaptive model specifically for postcondition generation, thereby achieving accurate adaptation and low-cost deployment of the open-source large model on specific tasks.

[0039] Open-source large language models possess general natural language processing capabilities after pre-training, but lack specific adaptation for post-condition generation. When directly applied to this task, their generation accuracy is low, failing to meet the requirements of software reliability verification for the correctness and completeness of post-conditions. To improve their specific performance, fine-tuning based on high-quality training datasets is necessary. However, traditional full-parameter fine-tuning requires updating all model parameters, which not only consumes a large amount of computing power and memory, leading to excessively high local deployment costs, but may also destroy the general semantic understanding capabilities obtained from model pre-training, causing capability degradation. Therefore, a fine-tuning scheme that balances adaptation effect, computational cost, and the original capabilities of the model is urgently needed.

[0040] Traditional fine-tuning methods have significant shortcomings. Full-parameter fine-tuning requires substantial computational resources, which is difficult for ordinary hardware to support, and it easily leads to model overfitting and loss of general capabilities. Partial fine-tuning methods that only adjust the top-level parameters of the model cannot fully explore the model's potential for specific tasks, resulting in limited adaptation. Fine-tuning schemes lacking parameter freezing mechanisms can lead to excessive modification of pre-trained weights, damaging the model's fundamental performance. To address this, a low-rank adaptive technique is employed. By injecting a low-rank matrix, only a small number of parameters are updated. This accurately learns the generation rules of post-conditions while reducing computational overhead and preserving the model's original general capabilities, achieving a balance between specific task adaptation and cost control.

[0041] First, the docstring-postcondition pair training dataset is formatted into instruction-response pairs required for model fine-tuning. The training dataset is high-quality data constructed by generating initial postconditions through predefined parameter type combinations, followed by semantic enhancement, rule mutation, and validity filtering to generate docstrings. The docstrings serve as natural language instructions describing the function's functionality, while the postconditions act as corresponding formal assertion responses. This format conversion ensures the dataset conforms to the input-output specifications for supervised model training, providing clear supervisory signals for establishing the mapping between natural language descriptions and formal postconditions. For example, a docstring describing a function that receives a list of integers and returns the number of even-numbered elements, along with its corresponding postcondition, forms an instruction-response pair used by the model to learn the postcondition generation logic for this type of function.

[0042] Next, a trainable low-rank matrix is ​​injected into the Transformer layer of the selected open-source large language model. The selected open-source large language model is lightweight and has good semantic understanding capabilities. Its Transformer layer is responsible for semantic feature extraction and mapping, and is the core functional module of the model. The injected low-rank matrix adopts a low-rank decomposition structure, and its dimension is much lower than the original weight matrix of the Transformer layer. At the same time, the original parameters of the base model are frozen to avoid modifying the pre-trained weights during fine-tuning, ensuring that the model retains its general semantic processing capabilities. It learns specialized knowledge generated by post-conditions only through the low-rank matrix, preventing capability degradation.

[0043] Using a formatted training dataset, a supervised model is trained via instruction-response. During training, the model receives docstring instructions and outputs predicted postconditions. By calculating the error between the predicted results and the true postconditions, the backpropagation algorithm is used to update only the parameters of the low-rank matrix. This process eliminates the need to adjust a large number of original parameters of the base model, significantly reducing computational resource consumption and enabling efficient fine-tuning on ordinary hardware. Simultaneously, the low-rank matrix can specifically capture the mapping pattern between docstrings and postconditions, gradually improving the model's generation accuracy on specific tasks.

[0044] Finally, the parameters of the trained low-rank matrix are merged with the original parameters of the base model. The merging process employs a parameter fusion mechanism, organically combining the postcondition generation-specific knowledge learned from the low-rank matrix with the general semantic understanding capabilities of the base model to form a task-adapted model specifically for postcondition generation. This model retains the low-cost advantage of local deployment of large open-source models while achieving high-accuracy postcondition generation capabilities through specialized fine-tuning. It can accurately receive function docstring inputs and output compliant postconditions, providing strong support for software reliability verification.

[0045] In one embodiment, the steps of building and maintaining a historical success case knowledge base include: S31, Initialize a vector knowledge base for storing function docstrings and their success postcondition pairs, and add new success case pairs to the knowledge base in real time during system operation; S32, using a semantic coding model to convert the document string of each case in the knowledge base into a vector, and storing it in association with the corresponding postcondition text; S33 uses a vector similarity algorithm to calculate the similarity value between the target document string vector and all case vectors in the knowledge base, and sorts them accordingly; S34. Based on the preset K value, select the K cases with the highest similarity from the sorting results and extract their document strings and postcondition text.

[0046] As described in steps S31-S34 above, by initializing and dynamically maintaining a vector knowledge base that stores function document strings and successful postcondition pairs, the document strings are converted into vector form using a semantic encoding model. The top K historical cases most similar to the target document string are selected using a vector similarity algorithm, providing effective contextual references for the reasoning stage and improving the accuracy and efficiency of the task adaptation model in generating postconditions.

[0047] During the inference phase, although the task-adapted model has undergone specific fine-tuning, it is still difficult to quickly generate accurate postconditions relying solely on its own model parameters when faced with complex or rare function docstrings. Historical success cases contain a large number of validated mapping relationships between docstrings and postconditions. This experience can provide direct reference for the model, helping it quickly grasp the postcondition generation logic of similar functions. Without such historical support, the model needs to re-explore the generation path, which not only increases inference time but may also introduce logical biases, leading to insufficient correctness or completeness of the generated postconditions. Therefore, it is necessary to build and maintain a knowledge base of historical success cases to achieve the reuse and efficient retrieval of experience.

[0048] Traditional methods often fail to establish a dedicated historical case knowledge base, resulting in isolated inference processes that cannot reuse past successes. A few schemes that do employ case libraries rely solely on simple methods like keyword matching to retrieve cases, which struggles to accurately capture the semantic relationships between document strings, leading to low relevance in search results and hindering effective model support. This approach addresses this by using a vector knowledge base to store cases. Semantic encoding transforms text into vectors, and vector similarity algorithms are used to accurately calculate semantic relevance, enabling efficient and precise retrieval of similar cases. Simultaneously, the knowledge base is dynamically updated to ensure the timeliness and richness of cases, fully leveraging the auxiliary role of historical experience in model reasoning.

[0049] First, a vector knowledge base is initialized. This knowledge base is specifically used to store function docstrings and their corresponding successful postcondition pairs that have passed correctness and defect detection completeness evaluations. During the initialization phase, a batch of high-quality case pairs, either manually verified or accumulated from early system operation, are imported. In subsequent system operation, each time a new postcondition is generated, iteratively improved, and passes evaluation, its corresponding target docstring is added to the knowledge base in real time, ensuring continuous enrichment and providing a more comprehensive reference for subsequent reasoning. For example, after the system generates a postcondition corresponding to the number of all positive numbers in the returned list and passes evaluation, this docstring and postcondition pair are immediately stored in the knowledge base, becoming a reference case for subsequent reasoning of similar functions.

[0050] Next, a semantic encoding model is used to vectorize the document strings of each case in the knowledge base. The semantic encoding model employs a pre-trained text encoder, which can transform natural language document strings into dense vectors of fixed dimensions. These vectors accurately capture the semantic features of the document strings, allowing the semantic similarity between different document strings to be quantified through vector operations. After the transformation, the vector for each case is associated with its corresponding post-condition text and stored, ensuring that the corresponding post-condition information can be quickly retrieved after the vector is retrieved, providing data support for subsequent construction of enhanced prompt generation.

[0051] Then, a vector similarity algorithm is used to calculate the similarity value between the target document string vector and all case vectors in the knowledge base. The target document string vector is obtained through the same semantic encoding model as the cases in the knowledge base, ensuring the consistency of the vector space. The vector similarity algorithm uses the cosine similarity algorithm, which represents the degree of semantic similarity by calculating the cosine value of the angle between two vectors. The closer the cosine value is to 1, the stronger the semantic relationship between the two. After calculation, all cases are sorted in descending order of similarity value, so that the semantically most similar cases are ranked first, providing a clear basis for subsequent screening. For example, if the target document string is "return the intersection of two lists of integers", its vector is similar to case vectors in the knowledge base such as "return the intersection of two lists of strings" and "return the common elements of two lists", and then sorted by numerical value to ensure that the most relevant cases are screened first.

[0052] Finally, based on a preset K value, the K most similar cases are selected from the ranking results, and their docstrings and postcondition texts are extracted. The K value is calibrated through offline experiments, taking into account both the effectiveness of the reference cases and the model's inference efficiency; it is typically set between 3 and 5 to ensure sufficient reference information while avoiding an excessive burden on the model's inference due to too many cases. The extracted case information serves as contextual reference, working together with the target docstring to construct enhanced generation prompts. This provides clear inference guidance for the task-adaptive model, helping it quickly generate suitable candidate postconditions.

[0053] In one embodiment, the step of inputting the target document string into the task adaptation model to generate candidate postconditions includes: S35, standardize the received target function document string and convert it into a dense vector representation using a semantic coding model; S36, calculate the semantic similarity between the target document string vector and all case document string vectors in the historical success case knowledge base, and retrieve the K most similar historical cases and their subsequent conditions according to the similarity ranking; S37, The retrieved K historical cases and their postconditions are used as reference context and together with the target document string to construct an enhanced generation prompt; S38, the enhanced generation prompts are input into the task adaptation model for inference, and candidate postconditions are output.

[0054] As described in steps S35-S38 above, by standardizing and vectorizing the target function document string, the K most similar cases in the historical successful case knowledge base are retrieved, and the case information and the target document string are used together to construct enhanced generation prompts. The input task-adaptive model is then used to generate accurate candidate postconditions. The core is to improve the accuracy and relevance of the model's reasoning by leveraging historical experience.

[0055] While the task-adapted model has been specifically fine-tuned to generate postconditions, it struggles to quickly grasp the core logic when dealing with complex or niche function docstrings, relying solely on its own parameters. This often results in incomplete postcondition logic or deviations from the function's intent. The mapping between docstrings and postconditions stored in historical success stories represents validated experience, providing direct logical references for the model and helping it quickly identify postcondition generation patterns for similar functions. Without such references, the model must re-explore the generation path, increasing inference time and potentially reducing generation quality due to logical deviations. Therefore, effective retrieval and suggestion construction are necessary to allow the model to fully leverage historical experience to improve generation performance.

[0056] Traditional methods sometimes directly input the target document string into the model without incorporating historical case references, resulting in quality that relies on the model's generalization ability. A few methods that use case-based assistance rely solely on keyword matching to retrieve cases, failing to accurately capture the semantic relationships within the document strings. This leads to low relevance of retrieved cases and hinders effective model guidance. This step specifically addresses these issues by employing standardization to eliminate format differences, achieving accurate case retrieval through semantic encoding and vector similarity algorithms, and then constructing enhanced generation prompts that include case context. This provides clear reasoning guidance for the model, effectively resolving the problems of inaccurate case matching and insufficient prompt information in traditional methods.

[0057] The received target function docstrings are standardized. These docstrings originate from user input or function descriptions written during software development, and may contain redundancy, inconsistent formatting, and other issues. During processing, meaningless descriptive phrases are removed, parameter names are standardized, and the logical order of function descriptions is streamlined. This ensures the processed docstrings are semantically clear and formatted consistently, laying the foundation for subsequent vector conversion and similarity calculations. For example, "This function takes two lists of integers and returns their common element" is standardized to "Taken two lists of integers and returns their intersection."

[0058] A semantic encoding model is used to convert standardized target document strings into dense vector representations. This model is consistent with the model used for case vector transformation in the historical successful case knowledge base, ensuring the uniformity of the vector space. This model can map natural language document strings to a fixed-dimensional vector space, where each dimension of the vector corresponds to a specific semantic feature of the document string. This allows for precise quantification of the semantic similarity between different document strings through vector operations, providing a mathematical foundation for subsequent case retrieval.

[0059] The semantic similarity between the target document string vector and the document string vectors of all cases in the historical success case knowledge base is calculated using a cosine similarity algorithm. The degree of semantic association is represented by the cosine value of the angle between the two vectors; the closer the cosine value is to 1, the more similar the functions they describe. After calculation, all cases are sorted from highest to lowest similarity value. The K cases with the highest similarity are selected based on a preset K value. The K value is calibrated to 3 to 5 through offline experiments to ensure sufficient reference information while avoiding excessive cases interfering with model inference. The document strings and postcondition text of these K cases are then extracted as reference context for model inference. For example, if the target document string is "Receive two lists of integers, return the intersection element", similar cases such as "Receive two lists of strings, return the intersection element" and "Receive two lists, return a unique intersection element" and their corresponding postconditions are retrieved from the knowledge base.

[0060] K retrieved historical cases and their postconditions are used as reference context, and together with the target docstring, they are used to construct enhanced generation hints. Hint construction follows a fixed logic: first, the docstrings of the reference cases and their corresponding postconditions are presented; then, the target docstring is explicitly defined, guiding the model to generate target postconditions by referencing the logical connections within the cases. The enhanced generation hints are input into the task-adaptive model for inference. Based on the contextual information in the hints and its own fine-tuned postcondition generation rules, the task-adaptive model outputs candidate postconditions that conform to the intent of the target function's docstring. For example, based on the reference case hints, the model generates the candidate postcondition "assertset(result)==set(list1)&set(list2)andlen(result)==len(set(result))" for the target docstring, ensuring the accuracy of the intersection logic while also covering the constraint of no repeating elements, thus improving generation quality.

[0061] In one embodiment, if the evaluation fails, the step of generating a feedback prompt based on the evaluation error information includes: S41, use the candidate postconditions to verify the correct implementation code of the objective function. If it passes all the preset test cases, it is determined to be correct. S42, the candidate postconditions are used to detect multiple preset defective code implementations of the objective function. If all preset defects can be identified, it is determined to be complete. S43, If the candidate postcondition fails the verification, analyze the specific failed test cases or unidentified defects, and generate a structured feedback prompt containing the error type and repair guidance; S44, the target document string, the failed candidate postconditions, and the structured feedback prompts are input into the task adaptation model to guide the model to generate the corrected postconditions, and the verification steps are repeated until the evaluation is passed.

[0062] As described in steps S41-S44 above, the candidate postconditions generated by the task adaptation model in conjunction with historical cases, while possessing a certain degree of specificity, may still have logical loopholes or incomplete coverage due to factors such as model generalization ability and case matching accuracy. Directly using such candidate postconditions for software reliability verification would lead to the inability to detect inconsistencies between code and docstrings, or the omission of potential defects, thus affecting the verification effect. The correctness of the postconditions is the foundation for their accurate reflection of the function's expected behavior, and their completeness is key to their comprehensive ability to identify defective code. Therefore, it is essential to ensure that the postconditions simultaneously meet these two core requirements through system evaluation and iterative repair.

[0063] Traditional methods often involve only simple syntactic checks on postconditions, lacking a systematic evaluation of logical correctness and defect detection completeness. A few methods that include an evaluation phase provide vague feedback, merely indicating errors without specifying error types or repair directions, making accurate model repair difficult. Furthermore, most methods lack an iterative repair mechanism, discarding candidate conditions upon a single failed evaluation, wasting existing results. This application specifically adopts a two-dimensional evaluation system, clearly defining evaluation criteria and pass / fail conditions, generating structured feedback prompts to provide precise repair guidance, and iteratively verifying that postconditions meet application requirements. This effectively solves the problems of incomplete evaluation, inaccurate feedback, and lack of repair direction in traditional methods. By performing a two-dimensional evaluation of candidate postconditions for both correctness and defect detection completeness, and generating structured feedback prompts containing error types and repair guidelines for those failing the evaluation, the application guides the task-adaptive model to iteratively repair candidate postconditions until they pass the two-dimensional evaluation, ensuring that the final output postconditions not only conform to the correct function implementation logic but also comprehensively detect pre-set defects.

[0064] Candidate postconditions are used to verify the correct implementation code of the target function. The correct implementation code is standard code written based on the functionality described in the target function's docstring. The preset test cases are designed around the function's input / output scenarios and boundary cases, covering various situations such as normal input, extreme value input, and special format input. During the verification process, the candidate postconditions are embedded in the test framework, and all preset test cases are executed. If no assertion failure is triggered by the candidate postcondition, that is, all test cases pass successfully, then the candidate postcondition is determined to be correct and can accurately match the correct implementation logic of the function. For example, the target function's docstring description is "receive two lists of integers and return the intersection elements with no duplicates." Its correct implementation code is written according to this logic. The preset test cases include scenarios such as two lists having multiple intersection elements, no intersection elements, and duplicate elements. During verification, the candidate postcondition "assertset(result)==set(list1)&set(list2)" fails the test case when the input list contains duplicate intersection elements because it does not constrain the result to have no duplicates, thus determining that its correctness does not meet the standard.

[0065] Candidate postconditions are used to detect multiple pre-defined defective code implementations of the target function. These pre-defined defective code implementations are based on the functionality corresponding to the function docstring and are intentionally written with common errors, including logical errors, missing boundary conditions, and formatting errors. During the detection process, the candidate postconditions are applied to all pre-defined defective codes. If the candidate postconditions trigger assertion failures in all defective codes, meaning each pre-defined defect is accurately identified, then the candidate postcondition is deemed complete and can comprehensively cover potential defect scenarios. Continuing with the example above, the pre-defined defective codes include three types: "returns the intersection but retains duplicate elements," "omits some intersection elements," and "returns the union instead of the intersection." While the candidate postcondition "assertset(result)==set(list1)&set(list2)" can detect the defect of "returning the union instead of the intersection," it cannot detect the defect of "returning the intersection but retaining duplicate elements," meaning its completeness is deemed insufficient.

[0066] If a candidate postcondition fails the correctness or completeness evaluation, the system will conduct an in-depth analysis of the specific reasons for the failure. For correctness failures, the system will locate the test case that triggered the assertion failure, analyze the differences between the candidate postcondition and the correct function implementation logic, and identify the error type as a logical relationship error, missing boundary conditions, or incorrect variable references. For completeness failures, the system will analyze unidentified defect code types and identify the error type as incomplete defect scenario coverage or insufficient constraints. Based on the analysis results, a structured feedback prompt will be generated. This prompt must clearly state the error type and provide specific repair guidance. For example, if the error type is "missing boundary conditions," the repair guidance might be "add a constraint that the result length is consistent with the length after deduplication."

[0067] The objective function docstring, candidate postconditions that failed evaluation, and structured feedback are input into the task adaptation model. The model, combining precise guidance from the feedback and referencing the functional description in the objective docstring, logically corrects and supplements the constraints of the candidate postconditions. After the corrections are complete, the new candidate postconditions are again fed into the two-dimensional evaluation process, repeating the verification, analysis, feedback, and correction steps until the candidate postconditions pass all test cases and all preset defects are identified. This ensures that the final output postconditions simultaneously meet the requirements of correctness and completeness, effectively supporting software reliability verification.

[0068] like Figure 2 As shown, this invention also discloses a Python postcondition generation system based on fine-tuning an open-source large language model, comprising: Module 1 is used to combine the input and output parameter types of Python functions, generate initial postconditions using a closed-source large language model, and after semantic enhancement, rule mutation and validity filtering, synthesize the corresponding function docstrings in reverse, thereby constructing the docstring-postcondition pair training dataset. The first generation module 2 is used to fine-tune the selected open-source large language model using the training dataset to obtain a task-adapted model specifically for post-condition generation. The second generation module 3 is used in the inference stage to receive the document string of the target function, build and maintain a knowledge base of historical successful cases, retrieve historical case postconditions similar to the target document string as context, and input the target document string into the task adaptation model in combination with the context to generate candidate postconditions. Repair module 4 is used to evaluate the correctness and defect detection completeness of the candidate postconditions. If the evaluation fails, feedback prompts are generated based on the evaluation error information to guide the task adaptation model to repair the candidate postconditions iteratively. Update module 5 is used to update the historical success case knowledge base by taking the candidate postconditions that have been evaluated successfully after iterative repair and their corresponding target document strings as new success case pairs.

[0069] In one embodiment, the second generation module includes: An addition unit is used to initialize a vector knowledge base for storing function docstrings and their success postcondition pairs. During system operation, new success case pairs are added to the knowledge base in real time. The transformation unit is used to convert the document string of each case in the knowledge base into a vector using a semantic coding model, and store it in association with the corresponding postcondition text; The calculation unit is used to calculate the similarity value between the target document string vector and all case vectors in the knowledge base using a vector similarity algorithm, and then sort them accordingly. The extraction unit is used to select the K cases with the highest similarity from the sorting results based on a preset K value, and extract their document strings and postcondition text.

[0070] This application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the steps of the above-described Python postcondition generation method based on fine-tuning an open-source large language model.

[0071] This application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the above-described Python postcondition generation method based on fine-tuning an open-source large language model.

[0072] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, storage, databases, or other media used in this application and in the embodiments can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual-speed SDRAM (SSRSDRAM), enhanced SDRAM (ESDRAM), synchronous link DRAM (SLDRAM), RAMbus direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).

[0073] It should be noted that, in this document, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, apparatus, article, or method 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, apparatus, article, or method. Unless otherwise specified, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, apparatus, article, or method that includes that element.

[0074] The above description is merely a preferred embodiment of the present invention and does not limit the scope of this application. Any equivalent results or equivalent process transformations made based on the content of the present invention's specification and drawings, or direct or indirect applications in other related technical fields, are similarly included within the scope of protection of this application.

Claims

1. A Python postcondition generation method based on fine-tuning an open-source large language model, characterized in that, Includes the following steps: Based on the combination of input and output parameter types of Python functions, initial postconditions are generated using a closed-source large language model. After semantic enhancement, rule mutation, and validity filtering, the corresponding function docstrings are synthesized in reverse, thereby constructing a docstring-postcondition pair training dataset. The selected open-source large language model was fine-tuned using the training dataset to obtain a task-adapted model specifically for post-condition generation. During the inference phase, the document string of the target function is received, a knowledge base of historical success cases is constructed and maintained, and historical case postconditions similar to the target document string are retrieved as context. Combined with the context, the target document string is input into the task adaptation model to generate candidate postconditions. The candidate postconditions are evaluated for correctness and completeness of defect detection. If the evaluation fails, feedback prompts are generated based on the evaluation error information to guide the task adaptation model to repair and iterate the candidate postconditions. The candidate postconditions that have been successfully evaluated after iterative repair and their corresponding target document strings are used as new success case pairs and updated to the historical success case knowledge base.

2. The Python postcondition generation method based on fine-tuning an open-source large language model according to claim 1, characterized in that, The steps for constructing the docstring-postcondition pair training dataset include: A predefined system of input and output parameter type combinations covering basic Python data types and type conversions is provided. For each type combination, a prompt word template is constructed, and initial postconditions conforming to the Python assertion statement format are generated using a closed-source large language model to form a basic assertion set. The initial postconditions in the basic assertion set are semantically enhanced by combining multiple conditional clauses, using inferences, or calling a subset of preset built-in functions to generate enhanced postconditions with higher logical complexity. Predefined mutation rules are applied to the enhanced postconditions, including relational operator replacement and object inspection method replacement, to generate semantically diverse mutated postconditions; The postconditions obtained through generation, enhancement, and mutation are filtered for syntactic and semantic validity. Based on the valid postconditions retained after filtering, the corresponding natural language function document strings are generated in reverse, forming a document string-postcondition pair training dataset.

3. The Python postcondition generation method based on fine-tuning an open-source large language model according to claim 1, characterized in that, The steps for obtaining a task adaptation model specifically for post-condition generation include: The document string-postcondition pair training dataset is formatted into instruction-response pairs required for model fine-tuning, where the document string is the instruction and the postcondition is the response. Inject a trainable low-rank matrix into the selected open-source large language model and freeze the original parameters of the base model. The model is trained in a supervised manner using the formatted training dataset and the instruction-response method, with only the parameters of the low-rank matrix being updated via backpropagation. The parameters of the low-rank matrix after training are combined with the original parameters of the base model to obtain the task adaptation model.

4. The Python postcondition generation method based on fine-tuning an open-source large language model according to claim 1, characterized in that, The steps for building and maintaining a knowledge base of historical success cases include: Initialize a vector knowledge base for storing function docstrings and their success postcondition pairs. During system operation, new success case pairs are added to the knowledge base in real time. The semantic encoding model is used to convert the document string of each case in the knowledge base into a vector and store it in association with the corresponding postcondition text; A vector similarity algorithm is used to calculate the similarity value between the target document string vector and all case vectors in the knowledge base, and then sort them accordingly. Based on the preset K value, select the K cases with the highest similarity from the sorting results and extract their document strings and postcondition text.

5. The Python postcondition generation method based on fine-tuning an open-source large language model according to claim 4, characterized in that, The step of inputting the target document string into the task adaptation model to generate candidate postconditions includes: The received target function document string is standardized and then converted into a dense vector representation using a semantic coding model; Calculate the semantic similarity between the target document string vector and all case document string vectors in the historical success case knowledge base, and retrieve the K most similar historical cases and their subsequent conditions based on the similarity ranking; The retrieved K historical cases and their postconditions are used as reference context and together with the target document string to construct an enhanced generation prompt; The enhanced generation prompts are input into the task adaptation model for inference, and candidate postconditions are output.

6. The Python postcondition generation method based on fine-tuning an open-source large language model according to claim 1, characterized in that, If the assessment fails, the steps for generating feedback based on the assessment error information include: The candidate postconditions are used to verify the correct implementation code of the objective function. If it passes all the preset test cases, it is determined to be correct. The candidate postconditions are used to detect multiple preset defective code implementations of the objective function. If all preset defects can be identified, the function is judged to be complete. If the candidate postcondition fails the verification, the specific failed test cases or unidentified defects are analyzed, and a structured feedback prompt containing the error type and repair guidance is generated. The target document string, the failed candidate postconditions, and the structured feedback prompts are input into the task adaptation model to guide the model to generate the corrected postconditions. The verification steps are repeated until the evaluation is passed.

7. A Python postcondition generation system based on fine-tuning an open-source large language model, characterized in that, include: The module is used to combine the input and output parameter types of Python functions, generate initial postconditions using a closed-source large language model, and after semantic enhancement, rule mutation and validity filtering, synthesize the corresponding function docstrings in reverse, thereby constructing the docstring-postcondition pair training dataset. The first generation module is used to fine-tune the selected open-source large language model using the training dataset to obtain a task-adapted model specifically for post-condition generation. The second generation module is used in the inference stage to receive the document string of the target function, build and maintain a knowledge base of historical successful cases, retrieve historical case postconditions similar to the target document string as context, and input the target document string into the task adaptation model in combination with the context to generate candidate postconditions. The repair module is used to evaluate the correctness and defect detection completeness of the candidate postconditions. If the evaluation fails, feedback prompts are generated based on the evaluation error information to guide the task adaptation model to repair the candidate postconditions iteratively. The update module is used to update the historical success case knowledge base by taking the candidate postconditions that have been evaluated as successful after iterative repair and their corresponding target document strings as new success case pairs.

8. The Python postcondition generation system based on fine-tuning open-source large language model according to claim 7, characterized in that, The second generation module includes: An addition unit is used to initialize a vector knowledge base for storing function docstrings and their success postcondition pairs. During system operation, new success case pairs are added to the knowledge base in real time. The transformation unit is used to convert the document string of each case in the knowledge base into a vector using a semantic coding model, and store it in association with the corresponding postcondition text; The calculation unit is used to calculate the similarity value between the target document string vector and all case vectors in the knowledge base using a vector similarity algorithm, and then sort them accordingly. The extraction unit is used to select the K cases with the highest similarity from the sorting results based on a preset K value, and extract their document strings and postcondition text.

9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 6.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 6.