Spaces:
Running
Running
File size: 3,598 Bytes
26bead7 cc7a977 26bead7 cc7a977 26bead7 cc7a977 26bead7 cc7a977 26bead7 cc7a977 26bead7 86b520c 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 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 | """Prompts for the AI Python C Extensions Generator application."""
# Define system message with instructions for generating the C extension code.
system_message = """
You are an assistant that reimplements Python code in high performance C extensions
for Python.
Your responses must always be a JSON with the following schema:
{schema}
Use comments sparingly and do not provide any explanation other than occasional comments.
The C extension for Python needs to produce identical output in the fastest possible
time.
Make sure the C extension for Python code is correct and can be compiled with
'python setup.py build_ext' and used in Python.
The usage example must include a time measurement and a comparison with the original
Python code, absolute and proportional difference.
Do not include an `if __name__ == '__main__'` guard in the usage script.
The usage script must be importable, and the usage example must invoke its functions
directly.
Do not include any additional text or explanation outside the JSON structure.
Make sure the JSON is correctly formatted.
"""
# Define user prompt template and function to fill it.
user_prompt = """
Reimplement this Python code as a C extension for Python with the fastest possible
implementation that produces identical output in the least time.
Respond only with C extension for Python code, do not explain your work other than
a few code comments.
The module name, used to import, must be "{module_name}", the generated C file will
be named "{module_name}.c".
The generated C extension module will be located in the "{compile_path}" folder.
Only the usage example file should import the module from that folder:
from {compile_path} import {module_name}
Do not use "{compile_path}" inside the generated C source or in the generated setup.py
file. The generated C source and setup.py must behave as if they are in the current
folder and must not reference any external path.
Do not include an `if __name__ == '__main__'` guard in the generated module or the
usage example. The usage script must be importable, and the usage example must invoke
its functions directly.
Pay attention to number types to ensure no int overflows.
Remember to #include all necessary C packages such as iomanip or <python.h>
The target architecture is {platform}, take that in mind while generating the C
code, specially when choosing types to use, and use the appropriate compiler flags.
Make sure to use the Python C API correctly and manage memory properly to avoid
leaks or crashes.
Here is the Python code to reimplement:
{python_code}
"""
# Define a footer disclaimer to be included in the interface.
footer_disclaimer = """
<div style='text-align: center; font-size: small;'>
This chatbot is powered by generative AI technology.<br>
While it strives to provide accurate information, it may occasionally make mistakes.
Additionally, conversations and source codes may be used to improve AI models.
</div>
"""
# Define function to create the messages for the LLM.
def messages_for(python_code, module_name, schema, platform, compile_path):
"""Create the messages given the Python code, module name, and platform."""
return [
{"role": "system", "content": system_message.format(schema=schema)},
{"role": "user", "content": user_prompt.format(python_code=python_code,
module_name=module_name,
compile_path=compile_path,
platform=platform)}]
|