-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.py
274 lines (215 loc) · 9.2 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
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
from fastapi import FastAPI, Query
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
import sqlite3
import openai
import toml
from datetime import datetime
from markdown import markdown
from collections import defaultdict
import statistics
from history import load_conversations
from utils import time_group, human_readable_time
from llms import load_create_embeddings, search_similar, openai_api_cost, TYPE_CONVERSATION, TYPE_MESSAGE
DB_EMBEDDINGS = "data/embeddings.db"
DB_SETTINGS = "data/settings.db"
SECRETS_PATH = "data/secrets.toml"
CONVERSATIONS_PATH = "data/conversations.json"
# Initialize FastAPI app
app = FastAPI()
api_app = FastAPI(title="API")
conversations = load_conversations(CONVERSATIONS_PATH)
try:
SECRETS = toml.load(SECRETS_PATH)
OPENAI_ENABLED = True
except:
print("-- No secrets found. Not able to access the OpenAI API.")
OPENAI_ENABLED = False
if OPENAI_ENABLED:
openai.organization = SECRETS["openai"]["organization"]
openai.api_key = SECRETS["openai"]["api_key"]
embeddings, embeddings_ids, embeddings_index = load_create_embeddings(DB_EMBEDDINGS, conversations)
# All conversation items
@api_app.get("/conversations")
def get_conversations():
# Get favorites
conn = connect_settings_db()
cursor = conn.cursor()
cursor.execute("SELECT conversation_id FROM favorites WHERE is_favorite = 1")
rows = cursor.fetchall()
favorite_ids = [row[0] for row in rows]
conn.close()
conversations_data = [{
"group": time_group(conv.created),
"id": conv.id,
"title": conv.title_str,
"created": conv.created_str,
"total_length": human_readable_time(conv.total_length, short=True),
"is_favorite": conv.id in favorite_ids
} for conv in conversations]
return JSONResponse(content=conversations_data)
# All messages from a specific conversation by its ID
@api_app.get("/conversations/{conv_id}/messages")
def get_messages(conv_id: str):
conversation = next((conv for conv in conversations if conv.id == conv_id), None)
if not conversation:
return JSONResponse(content={"error": "Invalid conversation ID"}, status_code=404)
messages = []
prev_created = None # Keep track of the previous message's creation time
for msg in conversation.messages:
if not msg:
continue
# If there's a previous message and the time difference is 1 hour or more
if prev_created and (msg.created - prev_created).total_seconds() >= 3600:
delta = msg.created - prev_created
time_str = human_readable_time(delta.total_seconds())
messages.append({
"text": f"{time_str} passed",
"role": "internal"
})
messages.append({
"text": markdown(msg.text),
"role": msg.role,
"created": msg.created_str
})
# Update the previous creation time for the next iteration
prev_created = msg.created
response = {
"conversation_id": conversation.id,
"messages": messages
}
return JSONResponse(content=response)
@api_app.get("/activity")
def get_activity():
activity_by_day = defaultdict(int)
for conversation in conversations:
for message in conversation.messages:
day = message.created.date()
activity_by_day[day] += 1
activity_by_day = {str(k): v for k, v in sorted(dict(activity_by_day).items())}
return JSONResponse(content=activity_by_day)
@api_app.get("/statistics")
def get_statistics():
# Calculate the min, max, and average lengths
lengths = []
for conv in conversations:
lengths.append((conv.total_length, conv.id))
# Sort conversations by length
lengths.sort(reverse=True)
if lengths:
min_threshold_seconds = 1
filtered_min_lengths = [l for l in lengths if l[0] >= min_threshold_seconds]
min_length = human_readable_time(min(filtered_min_lengths)[0])
max_length = human_readable_time(max(lengths)[0])
avg_length = human_readable_time(statistics.mean([l[0] for l in lengths]))
else:
min_length = max_length = avg_length = "N/A"
# Generate links for the top 3 longest conversations
top_3_links = "".join([f"<a href='https://chat.openai.com/c/{l[1]}' target='_blank'>Chat {chr(65 + i)}</a><br/>"
for i, l in enumerate(lengths[:3])])
# Get the last chat message timestamp and backup age
last_chat_timestamp = max(conv.created for conv in conversations)
return JSONResponse(content={
"Chat backup age": human_readable_time((datetime.now() - last_chat_timestamp).total_seconds()),
"Last chat message": last_chat_timestamp.strftime('%Y-%m-%d'),
"First chat message": min(conv.created for conv in conversations).strftime('%Y-%m-%d'),
"Shortest conversation": min_length,
"Longest conversation": max_length,
"Average chat length": avg_length,
"Top longest chats": top_3_links
})
@api_app.get("/ai-cost")
def get_ai_cost():
tokens_by_month = defaultdict(lambda: {'input': 0, 'output': 0})
for conv in conversations:
for msg in conv.messages:
year_month = msg.created.strftime('%Y-%m')
token_count = msg.count_tokens()
if msg.role == "user":
tokens_by_month[year_month]['input'] += openai_api_cost(msg.model_str,
input=token_count)
else:
tokens_by_month[year_month]['output'] += openai_api_cost(msg.model_str,
output=token_count)
# Make a list of dictionaries
tokens_list = [
{'month': month, 'input': int(data['input']), 'output': int(data['output'])}
for month, data in sorted(tokens_by_month.items())
]
return JSONResponse(content=tokens_list)
# Search conversations and messages
@api_app.get("/search")
def search_conversations(query: str = Query(..., min_length=3, description="Search query")):
def add_search_result(search_results, result_type, conv, msg):
search_results.append({
"type": result_type,
"id": conv.id,
"title": conv.title_str,
"text": markdown(msg.text),
"role": msg.role,
"created": conv.created_str if result_type == "conversation" else msg.created_str,
})
def find_conversation_by_id(conversations, id):
return next((conv for conv in conversations if conv.id == id), None)
def find_message_by_id(messages, id):
return next((msg for msg in messages if msg.id == id), None)
search_results = []
if query.startswith('"') and query.endswith('"'):
query = query[1:-1]
query_exact = True
else:
query_exact = False
if OPENAI_ENABLED and not query_exact:
for _id in search_similar(query, embeddings_ids, embeddings_index):
conv = find_conversation_by_id(conversations, embeddings[_id]["conv_id"])
if conv:
result_type = embeddings[_id]["type"]
if result_type == TYPE_CONVERSATION:
msg = conv.messages[0]
else:
msg = find_message_by_id(conv.messages, _id)
if msg:
add_search_result(search_results, result_type, conv, msg)
else:
for conv in conversations:
query_lower = query.lower()
if (conv.title or "").lower().find(query_lower) != -1:
add_search_result(search_results, "conversation", conv, conv.messages[0])
for msg in conv.messages:
if msg and msg.text.lower().find(query_lower) != -1:
add_search_result(search_results, "message", conv, msg)
if len(search_results) >= 10:
break
return JSONResponse(content=search_results)
# Toggle favorite status
@api_app.post("/toggle_favorite")
def toggle_favorite(conv_id: str):
conn = connect_settings_db()
cursor = conn.cursor()
# Check if the conversation_id already exists in favorites
cursor.execute("SELECT is_favorite FROM favorites WHERE conversation_id = ?", (conv_id,))
row = cursor.fetchone()
if row is None:
# Insert new entry with is_favorite set to True
cursor.execute("INSERT INTO favorites (conversation_id, is_favorite) VALUES (?, ?)", (conv_id, True))
is_favorite = True
else:
# Toggle the is_favorite status
is_favorite = not row[0]
cursor.execute("UPDATE favorites SET is_favorite = ? WHERE conversation_id = ?", (is_favorite, conv_id))
conn.commit()
conn.close()
return {"conversation_id": conv_id, "is_favorite": is_favorite}
def connect_settings_db():
conn = sqlite3.connect(DB_SETTINGS)
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS favorites (
conversation_id TEXT PRIMARY KEY,
is_favorite BOOLEAN
);
""")
conn.commit()
return conn
app.mount("/api", api_app)
app.mount("/", StaticFiles(directory="static", html=True), name="Static")