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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
import json
import os
from enum import Enum
from typing import Any, Dict, List, Optional, TypedDict

import pandas as pd
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import END, START, StateGraph
from langgraph.prebuilt import ToolNode
from pydantic import BaseModel, Field


# Enums for query types
class QueryType(str, Enum):
    STRUCTURED = "structured"
    UNSTRUCTURED = "unstructured"
    OUT_OF_SCOPE = "out_of_scope"
    RECOMMEND_QUERY = "recommend_query"


class AnalysisType(str, Enum):
    QUANTITATIVE = "quantitative"
    QUALITATIVE = "qualitative"
    OUT_OF_SCOPE = "out_of_scope"


# State definition
class AgentState(TypedDict):
    messages: List[Any]
    query_type: Optional[str]
    analysis_result: Optional[Dict[str, Any]]
    user_profile: Optional[Dict[str, Any]]
    session_context: Optional[Dict[str, Any]]
    recommendations: Optional[List[str]]


# User profile model
class UserProfile(BaseModel):
    interests: List[str] = Field(default_factory=list)
    query_history: List[str] = Field(default_factory=list)
    preferences: Dict[str, Any] = Field(default_factory=dict)
    expertise_level: str = "beginner"


# Dataset management
class DatasetManager:
    _instance = None
    _df = None

    def __new__(cls):
        if cls._instance is None:
            cls._instance = super(DatasetManager, cls).__new__(cls)
        return cls._instance

    def get_dataset(self) -> pd.DataFrame:
        if self._df is None:
            from datasets import load_dataset

            dataset = load_dataset(
                "bitext/Bitext-customer-support-llm-chatbot-training-dataset"
            )
            self._df = pd.DataFrame(dataset["train"])
        return self._df


# Tools for structured queries (quantitative analysis)
@tool
def get_category_distribution() -> Dict[str, int]:
    """Get the distribution of categories in the dataset."""
    df = DatasetManager().get_dataset()
    return df["category"].value_counts().to_dict()


@tool
def get_intent_distribution() -> Dict[str, int]:
    """Get the distribution of intents in the dataset."""
    df = DatasetManager().get_dataset()
    return df["intent"].value_counts().to_dict()


@tool
def get_dataset_stats() -> Dict[str, Any]:
    """Get basic statistics about the dataset."""
    df = DatasetManager().get_dataset()
    return {
        "total_records": len(df),
        "unique_categories": len(df["category"].unique()),
        "unique_intents": len(df["intent"].unique()),
        "columns": df.columns.tolist(),
    }


@tool
def get_examples_by_category(category: str, n: int = 5) -> List[Dict[str, Any]]:
    """Get examples from a specific category."""
    df = DatasetManager().get_dataset()
    filtered_df = df[df["category"].str.lower() == category.lower()]
    if filtered_df.empty:
        return []
    return filtered_df.head(n).to_dict("records")


@tool
def get_examples_by_intent(intent: str, n: int = 5) -> List[Dict[str, Any]]:
    """Get examples from a specific intent."""
    df = DatasetManager().get_dataset()
    filtered_df = df[df["intent"].str.lower() == intent.lower()]
    if filtered_df.empty:
        return []
    return filtered_df.head(n).to_dict("records")


@tool
def search_conversations(query: str, n: int = 5) -> List[Dict[str, Any]]:
    """Search for conversations containing specific keywords."""
    df = DatasetManager().get_dataset()
    mask = df["customer"].str.contains(query, case=False, na=False) | df[
        "agent"
    ].str.contains(query, case=False, na=False)
    filtered_df = df[mask]
    return filtered_df.head(n).to_dict("records")


# Tools for unstructured queries (qualitative analysis)
@tool
def get_category_summary(category: str) -> Dict[str, Any]:
    """Get a summary of conversations in a specific category."""
    df = DatasetManager().get_dataset()
    filtered_df = df[df["category"].str.lower() == category.lower()]
    if filtered_df.empty:
        return {"error": f"No data found for category: {category}"}

    return {
        "category": category,
        "count": len(filtered_df),
        "unique_intents": filtered_df["intent"].nunique(),
        "intents": filtered_df["intent"].value_counts().to_dict(),
        "sample_conversations": filtered_df.head(3).to_dict("records"),
    }


@tool
def get_intent_summary(intent: str) -> Dict[str, Any]:
    """Get a summary of conversations for a specific intent."""
    df = DatasetManager().get_dataset()
    filtered_df = df[df["intent"].str.lower() == intent.lower()]
    if filtered_df.empty:
        return {"error": f"No data found for intent: {intent}"}

    return {
        "intent": intent,
        "count": len(filtered_df),
        "categories": filtered_df["category"].value_counts().to_dict(),
        "sample_conversations": filtered_df.head(3).to_dict("records"),
    }


# Memory tools
@tool
def update_user_profile(
    interests: List[str], preferences: Dict[str, Any], expertise_level: str = "beginner"
) -> Dict[str, Any]:
    """Update the user's profile with new information."""
    return {
        "interests": interests,
        "preferences": preferences,
        "expertise_level": expertise_level,
        "updated": True,
    }


# Define tool lists for different agents
structured_tools = [
    get_category_distribution,
    get_intent_distribution,
    get_dataset_stats,
    get_examples_by_category,
    get_examples_by_intent,
    search_conversations,
]

unstructured_tools = [
    get_category_summary,
    get_intent_summary,
    search_conversations,
    get_examples_by_category,
    get_examples_by_intent,
]

memory_tools = [update_user_profile]


class DataAnalystAgent:
    def __init__(self, api_key: str, model_name: str = None):
        # Determine if using Nebius or OpenAI based on API key source
        is_nebius = os.environ.get("NEBIUS_API_KEY") == api_key

        if is_nebius:
            # Configure for Nebius API
            self.llm = ChatOpenAI(
                api_key=api_key,
                model=model_name or "Qwen/Qwen3-30B-A3B",
                base_url="https://api.studio.nebius.com/v1",
                temperature=0,
            )
        else:
            # Configure for OpenAI API
            self.llm = ChatOpenAI(
                api_key=api_key, model=model_name or "gpt-4o", temperature=0
            )

        self.memory = MemorySaver()
        self.graph = self._build_graph()

    def _build_graph(self) -> StateGraph:
        """Build the LangGraph workflow."""
        builder = StateGraph(AgentState)

        # Add nodes
        builder.add_node("classifier", self._classify_query)
        builder.add_node("structured_agent", self._structured_agent)
        builder.add_node("unstructured_agent", self._unstructured_agent)
        builder.add_node("structured_tools", ToolNode(structured_tools))
        builder.add_node("unstructured_tools", ToolNode(unstructured_tools))
        builder.add_node("summarizer", self._update_summary)
        builder.add_node("recommender", self._recommend_queries)
        builder.add_node("out_of_scope", self._handle_out_of_scope)

        # Add edges
        builder.add_edge(START, "classifier")

        # Conditional edges from classifier
        builder.add_conditional_edges(
            "classifier",
            self._route_query,
            {
                "structured": "structured_agent",
                "unstructured": "unstructured_agent",
                "out_of_scope": "out_of_scope",
                "recommend_query": "recommender",
            },
        )

        # Tool routing for structured agent
        builder.add_conditional_edges(
            "structured_agent",
            self._should_use_tools,
            {"tools": "structured_tools", "end": "summarizer"},
        )

        # Tool routing for unstructured agent
        builder.add_conditional_edges(
            "unstructured_agent",
            self._should_use_tools,
            {"tools": "unstructured_tools", "end": "summarizer"},
        )

        # From tools back to respective agents
        builder.add_edge("structured_tools", "structured_agent")
        builder.add_edge("unstructured_tools", "unstructured_agent")

        # End edges
        builder.add_edge("summarizer", END)
        builder.add_edge("out_of_scope", END)
        builder.add_edge("recommender", END)

        return builder.compile(checkpointer=self.memory)

    def _classify_query(self, state: AgentState) -> AgentState:
        """Classify the user query into different types."""
        last_message = state["messages"][-1]
        user_query = last_message.content.lower()

        # Simple keyword-based classification for better reliability
        # Check for recommendation requests first
        if any(
            word in user_query
            for word in [
                "what should i",
                "what to query",
                "recommend",
                "suggest",
                "advise",
                "what next",
                "what can i ask",
            ]
        ):
            query_type = "recommend_query"

        # Check for out of scope queries
        elif any(
            word in user_query
            for word in [
                "weather",
                "news",
                "sports",
                "politics",
                "cooking",
                "travel",
                "music",
                "movies",
                "games",
                "programming",
                "code",
            ]
        ) and not any(
            word in user_query
            for word in ["category", "intent", "customer", "support", "data", "records"]
        ):
            query_type = "out_of_scope"

        # Check for unstructured/qualitative queries
        elif any(
            word in user_query
            for word in [
                "summarize",
                "summary",
                "patterns",
                "insights",
                "analysis",
                "analyze",
                "themes",
                "trends",
                "what patterns",
                "understand",
            ]
        ):
            query_type = "unstructured"

        # Default to structured for data-related queries
        else:
            query_type = "structured"

        # Double-check with LLM for edge cases, but use simpler prompt
        if query_type == "out_of_scope":
            simple_prompt = f"""
            Is this question about customer support data analysis? 
            Question: "{last_message.content}"
            
            Answer only "yes" or "no".
            """

            try:
                response = self.llm.invoke([HumanMessage(content=simple_prompt)])
                if "yes" in response.content.lower():
                    query_type = "structured"  # Override if actually about data
            except Exception:
                pass  # Keep original classification if LLM fails

        state["query_type"] = query_type
        return state

    def _route_query(self, state: AgentState) -> str:
        """Route to appropriate agent based on classification."""
        return state["query_type"]

    def _structured_agent(self, state: AgentState) -> AgentState:
        """Handle structured/quantitative queries."""

        system_prompt = """
        You are a data analyst that MUST use tools to answer questions about 
        customer support data. You have access to these tools:
        
        - get_category_distribution: Get category counts
        - get_intent_distribution: Get intent counts  
        - get_dataset_stats: Get basic dataset statistics
        - get_examples_by_category: Get examples from a category
        - get_examples_by_intent: Get examples from an intent
        - search_conversations: Search for conversations with keywords
        
        IMPORTANT: Always use the appropriate tool to get real data. 
        Do NOT make up or guess answers. Use tools to get actual numbers.
        
        For questions about:
        - "How many categories" or "category distribution" β†’ use get_category_distribution
        - "How many intents" or "intent distribution" β†’ use get_intent_distribution  
        - "Total records" or "dataset size" β†’ use get_dataset_stats
        - "Examples of X" β†’ use get_examples_by_category or get_examples_by_intent
        - "Search for X" β†’ use search_conversations
        """

        llm_with_tools = self.llm.bind_tools(structured_tools)
        messages = [SystemMessage(content=system_prompt)] + state["messages"]
        response = llm_with_tools.invoke(messages)

        state["messages"].append(response)
        return state

    def _unstructured_agent(self, state: AgentState) -> AgentState:
        """Handle unstructured/qualitative queries."""

        system_prompt = """
        You are a data analyst that MUST use tools to provide insights about 
        customer support data. You have access to these tools:
        
        - get_category_summary: Get detailed summary of a category
        - get_intent_summary: Get detailed summary of an intent
        - search_conversations: Search conversations for patterns
        - get_examples_by_category: Get examples to analyze patterns
        - get_examples_by_intent: Get examples to analyze patterns
        
        IMPORTANT: Always use the appropriate tool to get real data.
        Do NOT make up or guess insights. Use tools to get actual data first.
        
        For questions about:
        - "Summarize X category" β†’ use get_category_summary
        - "Analyze X intent" β†’ use get_intent_summary
        - "Patterns in X" β†’ use get_examples_by_category or search_conversations
        """

        llm_with_tools = self.llm.bind_tools(unstructured_tools)
        messages = [SystemMessage(content=system_prompt)] + state["messages"]
        response = llm_with_tools.invoke(messages)

        state["messages"].append(response)
        return state

    def _should_use_tools(self, state: AgentState) -> str:
        """Determine if the agent should use tools or end."""
        last_message = state["messages"][-1]

        # Check if LLM made tool calls
        if hasattr(last_message, "tool_calls") and last_message.tool_calls:
            return "tools"

        # If no tool calls but this is the first response from agent,
        # force tool usage for data questions
        messages = state["messages"]
        human_messages = [msg for msg in messages if isinstance(msg, HumanMessage)]

        if len(human_messages) >= 1:
            last_human_msg = human_messages[-1].content.lower()

            # Check if this looks like a data question that needs tools
            needs_tools = any(
                word in last_human_msg
                for word in [
                    "how many",
                    "show me",
                    "examples",
                    "distribution",
                    "categories",
                    "intents",
                    "records",
                    "statistics",
                    "stats",
                    "count",
                    "total",
                    "billing",
                    "refund",
                    "payment",
                    "technical",
                    "support",
                ]
            )

            # Count AI messages - if this is first AI response and needs tools, force it
            ai_messages = [msg for msg in messages if not isinstance(msg, HumanMessage)]
            if needs_tools and len(ai_messages) <= 1:
                return "tools"

        return "end"

    def _update_summary(self, state: AgentState) -> AgentState:
        """Update user profile/summary based on the interaction."""
        user_profile = state.get("user_profile", {})
        last_human_message = None

        # Find the last human message
        for msg in reversed(state["messages"]):
            if isinstance(msg, HumanMessage):
                last_human_message = msg
                break

        if last_human_message:
            # Extract information about user interests
            system_prompt = """
            Based on the user's question, extract information about their 
            interests and update their profile. Consider:
            - What categories/intents they're interested in
            - Their level of technical detail preference
            - Types of analysis they prefer
            
            Return a JSON with:
            {
                "interests": ["list of topics they seem interested in"],
                "preferences": {"any preferences about analysis style"},
                "expertise_level": "beginner/intermediate/advanced"
            }
            
            If no clear information can be extracted, return empty lists/dicts.
            """

            messages = [
                SystemMessage(content=system_prompt),
                HumanMessage(content=f"User question: {last_human_message.content}"),
            ]

            try:
                response = self.llm.invoke(messages)
                profile_update = json.loads(response.content)

                # Merge with existing profile
                if not user_profile:
                    user_profile = {
                        "interests": [],
                        "preferences": {},
                        "expertise_level": "beginner",
                        "query_history": [],
                    }

                # Update interests (avoid duplicates)
                new_interests = profile_update.get("interests", [])
                existing_interests = user_profile.get("interests", [])
                user_profile["interests"] = list(
                    set(existing_interests + new_interests)
                )

                # Update preferences
                user_profile["preferences"].update(
                    profile_update.get("preferences", {})
                )

                # Update expertise level if provided
                if profile_update.get("expertise_level"):
                    user_profile["expertise_level"] = profile_update["expertise_level"]

                # Add to query history
                if "query_history" not in user_profile:
                    user_profile["query_history"] = []
                user_profile["query_history"].append(last_human_message.content)

                # Keep only last 10 queries
                user_profile["query_history"] = user_profile["query_history"][-10:]

            except (json.JSONDecodeError, Exception):
                # If parsing fails, just add to query history
                if not user_profile:
                    user_profile = {"query_history": []}
                if "query_history" not in user_profile:
                    user_profile["query_history"] = []
                user_profile["query_history"].append(last_human_message.content)
                user_profile["query_history"] = user_profile["query_history"][-10:]

        state["user_profile"] = user_profile
        return state

    def _recommend_queries(self, state: AgentState) -> AgentState:
        """Recommend next queries based on conversation history and user profile."""
        user_profile = state.get("user_profile", {})
        query_history = user_profile.get("query_history", [])
        interests = user_profile.get("interests", [])

        # Get dataset info for context
        df = DatasetManager().get_dataset()
        categories = df["category"].unique().tolist()
        intents = df["intent"].unique()[:20].tolist()

        system_prompt = f"""
        You are a query recommendation assistant. Based on the user's conversation 
        history and interests, suggest relevant follow-up questions they could ask 
        about the customer support dataset.
        
        User's query history: {query_history}
        User's interests: {interests}
        
        Available categories: {categories}
        Sample intents: {intents}
        
        Suggest 3-5 relevant questions the user might want to ask next. Consider:
        - Natural follow-ups to their previous questions
        - Related categories or intents they haven't explored
        - Different types of analysis (if they've only done quantitative, 
          suggest qualitative and vice versa)
        
        Be conversational and explain why each suggestion might be interesting.
        Start with "Based on your previous queries, you might want to..."
        """

        messages = [SystemMessage(content=system_prompt)]

        # Add conversation context
        if state["messages"]:
            messages.extend(state["messages"])
        else:
            messages.append(HumanMessage(content="What should I query next?"))

        response = self.llm.invoke(messages)
        state["messages"].append(response)

        return state

    def _handle_out_of_scope(self, state: AgentState) -> AgentState:
        """Handle queries that are out of scope."""
        response = AIMessage(
            content="I'm sorry, but I can only answer questions about the customer "
            "support dataset. Please ask questions about categories, intents, "
            "conversation examples, or data statistics."
        )
        state["messages"].append(response)
        return state

    def invoke(self, message: str, thread_id: str) -> Dict[str, Any]:
        """Invoke the agent with a message and thread ID."""
        config = {"configurable": {"thread_id": thread_id}}

        # Create input state
        input_state = {"messages": [HumanMessage(content=message)]}

        # Invoke the graph
        result = self.graph.invoke(input_state, config)

        return result

    def get_conversation_history(self, thread_id: str) -> List[Dict[str, Any]]:
        """Get conversation history for a thread."""
        config = {"configurable": {"thread_id": thread_id}}

        try:
            # Get the current state
            state = self.graph.get_state(config)
            if state and state.values.get("messages"):
                return [
                    {
                        "role": (
                            "human" if isinstance(msg, HumanMessage) else "assistant"
                        ),
                        "content": msg.content,
                    }
                    for msg in state.values["messages"]
                ]
        except Exception:
            pass

        return []

    def get_user_profile(self, thread_id: str) -> Dict[str, Any]:
        """Get user profile for a thread."""
        config = {"configurable": {"thread_id": thread_id}}

        try:
            state = self.graph.get_state(config)
            if state and state.values.get("user_profile"):
                return state.values["user_profile"]
        except Exception:
            pass

        return {}