allow AI to close the connection

This commit is contained in:
Cyberes 2024-01-20 16:03:49 -07:00
parent 4e2276541d
commit cec1fadab8
3 changed files with 10 additions and 1 deletions

View File

@ -26,5 +26,9 @@ function_description = [
},
"required": ["message"]
}
},
{
"name": "end_chat",
"description": "Close the chat connection with the user. The assistant is allowed to close the connection at any point if it desires to.",
}
]

View File

@ -11,6 +11,8 @@ def func_talk(response: ChatCompletion):
function_arguments = function_call.arguments
try:
j = json.loads(function_arguments)
if 'message' in j.get('message'):
print(response)
return j.get('message')
except json.decoder.JSONDecodeError:
# Sometimes the AI doesn't do JSON.

5
run.py
View File

@ -28,7 +28,7 @@ signal.signal(signal.SIGINT, signal_handler)
client = OpenAI(api_key=OPENAI_KEY)
temp_name = 'Sakura'
character_card = load_personality('Sakura', 'a shy girl', 'a desktop computer')
character_card = load_personality('Sakura', 'a shy girl', 'a desktop computer', 'Use Japanese emoticons.')
context: list[dict[str, str]] = [character_card]
@ -64,6 +64,9 @@ def main():
context.append({'role': 'assistant', 'content': response_text})
print(colored(response_text, 'blue') + '\n')
break
elif function_name == 'end_chat':
print(colored('The AI has terminated the connection.', 'red', attrs=['bold']))
sys.exit(1)
print(f'Executing {function_name}("{function_arguments}")' + '\n')