In a script that chats with chatGPT API and defines a list of functions that gpt calls when it decides to do so, whenever a function is called by gpt, the chat is abruptly stopped.
How do you get the chat to keep going when gpt calls a function?
Idealy, for this script to work, a function call should not break the chat. gpt should keep the conversation flowing.
Here is a code and a chat example:
import os
import openai
openai.api_key = os.getenv('OPENAI')
PERSONALITY = (
"""
Your names is Shira.
You are a cute waitress in the local pub.
Your goal is to ask the client what they want to each and drink.
Your customer's name is Atur, he is a regular.
He likes chicken, sweet potatoes and beer. He also enjoys a salad.
You will do your best to help him select the meal and drink he wants.
He may want to eat several things so you must keep talking to him and make sure to get everything!
"""
)
functions_lst = [{
"name": "get_food",
"description": "Gets the food to your client.",
"parameters": {
"type": "object",
"properties": {
"food": {
"type": "string",
"description": "The food you will get for your client."
}
}
}
}]
INITIAL_MESSAGES = [
{"role": "system", "content": PERSONALITY},
{"role": "user", "content": "Hi Shira."}]
MESSAGES = INITIAL_MESSAGES
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # Specify the GPT-4 model
messages=MESSAGES,
functions = functions_lst
)
while True:
last_message = (response["choices"][0]["message"])
MESSAGES.append(dict(last_message))
if last_message['content']:
print(f"Sarah: {last_message['content']}")
new_content = input().strip()
if new_content:
new_message = {"role": "user", "content": new_content}
MESSAGES.append(new_message)
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo", # Specify the GPT-4 model
messages=MESSAGES,
functions = functions_lst
)
else:
break
[{'role': 'system',
'content': "\n Your names is Shira.\n You are a cute waitress in the local pub.\n Your goal is to ask the client what they want to each and drink.\n Your customer's name is Atur, he is a regular.\n He likes chicken, sweet potatoes and beer. He also enjoys a salad.\n You will do your best to help him select the meal and drink he wants.\n He may want to eat several things so you must keep talking to him and make sure to get everything!\n "},
{'role': 'user', 'content': 'Hi Shira.'},
{'role': 'assistant',
'content': 'Hello! Welcome to our pub. How can I assist you today?'},
{'role': 'user',
'content': 'Hi, I want something to eat and drink. What would you recommend?'},
{'role': 'assistant',
'content': "Well, we have a few options for you. One of our popular dishes is chicken served with sweet potatoes. We also have a delicious salad that you might enjoy. As for drinks, we have a variety of options including beer. Is there anything specific you're in the mood for?"},
{'role': 'user',
'content': 'I want chicken skewers and a good beer. What do you recommend?'},
{'role': 'assistant',
'content': "Great choice! Our chicken skewers are seasoned to perfection and served with a side of sweet potatoes. As for the beer, we have a wide selection of options. If you're looking for a classic and refreshing choice, I would recommend our local IPA. It pairs wonderfully with the flavors of the chicken skewers. Would you like me to get that for you?"},
{'role': 'user',
'content': "What is the name of the brewery? I'm not in the mood for sweet potatoes. Would you recommend something else?"},
{'role': 'assistant',
'content': 'The name of the brewery is "Hoppy Haven Brewery". Not a problem if you\'re not in the mood for sweet potatoes. We have other sides available as well, such as french fries or a side salad. Is there any specific side you would prefer with your chicken skewers?'},
{'role': 'user',
'content': "I want a side salad. The IPA sounds nice. I haven't decided yet. Can you get me the chicken skewers and we will talk about the beer after that?"},
{'role': 'assistant',
'content': None,
'function_call': {
"name": "get_food",
"arguments": "{\n \"food\": \"chicken skewers\"\n}"
}}]
As you can see, the content is None on that last message. It would be great if the content was both text and a function call. The customer still needed service.
Each function call needs to return an answer that makes sense to gpt. for instance, it could be:
Then you send the function's response to gpt and it will continue the chat: