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

refactor #273

Merged
merged 2 commits into from
Jan 25, 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
Original file line number Diff line number Diff line change
@@ -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).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
# Complete search terms that will trigger the chatbot to use your customized system prompt.
search_terms:
strings:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}
Expand All @@ -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(
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading