diff --git a/.gitignore b/.gitignore index 0d20b64..54bcb8f 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,8 @@ +__pycache__/ *.pyc +*.pyo +*.egg-info/ +dist/ +build/ +.env +venv/ \ No newline at end of file diff --git a/ai/__init__.py b/ai/__init__.py new file mode 100644 index 0000000..4f7432c --- /dev/null +++ b/ai/__init__.py @@ -0,0 +1 @@ +from .functions import ai_function \ No newline at end of file diff --git a/ai_functions.py b/ai/functions.py similarity index 81% rename from ai_functions.py rename to ai/functions.py index a0d53a3..5e4a1c1 100644 --- a/ai_functions.py +++ b/ai/functions.py @@ -1,7 +1,7 @@ import openai def ai_function(function, args, description, model = "gpt-4"): - # parse args to comma separated string + """Takes a function, arguments, and a description and returns the result of the function with the given arguments.""" 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}] diff --git a/requirements.txt b/requirements.txt index ff11a64..f0e766e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ openai==0.27.0 pytest==7.2.2 +python-dotenv==0.19.1 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..ddd3474 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ +from .test_ai_function import test_1, test_2, test_3, test_4 \ No newline at end of file diff --git a/test_ai_function.py b/tests/test_ai_function.py similarity index 81% rename from test_ai_function.py rename to tests/test_ai_function.py index bb2e19d..a056de8 100644 --- a/test_ai_function.py +++ b/tests/test_ai_function.py @@ -1,16 +1,19 @@ import ast import json import time -import ai_functions +import ai.functions as functions import pytest import openai -import keys +from dotenv import load_dotenv +from os import environ +load_dotenv() # Initialize the OpenAI API client -openai.api_key = keys.OPENAI_API_KEY +openai.api_key = environ.get("OPENAI_API_KEY") # Run all tests, print the results, and return the number of failed tests def run_tests(model): + """Runs all tests and prints the results. Returns the number of failed tests.""" test_functions = [test_1, test_2, test_3, test_4, test_5, test_6] test_names = [ "Generate fake people", @@ -43,12 +46,13 @@ def run_tests(model): # Ai function test 1 def test_1(model): + """Generates n examples of fake data representing people, each with a name and an age.""" function_string = "def fake_people(n: int) -> list[dict]:" args = ["4"] description_string = """Generates n examples of fake data representing people, each with a name and an age.""" - result_string = ai_functions.ai_function(function_string, args, description_string, model) + result_string = functions.ai_function(function_string, args, description_string, model) print(f"Output: {result_string}") # Assert the result can be parsed as is a list of dictionaries @@ -72,11 +76,12 @@ def test_1(model): # Ai function test 2 def test_2(model): + """Generates a random password of given length with or without special characters.""" function_string = "def random_password_generator(length: int, special_chars: bool) -> str:" args = ["12", "True"] description_string = """Generates a random password of given length with or without special characters.""" - result_string = ai_functions.ai_function(function_string, args, description_string, model) + result_string = functions.ai_function(function_string, args, description_string, model) print(f"Output: {result_string}") @@ -86,11 +91,12 @@ def test_2(model): # Ai function test 3 def test_3(model): + """Calculates the area of a triangle given its base and height.""" function_string = "def calculate_area_of_triangle(base: float, height: float) -> float:" args = ["15", "6.5"] description_string = """Calculates the area of a triangle given its base and height.""" - result_string = ai_functions.ai_function(function_string, args, description_string, model) + result_string = functions.ai_function(function_string, args, description_string, model) print(f"Output: {result_string}") # Assert the result can be parsed as a float @@ -108,11 +114,12 @@ def test_3(model): # Ai function test 4 def test_4(model): + """Finds and returns the nth prime number.""" function_string = "def get_nth_prime_number(n: int) -> int:" args = ["10"] description_string = """Finds and returns the nth prime number.""" - result_string = ai_functions.ai_function(function_string, args, description_string, model) + result_string = functions.ai_function(function_string, args, description_string, model) print(f"Output: {result_string}") @@ -131,11 +138,12 @@ def test_4(model): # Ai function test 5 def test_5(model): + """Encrypts the given text using a simple character substitution based on the provided key.""" function_string = "def encrypt_text(text: str, key: str) -> str:" args = ["'Hello, World!'", "'abc123'"] description_string = """Encrypts the given text using a simple character substitution based on the provided key.""" - result_string = ai_functions.ai_function(function_string, args, description_string, model) + result_string = functions.ai_function(function_string, args, description_string, model) print(f"Output: {result_string}") @@ -145,11 +153,12 @@ def test_5(model): # Ai function test 6 def test_6(model): + """Finds and returns a list of missing numbers in a given sorted list.""" function_string = "def find_missing_numbers_in_list(numbers: list[int]) -> list[int]:" args = ["[3, 5, 8, 15, 16]"] description_string = """Finds and returns a list of missing numbers in a given sorted list.""" - result_string = ai_functions.ai_function(function_string, args, description_string, model) + result_string = functions.ai_function(function_string, args, description_string, model) print(f"Output: {result_string}")