| | 32 | Инсталација на потребните библиотеки |
| | 33 | {{{ |
| | 34 | pip install sentence-transformers psycopg2-binary python-dotenv |
| | 35 | }}} |
| | 36 | Во .env фајлот ги пополнуваме HOST, PORT, NAME, USER, PASSWORD. |
| | 37 | |
| | 38 | Креираме python скрипта generate_embeddings.py |
| | 39 | {{{ |
| | 40 | import psycopg2 |
| | 41 | from sentence_transformers import SentenceTransformer |
| | 42 | from dotenv import load_dotenv |
| | 43 | import os |
| | 44 | |
| | 45 | load_dotenv() |
| | 46 | |
| | 47 | conn = psycopg2.connect( |
| | 48 | host=os.getenv("DB_HOST"), |
| | 49 | port=os.getenv("DB_PORT"), |
| | 50 | database=os.getenv("DB_NAME"), |
| | 51 | user=os.getenv("DB_USER"), |
| | 52 | password=os.getenv("DB_PASSWORD") |
| | 53 | ) |
| | 54 | cursor = conn.cursor() |
| | 55 | |
| | 56 | model = SentenceTransformer('all-MiniLM-L6-v2') |
| | 57 | |
| | 58 | # Embeddings za Activity |
| | 59 | print("Generating embeddings for activities...") |
| | 60 | cursor.execute("SELECT activity_id, description FROM Activity WHERE description IS NOT NULL") |
| | 61 | activities = cursor.fetchall() |
| | 62 | |
| | 63 | for activity_id, description in activities: |
| | 64 | embedding = model.encode(description).tolist() |
| | 65 | cursor.execute( |
| | 66 | "UPDATE Activity SET embedding = %s WHERE activity_id = %s", |
| | 67 | (embedding, activity_id) |
| | 68 | ) |
| | 69 | |
| | 70 | conn.commit() |
| | 71 | print(f"Done! {len(activities)} activities executed.") |
| | 72 | |
| | 73 | # Embeddings za Service |
| | 74 | print("Generating embeddings for services...") |
| | 75 | cursor.execute("SELECT serviceId, description FROM Service WHERE description IS NOT NULL") |
| | 76 | services = cursor.fetchall() |
| | 77 | |
| | 78 | for service_id, description in services: |
| | 79 | embedding = model.encode(description).tolist() |
| | 80 | cursor.execute( |
| | 81 | "UPDATE Service SET embedding = %s WHERE serviceId = %s", |
| | 82 | (embedding, service_id) |
| | 83 | ) |
| | 84 | |
| | 85 | conn.commit() |
| | 86 | print(f"Done! {len(services)} services executed.") |
| | 87 | |
| | 88 | # Embeddings za Equipment |
| | 89 | print("Generating embeddings for Equipment...") |
| | 90 | cursor.execute("SELECT equipmentId, description FROM Equipment WHERE description IS NOT NULL") |
| | 91 | equipments = cursor.fetchall() |
| | 92 | |
| | 93 | for equipment_id, description in equipments: |
| | 94 | embedding = model.encode(description).tolist() |
| | 95 | cursor.execute( |
| | 96 | "UPDATE Equipment SET embedding = %s WHERE equipmentId = %s", |
| | 97 | (embedding, equipment_id) |
| | 98 | ) |
| | 99 | |
| | 100 | conn.commit() |
| | 101 | print(f"Done! {len(equipments)} equipments executed.") |
| | 102 | |
| | 103 | cursor.close() |
| | 104 | conn.close() |
| | 105 | |
| | 106 | }}} |