Skip to content

Commit c15f51f

Browse files
committed
Add llm search_vector and prospect migrations
Bump version to 2.1.9 and add full-text search support to the llm table: include search_vector in SELECTs, generate it on insert using to_tsvector('english', prompt || ' ' || completion), and add a migration + runner to create the tsvector column and GIN index. Update prospects endpoint to return the llm search_vector and adjust result field mapping. Add several SQL migration files to drop unused columns from the prospects table (cleaned, primary_email_catchall_status, primary_intent_score, primary_intent_topic, qualify_contact, secondary_intent_score, secondary_intent_topic, tertiary_email, tertiary_email_source, tertiary_email_status, tertiary_email_verification_source) and include small runner scripts for removing tertiary_email and its source.
1 parent 1f04314 commit c15f51f

31 files changed

Lines changed: 221 additions & 13 deletions

app/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
"""Python - FastAPI, Postgres, tsvector"""
22

33
# Current Version
4-
__version__ = "2.1.8"
4+
__version__ = "2.1.9"

app/api/llm/llm.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def get_llm_records(
2121
if prospect_id is not None:
2222
# No pagination for single prospect_id lookup
2323
select_query = """
24-
SELECT id, prompt, completion, duration, time, data, model, prospect_id
24+
SELECT id, prompt, completion, duration, time, data, model, prospect_id, search_vector
2525
FROM llm
2626
WHERE prospect_id = %s
2727
ORDER BY id DESC
@@ -38,6 +38,7 @@ def get_llm_records(
3838
"data": row[5],
3939
"model": row[6],
4040
"prospect_id": row[7],
41+
"search_vector": str(row[8]) if row[8] is not None else None,
4142
}
4243
for row in rows
4344
]
@@ -61,7 +62,7 @@ def get_llm_records(
6162
count_row = cur.fetchone()
6263
total = count_row[0] if count_row and count_row[0] is not None else 0
6364
cur.execute("""
64-
SELECT id, prompt, completion, duration, time, data, model, prospect_id
65+
SELECT id, prompt, completion, duration, time, data, model, prospect_id, search_vector
6566
FROM llm
6667
ORDER BY id DESC
6768
LIMIT %s OFFSET %s;
@@ -76,6 +77,7 @@ def get_llm_records(
7677
"data": row[5],
7778
"model": row[6],
7879
"prospect_id": row[7],
80+
"search_vector": str(row[8]) if row[8] is not None else None,
7981
}
8082
for row in cur.fetchall()
8183
]
@@ -146,13 +148,14 @@ def llm_post(payload: dict) -> dict:
146148
data_blob = json.dumps({"version": __version__})
147149
conn = get_db_connection_direct()
148150
cur = conn.cursor()
151+
# Generate tsvector from prompt and completion
149152
cur.execute(
150153
"""
151-
INSERT INTO llm (prompt, completion, duration, data, model, prospect_id)
152-
VALUES (%s, %s, %s, %s, %s, %s)
154+
INSERT INTO llm (prompt, completion, duration, data, model, prospect_id, search_vector)
155+
VALUES (%s, %s, %s, %s, %s, %s, to_tsvector('english', %s || ' ' || %s))
153156
RETURNING id;
154157
""",
155-
(prompt, completion, duration, data_blob, used_model, prospect_id)
158+
(prompt, completion, duration, data_blob, used_model, prospect_id, prompt, completion)
156159
)
157160
record_id_row = cur.fetchone()
158161
record_id = record_id_row[0] if record_id_row else None
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- Migration: Add search_vector tsvector column to llm table
2+
ALTER TABLE llm ADD COLUMN IF NOT EXISTS search_vector tsvector;
3+
4+
-- Optional: Create a GIN index for faster search
5+
CREATE INDEX IF NOT EXISTS idx_llm_search_vector ON llm USING GIN(search_vector);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import os
2+
import sys
3+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../..')))
4+
from app.utils.db import get_db_connection_direct
5+
6+
if __name__ == "__main__":
7+
sql = """
8+
ALTER TABLE llm ADD COLUMN IF NOT EXISTS search_vector tsvector;
9+
CREATE INDEX IF NOT EXISTS idx_llm_search_vector ON llm USING GIN(search_vector);
10+
"""
11+
conn = get_db_connection_direct()
12+
cur = conn.cursor()
13+
cur.execute(sql)
14+
conn.commit()
15+
cur.close()
16+
conn.close()
17+
print("Migration complete: search_vector column and index added to llm table.")

app/api/prospects/prospects.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,15 @@ def prospects_read_one(id: int = Path(..., description="ID of the prospect to re
103103
from app.utils.db import get_db_connection_direct
104104
llm_conn = get_db_connection_direct()
105105
llm_cur = llm_conn.cursor()
106-
llm_cur.execute("SELECT id, prompt, completion, duration, time, data, model FROM llm WHERE prospect_id = %s ORDER BY id DESC;", (id,))
106+
llm_cur.execute("SELECT id, duration, time, data, model, search_vector FROM llm WHERE prospect_id = %s ORDER BY id DESC;", (id,))
107107
llm_records = [
108108
{
109109
"id": r[0],
110-
"prompt": r[1],
111-
"completion": r[2],
112-
"duration": r[3],
113-
"time": r[4].isoformat() if r[4] else None,
114-
"data": r[5],
115-
"model": r[6],
110+
"duration": r[1],
111+
"time": r[2].isoformat() if r[2] else None,
112+
"data": r[3],
113+
"model": r[4],
114+
"search_vector": str(r[5]) if r[5] is not None else None,
116115
}
117116
for r in llm_cur.fetchall()
118117
]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Migration: Remove cleaned column from prospects table
2+
ALTER TABLE prospects DROP COLUMN IF EXISTS cleaned;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Migration: Remove do_not_call column from prospects table
2+
ALTER TABLE prospects DROP COLUMN IF EXISTS do_not_call;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Migration: Remove email_confidence column from prospects table
2+
ALTER TABLE prospects DROP COLUMN IF EXISTS email_confidence;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Migration: Remove home_phone column from prospects table
2+
ALTER TABLE prospects DROP COLUMN IF EXISTS home_phone;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- Migration: Remove primary_email_catchall_status column from prospects table
2+
ALTER TABLE prospects DROP COLUMN IF EXISTS primary_email_catchall_status;

0 commit comments

Comments
 (0)