Knowledge base-based outdated base detection and large-model multi-round optimization code generation method
By building a library migration knowledge base and combining it with a multi-round feedback mechanism, outdated libraries generated by large language models are detected and replaced, solving the problem of selecting outdated libraries in code generation, improving code reliability and security, and reducing maintenance costs and risks.
Patent Information
- Application Number
- CN202511527723.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-24
- Publication Date
- 2025-12-16
AI Technical Summary
Existing large language models suffer from outdated library selection and security risks when generating code, leading to high maintenance costs and potential security vulnerabilities in software development. Furthermore, existing technologies have failed to effectively address the issue of the rationality of library selection.
A library migration knowledge base is constructed, and code generation is detected and optimized through a multi-round feedback mechanism. Deprecated libraries are identified and alternative libraries are recommended. The steps include: S1 constructing the deprecated knowledge base, S2 generating code, S3 detecting library dependencies, S4 providing feedback and correction, and S5 outputting the optimal code.
It effectively avoids the use of outdated libraries, improves the reliability and security of generated code, reduces the later maintenance costs for developers, lowers the security risks of software systems, and enhances the application value of large language models in software development.
Smart Images

Figure CN121144167A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of program code generation and software engineering technology, specifically to the field of library selection detection and optimization during the generation of Python code based on natural language problem descriptions using large language models, and particularly to a knowledge base-based method for detecting outdated libraries and a multi-round optimization code generation method for large models. Background Technology
[0002] With the rapid development of artificial intelligence, Large Language Models (LLMs, or "Large Models") have been widely used in the field of software engineering, especially in code generation, code completion, and automated code repair, demonstrating great potential. LLMs have gradually become an important tool for developers, effectively improving development efficiency through natural language question answering and code snippet generation.
[0003] However, the code generated by existing LLMs often still requires further optimization. Besides correctness, researchers have focused on issues such as performance, maintainability, security vulnerabilities, and code smells, but significant shortcomings remain in the crucial aspect of library selection. Library selection plays a fundamental role in software development, and with library deprecation, security vulnerabilities being exposed, and the emergence of better alternatives, library migration has become commonplace. If the generated code references outdated or risky libraries, it not only increases later maintenance costs but may also introduce potential security vulnerabilities.
[0004] On the other hand, the training corpora of large language models often contain historical code, which may rely on deprecated or no longer maintained libraries, leading to continued use of outdated or insecure libraries in the generated results. This not only increases the burden on developers but may also introduce hidden risks into the software system. Existing research has attempted to analyze the type, popularity, maintenance status, and license characteristics of libraries used in LLM-generated code, but a key question remains unresolved: in an ecosystem of continuously evolving libraries, can large language models make reasonable library selections based on the latest developments?
[0005] Therefore, there is an urgent need for a method to detect library dependencies in LLM-generated code, identify outdated or security-risk libraries, and provide alternative library recommendations to optimize library selection and improve the usability and reliability of generated code. From an industry application perspective, the software project lifecycle is often accompanied by frequent library upgrades and migrations. Developers need to avoid continuing to use deprecated or insecure libraries in new code to reduce operational costs and security risks. If large language models can automatically avoid outdated libraries during code generation and prioritize recommending more functional or safer alternatives, it will significantly improve their usability and reliability in enterprise-level software development. Especially in application scenarios such as integrated development environments (IDEs), intelligent programming assistants, and code review platforms, this capability can help developers quickly obtain high-quality code suggestions, thereby effectively reducing later maintenance pressure. Summary of the Invention
[0006] The purpose of this invention is to address the shortcomings of existing technologies by providing a knowledge base-based method for detecting outdated libraries and generating code for multi-round optimization of large models. This invention comprehensively utilizes technologies such as library migration knowledge base construction, dependency detection, outdated library identification, and replacement recommendation to achieve automated analysis and intelligent optimization of library references in the generated code.
[0007] The objective of this invention is achieved through the following technical solution: a knowledge base-based method for detecting outdated libraries and generating code for large models through multi-round optimization, which avoids the use of outdated libraries during code generation through multi-round feedback. The method includes the following steps: Step S1: Build a knowledge base: Determine whether the code base is outdated or has security vulnerabilities based on the maintenance status, version update status and security vulnerability status of the library. Use the library migration datasets PyMigBench and SALM to determine whether there is better alternative library information, so as to build an outdated knowledge base and an alternative knowledge base for the code base. Step S2, Large Model Generates Code: Receive the natural language question input by the user, package it with prompt information, and guide the large model to generate code; Step S3, Library Dependency Detection: Parse the code generated by the large model, extract the libraries referenced by the code through syntax analysis, and compare them with the outdated knowledge base to detect whether the code generated by the large model contains outdated libraries; Step S4, Feedback and Correction of the Large Model: When it is detected that the code generated by the large model contains outdated libraries, the detection results of the library dependencies are fed back to the large model. At the same time, when the alternative knowledge base contains recommendable alternative libraries, the recommendation results are fed back to the large model to guide the large model to generate new code. Step S5, Optimal Code Output: When it is detected that the code generated by the large model does not contain outdated libraries, the code at this time is taken as the optimal code output.
[0008] Furthermore, step S1 specifically includes the following sub-steps: Step S1.1: Based on the library migration datasets PyMigBench and SALM, analyze and extract the migration instances recorded therein, extract library migration pairs, and obtain the correspondence between the source library and the target library. Step S1.2: Obtain the version release information, maintenance status, and update frequency of third-party libraries and standard libraries through the PyPI official platform and Python official resources respectively. Extract the last update time of the library and determine whether it meets the obsolescence condition based on the preset time threshold. Mark the libraries that meet the obsolescence condition as... ; Step S1.3: Obtain vulnerability disclosure information for each library from the CVE database to identify libraries with security risks and mark them as such. ; Step S1.4, based on the markings The library builds an obsolete knowledge base for the codebase, constructs an alternative knowledge base based on the correspondence between the source and target libraries, and marks it as obsolete. The outdated knowledge base is indexed and associated with its corresponding alternative knowledge base, security updates provided by the PyPI official platform, or vulnerability avoidance solutions provided by the CVE vulnerability database. A unified data structure is used for storage.
[0009] Furthermore, in step S1.2, meeting the obsolete condition means that the library is in a state of long-term non-maintenance or has been marked as obsolete or abandoned.
[0010] Further, step S1.3 specifically includes: Vulnerability disclosure information for each database is collected from the CVE database, including vulnerability type, risk level, and remediation status. Based on the risk level, it is determined whether a database contains dangerous vulnerabilities. If a database contains dangerous vulnerabilities and the corresponding remediation status is unpatched or unmaintained, the database is designated as a risky database and marked as such. .
[0011] Furthermore, step S2 specifically includes the following sub-steps: Step S2.1: Receive the natural language problem description input by the user, and preprocess the input text, including removing irrelevant symbols, word segmentation, and keyword extraction, to obtain the preprocessed input text; Step S2.2: Invoke the large model based on the constructed prompt information, wherein the prompt information includes preprocessed input text and library selection constraint rules. The library selection constraint rules are used to instruct the large model to prioritize libraries that are actively maintained, frequently updated, and have no known security vulnerabilities when generating code. Step S2.3: During the code generation process, the large model performs diversified outputs according to the preset generation parameters to obtain a set of candidate code fragments, where each candidate code fragment contains different library implementations.
[0012] Furthermore, in step S2.3, the generation parameters include temperature value, maximum generation length, and number of candidates.
[0013] Furthermore, step S3 specifically includes the following sub-steps: Step S3.1: Use an abstract syntax tree to parse the candidate code snippets generated by the large model, systematically extract the statement nodes involving module imports in the code snippets, and identify and extract the library names that the code depends on by parsing the statement nodes one by one. Step S3.2: Compare the names of the libraries that the code depends on with the obsolete knowledge base. Based on the library maintenance status, version update status and vulnerability disclosure information recorded in the obsolete knowledge base, verify and classify the libraries referenced by the code one by one, detect whether they belong to the obsolete knowledge base, and generate the detection results of library dependencies.
[0014] Furthermore, in step S3.1, the statement node includes the import statement and the from...import... statement.
[0015] Furthermore, step S4 specifically includes the following sub-steps: Step S4.1: When it is detected that the code generated by the large model contains outdated libraries, the detection results of the library dependencies are fed back to the large model in the form of structured feedback information. The feedback information includes the name of the outdated library and the reason for its obsolescence. The large model corrects the original code based on the feedback information, replaces or removes the outdated libraries in the original code, and regenerates the corrected code. Step S4.2: Perform library dependency detection on the corrected code again to determine whether there are still outdated libraries in the iteratively generated code. If there are, continue to loop through the feedback and correction of the large model, and at the same time, retrieve the alternative library corresponding to the function of the marked library from the alternative knowledge base and generate alternative recommendation information; otherwise, skip directly to step S5. Step S4.3: Feed back the alternative recommendation information to the large model to guide it to prioritize the use of the recommended alternative library during code correction or regeneration to obtain optimized code.
[0016] Compared with existing technologies, the beneficial effects of this invention are as follows: This invention can effectively solve the problem that large language models may reference outdated libraries or libraries with security risks during code generation; by constructing a knowledge base based on library migration pairs, version update information, maintenance status, and security vulnerabilities, and combining dependency detection and alternative recommendation mechanisms, this invention can automatically identify outdated libraries in the generated code, promptly provide feedback to the large language model and guide it to make corrections, and recommend functionally equivalent or better alternative libraries for the large model to use; at the same time, this invention not only improves the reliability and security of code generated by large language models, but also enhances the maintainability and practicality of the code, and can be widely applied in scenarios such as intelligent programming assistants, integrated development environments (IDEs), and code review platforms, which can significantly reduce the later maintenance costs of developers, reduce the security risks of software systems, and improve the application value of large language models in actual software development. Attached Figure Description
[0017] Figure 1 This is a flowchart of the knowledge base-based outdated library detection and large model multi-round optimization code generation method of the present invention; Figure 2 This is a flowchart illustrating the construction process of the knowledge base of this invention; Figure 3 This is a flowchart of the code generation and feedback optimization process of this invention; Figure 4 This is an example diagram of the large model code generation and feedback prompt information template of the present invention; Figure 5 These are example diagrams of the initial code and the code after feedback optimization for the large model generation of this invention. Detailed Implementation
[0018] Exemplary embodiments will be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present invention. Rather, they are merely examples of apparatuses and methods consistent with some aspects of the invention as detailed in the appended claims.
[0019] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the invention. The singular forms “a,” “the,” and “the” used in this invention and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more of the associated listed items.
[0020] It should be understood that although the terms first, second, third, etc., may be used in this invention to describe various information, this information should not be limited to these terms. These terms are only used to distinguish information of the same type from one another. For example, first information may also be referred to as second information without departing from the scope of this invention, and similarly, second information may also be referred to as first information. Depending on the context, the word "if" as used herein may be interpreted as "when," "when," or "in response to a determination."
[0021] The present invention will now be described in detail with reference to the accompanying drawings. Unless otherwise specified, the features of the following embodiments and implementations can be combined with each other.
[0022] See Figure 1 The present invention provides a knowledge base-based method for detecting outdated libraries and generating code for large models through multi-round optimization. This method uses multiple rounds of feedback to avoid using outdated libraries during code generation. Specifically, it includes the following steps: Step S1: Construct a knowledge base: Based on the maintenance status, version update status, and security vulnerability information of the codebase, determine whether the codebase is outdated or has security vulnerabilities. Use database migration datasets such as PyMigBench and SALM to determine if there are better alternative libraries, and construct an outdated knowledge base and an alternative knowledge base for the codebase. For example... Figure 2 As shown, the specific steps include the following: Step S1.1: Data collection of the library migration database: Based on library migration datasets such as PyMigBench and SALM, analyze and extract the migration instances recorded therein, extract library migration pairs, and obtain the correspondence between the source library and the target library.
[0023] Specifically, based on existing library migration datasets such as PyMigBench and SALM, which primarily originate from key change records in open-source communities like GitHub and are typically based on the project's requirements.txt or setup.py files, we can analyze and extract migration instances from these datasets to obtain library migration pairs and the correspondence between the source and target libraries. This can then serve as an important reference for determining whether a library is outdated, providing a basis for identifying potentially obsolete libraries and their replacements.
[0024] Step S1.2, Library Update Status Detection: Obtain version release information, maintenance status, and update frequency of third-party libraries and standard libraries through the PyPI official platform and Python official resources respectively. Extract the last update time of the library and determine whether it meets the obsolescence condition based on a preset time threshold. Mark libraries that meet the obsolescence condition as... Among them, meeting the obsolete condition means that the library has been in a state of long-term non-maintenance or has been marked as obsolete or deprecated.
[0025] Specifically, the release information, maintenance status, and update frequency of third-party libraries are obtained through the PyPI official platform. The last update time of the library is extracted, and combined with a preset 12-month time threshold, it is determined whether the library is in a long-term unmaintained state. Libraries in a long-term unmaintained state are marked as... In determining whether a third-party library is in a long-term unmaintained state, a time threshold is checked. A library is considered obsolete if it has exceeded this threshold. For example, the library `nose` was last updated in June 2015 and is therefore marked as obsolete. Similarly, version release information, maintenance status, and update frequency of the standard library are obtained from official Python resources. Based on the official Python documentation, it is determined whether the standard library is marked as obsolete or deprecated in a particular version. The release date of the previous version is used as the obsolete date for the standard library, and libraries marked as obsolete or deprecated are also marked as obsolete. In this context, a library is considered obsolete if it is marked as deprecated or obsolete in a specific version. For example, optparse was marked as deprecated in Python 2.7, and therefore is also considered obsolete. .
[0026] Step S1.3, Library Security Status Detection: Obtain vulnerability disclosure information for each library from the CVE database to identify libraries with security risks and mark them as such. .
[0027] Specifically, vulnerability disclosure information for each library is collected from the CVE database, including vulnerability type, risk level, and remediation status. The presence of dangerous vulnerabilities in a library is determined based on its risk level. For example, the risk level uses the CVSS score from a common vulnerability scoring system. If a third-party library's vulnerability has a CVSS score higher than 4.0, it is considered to have a dangerous vulnerability. If a library has a dangerous vulnerability and the corresponding remediation status is unpatched or unmaintained, it is classified as a risky library. To facilitate subsequent knowledge base construction and library identification and detection, risky libraries are also marked as... Unified into The collection is marked with the reason for its removal: the existence of dangerous vulnerabilities. For example, the pickle library has a vulnerability in deserialization (CVE-2025-1716), which could allow attackers to achieve remote code execution by constructing malicious input, and therefore it is classified as a high-risk library.
[0028] Step S1.4, Organizing Knowledge Base Data: Based on the markings... The library builds an obsolete knowledge base for the codebase, constructs an alternative knowledge base based on the correspondence between the source and target libraries, and marks it as obsolete. The outdated knowledge base is indexed and associated with its corresponding alternative knowledge base, security updates provided by the PyPI official platform, or vulnerability avoidance solutions provided by the CVE vulnerability database. A unified data structure is used for storage.
[0029] Specifically, firstly, based on the above-mentioned markings The first step involves building an obsolete knowledge base for the source and target libraries. Then, an alternative knowledge base is constructed based on the correspondence between the source and target libraries. This is done by extracting historically common library substitution relationships from library migration datasets PyMigBench and SALM, i.e., obtaining the correspondence between the source and target libraries in step S1.1. This is then combined with community practices and official recommendations to supplement the knowledge base with libraries that offer better functionality or are more actively maintained. For example, in the field of network requests, while urllib is an early standard library, it is relatively cumbersome to use and has limited functionality. The requests library, on the other hand, provides a simpler API, automatic session management, and a more robust security mechanism, and is more actively maintained and updated. Therefore, the requests library can be considered a superior and more actively maintained alternative to urllib. Finally, the knowledge base marked as... The database is indexed and associated with its corresponding alternative knowledge base, security updates provided by the PyPI official platform, or vulnerability mitigation solutions provided by the CVE vulnerability database. A unified data structure is used for storage, thus providing a basis for subsequent database detection and alternative database recommendations. Furthermore, since the outdated and alternative knowledge bases are based on time information, the database is updated regularly and continuously iterated based on code feedback from subsequent steps.
[0030] During the organization of the knowledge base data, the library substitution relationships in the library migration dataset were used as a foundation, and supplemented with more functionally superior or actively maintained alternative libraries based on community practices and official recommendations. Finally, for all libraries marked as... The knowledge base is indexed and associated with candidate alternatives or updated versions, forming a unified data structure that facilitates subsequent dependency detection and alternative recommendations. Furthermore, the knowledge base supports regular updates and iterative improvements through code correction feedback, ensuring its timeliness and usability.
[0031] Step S2, Large Model Code Generation: Receive the user's input natural language question, wrap it with specific prompts, and guide the large model to pay attention to library selection when generating code, thus leading to the generation of robust code. For example... Figure 3 As shown, the specific steps include the following: Step S2.1: Receive the natural language problem description input by the user, and preprocess the input text, including removing irrelevant symbols, word segmentation, and keyword extraction, to obtain the preprocessed input text, which facilitates the construction of subsequent prompt information and the calling of large models.
[0032] Step S2.2: Invoke the large model based on the constructed prompts. These prompts include preprocessed input text and library selection constraints. The library selection constraints instruct the large model to prioritize actively maintained, frequently updated libraries without known security vulnerabilities during code generation, avoiding the continued use of outdated or risky libraries and reducing the probability of the generated code containing outdated knowledge bases. In this way, the large model can be guided by constraints during the generation phase.
[0033] Step S2.3: During the code generation process, the large model performs diversified outputs according to the preset generation parameters to obtain a set of candidate code fragments, where each candidate code fragment contains different library implementations.
[0034] Specifically, during code generation, the large model outputs diverse values based on preset generation parameters, including temperature values, maximum generation length, and the number of candidates. By setting these parameters, a set of candidate code snippets can be obtained, each containing a different library implementation, potentially employing different libraries or implementations. This provides a richer selection space for subsequent dependency detection and filtering. The set of candidate code snippets output by the large model serves as preliminary results and enters the subsequent dependency detection and feedback optimization process.
[0035] Step S3, Library Dependency Detection: The code generated by the large model is parsed, and the libraries referenced by the code are extracted through syntax analysis. These libraries are then compared with an outdated knowledge base to detect whether the code generated by the large model contains outdated libraries. Figure 3 As shown, the specific steps include the following: Step S3.1: Use an Abstract Syntax Tree (AST) to parse the candidate code snippets generated by the large model, systematically extract the statement nodes involving module imports in the code snippets, and identify and extract the library names that the code depends on one by one by parsing the statement nodes. The statement nodes include import statements and from...import... statements.
[0036] Specifically, the code (i.e., candidate code fragments) generated by the large model in step S2 is parsed, and static analysis is performed using an abstract syntax tree (AST). Specifically, the generated Python code is first taken as input and converted into a corresponding syntax tree structure using the AST, allowing for systematic analysis of the code's syntactic elements. During the parsing of the syntax tree, the focus is on traversing and identifying statement nodes involving module imports, including `import` statements and `from...import...` statements. By parsing these statement nodes, the library names referenced in the code can be identified and extracted one by one, and then normalized to obtain the names of the third-party or standard libraries that the code depends on.
[0037] Step S3.2: Compare the names of the libraries that the code depends on with the obsolete knowledge base. Based on the library maintenance status, version update status and vulnerability disclosure information recorded in the obsolete knowledge base, verify and classify the libraries referenced by the code one by one, detect whether they belong to the obsolete knowledge base, and generate preliminary detection results as the detection results of library dependencies.
[0038] Specifically, the library names extracted in step S3.1 are compared with the obsolete knowledge base constructed in step S1. Based on the library maintenance status, version update information, and vulnerability disclosure information recorded in the obsolete knowledge base, the libraries referenced by the code are verified and classified one by one to check whether they belong to the obsolete knowledge base. And generate preliminary test results.
[0039] Step S4, Feedback and Correction of the Large Model: When outdated libraries are detected in the code generated by the large model, the detection results of the library dependencies are fed back to the large model. Simultaneously, when the alternative knowledge base contains recommendable alternative libraries, the recommendation results are fed back to the large model to guide it in generating new code. During the new code generation process, the use of outdated libraries is also avoided. For example... Figure 3 As shown, the specific steps include the following: Step S4.1: When step S3 detects that the code generated by the large model contains outdated libraries, the detection results of the library dependencies are fed back to the large model in the form of structured feedback information. The feedback information includes the name of the outdated library and the reason for its obsolescence. The large model corrects the original code based on the feedback information, replaces or removes the outdated libraries in the original code, and regenerates the corrected code.
[0040] Step S4.2: Execute the library dependency detection process from step S3 again on the corrected code to determine whether there are still outdated libraries in the iteratively generated code. If they are, continue the feedback and correction of the large model. At the same time, retrieve the alternative library corresponding to the function of the marked library from the alternative knowledge base built in step S1 and generate alternative recommendation information; otherwise, skip directly to step S5.
[0041] Step S4.3: Feed the alternative recommendation information back to the large model to guide it to prioritize the use of recommended alternative libraries during code correction or regeneration. Through the above iterative optimization, the optimized code that does not depend on outdated libraries and adopts a better functional alternative is finally obtained.
[0042] Step S5, Optimal Code Output: When it is detected that the code generated by the large model does not contain outdated libraries, the code at this time is taken as the optimal code output.
[0043] In summary, this invention mainly consists of two core parts: knowledge base construction and code generation optimization. For knowledge base construction, library migration pairs are extracted from existing library migration datasets, and combined with library version update information, maintenance status, security vulnerabilities, and substitution relationships, a knowledge base containing obsolete library markers and recommended alternative libraries is constructed. For code generation optimization, firstly, a large model receives the user's input natural language question description and, through pre-defined prompts, guides the large model to generate preliminary Python code. Then, dependency detection is used to parse the code generated by the large model, automatically extracting the referenced library information. Next, the extracted libraries are compared with the knowledge base, and their maintenance status, version update status, and security risks are used to determine whether they are obsolete. If an obsolete library is detected, the detection result is fed back to the large model, guiding it to correct the generated code and avoid continuing to use obsolete libraries. Simultaneously, if there are functionally equivalent or superior alternative libraries in the knowledge base, recommendation information is fed back to the large model, enabling it to regenerate or optimize the code based on the alternative libraries. This invention effectively reduces the risk of large language models referencing outdated or insecure libraries during code generation, improving the reliability and security of the generated code. Experimental results show that this invention can automatically recommend better alternative libraries, enhancing the maintainability and usability of the generated code and significantly increasing the application value of large models in practical software development.
[0044] like Figure 3 As shown, a flowchart of code generation optimization is presented, specifically including: ① Code generation and feedback optimization: First, the user inputs a natural language problem description, and the input text is preprocessed, including removing irrelevant symbols, word segmentation, and keyword extraction to improve the accuracy of subsequent processing. Then, based on preset prompts, the large model is invoked, where library selection constraints are embedded in the prompts, and candidate code snippets are generated in conjunction with preset generation parameters. The generated code snippets will be passed to... Figure 2The library dependency extraction and detection section analyzes the maintenance status and security risks of referenced libraries to determine whether they are outdated or vulnerable. If the detection results indicate the presence of an outdated library, the results are fed back to the large model based on relevant information from the knowledge base, prompting it to iteratively optimize and correct the code. Subsequently, dependency extraction and detection are performed on newly generated code snippets, and alternative library recommendations from the knowledge base are fed back to the large model again, guiding it to adopt better alternatives for further iterative generation, thereby gradually obtaining a final code output with higher security and maintainability. ② Library dependency extraction and detection: First, the code snippets generated by the large model are parsed, and library reference statement nodes, including "import…" and "from…import…" statements, are extracted using an abstract syntax tree, and the names of the dependent libraries are identified. Then, the extracted libraries are compared with the maintenance status, update records, and security information stored in the knowledge base to determine whether they are outdated or have security risks. If the detection results show that all dependent libraries are under maintenance and pose no risk, the generated code will be directly fed back to the user; if an obsolete library is found to be referenced, its obsolete marker or corresponding alternative library information will be retrieved from the knowledge base, and this information will be fed back to the large model to guide it in code correction and optimization.
[0045] For example, Figure 4 This is an example of a prompt message template for a large model in code generation and feedback optimization. Figure 5 It is a code example of a large model generating code and providing iterative feedback for a problem.
[0046] In the initial code generation phase, the user-input natural language code is first preprocessed, and a prompt template guides the large model to generate code snippets. Taking "How to get the column names of an Excel file" as an example, the prompt template explicitly requires the large model to prioritize secure, actively maintained, and community-recommended libraries, thus avoiding the default selection of outdated or vulnerable libraries and ensuring the reliability of the generated results. Furthermore, the generated code is required to have a clear structure and good runnability, along with necessary comments to improve readability and maintainability, thereby laying the foundation for subsequent library dependency extraction and iterative optimization.
[0047] During the code feedback and iterative generation phase, the quality of library selection in the generated code of the large model is first analyzed through library dependency extraction and detection. Figure 5Taking the initial code as an example, the abstract syntax tree parsing result shows that it references the xlrd library. Comparison with the knowledge base reveals that this library has been determined to be obsolete. This is because, with the evolution of Excel file formats, xlrd can no longer support common formats other than .xls, making it difficult to meet current practical application needs. Therefore, the obsolescence information of xlrd and the reasons are fed back to the large model. Simultaneously, the prompt message template explicitly requires the large model to maintain its functional requirements unchanged, avoid changing code logic, and emphasizes security and maintainability, highlighting the removal of obsolete libraries and prioritizing actively maintained alternatives, thereby guiding the large model to focus on library migration and optimization tasks. Furthermore, when the optimization effect of the large model in the initial iteration is unsatisfactory, it will be explicitly provided with recommended alternative libraries from the knowledge base in subsequent iterations to reduce the risk of the large model making arbitrary guesses. At the same time, the prompt message further constrains the large model's alternative implementation to conform to community practices and mainstream standards, rather than being arbitrarily pieced together, thereby ensuring the rationality and maintainability of the generated code.
[0048] During the evaluation process, natural language code questions related to library selection were extracted from the open-source community Stack Overflow. The StaQC (Stack Overflow Question-Code) dataset systematically collects a large number of questions and answers from Stack Overflow, constructing high-quality question-code pairs. These questions reflect real-world development scenarios, i.e., the library selection needs of developers when implementing specific functions. By analyzing the code in accepted user answers, we detected whether the libraries they depended on appeared in the set of outdated libraries marked by the knowledge base, and based on this, we selected 667 questions related to 20 outdated libraries. These questions cover a variety of real-world development scenarios, ensuring the representativeness of the experimental data and providing a targeted testing foundation for evaluating the performance of the large model in library selection.
[0049] During the experiment, several representative large-scale models were selected, including Qwen-Plus, LLaMA3.3-70B-Instruct, DeepSeek-V3, GPT-3.5-Turbo, and GPT-4o, to generate code for collected natural language code problems. These large-scale models have wide applications and high representativeness in code generation tasks; secondly, they cover different architectural designs and training methods, thus more comprehensively reflecting the differences and performance of large-scale models in library selection; thirdly, these large-scale models can, to some extent, capture the evolutionary trends of library usage patterns, demonstrating their ability to cope with dynamic software ecosystems. In addition, both open-source and closed-source models were included to ensure the comprehensiveness and comparability of the evaluation results.
[0050] The experimental results are shown in Table 1. The results indicate that the large model has certain advantages over human developers in the code generation process. Because community developers often continue existing library usage habits in their historical practice, their code is prone to dependencies on outdated libraries. The large model, on the other hand, can generate implementations that are more in line with mainstream trends. However, the large model's generation capabilities still have shortcomings, especially when dealing with certain specific libraries. It tends to use deprecated or long-unmaintained libraries, leading to potential security and maintenance risks in the generated results.
[0051] Table 1: Accuracy of generating code for various large models on different libraries Table 1 above lists two third-party libraries, Twisted and Pytz, and demonstrates the code generation performance of the large model in code problems involving these two libraries, as well as the improvement effects after applying the two optimization methods. The "Total" item in Table 1 represents the summary of overall experimental results for all 667 code problems.
[0052] To address the aforementioned issues, the iterative optimization method proposed in this invention demonstrates significant effectiveness. In experiments, without optimization, various large models generally exhibited a certain percentage of errors in library selection, with some large models achieving less than 20% accuracy on specific libraries. After introducing the iterative optimization strategy proposed in this invention, the code's reliance on outdated libraries was significantly reduced. Explicitly informing the large models of outdated libraries was sufficient to improve the overall accuracy to approximately 90%. Furthermore, in the second stage of iterative optimization, which further incorporates alternative library recommendations, the accuracy generally exceeded 97%. This indicates that through a closed-loop process of detection, feedback, and iterative correction, large models can effectively break free from historical dependencies in the training data, generating implementations more aligned with the current software ecosystem, thereby achieving significant improvements in security, maintainability, and usability.
[0053] Overall, the large-scale model demonstrates a certain advantage over human developers in library selection. This is attributed to its systematic training on large-scale corpora, which allows it to avoid outdated dependencies that have been used by community developers for a period of time. However, the performance of the large-scale model is still limited in several ways: firstly, pre-training data may contain historical code snippets, leading to a "data leak" effect; secondly, its response to the rapidly evolving library ecosystem is lagging, and its insufficient understanding of the ecosystem and migration context of specific libraries may result in unsatisfactory recommendation results. To alleviate these problems, an iterative intervention mechanism is introduced. When the initial recommendation of the large-scale model fails to meet expectations, explicit alternative library candidates are provided, requiring them to be generated based on community standards. This process significantly reduces the risk of the large-scale model guessing and arbitrarily piecing together solutions. Experiments show that this human-machine collaborative iterative optimization not only improves the performance of the large-scale model on specific library migration tasks but also effectively compensates for its shortcomings in knowledge updating and contextual understanding.
[0054] This invention discloses an optimization method for library selection when generating Python code based on a problem description using a large model. The method consists of two parts: knowledge base construction and code generation optimization. Evaluation results show that this method can reduce the probability of using outdated or risky libraries in the generated code, improve code security and reliability, and enhance maintainability and usability through alternative library recommendations, thereby increasing the application value of large models in software development.
[0055] The above embodiments are only used to illustrate the technical solutions of the present invention, and are not intended to limit it. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for knowledge base-based outdated library detection and large-model multi-round optimization code generation, characterized in that, The method, which uses multiple rounds of feedback to avoid using obsolete libraries during code generation, includes the following steps: Step S1: Build a knowledge base: Determine whether the code base is outdated or has security vulnerabilities based on the maintenance status, version update status and security vulnerability status of the library. Use the library migration datasets PyMigBench and SALM to determine whether there is better alternative library information, so as to build an outdated knowledge base and an alternative knowledge base for the code base. Step S2, Large Model Generates Code: Receive the natural language question input by the user, package it with prompt information, and guide the large model to generate code; Step S3, Library Dependency Detection: Parse the code generated by the large model, extract the libraries referenced by the code through syntax analysis, and compare them with the outdated knowledge base to detect whether the code generated by the large model contains outdated libraries; Step S4, Feedback and Correction of the Large Model: When it is detected that the code generated by the large model contains outdated libraries, the detection results of the library dependencies are fed back to the large model. At the same time, when the alternative knowledge base contains recommendable alternative libraries, the recommendation results are fed back to the large model to guide the large model to generate new code. Step S5, Optimal Code Output: When it is detected that the code generated by the large model does not contain outdated libraries, the code at this time is taken as the optimal code output.
2. The method for knowledge base-based outdated database detection and large model multi-round optimization code generation according to claim 1, characterized in that, Step S1 specifically includes the following sub-steps: Step S1.1: Based on the library migration datasets PyMigBench and SALM, analyze and extract the migration instances recorded therein, extract library migration pairs, and obtain the correspondence between the source library and the target library. Step S1.2: Obtain the version release information, maintenance status, and update frequency of third-party libraries and standard libraries through the PyPI official platform and Python official resources respectively. Extract the last update time of the library and determine whether it meets the obsolescence condition based on the preset time threshold. Mark the libraries that meet the obsolescence condition as... ; Step S1.3: Obtain vulnerability disclosure information for each library from the CVE database to identify libraries with security risks and mark them as such. ; Step S1.4, based on the markings The library builds an obsolete knowledge base for the codebase, constructs an alternative knowledge base based on the correspondence between the source and target libraries, and marks it as obsolete. The outdated knowledge base is indexed and associated with its corresponding alternative knowledge base, security updates provided by the PyPI official platform, or vulnerability avoidance solutions provided by the CVE vulnerability database. A unified data structure is used for storage.
3. The method for knowledge base-based outdated database detection and large model multi-round optimization code generation according to claim 2, characterized in that, In step S1.2, meeting the obsolete condition means that the library is in a state of long-term non-maintenance or has been marked as obsolete or abandoned.
4. The method for knowledge base-based outdated database detection and large model multi-round optimization code generation according to claim 2, characterized in that, Step S1.3 specifically includes: Vulnerability disclosure information for each database is collected from the CVE database, including vulnerability type, risk level, and remediation status. Based on the risk level, it is determined whether a database contains dangerous vulnerabilities. If a database contains dangerous vulnerabilities and the corresponding remediation status is unpatched or unmaintained, the database is designated as a risky database and marked as such. .
5. The method for knowledge base-based outdated database detection and large model multi-round optimization code generation according to claim 1, characterized in that, Step S2 specifically includes the following sub-steps: Step S2.1: Receive the natural language problem description input by the user, and preprocess the input text, including removing irrelevant symbols, word segmentation, and keyword extraction, to obtain the preprocessed input text; Step S2.2: Invoke the large model based on the constructed prompt information, wherein the prompt information includes preprocessed input text and library selection constraint rules. The library selection constraint rules are used to instruct the large model to prioritize libraries that are actively maintained, frequently updated, and have no known security vulnerabilities when generating code. Step S2.3: During the code generation process, the large model performs diversified outputs according to the preset generation parameters to obtain a set of candidate code fragments, where each candidate code fragment contains different library implementations.
6. The method for knowledge base-based outdated database detection and large model multi-round optimization code generation according to claim 5, characterized in that, In step S2.3, the generation parameters include temperature value, maximum generation length, and number of candidates.
7. The method for knowledge base-based outdated database detection and large model multi-round optimization code generation according to claim 1, characterized in that, Step S3 specifically includes the following sub-steps: Step S3.1: Use an abstract syntax tree to parse the candidate code snippets generated by the large model, systematically extract the statement nodes involving module imports in the code snippets, and identify and extract the library names that the code depends on by parsing the statement nodes one by one. Step S3.2: Compare the names of the libraries that the code depends on with the obsolete knowledge base. Based on the library maintenance status, version update status and vulnerability disclosure information recorded in the obsolete knowledge base, verify and classify the libraries referenced by the code one by one, detect whether they belong to the obsolete knowledge base, and generate the detection results of library dependencies.
8. The method for knowledge base-based outdated database detection and large model multi-round optimization code generation according to claim 7, characterized in that, In step S3.1, the statement node includes the import statement and the from...import... statement.
9. The method for knowledge base-based outdated database detection and large model multi-round optimization code generation according to claim 1, characterized in that, Step S4 specifically includes the following sub-steps: Step S4.1: When it is detected that the code generated by the large model contains outdated libraries, the detection results of the library dependencies are fed back to the large model in the form of structured feedback information. The feedback information includes the name of the outdated library and the reason for its obsolescence. The large model corrects the original code based on the feedback information, replaces or removes the outdated libraries in the original code, and regenerates the corrected code. Step S4.2: Perform library dependency detection on the corrected code again to determine whether there are still outdated libraries in the iteratively generated code. If there are, continue to loop through the feedback and correction of the large model, and at the same time, retrieve the alternative library corresponding to the function of the marked library from the alternative knowledge base and generate alternative recommendation information; otherwise, skip directly to step S5. Step S4.3: Feed back the alternative recommendation information to the large model to guide it to prioritize the use of the recommended alternative library during code correction or regeneration to obtain optimized code.