File size: 6,203 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Enhanced crew.py
from crewai import Agent, Crew, Process, Task, LLM
from crewai.project import CrewBase, agent, crew, task
import os

# Set your OpenAI API key
os.environ[
    "OPENAI_API_KEY"] = "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"

# Get the project root directory
project_root = os.path.dirname(os.path.abspath(__file__))

# Construct the relative path to tickets.db
db_path = os.path.join(project_root, "tickets.db")

# Use db_path wherever the database path is required
print(f"Database Path: {db_path}")

# Use OpenAI for testing
llm = LLM(model="gpt-3.5-turbo")

# Import all the enhanced tools
from sql_query_tool import (
    ticket_query_tool,
    status_query_tool,
    severity_query_tool,
    tag_query_tool,
    keyword_search_tool,
    advanced_ticket_search
)


@CrewBase
class EnhancedTicketSupportCrew():
    """Enhanced crew with comprehensive ticket search capabilities"""

    @agent
    def smart_support_agent(self) -> Agent:
        return Agent(
            role="Smart IT Support Agent",
            goal="""

            Analyze user queries and intelligently choose the best tool to find ticket information.



            Tool Selection Rules:

            1. For specific ticket numbers (TKT-XXXX): use ticket_query_tool

            2. For status queries (open, closed, in progress): use status_query_tool  

            3. For severity queries (low, medium, high, critical): use severity_query_tool

            4. For tag-based queries (backend, API, frontend): use tag_query_tool

            5. For keyword/symptom searches: use keyword_search_tool

            6. For complex queries with multiple criteria: use advanced_ticket_search



            Always provide comprehensive, well-formatted responses with:

            - Clear bullet points for multiple tickets

            - All relevant ticket details

            - Professional formatting

            """,
            backstory="""

            You are an intelligent IT support agent with deep knowledge of ticketing systems.

            You can automatically determine the best search approach based on user queries and

            provide comprehensive ticket information. You understand different query types and

            can extract relevant search criteria from natural language requests.

            """,
            llm=llm,
            tools=[
                ticket_query_tool,
                status_query_tool,
                severity_query_tool,
                tag_query_tool,
                keyword_search_tool,
                advanced_ticket_search
            ],
            verbose=True,
            max_iter=3
        )


    @task
    def enhanced_support_task(self) -> Task:
        return Task(
            description="""

            Process the user query: {query}



            Analysis Steps:

            1. Parse the query to identify search criteria:

               - Ticket numbers (TKT-XXXX format)

               - Status keywords (open, closed, in progress, resolved)

               - Severity levels (low, medium, high, critical)

               - Tags (backend, API, frontend, database, etc.)

               - Keywords/symptoms for description search

               - Assignee names



            2. Choose the most appropriate tool:

               - ticket_query_tool: For specific ticket numbers

               - status_query_tool: For status-based searches

               - severity_query_tool: For severity-based searches

               - tag_query_tool: For tag-based searches

               - keyword_search_tool: For keyword/symptom searches

               - advanced_ticket_search: For complex multi-criteria searches



            3. Format the response professionally:

               - Use bullet points for multiple tickets

               - Include all relevant details (status, severity, assignee, tags, etc.)

               - Provide clear, actionable information

               - Show ticket descriptions appropriately truncated



            4. Handle edge cases:

               - No results found

               - Database errors

               - Ambiguous queries

            """,
            expected_output="""

            Professional response with:

            - Clear formatting with bullet points for multiple results

            - Complete ticket information including:

              * Ticket number

              * Status and severity

              * Description (truncated if long)

              * Assignee and reporter

              * Tags and dates

              * Resolution information (if closed)

              Please provide a comprehensive response that includes:



                1. **Executive Summary**: Clear, concise answer to the user's question

            

                2. **Key Findings**: Most important insights from the data

            

                3. **Metrics & Statistics**: Relevant numbers, trends, and patterns

            

                4. **Recommendations**: Actionable suggestions based on the analysis

            

                5. **Risk Assessment**: Any concerns or potential issues identified

            

                6. **Next Steps**: Recommended actions for the user

            

                Format your response professionally and make it easy to understand. If the crew data contains errors or is incomplete, acknowledge this and provide what insights you can.

            

                Focus on being helpful, accurate, and actionable in your response.

            - Helpful context about the search results

            - User-friendly presentation

            """,
            agent=self.smart_support_agent()
        )

    @crew
    def crew(self) -> Crew:
        return Crew(
            agents=[self.smart_support_agent()],
            tasks=[self.enhanced_support_task()],
            process=Process.sequential,
            verbose=True,
            memory=True  # Enable memory for better context
        )