Python dependency library API migration method based on large language model

By simulating the programmer's workflow using a large language model and leveraging multi-agent strategies and unstructured data, this approach solves the problems of high cost and low efficiency in migrating Python dependency library APIs, achieving automated and accurate API migration. It is suitable for tasks such as upgrading artificial intelligence software and refactoring legacy code.

CN120429008BActive Publication Date: 2025-10-28FUDAN UNIVERSITY
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510532974.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-04-25
Publication Date
2025-10-28
Estimated Expiration
2045-04-25

AI Technical Summary

Technical Problem

In existing technologies, migrating Python dependency library APIs requires manual intervention, which is time-consuming and labor-intensive. Furthermore, existing automated methods rely on structured data that is difficult to obtain, resulting in high costs and low efficiency.

Method used

Employing a multi-agent strategy based on a large language model, this approach simulates the programmer's workflow. It achieves automated API migration through four modules: user intent recognition, network resource discovery and collection, migration, and testing and verification. By utilizing unstructured web page data and random walk strategies, combined with syntax trees and differential testing, it reduces labor costs and improves accuracy.

Benefits of technology

It enables efficient and accurate migration of Python dependency library APIs without relying on structured data, reducing manual costs and improving migration efficiency. It is suitable for code updates, code refactoring, and legacy code handling.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120429008B_ABST
    Figure CN120429008B_ABST
Patent Text Reader

Abstract

This invention discloses a Python dependency library API migration method based on a large language model. This method employs a multi-proxy strategy based on the large language model to simulate and automate the process of a programmer migrating an API. Using unstructured web page data easily accessible online, the method performs five steps: user intent recognition, network resource discovery, random walk-based information gathering, migration, and testing and verification. Ultimately, it migrates the user-specified dependency library API from the user-provided code. The multi-proxy strategy allows the large language model to perform well on complex tasks like API migration, while the intent recognition and testing and verification steps effectively control unreliable outputs caused by the large language model illusion problem. This invention can be applied to software engineering tasks such as code updates, code refactoring, and legacy code handling, helping to reduce human costs in software development and maintenance.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of computer software technology, specifically, it relates to the field of API migration technology of large language models and Python programming language, and particularly to a method for migrating Python dependency library APIs based on large language models. Background Technology

[0002] Python libraries are a crucial part of building software systems, especially artificial intelligence systems. This example uses the deep learning framework TensorFlow and the data processing library Pandas. TensorFlow is a widely used deep learning framework that provides a rich API (Application Programming Interface). API Pandas, a powerful data processing and analysis library, provides a wealth of data manipulation capabilities through its diverse APIs. Python libraries and their APIs play an infrastructure role in software engineering. Due to the need to add new features, fix security vulnerabilities, and update the technology stack, Python libraries are frequently updated, often requiring the migration of APIs from older versions to newer ones within existing code.

[0003] API migration can currently be performed using two methods: manual migration and automated migration. Manual migration involves programmers, after fully understanding the existing code, the API before migration, and the API after migration, manually searching for all equivalent APIs in the new version's dependency library and verifying their correctness. This manual migration process is a typical code debugging process, time-consuming, labor-intensive, and requires significant manpower. A range of automated API migration methods can reduce the manpower cost of manual API migration; however, most of these methods rely on pre-built structured data, such as dependency migration maps and code pairs. This structured data is often difficult to obtain. Therefore, API migration remains a time-consuming and labor-intensive problem, making it crucial to establish an automated API migration method that does not rely on pre-built structured data. Summary of the Invention

[0004] To address the shortcomings of existing technologies, this invention proposes an automated, data-independent method for migrating Python dependency library APIs based on large language models. This invention employs a multi-agent strategy based on large language models to simulate and automate the migration of Python dependency library APIs by programmers. This reduces the human cost of API migration and improves its efficiency. The multi-agent strategy allows large language models to perform well on complex tasks such as API migration. This invention can be applied to software engineering tasks such as code updates, code refactoring, and legacy code handling, helping to reduce human costs in software development and maintenance.

[0005] The system of this invention consists of four modules: user intent recognition, network resource discovery and collection, migration, and testing and verification. It involves several proxies based on a large language model. This invention uses unstructured web page data that can be easily obtained from the network. Through five steps—user intent recognition, network resource discovery, information collection based on random walks, migration, and testing and verification—it ultimately migrates the user-specified dependency library APIs from the user-provided code. The intent recognition and testing and verification steps effectively control unreliable outputs caused by the illusion problem of large language models. The technical solution of this invention is described in detail below.

[0006] This invention provides a Python dependency library API migration method based on a large language model. It employs a multi-proxy strategy based on a large language model to migrate and automate the process, including the following steps:

[0007] Step 1: User Intent Recognition

[0008] Identify the specific APIs in the code to be migrated that need to be migrated based on the user-inputted code and migration requirements;

[0009] Step 2: Network Resource Discovery and Collection

[0010] Based on the migration requirements input by the user, relevant network resources are discovered and collected to obtain the necessary knowledge for API migration.

[0011] Step 3: Migration

[0012] Based on the user's migration needs, API migration is carried out with reference to the relevant knowledge of API migration tasks that have already been acquired;

[0013] Step 4: Testing and Verification

[0014] The generated multiple migrated codes are verified to determine the correctness of the migration.

[0015] In this invention, in step 1, an agent based on a large language model is used to determine the user's true migration intention.

[0016] In this invention, in step 2, two proxies based on a large language model are used to discover network resources from trusted sources and determine whether the collected network resources are sufficient to support the API migration task. Specifically, one proxy is first used to search and record URLs in the Python documentation source according to the specific migration requirements input by the user, and then another proxy is used to determine whether all the recorded web page resources meet the requirements of the migration task.

[0017] In this invention, the Python documentation sources include PyPI, DuckDuckGo, and GitHub.

[0018] In this invention, step 2 involves using three agents based on a large language model to collect information from the discovered network resources. Specifically, this includes: first, using one agent to extract keywords from all discovered network resources; then, using another agent to score and rank the importance of each network resource to the migration task based on the extracted keywords; and finally, using another agent to collect API-related information contained in the discovered network resources according to their importance ranking using a random walk approach.

[0019] In this invention, when scoring and ranking the importance of migration tasks, the text matching degree between keywords and migration requirements and the semantic matching degree using cosine similarity are used for measurement.

[0020] In this invention, step 3 involves using a proxy based on a large language model to perform API migration; specifically, this includes using a proxy to generate multiple sets of migrated code based on the user-inputted code to be migrated and migration requirements, while referring to the AST syntax tree of the code to be migrated.

[0021] In this invention, step 4 involves using two proxies based on a large language model to test and verify the migrated code. Specifically, this includes: first, using one proxies based on a large language model to generate test cases for the code before and after migration; then, using another proxies based on a large language model to build a test environment and perform differential testing on the code before and after migration, verifying the correctness of the generated migrated code one by one.

[0022] This invention also provides a Python dependency library API migration system based on a large language model, which is used to execute the above-described method. It includes a user intent recognition module, a network resource discovery and collection module, a migration module, and a testing and verification module; wherein:

[0023] The user intent recognition module identifies the specific APIs in the code to be migrated that need to be migrated based on the user's input of the code to be migrated and the migration requirements.

[0024] The network resource discovery and collection module discovers and collects relevant network resources based on the migration requirements input by the user, thereby obtaining the necessary knowledge for API migration.

[0025] The migration module performs API migration based on the user's migration requirements and with reference to the acquired API migration task-related knowledge.

[0026] The testing and verification module verifies multiple migrated code snippets generated by the migration module to determine the correctness of the migration.

[0027] Compared with the prior art, the beneficial effects of the present invention are as follows:

[0028] An automated Python dependency library API migration method using the multi-proxy design pattern: This method mimics the workflow of human programmers performing API migrations and leverages a large language model for automation. Existing automated API migration methods often require pre-built, hard-to-obtain structured information. This method overcomes this deficiency, achieving accurate and low-cost automated migration of Python dependency library APIs. A traditional method for automatically migrating APIs with structured dependency information is used as a comparison. The execution flow of this method and the comparison method is compared as follows: Figure 3 As shown in the figure, this method eliminates the need for structured information construction and model training, achieving end-to-end input and output. In preliminary evaluation experiments, this method saves a significant amount of manpower and economic costs associated with structured information construction and model training.

[0029] Information gathering based on random walks: This method discovers relevant knowledge about APIs from trusted sources based on the user's input transition requirements and collects it according to importance, providing reliable, high-quality input for large language models. Random walks are often used in search engine page ranking, while traditional methods mostly use traversal for information gathering. The introduction of random walks can improve the relevance and quality of the collected information. This section uses a traditional traversal-based information gathering method as a comparison, and the execution flow of this method and the comparison method are compared as follows: Figure 4As shown in the figure, this method only collects information whose importance score exceeds a threshold, and the random walk strategy ensures that more important relevant information is collected first. The comparison method, however, does not judge importance or relevance. Therefore, the information collected by this method is of higher quality and more relevant to the transfer task. Since large language models are sensitive to input information, higher quality input information will lead to more accurate output. In preliminary evaluation experiments, compared with the comparison method, the information collection method based on random walk effectively improved the accuracy of automatic transfer.

[0030] Introducing a syntax tree in the transfer step: Methods using large language models to handle code-related tasks typically treat code and natural language equally, relying on the knowledge acquired by the large language model during extensive training to complete the task. This invention provides the large language model with the syntax tree information of the code to be transferred during the transfer step, and the transfer agent decides whether to use it. This is illustrated here with a comparison to traditional automated methods that treat code and natural language equally. The execution flow of this method and the comparison method is as follows: Figure 5 As shown in the figure, this method considers not only the semantic and syntactic information of the code, but also the structural information of the code as a semi-structured language, which is conveyed by the syntax tree. The comparison method, however, does not consider this structural information. In preliminary evaluation experiments, compared with the comparison method, the structural information of the semi-structured language conveyed by the syntax tree effectively improved the accuracy of automatic transfer.

[0031] A test verification method based on differential testing: This method is used to verify the accuracy of automatically generated API migration plans and remove inaccurate migration plans. Differential testing is commonly used to discover software system defects. Traditional automated API migration methods rely heavily on structured data (such as code pairs) to provide verification criteria. The introduction of differential testing eliminates the need for structured data represented by code pairs for verification, thus improving the accuracy of automated API migration. This section uses a traditional automated method that relies on code pairs for verification criteria as a comparison. The execution flow of this method and the comparison method is compared as follows: Figure 6 As shown in the figure, because the migration scheme is not unique, the migrated code is also not unique. The comparison method uses text matching between the migrated code and the verification criteria to determine whether the migration was successful. This method cannot handle cases where the migrated code is not unique, and is prone to false positives. This method does not focus on text matching, but instead focuses on the code's input and output. As long as the code before and after migration gives the same output to the same input, the migration is considered successful. This method effectively improves the accuracy of determining whether the migration was successful, thereby improving the quality of migration. Attached Figure Description

[0032] Figure 1 This is a schematic diagram of the Python dependency library API migration method framework based on the large language model.

[0033] Figure 2 This describes the execution process of Python dependency library API migration methods based on a large language model.

[0034] Figure 3 This diagram illustrates a comparison between Python dependency library API migration methods based on large language models and traditional API automatic migration methods that rely on structured information.

[0035] Figure 4 This diagram illustrates a comparison between the Python dependency library API migration method based on a large language model and the traditional information collection method that uses traversal.

[0036] Figure 5 This diagram illustrates a comparison between the Python dependency library API migration method based on a large language model and the traditional automated method that treats code and natural language equally during migration.

[0037] Figure 6 This diagram illustrates a comparison between the Python dependency library API migration method based on a large language model and the traditional automated method that relies on code pairs to provide verification standards during validation. Detailed Implementation

[0038] The technical solution of the present invention will now be described in detail with reference to the accompanying drawings and embodiments.

[0039] In API migration tasks, it can be observed that human programmers can accurately migrate APIs without requiring large amounts of structured data. During API migration, programmers primarily obtain information from sources such as documents and web pages, which provide unstructured content. After information acquisition, programmers process this information using their accumulated professional knowledge and experience to ultimately provide a migration plan. Furthermore, large language models are trained on massive datasets including code, thus it is reasonable to assume that they already possess code-related professional knowledge. These observations inspired this invention to use large language models to simulate the API migration process of human programmers, thereby obtaining an automated API migration method that does not rely on pre-constructed structured data.

[0040] However, two challenges need to be addressed to achieve the above approach. First, large language models are essentially more complex deep neural networks. The interpretability issues and black-box characteristics of deep neural networks make large language models susceptible to the illusion problem, potentially producing unreliable outputs (Challenge 1). Therefore, unreliable transfer schemes may be generated in API autotransfer. Second, the performance of large language models is sensitive to input information. If the quality of the input information is low, it is difficult to generate accurate transfer schemes, and the effectiveness of API autotransfer cannot be guaranteed (Challenge 2).

[0041] This invention employs a multi-agent design pattern to propose an automated method for migrating Python dependency library APIs based on a large language model. To address the first challenge, the invention includes a user intent recognition module and a testing and verification module. These two modules effectively avoid inaccurate API identification due to the illusion of a large language model, leading to ineffective code refactoring and inaccurate API migration solutions. To address the second challenge, the invention includes a network resource discovery and collection module. This module can discover relevant knowledge about the APIs from trusted sources based on the user's input migration requirements and collect it according to importance using a random walk strategy, providing reliable, high-quality input for the large language model.

[0042] The framework of this method is as follows Figure 1 As shown in the diagram. This method takes the code to be migrated and the migration requirements as input, and the migrated code as output. It mainly consists of four parts: a user intent recognition module, a network resource discovery and collection module, a migration module, and a testing and verification module. The relationship between the four parts is as follows: Figure 1 As shown.

[0043] The user intent recognition module identifies the specific APIs within the code to be migrated that require migration based on the user's input and migration requirements. A piece of code to be migrated typically calls multiple APIs, which may include APIs that do not need to be migrated. This module involves a proxy based on a large language model, which can prevent unnecessary refactoring of the code to be migrated due to the large language model's illusion problem.

[0044] The network resource discovery and acquisition module discovers and acquires relevant network resources based on the user's input migration requirements, thereby obtaining the necessary knowledge for API migration. This module first searches for network resources from three trusted sources, then judges the importance of the search results, and finally acquires information based on importance. This module involves five proxies based on a large language model. This module provides reliable and task-relevant knowledge for the migration task, effectively helping the large language model complete the migration task.

[0045] The migration module performs API migration based on the user's migration requirements and with reference to previously acquired API migration task knowledge. This module generates multiple copies of the migrated code for the code to be migrated. This module involves one agent based on a large language model, which can decide whether to refer to the syntax tree of the code to be migrated during the migration process. The introduction of the syntax tree helps increase the accuracy of API migration based on the large model.

[0046] The testing and verification module verifies multiple migrated code snippets generated by the migration module to determine the correctness of the migration. This module employs differential testing techniques and involves two proxies based on a large language model. A successful migration is determined when the code before and after migration produces the same output for all test cases generated by this module.

[0047] The execution process of this invention is as follows: Figure 2 As shown; the specific implementation method of the present invention is as follows:

[0048] Step 1: User Intent Recognition. This step involves one agent (Agent 1) based on a large language model and includes one sub-step. The user intent recognition part can effectively avoid unnecessary refactoring of the code to be migrated due to the illusion problem of the large language model.

[0049] Step 1.1: Agent 1 identifies the specific APIs in the code to be migrated that need to be migrated based on the user-input code to be migrated and the migration requirements.

[0050] Step 2: Network Resource Discovery. This part consists of two sub-steps, involving two proxies based on the large language model (Proxies 2 and 3). Network resource discovery provides the large language model with reliable information for the transfer task, ensuring the quality of the transfer task.

[0051] Step 2.1: Based on the user's migration requirements, Agent 2 searches for network resources from three trusted sources: PyPI, DuckDuckGo, and GitHub, creating a "migration task-related network resource directory." This directory records the URLs of the network resources.

[0052] Step 2.2: Agent 3 determines, based on the user's migration requirements, whether the acquired network resource directory related to the migration task is sufficient to support the migration task. If the information is sufficient, proceed to the next step. If the information is insufficient to support the migration task, repeat step 2.1.

[0053] Step 3: Network Resource Acquisition. This part consists of three sub-steps, involving three proxies based on the large language model (Agent 4-Agent 6). Network resource acquisition uses a random walk strategy, collecting network resources based on importance. This step provides the large language model with more task-relevant information, ensuring the quality of the transfer task.

[0054] Step 3.1: Agent 4 processes the network resources corresponding to each URL in the network resource directory related to the migration task.

[0055] Keyword extraction.

[0056] Step 3.2: Agent 5 scores the importance of network resources based on the keywords extracted in Step 2.3, according to the user's migration needs. The importance is measured using the textual matching degree between the keywords and the migration needs, and the semantic matching degree using cosine similarity.

[0057] Step 3.3: Based on the scoring results of Step 3.2, Agent 6 uses a random walk strategy to extract information contained in network resources in an importance-first manner, thereby constructing the knowledge base required for the migration task.

[0058] Step 4: Migration. This step consists of one sub-step involving one agent (agent 7) based on a large language model.

[0059] Step 4.1: Agent 7 uses the knowledge base required for the migration task built in Step 2 and decides on its own whether to refer to the syntax tree of the code to be migrated to generate multiple copies of the migrated code.

[0060] Step 5: Testing and Verification. This step consists of two sub-steps, involving two proxies based on a large language model (Proxies 8 and 9). The testing and verification part uses differential testing to determine whether the migration task was successful. The migration is considered successful if the code before and after the migration gives the same output to a series of test cases.

[0061] Step 5.1: Agent 8 generates a set of test cases for the code to be migrated and a copy of the migrated code.

[0062] Step 5.2: Agent 9 sets up a test environment and executes differential tests based on the test cases generated in Step 4.1. If the differential tests pass, the migration is successful, and the migrated code is output to the user. If the differential tests fail, it returns to Step 5.1 to continue verifying the correctness of the next set of migrated code.

[0063] This invention can be applied to the following main application scenarios:

[0064] 1) Upgrading and maintaining artificial intelligence software. During the development and maintenance of artificial intelligence software systems, Python dependency libraries are frequently updated, requiring constant API migration in the code. This invention can automatically complete the migration of dependency library APIs without manual code modification, thus significantly reducing labor costs.

[0065] 2) Legacy Code Retrofitting. Many enterprises have accumulated large amounts of legacy code that relies on older versions of Python libraries, but due to historical reasons, cannot run directly in modern software environments. This invention can be applied to the modernization of legacy code, automatically migrating the older Python library APIs that the legacy code depends on to current mainstream library versions. This invention avoids the huge costs incurred by enterprises due to redevelopment.

Claims

1. A Python dependency library API migration method based on a large language model, characterized in that, The migration and automation of Python dependency library APIs are achieved using a multi-proxy strategy based on a large language model, including the following steps: Step 1: User Intent Recognition Identify the specific APIs in the code to be migrated that need to be migrated based on the user-inputted code and migration requirements; Step 2: Network Resource Discovery and Collection Based on the migration requirements input by the user, relevant network resources are discovered and collected to obtain the necessary knowledge for API migration. Step 3: Migration Based on the user's migration needs, API migration is carried out with reference to the relevant knowledge of API migration tasks that have already been acquired; Step 4: Testing and Verification The generated multiple migrated code snippets are verified to determine the correctness of the migration; among them: In step 2, two proxies based on a large language model are used to discover trusted network resources and determine whether the collected network resources are sufficient to support the API migration task. First, one proxy is used to search and record URLs in the Python documentation source according to the specific migration requirements input by the user. Then, another proxy is used to determine whether all the recorded web page resources meet the requirements of the migration task. In step 2, three agents based on a large language model are used to collect information from the discovered network resources. Specifically, one agent is used to extract keywords from all the discovered network resources. Then, another agent is used to score and rank the importance of each network resource to the migration task based on the extracted keywords. Finally, another agent is used to collect API-related information contained in the discovered network resources according to the importance ranking in a random walk manner. In step 4, two proxies based on a large language model are used to test and verify the migrated code. Specifically, one proxy based on a large language model is used to generate test cases for the code before and after migration, and another proxy based on a large language model is used to build a test environment and perform differential testing on the code before and after migration, verifying the correctness of the generated migrated code one by one.

2. The Python dependency library API migration method based on a large language model according to claim 1, characterized in that, In step 1, an agent based on a large language model is used to determine the user's true migration intent.

3. The Python dependency library API migration method based on a large language model according to claim 1, characterized in that, In step 2, Python documentation sources include PyPI, DuckDuckGo, and GitHub.

4. The Python dependency library API migration method based on a large language model according to claim 1, characterized in that, In step 2, when scoring and ranking the importance of migration tasks, the text matching degree between keywords and migration requirements and the semantic matching degree using cosine similarity are used for measurement.

5. The Python dependency library API migration method based on a large language model according to claim 1, characterized in that, In step 3, an agent based on a large language model is used to perform API migration; specifically, an agent is used to generate multiple sets of migrated code based on the user-input code to be migrated and migration requirements, while referring to the AST syntax tree of the code to be migrated.

6. A Python dependency library API migration system based on a large language model, used to execute the method according to any one of claims 1-5, characterized in that, It includes a user intent recognition module, a network resource discovery and acquisition module, a migration module, and a testing and verification module; among which: The user intent recognition module identifies the specific APIs in the code to be migrated that need to be migrated based on the user's input of the code to be migrated and the migration requirements. The network resource discovery and collection module discovers and collects relevant network resources based on the migration requirements input by the user, thereby obtaining the necessary knowledge for API migration. The migration module performs API migration based on the user's migration requirements and with reference to the acquired API migration task-related knowledge. The testing and verification module verifies multiple migrated code snippets generated by the migration module to determine the correctness of the migration.

Citation Information

Patent Citations

  • Code abstract generation method based on code knowledge graph and knowledge migration

    CN111797242A

  • Data migration method and device, related equipment and computer program product

    CN118503227A