Skip to content
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

Modularizes Python Files and Introduce .env File for Improved Security #14

Open
wants to merge 6 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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
__pycache__/
*.pyc
*.pyo
*.egg-info/
dist/
build/
.env
venv/
1 change: 1 addition & 0 deletions ai/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .functions import ai_function
2 changes: 1 addition & 1 deletion ai_functions.py → ai/functions.py
Original file line number Diff line number Diff line change
@@ -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}]

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
openai==0.27.0
pytest==7.2.2
python-dotenv==0.19.1
1 change: 1 addition & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .test_ai_function import test_1, test_2, test_3, test_4
27 changes: 18 additions & 9 deletions test_ai_function.py → tests/test_ai_function.py
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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
Expand All @@ -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}")

Expand All @@ -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
Expand All @@ -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}")

Expand All @@ -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}")

Expand All @@ -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}")

Expand Down