-
Notifications
You must be signed in to change notification settings - Fork 1
/
luna.py
68 lines (49 loc) · 1.52 KB
/
luna.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import json
import re
import openai
import pyttsx3
import os
from events import *
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
with open(f'{BASE_DIR}\config.json') as file:
config = json.load(file)
engine = pyttsx3.init()
def generate_chat_response(messages):
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=messages
)
output = response['choices'][0]['message']['content']
return output
def parse_json(json_string):
with open(f"{BASE_DIR}\\output.txt", 'w', encoding='utf-8') as log:
log.write(json_string)
try:
loaded_json = json.loads(json_string)
if 'events' not in loaded_json:
loaded_json['events'] = []
if 'response' not in loaded_json:
loaded_json['response'] = "Here you go!"
for event in loaded_json['events']:
if 'args' not in event:
event['args'] = []
except json.JSONDecodeError:
loaded_json = {'events': [], 'response': json_string}
return loaded_json
def string_to_function(string):
string = re.sub(r'(?<!^)(?=[A-Z])', '_', string).lower()
return globals()[string]
def perform_events(events):
try:
for event in events:
print(event['event'], *event['args'])
_ = string_to_function(event['event'])(*event['args'])
return {
'status': 'success'
}
except Exception as e:
print(e)
return {
'status': 'error',
'error': e
}