Skip to content

Commit 8bcdf67

Browse files
committed
Handle linkedin_url input and use search_vector
Accept both linkedin_url and legacy linkedinUrl in request payloads and normalize to linkedin_url. Update error message to reference linkedin_url. Query logic now checks for a search_vector column and, if present, uses full-text search (plainto_tsquery) plus COALESCE(data->>'linkedin_url', data->>'linkedinUrl') to match stored data; otherwise fall back to previous prompt ILIKE / data match. Responses now return linkedin_url in the data payload for consistency and backwards-compatible lookup.
1 parent b340236 commit 8bcdf67

1 file changed

Lines changed: 42 additions & 12 deletions

File tree

app/api/prompt/linkedin.py

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
@router.post("/prompt/linkedin")
1111
def linkedin_prompt_success(payload: dict, api_key: str = Depends(get_api_key)) -> dict:
12-
"""POST /prompt/linkedin: return cached completion for linkedinUrl when available."""
13-
linkedin_url = (payload.get("linkedinUrl") or "").strip()
12+
"""POST /prompt/linkedin: return cached completion for linkedin_url when available."""
13+
linkedin_url = (payload.get("linkedin_url") or payload.get("linkedinUrl") or "").strip()
1414
if not linkedin_url:
15-
raise HTTPException(status_code=400, detail="Missing 'linkedinUrl' in request body.")
15+
raise HTTPException(status_code=400, detail="Missing 'linkedin_url' in request body.")
1616

1717
conn = None
1818
cur = None
@@ -21,14 +21,44 @@ def linkedin_prompt_success(payload: dict, api_key: str = Depends(get_api_key))
2121
cur = conn.cursor()
2222
cur.execute(
2323
"""
24-
SELECT id, completion, time, model, data
25-
FROM prompt
26-
WHERE (data->>'linkedinUrl' = %s OR prompt ILIKE %s)
27-
ORDER BY id DESC
28-
LIMIT 1;
29-
""",
30-
(linkedin_url, f"%{linkedin_url}%"),
24+
SELECT EXISTS (
25+
SELECT 1
26+
FROM information_schema.columns
27+
WHERE table_schema = 'public'
28+
AND table_name = 'prompt'
29+
AND column_name = 'search_vector'
30+
);
31+
"""
3132
)
33+
exists_row = cur.fetchone()
34+
has_search_vector = bool(exists_row and exists_row[0])
35+
36+
if has_search_vector:
37+
cur.execute(
38+
"""
39+
SELECT id, completion, time, model, data
40+
FROM prompt
41+
WHERE (
42+
COALESCE(data->>'linkedin_url', data->>'linkedinUrl') = %s
43+
OR search_vector @@ plainto_tsquery('english', %s)
44+
OR prompt ILIKE %s
45+
)
46+
ORDER BY id DESC
47+
LIMIT 1;
48+
""",
49+
(linkedin_url, linkedin_url, f"%{linkedin_url}%"),
50+
)
51+
else:
52+
cur.execute(
53+
"""
54+
SELECT id, completion, time, model, data
55+
FROM prompt
56+
WHERE (COALESCE(data->>'linkedin_url', data->>'linkedinUrl') = %s OR prompt ILIKE %s)
57+
ORDER BY id DESC
58+
LIMIT 1;
59+
""",
60+
(linkedin_url, f"%{linkedin_url}%"),
61+
)
3262
row = cur.fetchone()
3363

3464
if row:
@@ -37,7 +67,7 @@ def linkedin_prompt_success(payload: dict, api_key: str = Depends(get_api_key))
3767
"data": {
3868
"cached": True,
3969
"id": row[0],
40-
"linkedinUrl": linkedin_url,
70+
"linkedin_url": linkedin_url,
4171
"completion": row[1],
4272
"time": row[2].isoformat() if row[2] else None,
4373
"model": row[3],
@@ -49,7 +79,7 @@ def linkedin_prompt_success(payload: dict, api_key: str = Depends(get_api_key))
4979
"meta": make_meta("warning", "LinkedIn URL not analysed yet"),
5080
"data": {
5181
"cached": False,
52-
"linkedinUrl": linkedin_url,
82+
"linkedin_url": linkedin_url,
5383
"completion": None,
5484
},
5585
}

0 commit comments

Comments
 (0)