This repository was archived by the owner on Nov 24, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathdb.py
More file actions
407 lines (335 loc) · 13.4 KB
/
Copy pathdb.py
File metadata and controls
407 lines (335 loc) · 13.4 KB
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
import logging
import os
import re
import threading
from typing import List, Optional
import psycopg
from pgvector.psycopg import register_vector
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_openai import OpenAIEmbeddings
import metrics
logger = logging.getLogger("db")
_EMBEDDING_MODEL = "text-embedding-3-small"
# OpenAI text-embedding-3-small is 1536 dims.
_EMBEDDING_DIM = 1536
TRANSCRIPT_STATES = ("progress", "failed", "summarizing", "done")
_schema_lock = threading.Lock()
_schema_initialized = False
def is_configured() -> bool:
required = [
"PGVECTOR_HOST",
"PGVECTOR_PORT",
"PGVECTOR_USER",
"PGVECTOR_PASSWORD",
"PGVECTOR_DATABASE",
]
return all(os.getenv(k) for k in required)
def _conninfo() -> str:
host = os.getenv("PGVECTOR_HOST")
port = os.getenv("PGVECTOR_PORT")
user = os.getenv("PGVECTOR_USER")
password = os.getenv("PGVECTOR_PASSWORD")
dbname = os.getenv("PGVECTOR_DATABASE")
if not all([host, port, user, password, dbname]):
missing = [k for k in ["PGVECTOR_HOST", "PGVECTOR_PORT", "PGVECTOR_USER", "PGVECTOR_PASSWORD", "PGVECTOR_DATABASE"] if not os.getenv(k)]
raise RuntimeError(f"Postgres vectorstore is not configured; missing env vars: {', '.join(missing)}")
# psycopg conninfo format
return f"host={host} port={port} user={user} password={password} dbname={dbname}"
def _connect() -> psycopg.Connection:
conn = psycopg.connect(_conninfo())
try:
register_vector(conn)
except psycopg.ProgrammingError as e:
# When the database doesn't have pgvector installed yet, pgvector's
# registration fails with "vector type not found in the database".
# We install the extension and retry once.
if "vector type not found" not in str(e):
conn.close()
raise
conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
conn.commit()
register_vector(conn)
return conn
def _connect_without_pgvector() -> psycopg.Connection:
return psycopg.connect(_conninfo())
def ensure_schema() -> None:
"""Public entry point so callers can create the schema eagerly (e.g. at
process startup) instead of waiting for the first persisted transcript."""
_ensure_schema()
def _ensure_schema() -> None:
global _schema_initialized
if _schema_initialized:
return
with _schema_lock:
if _schema_initialized:
return
# Don't attempt pgvector type registration during bootstrap.
# We may need to install the extension first.
with _connect_without_pgvector() as conn:
conn.execute("CREATE EXTENSION IF NOT EXISTS vector")
# Be explicit: ensure the extension is committed before creating tables
# that depend on it.
conn.commit()
conn.execute(
"""
CREATE TABLE IF NOT EXISTS transcripts (
id BIGSERIAL PRIMARY KEY,
uniqueid TEXT NOT NULL,
linkedid TEXT,
src_number TEXT,
dst_number TEXT,
duration_seconds INTEGER,
raw_transcription TEXT NOT NULL,
state TEXT NOT NULL DEFAULT 'done',
cleaned_transcription TEXT,
summary TEXT,
sentiment SMALLINT CHECK (sentiment BETWEEN 0 AND 10),
deleted_at TIMESTAMPTZ NULL,
CONSTRAINT transcripts_state_check CHECK (state IN ('progress', 'failed', 'summarizing', 'done')),
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
)
"""
)
conn.execute(
"ALTER TABLE transcripts DROP CONSTRAINT IF EXISTS transcripts_uniqueid_key"
)
# Older deployments may still have a standalone unique index.
conn.execute("DROP INDEX IF EXISTS transcripts_uniqueid_key")
conn.execute(
"CREATE INDEX IF NOT EXISTS transcripts_uniqueid_idx ON transcripts (uniqueid)"
)
conn.execute(
f"""
CREATE TABLE IF NOT EXISTS transcript_chunks (
id BIGSERIAL PRIMARY KEY,
transcript_id BIGINT NOT NULL REFERENCES transcripts(id) ON DELETE CASCADE,
chunk_index INTEGER NOT NULL,
content TEXT NOT NULL,
embedding vector({_EMBEDDING_DIM}) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
UNIQUE (transcript_id, chunk_index)
)
"""
)
conn.execute(
"CREATE INDEX IF NOT EXISTS transcript_chunks_transcript_id_idx ON transcript_chunks (transcript_id)"
)
# Commit the core schema changes explicitly for clarity.
conn.commit()
# Idempotent migration: add participant tracking columns for
# existing databases created before these fields existed.
for col in ("linkedid", "src_number", "dst_number"):
col_exists = conn.execute(
"""
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'transcripts'
AND column_name = %s
""",
(col,),
).fetchone()
if col_exists is None:
conn.execute(f"ALTER TABLE transcripts ADD COLUMN {col} TEXT NULL")
conn.commit()
# Idempotent migration: per-segment conversation duration (seconds),
# used by the UI to show transfer sub-legs with a real duration.
duration_exists = conn.execute(
"""
SELECT 1
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'transcripts'
AND column_name = 'duration_seconds'
""",
).fetchone()
if duration_exists is None:
conn.execute("ALTER TABLE transcripts ADD COLUMN duration_seconds INTEGER NULL")
conn.commit()
# "Modern" pgvector index: HNSW (if supported by server pgvector version)
try:
# Run this in its own transaction so a failure doesn't leave the
# connection in an aborted transaction state.
with conn.transaction():
conn.execute(
"""
CREATE INDEX IF NOT EXISTS transcript_chunks_embedding_hnsw
ON transcript_chunks
USING hnsw (embedding vector_cosine_ops)
WITH (m = 16, ef_construction = 64)
"""
)
except Exception:
logger.warning("HNSW index creation failed; pgvector may be too old", exc_info=True)
_schema_initialized = True
_uniqueid_re = re.compile(r"^\d+\.\d+$")
def validate_uniqueid(uniqueid: str) -> None:
if not uniqueid or not uniqueid.strip():
raise ValueError("Missing required form field 'uniqueid'")
if not _uniqueid_re.match(uniqueid.strip()):
raise ValueError("Invalid 'uniqueid' format; expected like 1234567890.1234")
def validate_transcript_state(state: str) -> None:
if state not in TRANSCRIPT_STATES:
raise ValueError(f"Invalid transcript state {state!r}; expected one of {', '.join(TRANSCRIPT_STATES)}")
def create_transcript_progress(
*,
uniqueid: str,
linkedid: Optional[str] = None,
src_number: Optional[str] = None,
dst_number: Optional[str] = None,
duration_seconds: Optional[int] = None,
) -> int:
"""Create a transcript row in 'progress' state. Returns transcript id.
This is used to represent a requested transcription before the Deepgram request
completes, even when multiple transcript rows share the same uniqueid.
"""
validate_uniqueid(uniqueid)
_ensure_schema()
with _connect() as conn:
row = conn.execute(
"""
INSERT INTO transcripts (uniqueid, linkedid, src_number, dst_number, duration_seconds, raw_transcription, state)
VALUES (%s, %s, %s, %s, %s, %s, 'progress')
RETURNING id
""",
(uniqueid, linkedid, src_number, dst_number, duration_seconds, ""),
).fetchone()
if row is None:
raise RuntimeError("Failed to upsert transcript progress row")
return int(row[0])
def set_transcript_state(*, transcript_id: int, state: str) -> None:
validate_transcript_state(state)
_ensure_schema()
with _connect() as conn:
conn.execute(
"""
UPDATE transcripts
SET state = %s,
updated_at = now()
WHERE id = %s
""",
(state, transcript_id),
)
def set_transcript_state_by_uniqueid(*, uniqueid: str, state: str) -> None:
validate_uniqueid(uniqueid)
validate_transcript_state(state)
_ensure_schema()
with _connect() as conn:
conn.execute(
"""
WITH latest_transcript AS (
SELECT id
FROM transcripts
WHERE uniqueid = %s
ORDER BY id DESC
LIMIT 1
)
UPDATE transcripts
SET state = %s,
updated_at = now()
WHERE id IN (SELECT id FROM latest_transcript)
""",
(uniqueid, state),
)
def upsert_transcript_raw(
*,
transcript_id: Optional[int] = None,
uniqueid: str,
linkedid: Optional[str] = None,
src_number: Optional[str] = None,
dst_number: Optional[str] = None,
duration_seconds: Optional[int] = None,
raw_transcription: str,
) -> int:
"""Persist the raw transcript row and return its transcript id."""
validate_uniqueid(uniqueid)
_ensure_schema()
with _connect() as conn:
if transcript_id is None:
row = conn.execute(
"""
INSERT INTO transcripts (uniqueid, linkedid, src_number, dst_number, duration_seconds, raw_transcription)
VALUES (%s, %s, %s, %s, %s, %s)
RETURNING id
""",
(uniqueid, linkedid, src_number, dst_number, duration_seconds, raw_transcription),
).fetchone()
else:
row = conn.execute(
"""
UPDATE transcripts
SET linkedid = COALESCE(%s, linkedid),
src_number = COALESCE(%s, src_number),
dst_number = COALESCE(%s, dst_number),
duration_seconds = COALESCE(%s, duration_seconds),
raw_transcription = %s,
updated_at = now()
WHERE id = %s
AND uniqueid = %s
RETURNING id
""",
(linkedid, src_number, dst_number, duration_seconds, raw_transcription, transcript_id, uniqueid),
).fetchone()
if row is None:
raise RuntimeError("Failed to upsert transcript")
return int(row[0])
def update_transcript_ai_fields(
*,
transcript_id: int,
cleaned_transcription: str,
summary: str,
sentiment: Optional[int],
) -> None:
_ensure_schema()
with _connect() as conn:
conn.execute(
"""
UPDATE transcripts
SET cleaned_transcription = %s,
summary = %s,
sentiment = %s,
updated_at = now()
WHERE id = %s
""",
(cleaned_transcription, summary, sentiment, transcript_id),
)
def _split_text_for_embedding(text: str) -> List[str]:
splitter = RecursiveCharacterTextSplitter(
chunk_size=2000,
chunk_overlap=200,
separators=["\n\n", "\n", ". ", "? ", "! ", " ", ""],
)
chunks = [c.strip() for c in splitter.split_text(text or "")]
return [c for c in chunks if c]
def replace_transcript_embeddings(
*,
transcript_id: int,
raw_transcription: str,
uniqueid: Optional[str] = None,
) -> int:
"""Replace all chunk embeddings for transcript_id. Returns number of chunks stored."""
_ensure_schema()
if uniqueid is not None:
validate_uniqueid(uniqueid)
chunks = _split_text_for_embedding(raw_transcription)
if not chunks:
return 0
embedder = OpenAIEmbeddings(model=_EMBEDDING_MODEL)
try:
vectors = embedder.embed_documents(chunks)
except Exception as error:
metrics.record_call("openai", "embedding", ok=False, error=error)
raise
metrics.record_call("openai", "embedding", ok=True)
with _connect() as conn:
conn.execute("DELETE FROM transcript_chunks WHERE transcript_id = %s", (transcript_id,))
for idx, (chunk, vector) in enumerate(zip(chunks, vectors)):
conn.execute(
"""
INSERT INTO transcript_chunks (transcript_id, chunk_index, content, embedding)
VALUES (%s, %s, %s, %s)
""",
(transcript_id, idx, chunk, vector),
)
return len(chunks)