2023-04-01 09:34:32 +00:00
|
|
|
from typing import List, Optional
|
|
|
|
import json
|
|
|
|
import openai
|
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
|
|
|
|
def call_ai_function(function, args, description, model="gpt-4"):
|
2023-04-01 09:34:32 +00:00
|
|
|
# parse args to comma seperated string
|
|
|
|
args = ", ".join(args)
|
2023-04-02 08:13:15 +00:00
|
|
|
messages = [{"role": "system",
|
|
|
|
"content": f"You are now the following python function: ```# {description}\n{function}```\n\nOnly respond with your `return` value."},
|
|
|
|
{"role": "user",
|
|
|
|
"content": args}]
|
2023-04-01 09:34:32 +00:00
|
|
|
|
|
|
|
response = openai.ChatCompletion.create(
|
|
|
|
model=model,
|
|
|
|
messages=messages,
|
|
|
|
temperature=0
|
|
|
|
)
|
|
|
|
|
|
|
|
return response.choices[0].message["content"]
|
2023-04-01 09:35:32 +00:00
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
# Evaluating code
|
|
|
|
|
2023-04-01 09:35:32 +00:00
|
|
|
|
|
|
|
def evaluate_code(code: str) -> List[str]:
|
|
|
|
function_string = "def analyze_code(code: str) -> List[str]:"
|
|
|
|
args = [code]
|
|
|
|
description_string = """Analyzes the given code and returns a list of suggestions for improvements."""
|
|
|
|
|
|
|
|
result_string = call_ai_function(function_string, args, description_string)
|
|
|
|
return json.loads(result_string)
|
|
|
|
|
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
# Improving code
|
2023-04-01 09:35:32 +00:00
|
|
|
|
|
|
|
def improve_code(suggestions: List[str], code: str) -> str:
|
|
|
|
function_string = "def generate_improved_code(suggestions: List[str], code: str) -> str:"
|
|
|
|
args = [json.dumps(suggestions), code]
|
|
|
|
description_string = """Improves the provided code based on the suggestions provided, making no other changes."""
|
|
|
|
|
|
|
|
result_string = call_ai_function(function_string, args, description_string)
|
|
|
|
return result_string
|
|
|
|
|
|
|
|
|
2023-04-02 08:13:15 +00:00
|
|
|
# Writing tests
|
2023-04-01 09:35:32 +00:00
|
|
|
|
2023-04-01 15:01:36 +00:00
|
|
|
def write_tests(code: str, focus: List[str]) -> str:
|
2023-04-01 09:35:32 +00:00
|
|
|
function_string = "def create_test_cases(code: str, focus: Optional[str] = None) -> str:"
|
2023-04-01 15:01:36 +00:00
|
|
|
args = [code, json.dumps(focus)]
|
2023-04-01 09:35:32 +00:00
|
|
|
description_string = """Generates test cases for the existing code, focusing on specific areas if required."""
|
|
|
|
|
|
|
|
result_string = call_ai_function(function_string, args, description_string)
|
2023-04-02 08:13:15 +00:00
|
|
|
return result_string
|