forked from santhosh-tg/jugalbandi-api
-
Notifications
You must be signed in to change notification settings - Fork 4
/
database_functions.py
96 lines (86 loc) · 3.84 KB
/
database_functions.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
import asyncpg
import os
from datetime import datetime
import pytz
async def create_engine(timeout=5):
engine = await asyncpg.create_pool(
host=os.getenv('DATABASE_IP'),
port=os.getenv('DATABASE_PORT'),
user=os.getenv('DATABASE_USERNAME'),
password=os.getenv('DATABASE_PASSWORD'),
database=os.getenv('DATABASE_NAME'), max_inactive_connection_lifetime=timeout
)
await create_schema(engine)
return engine
async def create_schema(engine):
async with engine.acquire() as connection:
await connection.execute('''
CREATE TABLE IF NOT EXISTS qa_logs (
id SERIAL PRIMARY KEY,
model_name TEXT DEFAULT 'langchain',
uuid_number TEXT,
query TEXT,
paraphrased_query TEXT,
response TEXT,
source_text TEXT,
error_message TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS qa_voice_logs (
id SERIAL PRIMARY KEY,
uuid_number TEXT,
input_language TEXT DEFAULT 'en',
output_format TEXT DEFAULT 'TEXT',
query TEXT,
query_in_english TEXT,
paraphrased_query TEXT,
response TEXT,
response_in_english TEXT,
audio_output_link TEXT,
source_text TEXT,
error_message TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
CREATE TABLE IF NOT EXISTS document_store_logs (
id SERIAL PRIMARY KEY,
description TEXT,
uuid_number TEXT,
documents_list TEXT[],
error_message TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
);
''')
async def insert_qa_logs(engine, model_name, uuid_number, query, paraphrased_query, response, source_text,
error_message):
async with engine.acquire() as connection:
await connection.execute(
'''
INSERT INTO qa_logs
(model_name, uuid_number, query, paraphrased_query, response, source_text, error_message, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
''',
model_name, uuid_number, query, paraphrased_query, response, source_text, error_message,
datetime.now(pytz.UTC)
)
async def insert_document_store_logs(engine, description, uuid_number, documents_list, error_message):
async with engine.acquire() as connection:
await connection.execute(
f'''
INSERT INTO document_store_logs
(description, uuid_number, documents_list, error_message, created_at)
VALUES ($1, $2, ARRAY {documents_list}, $3, $4)
''', description, uuid_number, error_message, datetime.now(pytz.UTC))
async def insert_qa_voice_logs(engine, uuid_number, input_language, output_format, query, query_in_english,
paraphrased_query, response, response_in_english, audio_output_link, source_text,
error_message):
async with engine.acquire() as connection:
await connection.execute(
'''
INSERT INTO qa_voice_logs
(uuid_number, input_language, output_format, query, query_in_english, paraphrased_query, response,
response_in_english, audio_output_link, source_text, error_message, created_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
''',
uuid_number, input_language, output_format, query, query_in_english, paraphrased_query, response,
response_in_english, audio_output_link, source_text, error_message, datetime.now(pytz.UTC)
)