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

documentation #281

Merged
merged 4 commits into from
Jan 28, 2024
Merged
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
Binary file modified .coverage
Binary file not shown.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@

A [React](https://react.dev/) + [AWS Serverless](https://aws.amazon.com/serverless/) full stack implementation of the [30 example applications](https://platform.openai.com/examples) found in the official OpenAI API documentation. This repository is used as an instructional tool for the YouTube channel "[Full Stack With Lawrence](https://youtube.com/@FullStackWithLawrence)" as well as for University of British Columbia course, "[Artificial Intelligence Cloud Technology Implementation](https://extendedlearning.ubc.ca/courses/artificial-intelligence-cloud-technology-implementation/mg202)" taught by Lawrence McDaniel.

_New in v0.10: A new chat app named "OpenAI Function Calling". See [lambda_openai_function](./api/terraform/python/openai_api/lambda_openai_function/) for examples including the fully implemented "[get_current_weather()](https://platform.openai.com/docs/guides/function-calling)" from The official OpenAI API documentation, and also check out these example [custom configurations](./api/terraform/python/openai_api/lambda_openai_function/config/) that demonstrate some of the amazing things that you can quickly implement with this new feature!_
Features:

- **Prompting**: Uses [Terraform templates](./api/terraform/apigateway_endpoints.tf) to create 30 different ChatBots, each with its own customized UX and api endpoint.

- **Function Calling**: Uses [Yaml template](./api/terraform/python/openai_api/lambda_openai_function/config/) to easily configure highly customized ChatGPT prompting behavior that uses both dynamic prompting as well as [OpenAI Python Function Calling](https://platform.openai.com/docs/guides/function-calling) to integrate your own custom Python functions into chat response processing. Refer to the [Python source code](./api/terraform/python/openai_api/lambda_openai_function/) for examples including the fully implemented "[get_current_weather()](https://platform.openai.com/docs/guides/function-calling)" from The official OpenAI API documentation, and also [get_additional_info()](./api/terraform/python/openai_api/lambda_openai_function/function_refers_to.py) which implements your yaml templates. Additional documentation is available in this [README](./api/terraform/python/openai_api/lambda_openai_function/README.md)

![Marv](https://cdn.lawrencemcdaniel.com/marv.gif)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from pathlib import Path

import yaml
from botocore.exceptions import ClientError


HERE = os.path.abspath(os.path.dirname(__file__))
Expand All @@ -19,15 +20,18 @@
sys.path.append(PYTHON_ROOT) # noqa: E402


from openai_api.common.conf import settings

# pylint: disable=no-name-in-module
from openai_api.lambda_openai_function.custom_config import (
AdditionalInformation,
CustomConfig,
FunctionCalling,
Prompting,
SearchTerms,
SystemPrompt,
validate_required_keys,
)

# our stuff
from openai_api.lambda_openai_function.tests.test_setup import ( # noqa: E402
get_test_file_path,
get_test_file_yaml,
Expand All @@ -42,6 +46,19 @@ def setUp(self):
self.everlasting_gobbstopper = get_test_file_yaml("config/everlasting-gobbstopper.yaml")
self.everlasting_gobbstopper_invalid = get_test_file_yaml("config/everlasting-gobbstopper-invalid.yaml")

def test_validate_required_keys(self):
"""Test validate_required_keys."""
required_keys = ["meta_data", "prompting", "function_calling"]
validate_required_keys(
class_name="CustomConfig", config_json=self.everlasting_gobbstopper, required_keys=required_keys
)

with self.assertRaises(ValueError):
required_keys = ["meta_data", "prompting", "some_other_key"]
validate_required_keys(
class_name="CustomConfig", config_json=self.everlasting_gobbstopper_invalid, required_keys=required_keys
)

def test_system_prompt(self):
"""Test system_prompt."""
prompt = self.everlasting_gobbstopper["prompting"]["system_prompt"]
Expand All @@ -53,6 +70,7 @@ def test_system_prompt(self):
)
self.assertIsInstance(system_prompt, SystemPrompt)
self.assertIsInstance(system_prompt.system_prompt, str)
self.assertTrue(isinstance(system_prompt.to_json(), str))

def test_system_prompt_invalid(self):
"""Test system_prompt."""
Expand Down Expand Up @@ -132,3 +150,44 @@ def test_refers_to(self):
self.assertListEqual(
additional_information.keys, ["contact", "biographical", "sales_promotions", "coupon_codes"]
)

def test_prompting(self):
"""Test prompting."""
custom_config = CustomConfig(config_json=self.everlasting_gobbstopper)
prompting_config_json = custom_config.prompting.to_json()
Prompting(config_json=prompting_config_json)

with self.assertRaises(ValueError):
Prompting(config_json={})

def test_function_calling(self):
"""Test function_calling."""
custom_config = CustomConfig(config_json=self.everlasting_gobbstopper)
function_calling_config_json = custom_config.function_calling.to_json()
FunctionCalling(config_json=function_calling_config_json)

with self.assertRaises(ValueError):
FunctionCalling(config_json={})

def test_aws_s3_bucket(self):
"""Test aws_s3_bucket."""
aws_s3_bucket_name = settings.aws_s3_bucket_name
s3 = settings.aws_s3_client

folder_name = "test_folder/"
file_name = folder_name + "test_file"

# Connect to the aws_s3_bucket_name
try:
s3.head_bucket(Bucket=aws_s3_bucket_name)
except ClientError:
self.fail("Couldn't connect to the aws_s3_bucket_name.")

# Create a folder
s3.put_object(Bucket=aws_s3_bucket_name, Key=folder_name)

# Write a file to the folder
s3.put_object(Bucket=aws_s3_bucket_name, Key=file_name, Body=b"Test data")

# Delete the file and the folder
s3.delete_objects(Bucket=aws_s3_bucket_name, Delete={"Objects": [{"Key": file_name}, {"Key": folder_name}]})
Loading