Skip to content

Commit 3bc235e

Browse files
committed
implement task decomposer agent
1 parent 441b2f4 commit 3bc235e

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,4 @@ ci-test-docs: install-deps-docs docs ## run CI test for documentation
114114

115115
.PHONY: langgraph-studio
116116
langgraph-studio: ## run LangGraph Studio
117-
uv run langgraph dev --no-reload
117+
uv run langgraph dev

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ uv run python -m template_langgraph.tasks.run_issue_formatter_agent
4242
### LangGraph
4343

4444
- [Build a custom workflow](https://langchain-ai.github.io/langgraph/concepts/why-langgraph/)
45+
- [LangGraphの(LLMなし)Human-in-the-loopを試してみた](https://qiita.com/te_yama/items/db38201af60dec76384d)
4546

4647
### Sample Codes
4748

template_langgraph/agents/task_decomposer_agent/agent.py

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from langgraph.graph import END, StateGraph
2+
from langgraph.types import interrupt
23

3-
from template_langgraph.agents.chat_with_tools_agent.models import AgentState
4+
from template_langgraph.agents.task_decomposer_agent.models import AgentState, TaskList
45
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
56
from template_langgraph.loggers import get_logger
67

@@ -18,22 +19,58 @@ def create_graph(self):
1819

1920
# Create nodes
2021
workflow.add_node("chat", self.chat)
22+
workflow.add_node("human_feedback", self.human_feedback)
2123

2224
# Create edges
2325
workflow.set_entry_point("chat")
24-
workflow.add_edge("chat", END)
25-
26-
# Compile the graph
26+
workflow.add_edge("chat", "human_feedback")
27+
workflow.add_conditional_edges(
28+
source="human_feedback",
29+
path=self.route_human_feedback,
30+
path_map={
31+
"loopback": "chat",
32+
"end": END,
33+
},
34+
)
2735
return workflow.compile()
2836

2937
def chat(self, state: AgentState) -> AgentState:
3038
"""Chat with tools using the state."""
3139
logger.info(f"Chatting with tools using state: {state}")
32-
return {
33-
"messages": [
34-
self.llm.invoke(state["messages"]),
35-
]
36-
}
40+
41+
task_list = self.llm.with_structured_output(TaskList).invoke(
42+
input=state["messages"],
43+
)
44+
state["task_list"] = task_list
45+
logger.info(f"Decomposed tasks: {task_list}")
46+
return state
47+
48+
def human_feedback(self, state: AgentState) -> AgentState:
49+
"""Handle human feedback."""
50+
logger.info(f"Handling human feedback with state: {state}")
51+
feedback = interrupt("Type your feedback. If you want to end the conversation, type 'end'.")
52+
state["messages"].append(
53+
{
54+
"content": feedback,
55+
"role": "user",
56+
}
57+
)
58+
return state
59+
60+
def route_human_feedback(
61+
self,
62+
state: AgentState,
63+
):
64+
"""
65+
Use in the conditional_edge to route to the HumanFeedbackNode if the last message
66+
has human feedback. Otherwise, route to the end.
67+
"""
68+
human_feedback = state["messages"][-1].content.strip().lower()
69+
if human_feedback == "end":
70+
logger.info("Ending the conversation as per user request.")
71+
return "end"
72+
logger.info("Looping back to chat for further processing.")
73+
return "loopback"
3774

3875
def draw_mermaid_png(self) -> bytes:
3976
"""Draw the graph in Mermaid format."""

template_langgraph/agents/task_decomposer_agent/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ class Task(BaseModel):
1919
assigned_to: str | None = Field(None, description="Name of the agent assigned to the task")
2020

2121

22+
class TaskList(BaseModel):
23+
tasks: Sequence[Task] = Field(..., description="List of tasks to be decomposed")
24+
25+
2226
class AgentInput(BaseModel):
2327
request: str = Field(..., description="Request from the user")
2428

@@ -29,4 +33,4 @@ class AgentOutput(BaseModel):
2933

3034
class AgentState(TypedDict):
3135
messages: Annotated[Sequence[BaseMessage], add_messages]
32-
decomposed_tasks: Sequence[Task]
36+
task_list: TaskList

0 commit comments

Comments
 (0)