Skip to content

Implemented a AI function to enhance the time and space efficiency of… #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions ai_functions.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import openai

def ai_function(function, args, description, model = "gpt-4"):

def ai_function(function, args, description, model="gpt-4"):
# parse args to comma separated string
args = ", ".join(args)
messages = [{"role": "system", "content": f"You are now the following python function: ```# {description}\n{function}```\n\nOnly respond with your `return` value. Do not include any other explanatory text in your response."},{"role": "user", "content": args}]
messages = [{"role": "system", "content": f"You are now the following python function: ```# {description}\n{function}```\n\nOnly respond with your `return` value. Do not include any other explanatory text in your response."}, {"role": "user", "content": args}]

response = openai.ChatCompletion.create(
model=model,
Expand All @@ -12,3 +13,36 @@ def ai_function(function, args, description, model = "gpt-4"):
)

return response.choices[0].message["content"]


def optimize_function(function, args, model="gpt-4"):
# Generate a prompt to optimize the function
prompt = f"Optimize the following Python function:\n\n{function.__name__}({', '.join(args)})\n\nThe function's purpose is to {function.__doc__}\n\noptimize the function's time and space complexity.\n\nDo not include any other explanatory text in your response."
messages = [
{"role": "system",
"content": "You are a code optimizer and refactor. Just give only output not extra text with that."},
{"role": "user", "content": prompt}
]

response = openai.ChatCompletion.create(
model=model,
messages=messages,
n=1,
stop=None,
temperature=0.5,
)

# Extract the suggested optimizations from the GPT response
optimized_code = response.choices[0]['message']['content']
print(
f"Here is the optimized function in terms of time and space complexity:\n{optimized_code}")

# Compile the optimized function code into a function object
optimized_function = None
exec(optimized_code, globals(), locals())
for var in locals():
if var != '__builtins__':
optimized_function = locals()[var]
break

return optimized_function
28 changes: 28 additions & 0 deletions test_code_optimizations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from ai_functions import optimize_function
import openai
import keys

# Initialize the OpenAI API client
openai.api_key = keys.OPENAI_API_KEY


def test_optimized_function_returns_correct_result(model):
def calculate_square(x):
"""
This function returns the square of its input.
"""
n = x
res = 0
while n > 0:
res += x
n -= 1
return res

optimized_function = optimize_function(calculate_square, ["x"], model)
assert optimized_function(2) == 4
assert optimized_function(3) == 9
assert optimized_function(4) == 16


# test_optimized_function_returns_correct_result("gpt-3.5-turbo")
test_optimized_function_returns_correct_result("gpt-4")