Example

This section demonstrates how to run the main functionality of the project using a simple example. The example is designed to illustrate the core features and provide a starting point for users.

Example Code

The following example shows the core logic used in this project. Suppose you want to migrate aiohttp.ClientSession.get to httpx.AsyncClient.get. The code snippet below demonstrates how to achieve this.

  1from pathlib import Path
  2import ast
  3
  4from pig.synth.sketch import migrator, preparation
  5from pig.llm.query import ask_llm
  6from pig.mapping.compare_arg import api_mapping
  7from pig.mapping.api_lst import get_apis
  8from pig.synth.cens import CENs
  9
 10
 11# ---------------------------------------------------------------------
 12# Configuration
 13# ---------------------------------------------------------------------
 14
 15OLD_LIB = "aiohttp"
 16NEW_LIB = "httpx"
 17TARGET_API = "get"
 18
 19# Original code to migrate
 20ORIGINAL_CODE = """
 21import aiohttp
 22
 23async def fetch(url):
 24    async with aiohttp.ClientSession() as session:
 25        async with session.get(url) as response:
 26            return await response.text()
 27"""
 28
 29# Arguments of the original API
 30ORIGINAL_ARGS = ["url", "allow_redirects", "kwargs"]
 31
 32# Path to the new library source
 33NEW_LIB_PATH = Path("path/to/httpx")
 34
 35# LLM configuration
 36MODEL = ""  # e.g., "gpt-4"
 37ENGINE = ""  # "openai" or "ollama"
 38TEMPERATURE = 1
 39
 40
 41# ---------------------------------------------------------------------
 42# Step 1: Find candidate APIs
 43# ---------------------------------------------------------------------
 44
 45candidates = api_mapping(
 46    apios=[TARGET_API],
 47    libo=OLD_LIB,
 48    libn=NEW_LIB,
 49    libn_path=NEW_LIB_PATH,
 50    argso=ORIGINAL_ARGS,
 51)
 52
 53apins = [(name, spec) for name, spec, *_ in candidates]
 54
 55
 56# ---------------------------------------------------------------------
 57# Step 2: Ask LLM to generate a migrated snippet
 58# ---------------------------------------------------------------------
 59
 60migrated_code = ask_llm(
 61    libo=OLD_LIB,
 62    libn=NEW_LIB,
 63    apio=TARGET_API,
 64    apins=apins,
 65    codeb=ORIGINAL_CODE,
 66    model=MODEL,
 67    engine=ENGINE,
 68    temperature=TEMPERATURE,
 69)
 70
 71
 72# ---------------------------------------------------------------------
 73# Step 3: Prepare AST information
 74# ---------------------------------------------------------------------
 75
 76(
 77    OldTree,
 78    ParentO,
 79    OCNs,
 80    UnusedVarsO,
 81    UnAssignedVarsO,
 82    FuncDefsO,
 83) = preparation(ORIGINAL_CODE, [TARGET_API], OLD_LIB, NEW_LIB)
 84
 85(NewTree, ParentN, _, _, _, _) = preparation(migrated_code, [], OLD_LIB, NEW_LIB)
 86
 87OldTree1 = ast.parse(ORIGINAL_CODE)
 88
 89history = {}
 90
 91
 92# ---------------------------------------------------------------------
 93# Step 4: Run migration synthesis
 94# ---------------------------------------------------------------------
 95
 96migrated_tree = migrator(
 97    TARGET_API,
 98    OCNs,
 99    ParentN,
100    ParentO,
101    ast.parse(ORIGINAL_CODE),
102    NewTree,
103    OLD_LIB,
104    NEW_LIB,
105    history,
106    FuncDefsO,
107    None,
108    CENs,
109    OldTree1,
110    ORIGINAL_CODE,
111    migrated_code,
112    get_apis("path", NEW_LIB),
113)
114
115
116# ---------------------------------------------------------------------
117# Output result
118# ---------------------------------------------------------------------
119
120print(ast.unparse(migrated_tree))

Notes

Note

Ensure that all dependencies listed in requirements.txt are installed before running the example.

Warning

Running the example with a different Python version may produce slightly different results.