1. ํ์ ์ฌ์ ์ค์น ์๊ฑด & ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ฑ (Prerequisites)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Windows 10/11, macOS 12+, Linux (Ubuntu, Debian ๋ฑ) | Ubuntu 22.04 LTS / macOS Sonoma / Windows 11 | ํฌ๋ก์ค ํ๋ซํผ Python/Node.js ์คํฌ๋ฆฝํธ ์คํ ํ๊ฒฝ |
| CPU | 2 ์ฝ์ด ์ด์ | 4 ์ฝ์ด ์ด์ | ๋ก์ปฌ AST ํ์ฑ, ๋ฆฐํฐ ๊ตฌ๋ ๋ฐ ๋น๋๊ธฐ API ์์ฒญ ์ฒ๋ฆฌ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | 4 GB ์ด์ | 8 GB ~ 16 GB ์ด์ | ๋๊ท๋ชจ ์์ค ์ฝ๋ ๋ฒํผ๋ง ๋ฐ ๋ฐ์ดํฐ ์ฒ๋ฆฌ |
| ๋์คํฌ (Storage) | 1 GB ์ด์ | 5 GB ์ด์ | ๊ฐ์ํ๊ฒฝ(venv) ๋ฐ ํจํค์ง ์บ์ ์ ์ฅ ๊ณต๊ฐ |
| ๋ฐํ์ ๋ฐ ์ธ์ด | Python 3.10+ ๋๋ Node.js 18+ | Python 3.11+ / 3.12+ (uv ๊ถ์ฅ) | ์ต์ openai ๊ณต์ SDK ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ตฌ๋ |
| ๋คํธ์ํฌ & ์ธ์ฆ | OpenAI API Key (OPENAI_API_KEY) | OpenAI Platform ๊ณ์ (Tier 2+ ๊ถ์ฅ) | HTTPS (ํฌํธ 443) api.openai.com ํต์ ํ์ |
2. ํต์ฌ ๋ฉํ ๋ชจ๋ธ: ์ฝ๋ ์์ฑ ๋ฐ ์คํ ํผ๋๋ฐฑ ๋ฃจํ (Mental Model & Architecture)
OpenAI Codex ๊ณ์ด ๋ชจ๋ธ์ ๋จ์ ํ ์คํธ ์์ฑ์ ๋์ด, ์ฝ๋ ํ์ฑ โ AST ๋ถ์ โ ์ฝ๋ ์์ฑ โ ์๋๋ฐ์ค ์คํ(Code Interpreter) โ ์๊ฐ ๊ต์ (Self-Correction) ๋ฃจํ๋ฅผ ์์ฑํฉ๋๋ค.
+-----------------------------------------------------------------------------------+
| Developer Prompt / Code Context |
| "JSON ๋ฐ์ดํฐ๋ฅผ ํ์ฑํ๊ณ ๋ ์ง๋ณ๋ก ์ง๊ณํ๋ Python ํจ์ ์์ฑ" |
+-----------------------------------------------------------------------------------+
โ
โผ
+-----------------------------------------------------------------------------------+
| Context & Prompt Engine (AST / Types) |
| - System Prompt (์ฝ๋ฉ ์คํ์ผ, ํ๋ ์์ํฌ ์ ์ฝ, Type Hint ๊ท์น) |
| - Few-shot Examples ๋ฐ ๊ธฐ์กด ์ธํฐํ์ด์ค ์ ์ ์ฃผ์
|
+-----------------------------------------------------------------------------------+
โ
โผ
+-----------------------------------------------------------------------------------+
| OpenAI Reasoning & Codex Model (GPT-4o / o3) |
| - ์ฝ๋ ์์ฑ (Zero-shot / Chain-of-Thought ์ถ๋ก ) |
| - Structured Outputs (Pydantic / JSON Schema ์๊ฒฉ ํ์
๊ฒ์ฆ) |
+-----------------------------------------------------------------------------------+
โ
Tool Calling / Code Interpreter
โผ
+-----------------------------------------------------------------------------------+
| Execution Sandbox & Evaluator |
| - ์ฝ๋ ์คํ ๊ฒฐ๊ณผ (Standard Output, Execution Error, Traceback) |
| - ์ ๋ ํ
์คํธ ๋จ์ธ๋ฌธ(Assertion) ํ์ |
+-----------------------------------------------------------------------------------+
โ
(์ค๋ฅ ๋ฐ์ ์ ์๋ ํผ๋๋ฐฑ ๋ฃจํ) โโโโ
โ ํต๊ณผ โ
โผ โผ
+-----------------------------------------------------------------------------------+
| Production-Ready Code Output |
| - ํ์
์์ ํ ํด๋ฆฐ ์ฝ๋ + ํ๊ตญ์ด ์ฃผ์ + ํ
์คํธ ์ผ์ด์ค ์ผ์ฒด |
+-----------------------------------------------------------------------------------+
3. ๋จ๊ณ๋ณ ์ค์ต ๊ฐ์ด๋ (Step-by-Step Walkthrough)
[์ด๋ณด์ 1๋จ๊ณ] OpenAI SDK ์ค์น ๋ฐ ์ต์ ๋์ ์ฝ๋ ์์ฑ
Python ๊ฐ์ํ๊ฒฝ์ ์์ฑํ๊ณ ๊ณต์ openai SDK๋ฅผ ํตํด ์์ฐ์ด ์ง์๋ฌธ์ ์คํ ๊ฐ๋ฅํ Python ํจ์๋ก ๋ณํํฉ๋๋ค.
1) ๊ฐ์ํ๊ฒฝ ๊ตฌ์ฑ ๋ฐ ํจํค์ง ์ค์น
# ๊ฐ์ํ๊ฒฝ ์์ฑ ๋ฐ ํ์ฑํ
python -m venv .venv
# Windows: .venv\Scripts\activate
# Linux/macOS: source .venv/bin/activate
# ์ต์ OpenAI SDK ๋ฐ Pydantic ์ค์น
pip install openai pydantic
2) ์ต์ ๋์ ์์ : ์์ฐ์ด๋ฅผ ํตํ ์์ ํ ๋ฐ์ดํฐ ๋ณํ ํจ์ ์์ฑ
# codex_hello.py
import os
from openai import OpenAI
# OpenAI ํด๋ผ์ด์ธํธ ์ด๊ธฐํ (ํ๊ฒฝ ๋ณ์ OPENAI_API_KEY ์ฌ์ฉ)
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
prompt = """
์๊ตฌ์ฌํญ:
1. ์
๋ ฅ: ISO 8601 ํ์์ ๋ ์ง ๋ฌธ์์ด ๋ฆฌ์คํธ (์: ['2026-10-11T15:00:00Z', 'invalid-date', '2026-10-12T09:00:00Z'])
2. ์ ํจํ์ง ์์ ๋ ์ง๋ ๊ฑด๋๋ฐ๊ณ ์ ์ ๋ ์ง๋ง 'YYYY-MM-DD' ํฌ๋งท์ผ๋ก ๋ณํํ์ฌ ์ ๋ ฌ๋ ๊ณ ์ ๊ฐ ๋ฆฌ์คํธ๋ฅผ ๋ฐํํ๋ ํจ์ ์์ฑ.
3. ์์ ํ Python ์ฝ๋(ํจ์๋ช
: clean_and_sort_dates)์ ์น์ ํ ์ฃผ์์ ํฌํจํ ๊ฒ.
"""
response = client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "system", "content": "๋น์ ์ ์ต๊ณ ์์ค์ Python ์ํํธ์จ์ด ์์ง๋์ด์
๋๋ค. ๊ฐ๊ฒฐํ๊ณ ์์ ํ ์ฝ๋๋ฅผ ์์ฑํ์ธ์."},
{"role": "user", "content": prompt}
],
temperature=0.1 # ๊ฒฐ์ ๋ก ์ ์ด๊ณ ์์ ์ ์ธ ์ฝ๋ ์์ฑ์ ์ํด ๋ฎ์ ์จ๋ ์ค์
)
generated_code = response.choices[0].message.content
print("=== ์์ฑ๋ ์ฝ๋ ===")
print(generated_code)
์์ ํฐ๋ฏธ๋ ์ถ๋ ฅ(Output):
=== ์์ฑ๋ ์ฝ๋ ===
from datetime import datetime
from typing import List
def clean_and_sort_dates(date_strings: List[str]) -> List[str]:
"""
ISO 8601 ๋ฌธ์์ด ๋ฆฌ์คํธ๋ฅผ ํ์ฑํ์ฌ ์ ํจํ ๋ ์ง๋ง 'YYYY-MM-DD' ํํ๋ก ์ ๋ ฌํ์ฌ ๋ฐํํฉ๋๋ค.
"""
valid_dates = set()
for ds in date_strings:
try:
# ISO 8601 ํ์ ํ์ฑ
dt = datetime.fromisoformat(ds.replace("Z", "+00:00"))
valid_dates.add(dt.strftime("%Y-%m-%d"))
except (ValueError, TypeError):
# ํ์ฑ ์คํจ ์ ๊ฑด๋๋
continue
return sorted(list(valid_dates))
[2๋จ๊ณ] ํต์ฌ ๊ธฐ๋ฅ: Pydantic ๊ธฐ๋ฐ ๊ตฌ์กฐํ๋ ์ถ๋ ฅ (Structured Code & AST)
LLM์ ์๋ต์ ๋งํฌ๋ค์ด ๋ฌธ์์ด์ด ์๋, ํ๋ก๊ทธ๋จ์์ ์ฆ์ ์๋น ๊ฐ๋ฅํ ์๊ฒฉํ JSON/Pydantic ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ก ์ถ์ถํฉ๋๋ค.
# codex_structured.py
import os
from openai import OpenAI
from pydantic import BaseModel, Field
from typing import List
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
# 1. ๋ฐํ๋ฐ์ ์ฝ๋ ๊ตฌ์กฐ ์คํค๋ง ์ ์
class CodeRefactorResult(BaseModel):
function_name: str = Field(description="๋ฆฌํฉํ ๋ง๋ ๋์ ํจ์ ์ด๋ฆ")
time_complexity: str = Field(description="์๊ฐ ๋ณต์ก๋ ๋ถ์ (์: O(N))")
space_complexity: str = Field(description="๊ณต๊ฐ ๋ณต์ก๋ ๋ถ์ (์: O(1))")
refactored_code: str = Field(description="๋ฆฌํฉํ ๋ง ๋ฐ ํ์
ํํธ๊ฐ ์ ์ฉ๋ ์์ ํ ์ฝ๋")
unit_tests: List[str] = Field(description="pytest ๊ธฐ๋ฐ ์ ๋ ํ
์คํธ ํจ์ ๋ชฉ๋ก")
legacy_code = """
def find_dup(arr):
res = []
for i in range(len(arr)):
for j in range(i+1, len(arr)):
if arr[i] == arr[j] and arr[i] not in res:
res.append(arr[i])
return res
"""
# 2. Structured Outputs ํธ์ถ (Strict ๋ชจ๋)
completion = client.beta.chat.completions.parse(
model="gpt-4o",
messages=[
{"role": "system", "content": "๋นํจ์จ์ ์ธ ์ฝ๋๋ฅผ O(N)์ผ๋ก ๋ฆฌํฉํ ๋งํ๊ณ pytest ํ
์คํธ๋ฅผ ์์ฑํ์ธ์."},
{"role": "user", "content": f"๋ค์ ๋ ๊ฑฐ์ ์ฝ๋๋ฅผ ์ต์ ํํด์ค:\n{legacy_code}"}
],
response_format=CodeRefactorResult,
temperature=0.0
)
result: CodeRefactorResult = completion.choices[0].message.parsed
print(f"ํจ์๋ช
: {result.function_name}")
print(f"์๊ฐ ๋ณต์ก๋: {result.time_complexity} / ๊ณต๊ฐ ๋ณต์ก๋: {result.space_complexity}")
print("\n[์ต์ ํ ์ฝ๋]")
print(result.refactored_code)
print(f"\n์์ฑ๋ ์ ๋ ํ
์คํธ ์: {len(result.unit_tests)}๊ฐ")
[3๋จ๊ณ] ์ค๋ฌด ์์ฉ: ์๊ฐ ๊ต์ ์คํ ๋ฃจํ (Self-Correction Execution Loop)
์์ฑ๋ ์ฝ๋๋ฅผ ๋ก์ปฌ ๊ฐ์ ์๋๋ฐ์ค์์ ์คํํด๋ณด๊ณ , ์ค๋ฅ ๋ฐ์ ์ ํธ๋ ์ด์ค๋ฐฑ(Traceback)์ ๋ชจ๋ธ์ ์ฌ์ฃผ์ ํ์ฌ ์ฑ๊ณตํ ๋๊น์ง ์๋ ์์ ํ๋ ์์ ์๋ํ ํจํด์ ๋๋ค.
# codex_self_correction.py
import os
import sys
import io
import traceback
from openai import OpenAI
client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
def execute_python_code(code_str: str) -> tuple[bool, str]:
"""์ฝ๋๋ฅผ ๋ฌธ์์ด ํํ๋ก ์คํํ๊ณ ํ์ค ์ถ๋ ฅ ๋๋ ์ค๋ฅ ์คํํธ๋ ์ด์ค๋ฅผ ๋ฐํํฉ๋๋ค."""
stdout_capture = io.StringIO()
old_stdout = sys.stdout
sys.stdout = stdout_capture
try:
# ์์ ํ ๋ก์ปฌ ๋ค์์คํ์ด์ค ๋ด์์ ์คํ
local_scope = {}
exec(code_str, {}, local_scope)
sys.stdout = old_stdout
return True, stdout_capture.getvalue()
except Exception as e:
sys.stdout = old_stdout
return False, traceback.format_exc()
def generate_verified_solution(task_description: str, max_retries: int = 3) -> str:
messages = [
{"role": "system", "content": "์คํ ๊ฐ๋ฅํ Python ์ฝ๋๋ง ๋งํฌ๋ค์ด ๋ธ๋ก(```python ... ```) ์์ด ์์ ์ฝ๋๋ก ์์ฑํ์ธ์."},
{"role": "user", "content": task_description}
]
for attempt in range(1, max_retries + 1):
response = client.chat.completions.create(
model="gpt-4o",
messages=messages,
temperature=0.1
)
current_code = response.choices[0].message.content.strip()
# ๋งํฌ๋ค์ด ๋ํผ ์ ๊ฑฐ ์ฒ๋ฆฌ
if current_code.startswith("```python"):
current_code = current_code.replace("```python", "").replace("```", "").strip()
print(f"[{attempt}ํ์ฐจ ์๋] ์ฝ๋ ์คํ ๊ฒ์ฆ ์ค...")
success, output = execute_python_code(current_code)
if success:
print("โ ์ฝ๋ ๊ฒ์ฆ ์ฑ๊ณต! ํ
์คํธ ํต๊ณผ.")
return current_code
else:
print(f"โ ์คํ ์ค๋ฅ ๋ฐ์:\n{output[:180]}...")
# ์๋ฌ ํผ๋๋ฐฑ์ ์ ๋ฌํ์ฌ ์๊ฐ ์์ ์ ๋
messages.append({"role": "assistant", "content": current_code})
messages.append({"role": "user", "content": f"์คํ ์ค ๋ค์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ์์ ํด์ฃผ์ธ์:\n{output}"})
raise RuntimeError("์ต๋ ์ฌ์๋ ํ์ ์ด๊ณผ: ์ ํจํ ์ฝ๋๋ฅผ ์์ฑํ์ง ๋ชปํ์ต๋๋ค.")
# ์คํ ๊ฒ์ฆ ํ
์คํธ
test_task = "sys ๋ชจ๋์ ํ์ฉํ์ฌ ํ์ฌ Python ๋ฒ์ ์ major, minor ๋ฒ์ ์ ์ถ๋ ฅํ๊ณ assert 3 <= sys.version_info.major ๋ฌธ์ ์คํํ๋ ์ฝ๋"
verified_code = generate_verified_solution(test_task)
print("\n[์ต์ข
๊ฒ์ฆ ์๋ฃ๋ ์ฝ๋]:\n" + verified_code)
4. ์์ฐ์ฑ ์ต์ ํ & ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ
| ์ ๊ฒ ํญ๋ชฉ | ๊ถ์ฅ ์ค์ ๋ฐ ํจํด | ์ฃผ์์ฌํญ |
|---|---|---|
| Temperature ์ค์ | ์ฝ๋ ์์ฑ ์ 0.0 ~ 0.2๋ก ๊ณ ์ | ์จ๋๊ฐ ๋์ ๊ฒฝ์ฐ ์กด์ฌํ์ง ์๋ ๊ฐ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ(Hallucinated Package) ์ํฌํธ ์ํ ์ฆ๊ฐ |
| ํ์ ์์ ์ฑ ๋ณด์ฅ | response_format=PydanticModel ์ฌ์ฉ | ์์ ๋ฌธ์์ด ํ์ฑ๋ณด๋ค JSON ํ์ฑ ์ค๋ฅ๊ฐ 100% ๋ฐฉ์ง๋จ |
| ์ฝ๋ ์คํ ๊ฒฉ๋ฆฌ (Sandbox) | ๋ก์ปฌ exec() ๋์ Docker ์ปจํ
์ด๋ ๋๋ gVisor ๊ฒฉ๋ฆฌ ํ๊ฒฝ ์ ์ฉ | ์ ๋ขฐํ ์ ์๋ ํ๋กฌํํธ ์ ๋ ฅ ์ ์์คํ ํ์ผ ์์ ์ํ ์์ฒ ์ฐจ๋จ |
| Reasoning Model ํ์ฉ | ๋ณต์กํ ์๊ณ ๋ฆฌ์ฆ์ด๋ ๋์์ฑ ์ฝ๋๋ o1 / o3-mini ๋ชจ๋ธ ์ฑํ | ์ถ๋ก ํ ํฐ(Thinking Tokens)์ ๋ฐ๋ฅธ ์ถ๊ฐ ์ง์ฐ ์๊ฐ ๋ฐ ๊ณผ๊ธ ๊ตฌ์กฐ ๊ณ ๋ ค ํ์ |
| ์ปจํ ์คํธ ํ ํฐ ์ ์ฝ | ์์ค ํ์ผ ์ ์ฒด ๋์ AST ๊ธฐ๋ฐ ๊ด๋ จ ์๊ทธ๋์ฒ๋ง ๋ฐ์ทํ์ฌ ํ๋กฌํํธ ๊ตฌ์ฑ | ๋น์ฉ ์ ๊ฐ ๋ฐ ๋ถํ์ํ ์ฝ๋ ํจํด์ ์ค์ผ ๋ฐฉ์ง |
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์