-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmigrate.py
More file actions
275 lines (220 loc) · 11.4 KB
/
Copy pathmigrate.py
File metadata and controls
275 lines (220 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
"""azure-openai-to-responses — CLI entry point for Azure OpenAI Responses API migration.
One command to scan a codebase for legacy Chat Completions patterns, display
a categorised report, optionally smoke-test the target deployment, and print
a migration plan.
Usage:
# Scan a directory for legacy patterns
python migrate.py scan ./my-app
# Scan and also smoke-test the target Azure OpenAI deployment
python migrate.py scan ./my-app --smoke-test
# Search a GitHub org for repos that need migration
python migrate.py org-scan --org my-org
# Run the live Responses API test suite against your deployment
python migrate.py test
# Print the recommended migration workflow
python migrate.py plan
"""
import argparse
import importlib.util
import os
import subprocess
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SCRIPTS = ROOT / ".github" / "skills" / "azure-openai-to-responses" / "scripts"
TOOLS = ROOT / "tools"
# ---------------------------------------------------------------------------
# scan — runs detect_legacy.py
# ---------------------------------------------------------------------------
def cmd_scan(args: argparse.Namespace) -> int:
"""Scan directories for legacy Chat Completions patterns."""
scanner = SCRIPTS / "detect_legacy.py"
if not scanner.exists():
print(f"Error: scanner not found at {scanner}", file=sys.stderr)
return 1
dirs = args.directories or ["."]
rc = subprocess.run([sys.executable, str(scanner)] + dirs).returncode
if args.smoke_test:
print("\n--- Smoke-testing Azure OpenAI Responses API deployment ---\n")
rc_smoke = _run_smoke_test()
return max(rc, rc_smoke)
return rc
def _run_smoke_test() -> int:
"""Quick smoke test against the configured Azure OpenAI deployment."""
endpoint = os.environ.get("AZURE_OPENAI_ENDPOINT", "")
deployment = os.environ.get("AZURE_OPENAI_DEPLOYMENT", "")
api_key = os.environ.get("AZURE_OPENAI_API_KEY", "")
if not endpoint:
print("Error: AZURE_OPENAI_ENDPOINT not set", file=sys.stderr)
return 1
if not deployment:
print("Error: AZURE_OPENAI_DEPLOYMENT not set", file=sys.stderr)
return 1
if not api_key:
print("Warning: AZURE_OPENAI_API_KEY not set — trying EntraID auth", file=sys.stderr)
try:
from openai import OpenAI
except ImportError:
print("Error: openai package not installed. Run: pip install openai>=1.108.1", file=sys.stderr)
return 1
base_url = f"{endpoint.rstrip('/')}/openai/v1/"
if api_key:
client = OpenAI(api_key=api_key, base_url=base_url)
else:
try:
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
token_provider = get_bearer_token_provider(
DefaultAzureCredential(),
"https://cognitiveservices.azure.com/.default",
)
client = OpenAI(api_key=token_provider, base_url=base_url)
except ImportError:
print("Error: azure-identity not installed. Run: pip install azure-identity", file=sys.stderr)
return 1
try:
resp = client.responses.create(
model=deployment,
input="Say hello in one word.",
max_output_tokens=50,
store=False,
)
print(f"[PASS] Deployment '{deployment}' supports Responses API")
print(f" Model output: {resp.output_text}")
print(f" Status: {resp.status}")
return 0
except Exception as e:
print(f"[FAIL] Deployment '{deployment}' does NOT support Responses API")
print(f" Error: {e}")
return 1
# ---------------------------------------------------------------------------
# org-scan — runs find_legacy_openai_repos.py
# ---------------------------------------------------------------------------
def cmd_org_scan(args: argparse.Namespace) -> int:
"""Search a GitHub org for repos using legacy patterns."""
script = TOOLS / "find_legacy_openai_repos.py"
if not script.exists():
print(f"Error: script not found at {script}", file=sys.stderr)
return 1
cmd = [sys.executable, str(script), "--org", args.org]
if args.language:
cmd.extend(["--language", args.language])
if args.json:
cmd.append("--json")
return subprocess.run(cmd).returncode
# ---------------------------------------------------------------------------
# test — runs the live Responses API test suite
# ---------------------------------------------------------------------------
def cmd_test(args: argparse.Namespace) -> int:
"""Run the live Responses API test harness."""
test_file = TOOLS / "test_migration.py"
if not test_file.exists():
print(f"Error: test harness not found at {test_file}", file=sys.stderr)
return 1
cmd = [sys.executable, "-m", "pytest", str(test_file), "-v"]
return subprocess.run(cmd).returncode
# ---------------------------------------------------------------------------
# plan — print the migration workflow
# ---------------------------------------------------------------------------
def cmd_plan(args: argparse.Namespace) -> int:
"""Print the recommended migration workflow."""
plan = """
╔══════════════════════════════════════════════════════════════════╗
║ Azure OpenAI → Responses API Migration Plan ║
╠══════════════════════════════════════════════════════════════════╣
║ ║
║ APPROACH A: Single-repo migration ║
║ ───────────────────────────────── ║
║ 1. Scan: python migrate.py scan /path/to/your-app ║
║ 2. Migrate: @azure-openai-to-responses migrate /path/... ║
║ Or follow skills/azure-openai-to-responses/SKILL.md ║
║ 3. Verify: python migrate.py scan /path/to/your-app ║
║ cd /path/to/your-app && pytest ║
║ ║
║ APPROACH B: Bulk migration across repos ║
║ ─────────────────────────────────────── ║
║ 1. Discover: python migrate.py org-scan --org YOUR_ORG ║
║ 2. Scan: python migrate.py scan /path/to/each-repo ║
║ 3. Migrate: Use Approach A per repo, then send PRs ║
║ 4. Track: Re-run org-scan to see remaining repos ║
║ ║
║ APPROACH C: Skill-only (no agent) ║
║ ───────────────────────────────── ║
║ Feed skills/azure-openai-to-responses/SKILL.md to ║
║ any LLM (Copilot, Claude, ChatGPT) as context, then ask it ║
║ to migrate your code. ║
║ ║
╚══════════════════════════════════════════════════════════════════╝
"""
print(plan)
return 0
# ---------------------------------------------------------------------------
# models — delegates to tools/model_compat.py
# ---------------------------------------------------------------------------
def cmd_models(args: argparse.Namespace) -> int:
"""List Azure OpenAI models and their Responses API support."""
script = TOOLS / "model_compat.py"
if not script.exists():
print(f"Error: model_compat.py not found at {script}", file=sys.stderr)
return 1
cmd = [sys.executable, str(script)] + args.model_args
return subprocess.run(cmd).returncode
# ---------------------------------------------------------------------------
# bulk — delegates to tools/bulk_migrate.py
# ---------------------------------------------------------------------------
def cmd_bulk(args: argparse.Namespace) -> int:
"""Bulk migration workflow: prepare, status, send-prs across repos."""
script = TOOLS / "bulk_migrate.py"
if not script.exists():
print(f"Error: bulk_migrate.py not found at {script}", file=sys.stderr)
return 1
# Forward remaining args to the bulk script
cmd = [sys.executable, str(script)] + args.bulk_args
return subprocess.run(cmd).returncode
# ---------------------------------------------------------------------------
# Main
# ---------------------------------------------------------------------------
# Commands that delegate all args directly to a sub-script.
# We intercept these before argparse to avoid flag conflicts.
_DELEGATE_COMMANDS = {
"models": ("model_compat.py", TOOLS),
"bulk": ("bulk_migrate.py", TOOLS),
}
def main() -> int:
# Fast-path: delegated commands forward all remaining args to their script
if len(sys.argv) >= 2 and sys.argv[1] in _DELEGATE_COMMANDS:
script_name, script_dir = _DELEGATE_COMMANDS[sys.argv[1]]
script = script_dir / script_name
if not script.exists():
print(f"Error: {script_name} not found at {script}", file=sys.stderr)
return 1
return subprocess.run([sys.executable, str(script)] + sys.argv[2:]).returncode
parser = argparse.ArgumentParser(
prog="azure-openai-to-responses",
description="Azure OpenAI Chat Completions → Responses API migration toolkit",
)
sub = parser.add_subparsers(dest="command", required=True)
# scan
p_scan = sub.add_parser("scan", help="Scan directories for legacy Chat Completions patterns")
p_scan.add_argument("directories", nargs="*", help="Directories to scan (default: .)")
p_scan.add_argument("--smoke-test", action="store_true", help="Also smoke-test the Azure OpenAI deployment")
p_scan.set_defaults(func=cmd_scan)
# org-scan
p_org = sub.add_parser("org-scan", help="Search a GitHub org for repos needing migration")
p_org.add_argument("--org", required=True, help="GitHub organization name")
p_org.add_argument("--language", help="Filter by language (e.g., python)")
p_org.add_argument("--json", action="store_true", help="Output as JSON")
p_org.set_defaults(func=cmd_org_scan)
# test
p_test = sub.add_parser("test", help="Run the live Responses API test suite")
p_test.set_defaults(func=cmd_test)
# plan
p_plan = sub.add_parser("plan", help="Print the recommended migration workflow")
p_plan.set_defaults(func=cmd_plan)
# Register delegated commands for --help visibility
sub.add_parser("bulk", help="Bulk migration: prepare repos, track status, send PRs")
sub.add_parser("models", help="List Azure OpenAI models and Responses API support")
args = parser.parse_args()
return args.func(args)
if __name__ == "__main__":
sys.exit(main())