File size: 3,149 Bytes
b9b4639
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import sqlite3
import json
from datetime import datetime

# Load ticket data
with open("C:/Users/anike/PycharmProjects/AI_Agents_crew/ticket_data.json") as f:
    tickets = json.load(f)

# Connect to SQLite
conn = sqlite3.connect("tickets.db")
cursor = conn.cursor()

# def get_ticket_by_number(ticket_number):
#     cursor.execute("SELECT * FROM tickets WHERE ticket_number = ?", (ticket_number,))
#     row = cursor.fetchone()
#     if row:
#         return dict(zip([desc[0] for desc in cursor.description], row))
#     return None
#
# # Example
# ticket = get_ticket_by_number("TKT-1050")
# print(ticket)


#
# # Drop if exists
# cursor.execute("DROP TABLE IF EXISTS tickets")
#
# # Create table
# cursor.execute("""
# CREATE TABLE tickets (
#     ticket_number TEXT PRIMARY KEY,
#     opened_at TEXT,
#     closed_at TEXT,
#     description TEXT,
#     resolution_comment TEXT,
#     closed_by TEXT,
#     type TEXT,
#     status TEXT,
#     severity TEXT,
#     reported_by TEXT,
#     assigned_to TEXT,
#     last_updated TEXT,
#     environment TEXT,
#     tags TEXT
# )
# """)
#
# # Insert data
# for ticket in tickets:
#     cursor.execute("""
#     INSERT INTO tickets VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
#     """, (
#         ticket["ticket_number"],
#         ticket["opened_at"],
#         ticket["closed_at"],
#         ticket["description"],
#         ticket["resolution_comment"],
#         ticket["closed_by"],
#         ticket["type"],
#         ticket["status"],
#         ticket["severity"],
#         ticket["reported_by"],
#         ticket["assigned_to"],
#         ticket["last_updated"],
#         ticket["environment"],
#         ",".join(ticket["tags"]),
#     ))
#
# conn.commit()

import requests

def extract_query_intent_with_llama(prompt: str, model_url: str = "http://localhost:11434/api/generate") -> dict:
    system_prompt = """Extract the intent from the user's query. Return a JSON object with at least one of:

            - "ticket_number" (e.g., "TKT-1050")

            - "tag" (e.g., "database", "login", "UI")

            - "keyword" (e.g., a symptom like 'rate limiter', 'timeout', etc.)

            

            Only include keys that are present or clearly implied. Do NOT add extra fields.

            Example output:

            { "ticket_number": "TKT-1050" }

            or

            { "tag": "database" }

            or

            { "keyword": "rate limiter" }

"""

    response = requests.post(
        model_url,
        json={
            "model": "llama3.2",
            "prompt": f"<|system|>\n{system_prompt}\n<|user|>\n{prompt}\n<|assistant|>",
            "stream": False
        }
    )

    try:
        raw = response.json()["response"]
        # Clean JSON if LLM adds extra text
        raw = raw.strip().split("\n")[0].strip("` ")
        return eval(raw) if raw.startswith("{") else {}
    except Exception as e:
        print(f"LLM parsing failed: {e}")
        return {}

intent = extract_query_intent_with_llama("Summarize ticket TKT-1050 ?")
print(intent)