-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
199 lines (170 loc) · 6.97 KB
/
db.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
import os
import psycopg2
from psycopg2.extras import DictCursor
from datetime import datetime, timezone
from zoneinfo import ZoneInfo
RUN_TIMEZONE_CHECK = os.getenv('RUN_TIMEZONE_CHECK', '1') == '1'
TZ_INFO = os.getenv("TZ", "Europe/Berlin")
tz = ZoneInfo(TZ_INFO)
def get_db_connection():
return psycopg2.connect(
host=os.getenv("POSTGRES_HOST", "postgres"),
database=os.getenv("POSTGRES_DB", "course_assistant"),
user=os.getenv("POSTGRES_USER", "your_username"),
password=os.getenv("POSTGRES_PASSWORD", "your_password"),
)
def init_db():
conn = get_db_connection()
try:
with conn.cursor() as cur:
cur.execute("DROP TABLE IF EXISTS feedback")
cur.execute("DROP TABLE IF EXISTS conversations")
cur.execute("""
CREATE TABLE conversations (
id TEXT PRIMARY KEY,
question TEXT NOT NULL,
answer TEXT NOT NULL,
model_used TEXT NOT NULL,
response_time FLOAT NOT NULL,
relevance TEXT NOT NULL,
relevance_explanation TEXT NOT NULL,
prompt_tokens INTEGER NOT NULL,
completion_tokens INTEGER NOT NULL,
total_tokens INTEGER NOT NULL,
eval_prompt_tokens INTEGER NOT NULL,
eval_completion_tokens INTEGER NOT NULL,
eval_total_tokens INTEGER NOT NULL,
openai_cost FLOAT NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL
)
""")
cur.execute("""
CREATE TABLE feedback (
id SERIAL PRIMARY KEY,
conversation_id TEXT REFERENCES conversations(id),
feedback INTEGER NOT NULL,
timestamp TIMESTAMP WITH TIME ZONE NOT NULL
)
""")
conn.commit()
finally:
conn.close()
def save_conversation(conversation_id, question, answer_data, timestamp=None):
if timestamp is None:
timestamp = datetime.now(tz)
conn = get_db_connection()
try:
with conn.cursor() as cur:
cur.execute(
"""
INSERT INTO conversations
(id, question, answer, model_used, response_time, relevance,
relevance_explanation, prompt_tokens, completion_tokens, total_tokens,
eval_prompt_tokens, eval_completion_tokens, eval_total_tokens, openai_cost, timestamp)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""",
(
conversation_id,
question,
answer_data["answer"],
answer_data["model_used"],
answer_data["response_time"],
answer_data["relevance"],
answer_data["relevance_explanation"],
answer_data["prompt_tokens"],
answer_data["completion_tokens"],
answer_data["total_tokens"],
answer_data["eval_prompt_tokens"],
answer_data["eval_completion_tokens"],
answer_data["eval_total_tokens"],
answer_data["openai_cost"],
timestamp
),
)
conn.commit()
finally:
conn.close()
def save_feedback(conversation_id, feedback, timestamp=None):
if timestamp is None:
timestamp = datetime.now(tz)
conn = get_db_connection()
try:
with conn.cursor() as cur:
cur.execute(
"INSERT INTO feedback (conversation_id, feedback, timestamp) VALUES (%s, %s, COALESCE(%s, CURRENT_TIMESTAMP))",
(conversation_id, feedback, timestamp),
)
conn.commit()
finally:
conn.close()
def get_recent_conversations(limit=5, relevance=None):
conn = get_db_connection()
try:
with conn.cursor(cursor_factory=DictCursor) as cur:
query = """
SELECT c.*, f.feedback
FROM conversations c
LEFT JOIN feedback f ON c.id = f.conversation_id
"""
if relevance:
query += f" WHERE c.relevance = '{relevance}'"
query += " ORDER BY c.timestamp DESC LIMIT %s"
cur.execute(query, (limit,))
return cur.fetchall()
finally:
conn.close()
def get_feedback_stats():
conn = get_db_connection()
try:
with conn.cursor(cursor_factory=DictCursor) as cur:
cur.execute("""
SELECT
SUM(CASE WHEN feedback > 0 THEN 1 ELSE 0 END) as thumbs_up,
SUM(CASE WHEN feedback < 0 THEN 1 ELSE 0 END) as thumbs_down
FROM feedback
""")
return cur.fetchone()
finally:
conn.close()
def check_timezone():
conn = get_db_connection()
try:
with conn.cursor() as cur:
cur.execute("SHOW timezone;")
db_timezone = cur.fetchone()[0]
print(f"Database timezone: {db_timezone}")
cur.execute("SELECT current_timestamp;")
db_time_utc = cur.fetchone()[0]
print(f"Database current time (UTC): {db_time_utc}")
db_time_local = db_time_utc.astimezone(tz)
print(f"Database current time ({TZ_INFO}): {db_time_local}")
py_time = datetime.now(tz)
print(f"Python current time: {py_time}")
# Use py_time instead of tz for insertion
cur.execute("""
INSERT INTO conversations
(id, question, answer, model_used, response_time, relevance,
relevance_explanation, prompt_tokens, completion_tokens, total_tokens,
eval_prompt_tokens, eval_completion_tokens, eval_total_tokens, openai_cost, timestamp)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
RETURNING timestamp;
""",
('test', 'test question', 'test answer', 'test model', 0.0, 0.0,
'test explanation', 0, 0, 0, 0, 0, 0, 0.0, py_time))
inserted_time = cur.fetchone()[0]
print(f"Inserted time (UTC): {inserted_time}")
print(f"Inserted time ({TZ_INFO}): {inserted_time.astimezone(tz)}")
cur.execute("SELECT timestamp FROM conversations WHERE id = 'test';")
selected_time = cur.fetchone()[0]
print(f"Selected time (UTC): {selected_time}")
print(f"Selected time ({TZ_INFO}): {selected_time.astimezone(tz)}")
# Clean up the test entry
cur.execute("DELETE FROM conversations WHERE id = 'test';")
conn.commit()
except Exception as e:
print(f"An error occurred: {e}")
conn.rollback()
finally:
conn.close()
if RUN_TIMEZONE_CHECK:
check_timezone()