| 1 | from sentence_transformers import SentenceTransformer
|
|---|
| 2 | import psycopg2
|
|---|
| 3 |
|
|---|
| 4 | model = SentenceTransformer("all-MiniLM-L6-v2")
|
|---|
| 5 |
|
|---|
| 6 | conn = psycopg2.connect(
|
|---|
| 7 | host="localhost",
|
|---|
| 8 | database="tcs_local",
|
|---|
| 9 | user="postgres",
|
|---|
| 10 | password="postgres123"
|
|---|
| 11 | )
|
|---|
| 12 |
|
|---|
| 13 | read_cursor = conn.cursor()
|
|---|
| 14 | write_cursor = conn.cursor()
|
|---|
| 15 |
|
|---|
| 16 | try:
|
|---|
| 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 |
|
|---|
| 62 | except Exception as e:
|
|---|
| 63 | conn.rollback()
|
|---|
| 64 | print(e)
|
|---|
| 65 |
|
|---|
| 66 | finally:
|
|---|
| 67 | read_cursor.close()
|
|---|
| 68 | write_cursor.close()
|
|---|
| 69 | conn.close() |
|---|