AdvancedTopics: embed_task_requests_description.py

File embed_task_requests_description.py, 1.5 KB (added by 231141, 28 hours ago)
Line 
1from sentence_transformers import SentenceTransformer
2import psycopg2
3
4model = SentenceTransformer("all-MiniLM-L6-v2")
5
6conn = psycopg2.connect(
7 host="localhost",
8 database="tcs_local",
9 user="postgres",
10 password="postgres123"
11)
12
13read_cursor = conn.cursor()
14write_cursor = conn.cursor()
15
16try:
17 read_cursor.execute("""
18 SELECT
19 tr.id,
20 tr.description
21 FROM
22 TaskRequest tr
23
24 LEFT JOIN
25 task_request_description_vector trdv
26 ON trdv.task_request_id = tr.id
27
28 WHERE
29 tr.status = 'OPEN'
30 AND tr.description IS NOT NULL
31 AND trdv.task_request_id IS NULL
32
33 ORDER BY
34 tr.id
35 """)
36
37
38 for task_request_id, description in read_cursor.fetchall():
39
40 embedding = model.encode(
41 description,
42 convert_to_numpy=True,
43 normalize_embeddings=True
44 ).tolist()
45
46 write_cursor.execute("""
47 INSERT INTO task_request_description_vector
48 (
49 task_request_id,
50 embedding
51 )
52 VALUES
53 (%s, %s)
54
55 ON CONFLICT (task_request_id)
56 DO UPDATE SET
57 embedding = EXCLUDED.embedding
58 """, (task_request_id, embedding))
59
60 conn.commit()
61
62except Exception as e:
63 conn.rollback()
64 print(e)
65
66finally:
67 read_cursor.close()
68 write_cursor.close()
69 conn.close()