-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
192 lines (155 loc) · 6.81 KB
/
app.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
# -*- coding: utf-8 -*-
"""Wrapper for OpenAI API for Language Models"""
from flask import Flask, render_template, jsonify
import openai
import time
import threading
import sys
import traceback
import webbrowser
import psutil
from markdown import markdown
from history import QueryDB
config = {
"api_key": "insert-api-key-here",
"model": "gpt-3.5-turbo", # check https://platform.openai.com/docs/models/ for other models
"max_tokens": 512, # maximum amount of returned tokens
"temperature": 0.15, # increases randomness
"stream": True, # stream response per token instead of a single string
"session_spent": 0,
"session_spent_text": "",
"status": "N/A",
"price_per_token": 0.002 / 1000, # estimation of cost, based on text-davinci-003 model
"stop_after_one_request": False,
"done": False,
"completion_text": "",
"input_prompt": "",
"last_history_id": False,
}
openai.api_key = config["api_key"]
app = Flask(__name__, template_folder=".")
@app.route('/')
def index():
"""Landing page; If app is called with arguments, server will shut down after first request"""
global config
if config["input_prompt"]:
config["stop_after_one_request"] = True
threading.Thread(target=openai_call_thread).start()
return render_template('index.html')
def shutdown_flask():
for p in psutil.process_iter():
if p.name().startswith("python"): # might need to exchange for sys.executable
if len(p.cmdline()) > 2 and p.cmdline()[1] == "app.py":
time.sleep(1.5)
p.kill()
def openai_call_thread():
"""Worker thread for API call to OpenAI"""
global config
config["completion_text"] = ""
config["done"] = False
collected_events = []
start_time = time.time()
response = None
chat_completion = False
# GPT-3.5-turbo requires a different method ChatCompletion with an altered response
if config["model"] == "gpt-3.5-turbo":
chat_completion = True
# check https://platform.openai.com/docs/api-reference/completions/create for arguments
try:
if chat_completion:
response = openai.ChatCompletion.create(model=config["model"],
messages=[{"role": "user",
"content": config["input_prompt"]}],
temperature=config["temperature"],
max_tokens=config["max_tokens"],
stream=config["stream"])
else:
response = openai.Completion.create(model=config["model"],
prompt=config["input_prompt"],
temperature=config["temperature"],
max_tokens=config["max_tokens"],
stream=config["stream"])
except Exception as exc:
config["done"] = True
traceback_lines = traceback.format_exc().splitlines()
config["status"] = f"<span class='error'>{traceback_lines[-1]}</span>"
shutdown_flask()
return
if config["stream"]:
for event in response:
collected_events.append(event)
if chat_completion:
if event["choices"][0]["delta"].get("content"):
config["completion_text"] += event["choices"][0]["delta"]["content"]
else:
config["completion_text"] += event["choices"][0]["text"]
used_tokens = len(collected_events) # unsure if 1 event equals 1 token; last event is empty
config["done"] = True
else:
if chat_completion:
config["completion_text"] = response["choices"][0]["message"]["content"]
# ChatCompletion has additional fields we can use
config["response_ms"] = response.response_ms
config["exact_model"] = response.model
config["role"] = response.choices[0].message.role
else:
config["completion_text"] = response["choices"][0]["text"]
used_tokens = response.usage.total_tokens
config["done"] = True
# calculate costs
call_cost = used_tokens * config["price_per_token"]
config["session_spent"] += call_cost
config["session_spent_text"] = f"{used_tokens} tokens ({round(call_cost, 5)}$)"
config["status"] = f"Finished in {round(time.time() - start_time, 3)} s"
if not config["stop_after_one_request"]:
config["session_spent_text"] += f" (session: {round(config['session_spent'], 5)}$)"
# record query history
with QueryDB() as query_db:
query_db.insert_query(config)
config["last_history_id"] = query_db.cursor.lastrowid
# convert markdown to html
config["completion_text"] = markdown(config["completion_text"])
if config["stop_after_one_request"] and config["done"]:
shutdown_flask()
@app.route("/openai_call/<string:prompt>")
def openai_call(prompt: str = None):
"""API endpoint if app is started without arguments; Could be implemented w. input + button """
global config
config["input_prompt"] = prompt
config["status"] = "working .."
threading.Thread(target=openai_call_thread).start()
# hide API_KEY in returned config
return jsonify(status="Started API call in thread",
config={key: val for key, val in config.items()
if key not in ["api_key", "completion_text"]},
result=config.get("response"))
@app.route('/update')
def update():
"""Routine to fetch data, started with setInterval(getResults, interval) in index.html"""
return jsonify(status="Update interval running",
config={key: val for key, val in config.items()
if key not in ["api_key", "completion_text"]},
result=config.get("completion_text"))
@app.route('/get_history')
def get_history():
with QueryDB() as query_db:
query_history = query_db.get_all()
return jsonify(status="Get history queries",
data=query_history)
@app.route('/get_query/<int:query_id>')
def get_query(query_id: int):
global config
with QueryDB() as query_db:
query = query_db.get_by_id(query_id-1)
return jsonify(status="Get query by id", data=query)
@app.route('/close_process', methods=['GET'])
def close_process():
shutdown_flask()
return jsonify(status='thread killed')
if __name__ == "__main__":
if len(sys.argv) > 1:
config["input_prompt"] = " ".join(sys.argv[1:])
else:
config["status"] = "waiting for queries: http://127.0.0.1:5000/openai_call/QUERY"
webbrowser.open("http://127.0.0.1:5000")
app.run(host="127.0.0.1", port=5000, debug=False)