File size: 2,243 Bytes
98346d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Annotated, TypedDict, List #Better code writing for readabality 
from langgraph.graph import StateGraph, END
from langgraph.graph.message import add_messages
from langchain_core.messages import HumanMessage, AIMessage, SystemMessage
from src.utils.llm import get_llm



# === STATE DEFINITION ===

class ChatState(TypedDict):
    messages : Annotated[list, add_messages]



 
# === SYSTEM PROMPT ===

SYSTEM_PROMPT = SystemMessage(content="""You are a helpful assistant.

You provide clear, accurate, and concise responses.
If you don't know something, you say so honestly.
You maintain context throughout the conversation.""")

# === NODES ===
def chat_node(state: ChatState) -> ChatState:
    # Get the last message from the state
    last_message = state["messages"][-1]

    # Get the LLM
    llm = get_llm()

    # Generate a response
    response = llm.invoke([SYSTEM_PROMPT] + state["messages"])

    # Add the response to the state
    state["messages"].append(response)

    return state        


# === BUILD GRAPH ===
def build_chat_graph():
    graph = StateGraph(ChatState)
    graph.add_node("chat", chat_node)
    graph.set_entry_point("chat")
    graph.add_edge("chat", END)
    return graph.compile()

# === CHAT SESSION ===
class ChatSession:
    """Manages a multi-turn chat conversation."""

    def __init__(self):
        self.graph = build_chat_graph()
        self.history: List = []

    def chat(self, user_message: str) -> str:
        """
        Send a message and get a response.

        Args:
            user_message: User input string

        Returns:
            AI response string
        """
        if not user_message.strip():
            return "⚠️ Please enter a message."
        # Add user message to history
        self.history.append(HumanMessage(content=user_message))

        # Run graph
        result = self.graph.invoke({"messages": self.history})

        # Extract and store response
        ai_message = result["messages"][-1]
        self.history.append(ai_message)


        return ai_message.content    
    
    def clear_history(self):
        """Clears conversation history."""
        self.history = []
        return "✅ Conversation cleared."