From d3359f839a51f85396ac786ee19c9dd349a6c83f Mon Sep 17 00:00:00 2001 From: lpm0073 Date: Thu, 25 Jan 2024 14:18:14 -0600 Subject: [PATCH 1/2] style: lint yaml configs --- .../config/everlasting-gobstopper.yaml | 5 +++++ .../lambda_openai_function/config/lawrence-mcdaniel.yaml | 1 + 2 files changed, 6 insertions(+) diff --git a/api/terraform/python/openai_api/lambda_openai_function/config/everlasting-gobstopper.yaml b/api/terraform/python/openai_api/lambda_openai_function/config/everlasting-gobstopper.yaml index 8f8a6db1..cf82cf04 100644 --- a/api/terraform/python/openai_api/lambda_openai_function/config/everlasting-gobstopper.yaml +++ b/api/terraform/python/openai_api/lambda_openai_function/config/everlasting-gobstopper.yaml @@ -1,11 +1,16 @@ +--- # Complete search terms that will trigger the chatbot to use your customized system prompt. search_terms: strings: - Gobstopper + - Gobstoppers - Gobbstopper + - Gobbstoppers pairs: - - everlasting - gobstopper + - - everlasting + - gobstoppers function_description: Get additional information about the Everlasting Gobstopper product created by Willy Wonka Chocolate Factory. Information includes sales promotions, coupon codes, company contact information and biographical background on the company founder. system_prompt: > You are a helpful marketing agent for the [Willy Wonka Chocolate Factory](https://wwcf.com). diff --git a/api/terraform/python/openai_api/lambda_openai_function/config/lawrence-mcdaniel.yaml b/api/terraform/python/openai_api/lambda_openai_function/config/lawrence-mcdaniel.yaml index dcc76398..89c12604 100644 --- a/api/terraform/python/openai_api/lambda_openai_function/config/lawrence-mcdaniel.yaml +++ b/api/terraform/python/openai_api/lambda_openai_function/config/lawrence-mcdaniel.yaml @@ -1,3 +1,4 @@ +--- # Complete search terms that will trigger the chatbot to use your customized system prompt. search_terms: strings: From a8e0a5e032360d47fdd62ccf61ecf1ce0be7a722 Mon Sep 17 00:00:00 2001 From: lpm0073 Date: Thu, 25 Jan 2024 14:41:11 -0600 Subject: [PATCH 2/2] refactor: switch tool factory functions to dicts --- .../function_refers_to.py | 43 +++++++++---------- .../function_weather.py | 34 +++++++-------- .../lambda_openai_function/lambda_handler.py | 12 ++---- .../tests/test_lambda_openai_refers_to.py | 9 ++-- .../tests/test_lambda_openai_weather.py | 9 ++-- 5 files changed, 49 insertions(+), 58 deletions(-) diff --git a/api/terraform/python/openai_api/lambda_openai_function/function_refers_to.py b/api/terraform/python/openai_api/lambda_openai_function/function_refers_to.py index e8d86a59..fbaec417 100644 --- a/api/terraform/python/openai_api/lambda_openai_function/function_refers_to.py +++ b/api/terraform/python/openai_api/lambda_openai_function/function_refers_to.py @@ -35,14 +35,15 @@ def search_terms_are_in_messages(messages: list, search_terms: list = None, sear def customized_prompt(config: RefersTo, messages: list) -> list: - """Return a prompt for Lawrence McDaniel""" - custom_prompt = { - "role": "system", - "content": config.system_prompt.system_prompt, - } + """Modify the system prompt based on the custom configuration object""" for i, message in enumerate(messages): if message.get("role") == "system": + system_prompt = message.get("content") + custom_prompt = { + "role": "system", + "content": system_prompt + "\n\n and also " + config.system_prompt.system_prompt, + } messages[i] = custom_prompt break @@ -68,23 +69,21 @@ def info_tool_factory(config: RefersTo): """ Return a dictionary of chat completion tools. """ - tools = [ - { - "type": "function", - "function": { - "name": "get_additional_info", - "description": config.function_description, - "parameters": { - "type": "object", - "properties": { - "inquiry_type": { - "type": "string", - "enum": config.additional_information.keys, - }, + tool = { + "type": "function", + "function": { + "name": "get_additional_info", + "description": config.function_description, + "parameters": { + "type": "object", + "properties": { + "inquiry_type": { + "type": "string", + "enum": config.additional_information.keys, }, - "required": ["inquiry_type"], }, + "required": ["inquiry_type"], }, - } - ] - return tools + }, + } + return tool diff --git a/api/terraform/python/openai_api/lambda_openai_function/function_weather.py b/api/terraform/python/openai_api/lambda_openai_function/function_weather.py index 4f602336..eb523fba 100644 --- a/api/terraform/python/openai_api/lambda_openai_function/function_weather.py +++ b/api/terraform/python/openai_api/lambda_openai_function/function_weather.py @@ -111,24 +111,22 @@ def get_current_weather(location, unit="METRIC"): def weather_tool_factory(): """Return a list of tools that can be called by the OpenAI API""" - tools = [ - { - "type": "function", - "function": { - "name": "get_current_weather", - "description": "Get the current weather in a given location", - "parameters": { - "type": "object", - "properties": { - "location": { - "type": "string", - "description": "The city and state, e.g. San Francisco, CA", - }, - "unit": {"type": "string", "enum": ["METRIC", "USCS"]}, + tool = { + "type": "function", + "function": { + "name": "get_current_weather", + "description": "Get the current weather in a given location", + "parameters": { + "type": "object", + "properties": { + "location": { + "type": "string", + "description": "The city and state, e.g. San Francisco, CA", }, - "required": ["location"], + "unit": {"type": "string", "enum": ["METRIC", "USCS"]}, }, + "required": ["location"], }, - } - ] - return tools + }, + } + return tool diff --git a/api/terraform/python/openai_api/lambda_openai_function/lambda_handler.py b/api/terraform/python/openai_api/lambda_openai_function/lambda_handler.py index d7b9e045..39ee80af 100644 --- a/api/terraform/python/openai_api/lambda_openai_function/lambda_handler.py +++ b/api/terraform/python/openai_api/lambda_openai_function/lambda_handler.py @@ -67,7 +67,8 @@ def handler(event, context): OpenAI API endpoint based on the contents of the request. """ cloudwatch_handler(event, settings.dump, debug_mode=settings.debug_mode) - tools = weather_tool_factory() + weather_tool = weather_tool_factory() + tools = [weather_tool] try: openai_results = {} @@ -82,10 +83,9 @@ def handler(event, context): ): model = "gpt-3.5-turbo-1106" messages = customized_prompt(config=config, messages=messages) - custom_tool = info_tool_factory(config=config)[0] + custom_tool = info_tool_factory(config=config) tools.append(custom_tool) - print(f"Using custom configuration: {config.name} and adding custom tool: {custom_tool}") - break + print(f"Adding custom configuration: {config.name}") # https://platform.openai.com/docs/guides/gpt/chat-completions-api validate_item( @@ -94,10 +94,6 @@ def handler(event, context): item_type="ChatCompletion models", ) validate_completion_request(request_body) - print("Calling OpenAI Chat Completion API...") - print( - f"model: {model}, messages: {messages}, tools: {tools}, temperature: {temperature}, max_tokens: {max_tokens}" - ) openai_results = openai.chat.completions.create( model=model, messages=messages, diff --git a/api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_refers_to.py b/api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_refers_to.py index dd6ecb93..c19f24d0 100644 --- a/api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_refers_to.py +++ b/api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_refers_to.py @@ -51,9 +51,8 @@ def test_get_additional_info(self): def test_info_tool_factory(self): """Test integrity info_tool_factory()""" itf = info_tool_factory(config=self.config) - self.assertIsInstance(itf, list) + self.assertIsInstance(itf, dict) - d = itf[0] - self.assertIsInstance(d, dict) - self.assertTrue("type" in d) - self.assertTrue("function" in d) + self.assertIsInstance(itf, dict) + self.assertTrue("type" in itf) + self.assertTrue("function" in itf) diff --git a/api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_weather.py b/api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_weather.py index ae1f5ce7..d1a49634 100644 --- a/api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_weather.py +++ b/api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_weather.py @@ -50,9 +50,8 @@ def test_get_current_weather(self): def test_weather_tool_factory(self): """Test integrity weather_tool_factory()""" wtf = weather_tool_factory() - self.assertIsInstance(wtf, list) + self.assertIsInstance(wtf, dict) - d = wtf[0] - self.assertIsInstance(d, dict) - self.assertTrue("type" in d) - self.assertTrue("function" in d) + self.assertIsInstance(wtf, dict) + self.assertTrue("type" in wtf) + self.assertTrue("function" in wtf)