forked from daveshap/ChromaDB_Chatbot_Public
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchat_converse.py
195 lines (171 loc) · 7.25 KB
/
chat_converse.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#
# Imports
#
import os, sys
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/System/imports')
sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/System/gpt-observers')
os.system('cls' if os.name == 'nt' else 'clear')
import functions_helper as file
import functions_observer as observer
import functions_chatlog as chat
import functions_generate as botlib
import functions_kb as kb
def center_string(string, width):
if len(string) >= width:
return string # No need for padding if string is already equal to or longer than the width
string = ' ' + string + ' '
padding = width - len(string)
left_padding = padding // 2
right_padding = padding - left_padding
centered_string = ' ' * left_padding + string + ' ' * right_padding
return ' |' + centered_string + '|'
def inputBool(prompt):
while True:
user_input = input(prompt)
if user_input.upper() == 'Y':
return 'Y'
elif user_input.upper() == 'N':
return 'N'
else:
print("Invalid input. Please enter 'Y' or 'N'")
def lnfeed():
print('\n')
def format_user_input(text):
# Split the text into time, username, and user message
time, username, message = text.split(' ', 2)
# Calculate the sum of ASCII values of the username characters
ascii_sum = sum(ord(char) for char in username) * 3
# Calculate the text color based on the ASCII sum (0-255)
text_color = ascii_sum % 256
# Calculate the background color by subtracting a fixed value (e.g., 40) from the text color
background_color = (text_color - 40) % 256
# Calculate the font color as the opposite of the background color
font_color = (background_color + 128) % 256
# Format the username with the calculated font and background colors
formatted_username = f'\033[38;5;{font_color};48;5;{background_color}m{username}\033[0m'
# Combine the formatted username and the rest of the message
formatted_text = f'[{time}] {formatted_username}: {message}'
return formatted_text
if __name__ == '__main__':
os.system('cls' if os.name == 'nt' else 'clear')
print('<========================================================>')
print(center_string(' ', 54))
print(center_string('~~Welcome to the group chat~~', 54))
print(center_string('To get started we need some info', 54))
print(center_string(' ', 54))
print('<========================================================>')
lnfeed()
# Get Bots
current_bots = []
bot_input = 'starting'
while bot_input != '':
bot_input = input('Enter a bot that will participate in the conversation (blank=finished): ')
if bot_input == '':
if len(current_bots) == 0:
current_bots.append('Spongebob')
current_bots.append('Patrick')
break
else:
current_bots.append(bot_input)
# Choose chat
current_chat = input('Enter the name of the chat: ')
if current_chat == '':
current_chat = 'main'
chatlog = chat.fetch(current_chat,'all_messages',-1)
chatlength = str(len(chatlog))
chat_itearation = 0
bot_iteration = 0
while True:
# Print system message
os.system('cls' if os.name == 'nt' else 'clear')
print('<========================================================>')
print(center_string('Welcome to the chat!', 54))
print(center_string(' ', 54))
print(center_string('Currently Chatting', 54))
for bot in current_bots:
print(center_string(bot + ' (' + observer.get_observation('Track_Mood', bot) + ') ' , 54))
print(center_string(' ', 54))
print(center_string('Conversation is saved to: ' + current_chat, 54))
print(center_string('Conversation Length: ' + chatlength, 54))
print(center_string(' ', 54))
print('<========================================================>')
lnfeed()
# Display chat
chatlog = chat.fetch(current_chat, 'all_messages', 10)
for message in chatlog:
print(format_user_input(message))
# Iterate through bots
current_bot = current_bots[bot_iteration]
bot_iteration += 1
bot_iteration = bot_iteration % len(current_bots)
# User input or generate
current_dialog = input( current_bot + ' Says (...=Autogen, blank=skip): ')
if current_dialog == '':
continue
if current_dialog == '/kick':
if len(current_bots) > 1:
current_bots.remove(current_bot)
bot_iteration = 0
continue
if current_dialog[:4] == '/add':
arguments = current_dialog.split(' ')
current_bots.append(arguments[1])
bot_iteration = len(current_bots)-2
continue
if current_dialog == '...':
# Generate a response
known_kb = []
known_kb.append( current_chat )
# Chatroom participents
known_profiles = {}
for bot in current_bots:
known_profiles[ bot ] = []
if "Bot" not in bot:
known_profiles[ bot ].append('Emotional_State')
known_profiles[ bot ].append('Preferences')
known_profiles[current_bot].append('persona')
# Chatroom properties
extra_tags = {}
extra_tags['Scene'] = file.open_file('Profiles/' + current_chat + '.location.txt')
print('\n\nGenerating Repsonse To Chat...')
current_dialog = botlib.generate(current_bot, current_chat, known_kb, known_profiles)
#
# Add Chat
#
print('Adding Repsonse To Chat...')
chat.add(current_chat, 'user_' + current_bot, current_bot + ': ' + current_dialog) # Add user message to personal log
chat.add(current_chat, 'all_messages', current_bot + ': ' + current_dialog) # Add user message to merged conversation log
#
# Run observers
#
chat_itearation += 1
chat_itearation = chat_itearation % 5
if chat_itearation == 0:
# Code for option 1
print("Processing... 0")
elif chat_itearation == 1:
# Code for option 2
print("Processing... 1")
print(' Running Scene Observer...')
observer.observe('CurrentScene', '', current_chat)
elif chat_itearation == 2:
# Code for option 3
print("Processing... 2")
elif chat_itearation == 3:
# Generate Memory
print("Processing...")
print(' Creating a memory of the chat...')
# Make memory
# latest_conversation = '\n\n'.join( chat.fetch( current_chat, 'all_messages', 10 )).strip()
# save_kbs = [current_chat]
# print( kb.update(save_kbs, latest_conversation) )
elif chat_itearation == 4:
print("Processing...")
print(' Running Observers...')
for bot in current_bots:
if "Bot" not in bot:
observer.observe('Track_Mood', bot, current_chat)
observer.observe('Get_Preferences', bot, current_chat)
else:
print("Processing... Nothing...")
input('Prss Enter')