File size: 2,434 Bytes
26bead7
 
 
 
 
 
cc7a977
26bead7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc7a977
 
 
 
 
 
 
 
 
 
 
 
26bead7
 
 
 
 
 
 
 
 
 
 
cc7a977
26bead7
 
 
cc7a977
 
26bead7
 
cc7a977
 
26bead7
 
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
"""Optimizer module for the AI Python C Extensions Generator application."""

from logging import getLogger

from dotenv import load_dotenv
from openai import OpenAI
from pydantic import BaseModel, Field

from .prompts import messages_for


_logger = getLogger(__name__)


# Environment initialization.
load_dotenv(override=True)


# Initialize client and set the default LLM model to use.
openai = OpenAI()
OPENAI_MODEL = "gpt-5"
_logger.info(f'INITIALIZED OPTIMIZER MODULE')


# Define Pydantic model class for GPT response parsing.
class _extension_codes(BaseModel):
    c_code: str = Field(...,
                        description="Generated C extension source "
                                    "code for the Python module.")
    setup: str = Field(...,
                       description="The generated setup.py build script "
                                   "used to compile the C extension module."
                                   "It must assume the module is in the current folder "
                                   "and not in any subfolder.")
    usage: str = Field(...,
                       description="A Python usage example that imports the compiled "
                                   "extension and compares its execution time with the "
                                   "original Python implementation.")

    def __str__(self):
        """Return a string representation of the optimization codes."""
        return (f"C CODE:\n{self.c_code}\n"
                f"---------------------------\n"
                f"setup.py:\n{self.setup}\n"
                f"---------------------------\n"
                f"USAGE:\n{self.usage}")


# Define optimization function using OpenAI's GPT model.
def optimize_gpt(python_code, module_name, platform, compile_path, model=OPENAI_MODEL):
    """Generate an optimized C extension for Python."""
    schema = _extension_codes.model_json_schema()
    _logger.info('SENDING OPTIMIZATION REQUEST TO OPENAI... '
                 f'(MODEL: {model}, PLATFORM: {platform}, '
                 f'MODULE: {module_name}, COMPILE PATH: {compile_path})')
    response = openai.responses.parse(
        model=model, text_format=_extension_codes,
        input=messages_for(python_code, module_name, schema, platform, compile_path)
        ).output_parsed
    _logger.info('RECEIVED OPTIMIZATION RESPONSE FROM OPENAI')
    return response.c_code, response.setup, response.usage