Skip to content

Commit 79744f6

Browse files
authored
Merge pull request #273 from FullStackWithLawrence/next
refactor
2 parents 6e4bf3e + a8e0a5e commit 79744f6

File tree

7 files changed

+55
-58
lines changed

7 files changed

+55
-58
lines changed

api/terraform/python/openai_api/lambda_openai_function/config/everlasting-gobstopper.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
---
12
# Complete search terms that will trigger the chatbot to use your customized system prompt.
23
search_terms:
34
strings:
45
- Gobstopper
6+
- Gobstoppers
57
- Gobbstopper
8+
- Gobbstoppers
69
pairs:
710
- - everlasting
811
- gobstopper
12+
- - everlasting
13+
- gobstoppers
914
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.
1015
system_prompt: >
1116
You are a helpful marketing agent for the [Willy Wonka Chocolate Factory](https://wwcf.com).

api/terraform/python/openai_api/lambda_openai_function/config/lawrence-mcdaniel.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
# Complete search terms that will trigger the chatbot to use your customized system prompt.
23
search_terms:
34
strings:

api/terraform/python/openai_api/lambda_openai_function/function_refers_to.py

+21-22
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,15 @@ def search_terms_are_in_messages(messages: list, search_terms: list = None, sear
3535

3636

3737
def customized_prompt(config: RefersTo, messages: list) -> list:
38-
"""Return a prompt for Lawrence McDaniel"""
39-
custom_prompt = {
40-
"role": "system",
41-
"content": config.system_prompt.system_prompt,
42-
}
38+
"""Modify the system prompt based on the custom configuration object"""
4339

4440
for i, message in enumerate(messages):
4541
if message.get("role") == "system":
42+
system_prompt = message.get("content")
43+
custom_prompt = {
44+
"role": "system",
45+
"content": system_prompt + "\n\n and also " + config.system_prompt.system_prompt,
46+
}
4647
messages[i] = custom_prompt
4748
break
4849

@@ -68,23 +69,21 @@ def info_tool_factory(config: RefersTo):
6869
"""
6970
Return a dictionary of chat completion tools.
7071
"""
71-
tools = [
72-
{
73-
"type": "function",
74-
"function": {
75-
"name": "get_additional_info",
76-
"description": config.function_description,
77-
"parameters": {
78-
"type": "object",
79-
"properties": {
80-
"inquiry_type": {
81-
"type": "string",
82-
"enum": config.additional_information.keys,
83-
},
72+
tool = {
73+
"type": "function",
74+
"function": {
75+
"name": "get_additional_info",
76+
"description": config.function_description,
77+
"parameters": {
78+
"type": "object",
79+
"properties": {
80+
"inquiry_type": {
81+
"type": "string",
82+
"enum": config.additional_information.keys,
8483
},
85-
"required": ["inquiry_type"],
8684
},
85+
"required": ["inquiry_type"],
8786
},
88-
}
89-
]
90-
return tools
87+
},
88+
}
89+
return tool

api/terraform/python/openai_api/lambda_openai_function/function_weather.py

+16-18
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,22 @@ def get_current_weather(location, unit="METRIC"):
111111

112112
def weather_tool_factory():
113113
"""Return a list of tools that can be called by the OpenAI API"""
114-
tools = [
115-
{
116-
"type": "function",
117-
"function": {
118-
"name": "get_current_weather",
119-
"description": "Get the current weather in a given location",
120-
"parameters": {
121-
"type": "object",
122-
"properties": {
123-
"location": {
124-
"type": "string",
125-
"description": "The city and state, e.g. San Francisco, CA",
126-
},
127-
"unit": {"type": "string", "enum": ["METRIC", "USCS"]},
114+
tool = {
115+
"type": "function",
116+
"function": {
117+
"name": "get_current_weather",
118+
"description": "Get the current weather in a given location",
119+
"parameters": {
120+
"type": "object",
121+
"properties": {
122+
"location": {
123+
"type": "string",
124+
"description": "The city and state, e.g. San Francisco, CA",
128125
},
129-
"required": ["location"],
126+
"unit": {"type": "string", "enum": ["METRIC", "USCS"]},
130127
},
128+
"required": ["location"],
131129
},
132-
}
133-
]
134-
return tools
130+
},
131+
}
132+
return tool

api/terraform/python/openai_api/lambda_openai_function/lambda_handler.py

+4-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def handler(event, context):
6767
OpenAI API endpoint based on the contents of the request.
6868
"""
6969
cloudwatch_handler(event, settings.dump, debug_mode=settings.debug_mode)
70-
tools = weather_tool_factory()
70+
weather_tool = weather_tool_factory()
71+
tools = [weather_tool]
7172

7273
try:
7374
openai_results = {}
@@ -82,10 +83,9 @@ def handler(event, context):
8283
):
8384
model = "gpt-3.5-turbo-1106"
8485
messages = customized_prompt(config=config, messages=messages)
85-
custom_tool = info_tool_factory(config=config)[0]
86+
custom_tool = info_tool_factory(config=config)
8687
tools.append(custom_tool)
87-
print(f"Using custom configuration: {config.name} and adding custom tool: {custom_tool}")
88-
break
88+
print(f"Adding custom configuration: {config.name}")
8989

9090
# https://platform.openai.com/docs/guides/gpt/chat-completions-api
9191
validate_item(
@@ -94,10 +94,6 @@ def handler(event, context):
9494
item_type="ChatCompletion models",
9595
)
9696
validate_completion_request(request_body)
97-
print("Calling OpenAI Chat Completion API...")
98-
print(
99-
f"model: {model}, messages: {messages}, tools: {tools}, temperature: {temperature}, max_tokens: {max_tokens}"
100-
)
10197
openai_results = openai.chat.completions.create(
10298
model=model,
10399
messages=messages,

api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_refers_to.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,8 @@ def test_get_additional_info(self):
5151
def test_info_tool_factory(self):
5252
"""Test integrity info_tool_factory()"""
5353
itf = info_tool_factory(config=self.config)
54-
self.assertIsInstance(itf, list)
54+
self.assertIsInstance(itf, dict)
5555

56-
d = itf[0]
57-
self.assertIsInstance(d, dict)
58-
self.assertTrue("type" in d)
59-
self.assertTrue("function" in d)
56+
self.assertIsInstance(itf, dict)
57+
self.assertTrue("type" in itf)
58+
self.assertTrue("function" in itf)

api/terraform/python/openai_api/lambda_openai_function/tests/test_lambda_openai_weather.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,8 @@ def test_get_current_weather(self):
5050
def test_weather_tool_factory(self):
5151
"""Test integrity weather_tool_factory()"""
5252
wtf = weather_tool_factory()
53-
self.assertIsInstance(wtf, list)
53+
self.assertIsInstance(wtf, dict)
5454

55-
d = wtf[0]
56-
self.assertIsInstance(d, dict)
57-
self.assertTrue("type" in d)
58-
self.assertTrue("function" in d)
55+
self.assertIsInstance(wtf, dict)
56+
self.assertTrue("type" in wtf)
57+
self.assertTrue("function" in wtf)

0 commit comments

Comments
 (0)