-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow.py
More file actions
132 lines (121 loc) · 4.44 KB
/
Copy pathworkflow.py
File metadata and controls
132 lines (121 loc) · 4.44 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
import aiosqlite
from pathlib import Path
from langgraph.graph import StateGraph, END
from langgraph.checkpoint.sqlite.aio import AsyncSqliteSaver
import logging
from config import CHECKPOINT_DB
from models import AgentState
from agents import (
question_rewriter,
question_classifier,
on_topic_router,
retriever_node,
batch_relevant_code_chunk,
router,
refine_question,
agent_selection,
code_analyst_router,
security_router,
performance_router,
present_teller_code_analyst,
present_teller_security,
present_teller_performance,
present_teller_quality,
code_analyst_agent,
security_agent,
performance_agent,
quality_agent,
cannot_answer,
off_topic
)
async def create_workflow():
"""Create and configure the LangGraph workflow"""
workflow = StateGraph(AgentState)
# Add all nodes
workflow.add_node("question_rewriter", question_rewriter)
workflow.add_node("question_classifier", question_classifier)
workflow.add_node("retriever", retriever_node)
workflow.add_node("batch_relevant_code_chunk", batch_relevant_code_chunk)
workflow.add_node("refine_question", refine_question)
workflow.add_node("agent_selection", agent_selection)
workflow.add_node("present_teller_code_analyst", present_teller_code_analyst)
workflow.add_node("present_teller_security", present_teller_security)
workflow.add_node("present_teller_performance", present_teller_performance)
workflow.add_node("present_teller_quality", present_teller_quality)
workflow.add_node("code_analyst_agent", code_analyst_agent)
workflow.add_node("security_agent", security_agent)
workflow.add_node("performance_agent", performance_agent)
workflow.add_node("quality_agent", quality_agent)
workflow.add_node("cannot_answer", cannot_answer)
workflow.add_node("off_topic", off_topic)
# Add edges
workflow.add_edge("question_rewriter", "question_classifier")
workflow.add_conditional_edges(
"question_classifier",
on_topic_router,
{"retriever": "retriever", "off_topic": "off_topic"}
)
workflow.add_edge("retriever", "batch_relevant_code_chunk")
workflow.add_conditional_edges(
"batch_relevant_code_chunk",
router,
{
"agent_selection": "agent_selection",
"refine_question": "refine_question",
"cannot_answer": "cannot_answer"
}
)
workflow.add_edge("refine_question", "retriever")
workflow.add_edge("agent_selection", "present_teller_code_analyst")
workflow.add_edge("present_teller_code_analyst", "code_analyst_agent")
workflow.add_conditional_edges(
"code_analyst_agent",
code_analyst_router,
{
"present_teller_security": "present_teller_security",
"present_teller_performance": "present_teller_performance",
"present_teller_quality": "present_teller_quality",
END: END
}
)
workflow.add_edge("present_teller_security", "security_agent")
workflow.add_conditional_edges(
"security_agent",
security_router,
{
"present_teller_performance": "present_teller_performance",
"present_teller_quality": "present_teller_quality",
END: END
}
)
workflow.add_edge("present_teller_performance", "performance_agent")
workflow.add_conditional_edges(
"performance_agent",
performance_router,
{
"present_teller_quality": "present_teller_quality",
END: END
}
)
workflow.add_edge("present_teller_quality", "quality_agent")
workflow.add_edge("quality_agent", END)
workflow.add_edge("off_topic", END)
workflow.add_edge("cannot_answer", END)
workflow.set_entry_point("question_rewriter")
try:
checkpoint_db_path = Path(CHECKPOINT_DB)
checkpoint_db_path.parent.mkdir(exist_ok=True)
conn = await aiosqlite.connect(CHECKPOINT_DB)
checkpointer = AsyncSqliteSaver(conn=conn)
logging.info(f"Checkpointer at: {CHECKPOINT_DB}")
except Exception as e:
logging.info(f"Checkpointer error: {e}")
raise e
return workflow.compile(checkpointer=checkpointer)
workflow_app = None
async def get_workflow():
"""Get the workflow instance (singleton pattern)"""
global workflow_app
if workflow_app is None:
workflow_app = await create_workflow()
return workflow_app