-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinterface.py
159 lines (140 loc) Β· 5.35 KB
/
interface.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
import json
import re
import streamlit as st
from colorama import Fore, Style, init
from memgpt.utils import printd
init(autoreset=True)
DEBUG = True # puts full message outputs in the terminal
#DEBUG = False # only dumps important messages in the terminal
def important_message(msg):
print(f'{msg}')
st.sidebar.warning(f'{msg}')
def internal_monologue(msg):
# ANSI escape code for italic is '\x1B[3m'
print(f'π {msg}')
st.sidebar.markdown("### π Internal Thoughts:")
st.sidebar.write(f'π {msg}')
def assistant_message(msg):
print(f'π€ {msg}')
#st.sidebar.markdown("### π€ Assistant:")
#st.sidebar.write(f'π€ {msg}')
def memory_message(msg):
print(f'π§ {msg}')
st.sidebar.markdown("### π§ Memory:")
st.sidebar.write(f'π§ {msg}')
def system_message(msg):
printd(f'π₯οΈ [system] {msg}')
st.sidebar.markdown("### π§ System:")
st.sidebar.write(f'π₯οΈ {msg}')
def user_message(msg, raw=False):
if isinstance(msg, str):
if raw:
printd(f'π§ {msg}')
return
else:
try:
msg_json = json.loads(msg)
except:
printd(f"Warning: failed to parse user message into json")
printd(f'π§ {msg}')
return
if msg_json['type'] == 'user_message':
msg_json.pop('type')
printd(f'π§ {msg_json}')
elif msg_json['type'] == 'heartbeat':
if DEBUG:
msg_json.pop('type')
printd(f'π {msg_json}')
elif msg_json['type'] == 'system_message':
msg_json.pop('type')
printd(f'π₯οΈ {msg_json}')
else:
printd(f'π§ {msg_json}')
def function_message(msg):
if isinstance(msg, dict):
printd(f'β‘ [function] {msg}')
return
if msg.startswith('Success: '):
printd(f'β‘π’ [function] {msg}')
elif msg.startswith('Error: '):
printd(f'β‘π΄ [function] {msg}')
elif msg.startswith('Running '):
if DEBUG:
printd(f'β‘ [function] {msg}')
else:
if 'memory' in msg:
match = re.search(r'Running (\w+)\((.*)\)', msg)
if match:
function_name = match.group(1)
function_args = match.group(2)
print(f'β‘π§ [function] updating memory with {function_name}:')
st.sidebar.markdown("### π Updating Memory:")
st.sidebar.write(f'β‘π§ Updating memory with function: {function_name}:')
try:
msg_dict = eval(function_args)
if function_name == 'archival_memory_search':
print(f'\tquery: {msg_dict["query"]}, page: {msg_dict["page"]}')
st.sidebar.write(f'\tquery: {msg_dict["query"]}, page: {msg_dict["page"]}')
else:
st.sidebar.warning(msg_dict)
print(f'\t {msg_dict["old_content"]}\n\tβ {msg_dict["new_content"]}')
st.sidebar.write(f'\t {msg_dict["old_content"]}\n\tβ {msg_dict["new_content"]}')
except Exception as e:
printd(e)
printd(msg_dict)
pass
else:
printd(f"Warning: did not recognize function message")
printd(f'β‘ [function] {msg}')
elif 'send_message' in msg:
# ignore in debug mode
pass
else:
printd(f'β‘ [function] {msg}')
else:
try:
msg_dict = json.loads(msg)
if "status" in msg_dict and msg_dict["status"] == "OK":
printd(f'β‘ [function] {msg}')
except Exception:
printd(f"Warning: did not recognize function message {type(msg)} {msg}")
printd(f'β‘ [function] {msg}')
def print_messages(message_sequence):
for msg in message_sequence:
role = msg['role']
content = msg['content']
if role == 'system':
system_message(content)
elif role == 'assistant':
# Differentiate between internal monologue, function calls, and messages
if msg.get('function_call'):
if content is not None:
internal_monologue(content)
function_message(msg['function_call'])
# assistant_message(content)
else:
internal_monologue(content)
elif role == 'user':
user_message(content)
elif role == 'function':
function_message(content)
else:
print(f'Unknown role: {content}')
st.sidebar.write(f'Unknown role: {content}')
def print_messages_simple(message_sequence):
for msg in message_sequence:
role = msg['role']
content = msg['content']
if role == 'system':
system_message(content)
elif role == 'assistant':
assistant_message(content)
elif role == 'user':
user_message(content, raw=True)
else:
print(f'Unknown role: {content}')
st.sidebar.write(f'Unknown role: {content}')
def print_messages_raw(message_sequence):
for msg in message_sequence:
print(msg)
st.write(msg)