-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraph.py
More file actions
98 lines (82 loc) · 3.23 KB
/
graph.py
File metadata and controls
98 lines (82 loc) · 3.23 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
"""Main graph definition and construction for the LangGraph agent."""
from langgraph.graph import END, START, StateGraph
from app.agents.nodes import (
agent_host,
context_builder,
fallback_final,
fallback_inicial,
guard_final,
guard_inicial,
parafraseo,
retriever,
)
from app.agents.routing import (
route_after_guard_final,
route_after_guard_inicial,
)
from app.agents.state import AgentState
def create_agent_graph() -> StateGraph:
"""
Create and configure the LangGraph agent graph.
The graph implements the following flow:
1. START -> agent_host (Nodo 1) - Prepares state and retrieves chat history
2. agent_host -> guard_inicial (Nodo 2) - Validates for malicious content
3. guard_inicial -> [conditional]:
- malicious -> fallback_inicial -> END (stops processing, chat history available)
- continue -> parafraseo (Nodo 4)
4. parafraseo -> Saves message to DB and paraphrases using chat history
5. parafraseo -> retriever (Nodo 5) - Retrieves relevant chunks from vector DB
6. retriever -> context_builder (Nodo 6) - Builds enriched query and generates response
7. context_builder -> guard_final (Nodo 8) - Validates response for risky content
8. guard_final -> [conditional]:
- risky -> fallback_final -> END
- continue -> END (success, returns generated message)
Returns:
Configured StateGraph instance ready for execution
"""
# Create the graph
workflow = StateGraph(AgentState)
# Add nodes
workflow.add_node("agent_host", agent_host)
workflow.add_node("guard_inicial", guard_inicial)
workflow.add_node("fallback_inicial", fallback_inicial)
workflow.add_node("parafraseo", parafraseo)
workflow.add_node("retriever", retriever)
workflow.add_node("context_builder", context_builder)
workflow.add_node("guard_final", guard_final)
workflow.add_node("fallback_final", fallback_final)
# Define edges
# Start -> agent_host
workflow.add_edge(START, "agent_host")
# agent_host -> guard_inicial
workflow.add_edge("agent_host", "guard_inicial")
# guard_inicial -> conditional routing
workflow.add_conditional_edges(
"guard_inicial",
route_after_guard_inicial,
{
"malicious": "fallback_inicial", # Exception path: malicious content detected
"continue": "parafraseo", # Normal path: continue processing
},
)
# fallback_inicial -> END (stop flow with error message)
workflow.add_edge("fallback_inicial", END)
# parafraseo -> retriever
workflow.add_edge("parafraseo", "retriever")
# retriever -> context_builder
workflow.add_edge("retriever", "context_builder")
# context_builder -> guard_final
workflow.add_edge("context_builder", "guard_final")
# guard_final -> conditional routing
workflow.add_conditional_edges(
"guard_final",
route_after_guard_final,
{
"risky": "fallback_final", # Exception path: risky content detected
"continue": END, # Normal path: end successfully
},
)
# fallback_final -> END (stop flow with error message)
workflow.add_edge("fallback_final", END)
# Compile the graph
return workflow.compile()