Code generation method and system based on artificial intelligence

By building a code generation method that combines function call graphs and external knowledge bases, we have solved many shortcomings of existing technologies in code generation tools, achieved structural consistency, semantic integrity and security improvements, adapted to enterprise-level development needs, and improved development efficiency and user satisfaction.

CN120803432APending Publication Date: 2025-10-17YINGBO ZHIYUN (CHENGDU) TECH CO LTD

Patent Information

Application Number
CN202510944586.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-09
Publication Date
2025-10-17

AI Technical Summary

Technical Problem

Existing code generation tools have shortcomings in context retrieval accuracy, code maintainability, security detection capabilities, cross-language capabilities, collaborative processing of complex tasks, version control and automatic debugging capabilities, and are unable to meet enterprise-level code management and cross-project collaboration needs.

Method used

Generate function call graphs through function-level analysis, combine with external knowledge base for retrieval, generate structured code snippets, perform static analysis and security testing, support multi-user collaborative development, automatically generate test code, and integrate real-time feedback and learning mechanisms.

Benefits of technology

It improves the structural consistency and semantic integrity of code generation, increases the Top-1 accuracy by 22%, identifies potential vulnerabilities and reduces the code defect rate by 35%, improves development efficiency by 40%, and enhances multi-language support and user satisfaction.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120803432A_ABST
    Figure CN120803432A_ABST
Patent Text Reader

Abstract

The invention discloses an artificial intelligence-based code generation method, which comprises the following steps of: S1, performing function level analysis on a generation request input by a user, extracting a function name, a parameter, a return value, a calling relationship and a dependency relationship, and generating a function calling graph; s2, taking the generation request input in the step S1 as a query, performing retrieval in combination with the function call atlas and the external knowledge base generated in the step S1, and jointly inputting the retrieved content and the coding model to generate a structured code snippet; and S3, taking the generation request input in the S1 and the structured code snippets generated in the S2 as joint input, and generating codes meeting individuation and project specifications. Through a function graph and a retrieval mechanism, a generated result has structural consistency and semantic integrity, and the matching degree with context requirements is remarkably improved (the actual measurement Top-1 accuracy is improved by about 22%).
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The technical field relates to the field of computer software technology, in particular to an artificial intelligence-based code generation method and system. BACKGROUND

[0002] With the rapid development of artificial intelligence (AI) and large language model (LLM) technologies, code generation tools have received widespread attention. Existing products such as GitHub Copilot, Amazon CodeWhisperer, and TabNine can achieve preliminary conversion from natural language to code. However, the current mainstream technologies still have obvious deficiencies in context retrieval accuracy, code maintainability, security detection capability, cross-language capability, complex task collaborative processing, version management and automatic debugging capability: single retrieval mechanism: most systems only retrieve training data based on semantic similarity, fail to effectively integrate external knowledge bases, and are difficult to support large-scale dynamic knowledge updates. Weak function dependence and call relationship processing: lack of function analysis and graph modeling capabilities, difficult to support logical association reasoning between complex functions. Limited support for collaborative development and debugging: most systems only serve the generation of single-user code snippets, lack of multi-task scheduling, multi-person collaborative editing and version control functions. Poor multi-language support and translation adaptation capability: existing technologies are generally based on English, and have weak adaptation and processing capabilities for multi-language inputs. Weak intelligent feedback and learning mechanism: the generated results lack real-time interaction and self-optimization mechanism, and cannot continuously learn and adjust the generation strategy according to user behavior. For example, GitHub Copilot uses the OpenAI Codex model to generate code, but it mainly relies on static language model reasoning, lacks graph-based code structure understanding and dynamic environment adaptation mechanism, and is difficult to meet the needs of enterprise-level code management and cross-project collaboration in function. Therefore, there is an urgent need for a more systematic, intelligent and expandable code generation and management platform to achieve precise code generation, security compliance checking, multi-language cross-platform adaptation and personalized intelligent optimization.

[0003] SUMMARY

[0004] The purpose of the present application is to provide an artificial intelligence-based code generation method and system, and the specific technical solutions are as follows:

[0005] An artificial intelligence-based code generation method, comprising: S1, function-level analysis of the user's input generation request, extracting function name, parameters, return value, calling relationship and dependency relationship, and generating a function call graph; S2, input the generation request in S1 as a query, combine the function call graph generated in S1 and the external knowledge base for retrieval, input the retrieved content and the coding model jointly to generate a structured code segment; S3, input the generation request in S1 and the structured code segment generated in S2 as joint input to generate code that meets individualization and project specifications. S4, static analysis of the code generated in S3 to identify sensitive operations and risky behaviors in the code, output quality score report and repair suggestions, and optimize the generated code accordingly. S5, store the code generated in S3 or the optimized code in S4 in the version control system, and the version control system supports multiple user collaborative development. S6, automatically generate unit test and integration test code based on the code generated in S3 or the optimized code in S4, run the test code, and automatically adjust the generation strategy based on the running results. S7, collect and save user operation records for user-level individualization strategy optimization.

[0006] When the code that meets individualization and project specifications is generated in S3, the user can set customization items when inputting the generation request, including function templates, naming specifications, comment standards and / or external knowledge bases.

[0007] When the code generated in S3 is analyzed statically in S4, it includes potential vulnerabilities, unused variables, performance bottlenecks and repeated logic detection.

[0008] When multiple users collaborate to develop in S5, tasks are automatically split and distributed according to roles, and real-time editing and change conflict resolution mechanisms are provided.

[0009] An artificial intelligence-based code generation system for executing the artificial intelligence-based code generation method described above is characterized by comprising: an input module, through which a user inputs a generation request; a function graph module, which performs function-level parsing on the generation request received by the input module and constructs a structured function graph; a vector retrieval module, which vectorizes the generation request received by the input module, compares the vectorized generation request with the structured function graph constructed in the function graph module, and combines it with vector content in an external knowledge base to retrieve relevant examples and information such as API usage, ultimately aggregating the retrieved content and the generation request received by the input module into prompt information; a language module, which provides multilingual code generation and outputs code that meets personalized and project specifications based on the prompt information provided by the vector retrieval module; and an external knowledge base module, which converts an external knowledge base into a vector library and provides the vector content of the external knowledge base to the vector retrieval module. The external knowledge base includes project-related API documentation, historical code repositories, and internal enterprise documents.

[0010] The beneficial effect of this application is that, through the function graph and retrieval mechanism, the generated results have structural consistency and semantic integrity, and the matching degree with context requirements is significantly improved (the measured Top-1 accuracy rate is increased by about 22%).

[0011] Figures in the specification

[0012] Figure 1 This is a schematic diagram of the application process. Specific embodiments

[0013] To make the objectives, technical solutions, and advantages of the present invention more clearly understood, the present invention will be further described in detail below in conjunction with specific embodiments and with reference to the accompanying drawings. It should be understood that these descriptions are merely exemplary and are not intended to limit the scope of the present invention. In addition, in the following description, descriptions of well-known structures and technologies are omitted to avoid unnecessary confusion of the concepts of the present invention.

[0014] A code generation method based on artificial intelligence, comprising:

[0015] S1. Perform function-level parsing on the generation request input by the user, extract the function name, parameters, return value, call relationship and dependency relationship, and generate a function call graph.

[0016] S2, take the generation request input in S1 as a query, combine the function call graph generated in S1 and the external knowledge base to perform retrieval, input the retrieved content and the encoding model jointly to generate a structured code segment. Through the function graph and retrieval mechanism, the generated result has structural consistency, semantic integrity, and significantly improved matching degree with the context requirements (Top-1 accuracy rate is improved by about 22%).

[0017] S3, take the generation request input in S1 and the structured code segment generated in S2 as joint input to generate code that meets individualization and project specifications. When generating code that meets individualization and project specifications in S3, the user can set customization items when inputting the generation request, including function templates, naming specifications, comment standards, and / or external knowledge bases.

[0018] S4, perform static analysis on the code generated in S3 to identify sensitive operations and risky behaviors in the code, output a quality score report and repair suggestions, and optimize the generated code accordingly. When performing static analysis on the code generated in S3 in S4, it includes potential vulnerabilities, unused variables, performance bottlenecks, and repeated logic detection. In actual application, integrating static analysis and AI risk detection capabilities can effectively identify potential security problems, avoid sensitive function misuse and privacy leakage, and reduce code defect rate by about 35%.

[0019] S5, store the code generated in S3 or the optimized code in S4 into a version control system, and the version control system supports multiple user collaborative development. When multiple users collaborate in S5, tasks are automatically split and distributed according to roles, and real-time editing and change conflict resolution mechanisms are provided. In actual application, the platform supports multi-task collaborative scheduling, multi-person role distribution, version control, and other functions, which adapt to enterprise-level team development needs and improve development efficiency by about 40%.

[0020] S6, automatically generate unit test and integration test code based on the code generated in S3 or the optimized code in S4, run the test code, and automatically adjust the generation strategy based on the running results.

[0021] S7, collect and save user operation records to realize user-level individualization strategy optimization. In actual application, through adaptive learning driven by user feedback, the generated content is more consistent with the user's project background and style, improving user satisfaction and repeat usage rate.

[0022] An artificial intelligence-based code generation system for executing the artificial intelligence-based code generation method described above, characterized by comprising: an input module through which a user inputs a generation request; a function graph module configured to perform function-level analysis on the generation request received by the input module and construct a structured function graph; a vector retrieval module configured to perform vectorization processing on the generation request received by the input module, compare the vectorized generation request with the structured function graph constructed by the function graph module and vector content in an external knowledge base, retrieve relevant examples and API usage information, and finally aggregate the retrieved content and the generation request received by the input module into prompt information; a language module configured to provide multi-language code generation and output code meeting individualization and project specifications according to the prompt information provided by the vector retrieval module; and an external knowledge base module configured to convert an external knowledge base into a vector library and provide the vector content of the external knowledge base for the vector retrieval module, wherein the external knowledge base includes project-related API documents, historical code repositories, and internal enterprise documents.

[0023] In order to make the present application easier to be understood, the following further describes the present application in conjunction with embodiments.

[0024] The present embodiment describes an implementation of a code generation and management platform that can be run in an enterprise-level software development environment.

[0025] Implementation scheme of an enterprise-level code automatic generation platform

[0026] (1) System initialization configuration

[0027] The server system is built, including: a language model service (supporting multi-language code generation), a function graph service, a vector retrieval service (such as FAISS), a task scheduling service, a user permission management and log monitoring module.

[0028] The external knowledge base interface is configured to load project-related API documents, historical code repositories, internal enterprise documents and other resources, and convert them into a vector library for RAG retrieval.

[0029] (2) Code generation process

[0030] Multi-modal input, the user submits a generation request through natural language requirements, hand-drawn flowcharts, existing code snippets or voice descriptions (converted using STT) and other ways inputted through the platform.

[0031] Function graph construction, if the user uploads an existing code, the platform analyzes it to identify function units, call relationships and dependency relationships, constructs a structured function graph, and uses it as auxiliary input.

[0032] RAG retrieval enhancement, the platform vectorizes user requests and compares them with vectorized content in the knowledge base, retrieves relevant examples, API usage, etc. information, and aggregates prompt content to supplement model input.

[0033] AI model generates code, uses large language models (such as LLaMA, CodeT5, GPT models) and outputs code that meets business logic and project style.

[0034] Code quality and security checks, the platform uses static analysis tools (such as SonarQube) and AI models to score generated code, perform security checks and vulnerability scans, and output suggestions and repair solutions.

[0035] Automatic test generation and debugging, the platform automatically generates basic unit test code, runs tests, and outputs coverage reports. If the run fails, you can get stack information through the debugging interface, locate the problem and regenerate.

[0036] Version management and collaborative editing, all generated and modified code is automatically included in Git version control, users can use Web IDE for multi-user collaborative editing, support online review and code merging.

[0037] Real-time feedback and learning optimization, user behavior is recorded (such as retaining / giving up / modifying suggested code), the platform uses these behavior data to fine-tune model preferences, and implements user-level personalized strategy optimization.

[0038] Effect verification experiment

[0039] Experimental project: A medium-sized enterprise develops a data synchronization service (including interface communication, data cleaning and conversion modules) based on this platform.

[0040] Comparison scheme: GitHub Copilot + manual debugging development method vs. the platform of the present application.

[0041] Sample size: A total of 97 functions were generated, generating about 53,000 lines of code.

[0042]

Claims

1. A code generation method based on artificial intelligence, characterized in that: include: S1. Perform function-level analysis on the generation request input by the user, extract the function name, parameters, return value, call relationship and dependency relationship, and generate a function call graph; S2. Using the generated request input in S1 as a query, searching the function call graph generated in S1 and the external knowledge base, and inputting the retrieved content into the coding model to generate a structured code snippet; S3: Take the generation request input in S1 and the structured code snippet generated in S2 as joint input to generate code that meets the personalized and project specifications.

2. The code generation method based on artificial intelligence according to claim 1, characterized in that: Also includes: S4. Perform static analysis on the code generated in S3, identify sensitive operations and risky behaviors in the code, output a quality score report and repair suggestions, and use this to optimize the generated code.

3. The code generation method based on artificial intelligence as claimed in claim 2, characterized in that: Also includes: S5. Store the code generated in S3 or the code optimized in S4 into a version control system, where the version control system supports collaborative development by multiple users.

4. The code generation method based on artificial intelligence as claimed in claim 2, characterized in that: Also includes: S6. Automatically generate unit test and integration test code based on the code generated in S3 or the code optimized in S4, run the test code, and automatically adjust the generation strategy based on the running results.

5. The code generation method based on artificial intelligence as claimed in claim 1, characterized in that: Also includes: S7. Collect and save user operation records for user-level personalized policy optimization.

6. The code generation method based on artificial intelligence according to claim 1, characterized in that: When generating codes that meet personalization and project specifications in S3, the user can set customization items when inputting a generation request. The customization items include function templates, naming specifications, annotation standards and / or external knowledge bases.

7. The code generation method based on artificial intelligence as claimed in claim 2, characterized in that: The static analysis of the code generated in S3 in S4 includes detection of potential vulnerabilities, unused variables, performance bottlenecks, and repeated logic.

8. The code generation method based on artificial intelligence as claimed in claim 3, characterized in that: When multiple users collaborate on development in S5, tasks are automatically split and distributed according to roles, and a real-time editing and change conflict resolution mechanism is provided.

9. An artificial intelligence-based code generation system for executing the artificial intelligence-based code generation method according to claims 1-8, characterized in that: include: an input module, through which a user inputs a generation request; A function graph module, configured to perform function-level analysis on the generation request received by the input module and construct a structured function graph; A vector retrieval module, which is used to vectorize the generation request received by the input module, compare the vectorized generation request with the structured function graph constructed in the function graph module and the vector content in the external knowledge base, retrieve relevant examples and API usage information, and finally aggregate the retrieved content and the generation request received by the input module into prompt information; A language module is used to provide multi-language code generation and output code that meets personalization and project specifications based on the prompt information provided by the vector retrieval module.

10. The artificial intelligence-based code generation system according to claim 9, characterized in that: Also includes: An external knowledge base module is used to convert an external knowledge base into a vector library and provide the vector content of the external knowledge base to the vector retrieval module. The external knowledge base includes project-related API documents, historical code repositories, and internal enterprise documents.

Citation Information

Patent Citations

  • Intelligent auxiliary method and platform for software development

    CN117369786A

  • Code processing method fusing multiple retrieval modes, model end and storage medium

    CN119002930A

  • Code generation method and related equipment

    CN120255895A

Cited By

  • Large model function call optimization method based on code generation, electronic equipment, computer readable storage medium and computer program product

    CN121455563A