-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
344 lines (291 loc) · 11.6 KB
/
main.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import json
import os
import signal
import sqlite3
import subprocess
from flask import Flask, render_template, request, redirect, url_for, make_response, jsonify
import logging
from flask_sqlalchemy import SQLAlchemy
from flask_httpauth import HTTPBasicAuth
from werkzeug.security import generate_password_hash, check_password_hash
import openai
import configuration as C
from time import sleep
# TODO: Barre de progression "budget tokens"
# TODO: Checkbox à coté des fonctions qui marchent vraiment
# TODO: Arriver à afficher le python/HTML avec les retours à la ligne
# TODO: Statut dynamique ne rechargeant pas la page à chaque fois
# TODO: "Run for X thoughts"
# TODO: Debug de temps en temps le dernier commentaire user est ignoré
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///messages.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
os.environ["FLASK_ENV"] = "development"
app.debug = True
db = SQLAlchemy(app)
# openai.api_key = open("/home/yves/keys/openAIAPI", "r").read().rstrip("\n")
auth = HTTPBasicAuth()
users = {
"user": generate_password_hash(C.HTTP_PASSWORD)
}
@app.before_request
def require_auth():
auth.login_required()
@auth.verify_password
def verify_password(username, password):
if username in users and \
check_password_hash(users.get(username), password):
return username
class IgnoreStatusCheckFilter(logging.Filter):
def filter(self, record):
return '/playground_status' not in record.getMessage()
logging.getLogger('werkzeug').addFilter(IgnoreStatusCheckFilter())
playground_process = None
def is_port_occupied(port):
result = subprocess.run(['netstat', '-tuln'], capture_output=True, text=True)
return str(port) in result.stdout
def is_playground_running():
global playground_process
if playground_process is None:
return False
return playground_process.poll() is None
# Extract tools information
import tools
documented_functions = {}
openai_functions_arg = []
for item_name in dir(tools):
item = getattr(tools, item_name)
if callable(item) and hasattr(item, "openai_desc") and item.__module__ == "tools":
documented_functions[item_name] = (item, item.openai_desc)
openai_functions_arg.append(item.openai_desc)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
role = db.Column(db.String(10), nullable=False)
content = db.Column(db.Text)
name = db.Column(db.String(50))
# New fields
function_call = db.Column(db.Text)
arguments = db.Column(db.Text)
def __repr__(self):
return '<Message %r>' % self.id
# Does a SQL request, returns result if any
def db_req(dbname, req, args=None, row_factory=False):
if args is None:
args = []
conn = sqlite3.connect(dbname)
if row_factory:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
res = cursor.execute(req, args)
if res:
res = res.fetchall()
conn.commit()
conn.close()
return res
@app.route("/playground/")
@auth.login_required
def playground():
tables = db_req(tools.sql_file,
"SELECT name FROM sqlite_master WHERE type='table';", row_factory=True)
table_data = {}
for table in tables:
table_name = table['name']
rows=db_req(tools.sql_file, f"SELECT * FROM {table_name};", row_factory=True)
table_data[table_name] = [dict(row) for row in rows]
return render_template('playground_db.html', table_data=table_data)
@app.route("/playground/reset", methods=["POST"])
@auth.login_required
def playground_reset():
tables = db_req(tools.sql_file, f"SELECT name FROM sqlite_master WHERE type='table';")
for table in tables:
if table[0].startswith("sqlite_"):
continue
table_name = table[0]
db_req(tools.sql_file, f"DROP TABLE IF EXISTS {table_name};")
return make_response("", 200)
@app.route("/delete/<int:id>")
@auth.login_required
def delete(id):
message = Message.query.get_or_404(id)
db.session.delete(message)
db.session.commit()
return redirect(url_for('home'))
@app.route("/delete_all/")
@auth.login_required
def delete_all():
try:
db.session.query(Message).delete()
db.session.commit()
return redirect(url_for('home'))
except Exception as e:
db.session.rollback()
return jsonify(success=False, message=str(e)), 500
@app.route('/playground_start', methods=['POST'])
@auth.login_required
def start_app_playground():
global playground_process
if playground_process is None or playground_process.poll() is not None:
playground_process = subprocess.Popen(
["python", "-m", "gunicorn", "--log-level", "debug", "--reload", "-b", ":5481", "app:app"],
cwd='./playground/')
return jsonify(success=True)
@app.route('/playground_stop', methods=['POST'])
@auth.login_required
def stop_app_playground():
global playground_process
# Get the process ID that occupies the port
proc = subprocess.Popen(['lsof', '-t', '-i:5481'], stdout=subprocess.PIPE)
out, err = proc.communicate()
pids = out.decode().strip().split("\n")
# If a process is running on the port, kill it
if pids:
for pid in pids:
print(pid)
# os.kill(int(pid), signal.SIGKILL)
# Then, stop app_B
if playground_process is not None:
os.kill(playground_process.pid, signal.SIGTERM)
return jsonify(success=True)
@app.route('/playground_restart', methods=['POST'])
@auth.login_required
def restart_app_playground():
global playground_process
# Get the process ID that occupies the port
proc = subprocess.Popen(['lsof', '-t', '-i:5481'], stdout=subprocess.PIPE)
out, err = proc.communicate()
pid = out.decode().strip()
# If a process is running on the port, kill it
if pid:
os.kill(int(pid), signal.SIGKILL)
# Then, restart app_B
if playground_process is not None and playground_process.poll() is None:
playground_process.terminate()
playground_process.wait()
start_app_playground()
return jsonify(success=True)
@app.route('/playground_status')
def status():
status = is_playground_running()
port_occupied = is_port_occupied(5481)
return jsonify(running=status, port_occupied=port_occupied)
@app.route('/', methods=['GET', 'POST'])
@auth.login_required
def home():
if request.method == 'POST':
role = request.form.get('role')
name = request.form.get('name') if role == 'function' else None
temperature = float(request.form.get('temperature'))
content = request.form.get('content')
messages_to_send = []
if content != "":
message = Message(role=role, name=name, content=content)
db.session.add(message)
db.session.commit()
db.session.flush()
messages_to_send.append(message)
# Get selected messages
for message in Message.query.all():
if request.form.get(f'include_{message.id}'):
messages_to_send.append(message)
# Construct the 'messages' list for the OpenAI API call
openai_messages = list()
for message in messages_to_send:
openai_messages.append(dict())
openai_messages[-1]['role'] = message.role
openai_messages[-1]['content'] = message.content
if message.function_call and message.function_call != "null":
openai_messages[-1]['function_call'] = json.loads(message.function_call)
if message.name and message.name !="null":
openai_messages[-1]['name'] = message.name
# Clean up HTML content when applicable
update_index = -1
for i,om in enumerate(openai_messages):
if "function_call" in om.keys() and om["function_call"]:
if om["function_call"]["name"]=="search_status_update":
update_index = i
last_content_index = -1
for i,om in enumerate(openai_messages):
if om["role"] == "function":
if om["name"] == "get_url_page":
last_content_index = i
if update_index>-1:
for i, om in enumerate(openai_messages):
if om["role"] == "function":
if om["name"] == "get_url_page" and i != last_content_index:
try:
d = json.loads(om['content'])
d["page_content"]="..."
om['content'] = json.dumps(d)
except Exception:
om['content'] = '...'
print("*" * 80)
for m in openai_messages:
print(m)
print("*" * 80)
# Call to OpenAI API
response = openai.ChatCompletion.create(
model="gpt-3.5-turbo",
messages=openai_messages,
temperature=temperature,
functions=openai_functions_arg
)
# Add the assistant's response to the database
print(response)
# Fixing weirg OpenAI bug
args = response['choices'][0]['message'].get("arguments")
if args and args.endswith(",\n}"):
args = args[-3:]+"}"
assistant_message = Message(
role='assistant',
content=response['choices'][0]['message']['content'],
function_call=json.dumps(response['choices'][0]['message'].get("function_call")),
arguments=json.dumps(args),
)
db.session.add(assistant_message)
db.session.commit()
response_message = response['choices'][0]['message']
if response_message.get("function_call"):
function_name = response_message["function_call"]["name"]
args = response['choices'][0]['message']["function_call"].get("arguments")
# Fixing weird OpenAI bug
if args and args.endswith(",\n}"):
args = args[:-3] + '}'
function_args = json.loads(args)
function_answer = documented_functions[function_name][0](**function_args)
print(function_name)
print(function_args)
print("__", function_answer)
assistant_message = Message(
role='function',
name=function_name,
content=json.dumps(function_answer),
)
db.session.add(assistant_message)
db.session.commit()
messages = Message.query.all()
for message in messages:
if message.function_call:
message.function_call = json.loads(message.function_call)
if message.content is None:
if message.function_call:
message.content = f"function call: {message.function_call['name']}\n"
message.content += f"arguments:\n"
print(message.function_call.get("arguments", {}))
try:
for k,v in json.loads(message.function_call.get("arguments", {})).items():
message.content += f" * {k}: {repr(v)}"
except json.decoder.JSONDecodeError:
message.content += str(message.function_call.get("arguments"))
else:
message.content = ""
print("*", message.content)
print(openai_functions_arg)
return render_template('home.html',
messages=messages,
context=[{"title": i['name'], "content": i['description']} for i in openai_functions_arg],
playground_url=C.PLAYGROUND_URL)
if __name__ == "__main__":
with app.app_context():
db.create_all()
app.run(host='0.0.0.0', port=5000)
sleep(5)