-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
136 lines (104 loc) · 4.12 KB
/
Copy pathmain.py
File metadata and controls
136 lines (104 loc) · 4.12 KB
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
from dotenv import load_dotenv
from typing import Annotated, Literal
from langgraph.graph import StateGraph, START, END
from langgraph.graph.message import add_messages
from langchain.chat_models import init_chat_model
from pydantic import BaseModel, Field
from typing_extensions import TypedDict
from mcp import ClientSession
from mcp.client.streamable_http import streamablehttp_client
load_dotenv(".env", override=True)
llm = init_chat_model(
"claude-haiku-4-5"
)
class MessageClassifier(BaseModel):
message_type: Literal["emotional", "logical"] = Field(
...,
description="Classify if the message requires an emotional (therapist) or logical response."
)
class State(TypedDict):
messages: Annotated[list, add_messages]
message_type: str | None
graph_builder = StateGraph(State)
def classify_message(state: State):
last_message = state["messages"][-1]
classifier_llm = llm.with_structured_output(MessageClassifier)
result = classifier_llm.invoke([
{
"role": "system",
"content": """Classify the user message as either:
- 'emotional': if it asks for emotional support, therapy, deals with feelings, or personal problems
- 'logical': if it asks for facts, information, logical analysis, or practical solutions
"""
},
{"role": "user", "content": last_message.content}
])
return {"message_type": result.message_type} # type: ignore
def router(state: State):
message_type = state.get("message_type", "logical")
if message_type == "emotional":
return {"next": "therapist"}
return {"next": "logical"}
def therapist_agent(state: State):
last_message = state["messages"][-1]
messages = [
{"role": "system",
"content": """You are a compassionate therapist. Focus on the emotional aspects of the user's message.
Show empathy, validate their feelings, and help them process their emotions.
Ask thoughtful questions to help them explore their feelings more deeply.
Avoid giving logical solutions unless explicitly asked."""
},
{
"role": "user",
"content": last_message.content
}
]
reply = llm.invoke(messages)
return {"messages": [{"role": "assistant", "content": reply.content}]}
def logical_agent(state: State):
last_message = state["messages"][-1]
messages = [
{"role": "system",
"content": """You are a purely logical assistant. Focus only on facts and information.
Provide clear, concise answers based on logic and evidence.
Do not address emotions or provide emotional support.
Be direct and straightforward in your responses."""
},
{
"role": "user",
"content": last_message.content
}
]
reply = llm.invoke(messages)
return {"messages": [{"role": "assistant", "content": reply.content}]}
graph_builder = StateGraph(State)
graph_builder.add_node("classifier", classify_message)
graph_builder.add_node("router", router)
graph_builder.add_node("therapist", therapist_agent)
graph_builder.add_node("logical", logical_agent)
graph_builder.add_edge(START, "classifier")
graph_builder.add_edge("classifier", "router")
graph_builder.add_conditional_edges(
"router",
lambda state: state.get("next"),
{"therapist": "therapist", "logical": "logical"}
)
graph_builder.add_edge("therapist", END)
graph_builder.add_edge("logical", END)
graph = graph_builder.compile()
def run_chatbot():
state = {"messages": [], "message_type": None}
while True:
user_input = input("Message: ")
if user_input == "exit":
print("Bye")
break
state["messages"] = state.get("messages", []) + [
{"role": "user", "content": user_input}
]
state = graph.invoke(state) # type: ignore
if state.get("messages") and len(state["messages"]) > 0:
last_message = state["messages"][-1]
print(f"Assistant: {last_message.content}")
if __name__ == "__main__":
run_chatbot()