Skip to content

Commit 0d20f8b

Browse files
authored
Merge pull request #110 from ks6088ts-labs/feature/issue-86_deep-agent
add deep agent
2 parents 2fdd634 + c8b39f3 commit 0d20f8b

File tree

9 files changed

+775
-0
lines changed

9 files changed

+775
-0
lines changed

docs/references.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
- [Combine control flow and state updates with Command](https://langchain-ai.github.io/langgraph/how-tos/graph-api/#combine-control-flow-and-state-updates-with-command)
1414
- [Command: a new tool for building multi-agent architectures in LangGraph](https://www.youtube.com/watch?v=6BJDKf90L9A)
1515
- [masamasa59/genai-agent-advanced-book > chapter6](https://github.com/masamasa59/genai-agent-advanced-book/blob/main/chapter6/arxiv_researcher/agent/paper_search_agent.py)
16+
- [langchain-ai/deepagents](https://github.com/langchain-ai/deepagents)
17+
- [Custom UI for Deep Agents](https://github.com/langchain-ai/deep-agents-ui)
1618

1719
### Sample Codes
1820

langgraph.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"chat_with_tools_agent": "template_langgraph.agents.chat_with_tools_agent.agent:graph",
55
"demo_agents_parallel_rag_agent": "template_langgraph.agents.demo_agents.parallel_rag_agent.agent:graph",
66
"demo_agents_multi_agent": "template_langgraph.agents.demo_agents.multi_agent:graph",
7+
"demo_agents_research_deep_agent": "template_langgraph.agents.demo_agents.research_deep_agent:graph",
78
"demo_agents_weather_agent": "template_langgraph.agents.demo_agents.weather_agent:graph",
89
"image_classifier_agent": "template_langgraph.agents.image_classifier_agent.agent:graph",
910
"issue_formatter_agent": "template_langgraph.agents.issue_formatter_agent.agent:graph",
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# ruff: noqa
2+
from template_langgraph.agents.demo_agents.deep_agent_core.graph import create_deep_agent
3+
from template_langgraph.agents.demo_agents.deep_agent_core.state import DeepAgentState
4+
from template_langgraph.agents.demo_agents.deep_agent_core.sub_agent import SubAgent
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# ruff: noqa: E501
2+
from collections.abc import Callable, Sequence
3+
from typing import Any, TypeVar
4+
5+
from langchain_core.language_models import LanguageModelLike
6+
from langchain_core.tools import BaseTool
7+
from langgraph.prebuilt import create_react_agent
8+
from langgraph.types import Checkpointer
9+
10+
from template_langgraph.agents.demo_agents.deep_agent_core.state import DeepAgentState
11+
from template_langgraph.agents.demo_agents.deep_agent_core.sub_agent import SubAgent, _create_task_tool
12+
from template_langgraph.agents.demo_agents.deep_agent_core.tools import (
13+
edit_file,
14+
ls,
15+
read_file,
16+
write_file,
17+
write_todos,
18+
)
19+
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper
20+
21+
StateSchema = TypeVar("StateSchema", bound=DeepAgentState)
22+
StateSchemaType = type[StateSchema]
23+
24+
base_prompt = """You have access to a number of standard tools
25+
26+
## `write_todos`
27+
28+
You have access to the `write_todos` tools to help you manage and plan tasks. Use these tools VERY frequently to ensure that you are tracking your tasks and giving the user visibility into your progress.
29+
These tools are also EXTREMELY helpful for planning tasks, and for breaking down larger complex tasks into smaller steps. If you do not use this tool when planning, you may forget to do important tasks - and that is unacceptable.
30+
31+
It is critical that you mark todos as completed as soon as you are done with a task. Do not batch up multiple tasks before marking them as completed.
32+
## `task`
33+
34+
- When doing web search, prefer to use the `task` tool in order to reduce context usage."""
35+
36+
37+
def create_deep_agent(
38+
tools: Sequence[BaseTool | Callable | dict[str, Any]],
39+
instructions: str,
40+
model: str | LanguageModelLike | None = None,
41+
subagents: list[SubAgent] = None,
42+
state_schema: StateSchemaType | None = None,
43+
config_schema: type[Any] | None = None,
44+
checkpointer: Checkpointer | None = None,
45+
):
46+
"""Create a deep agent.
47+
48+
This agent will by default have access to a tool to write todos (write_todos),
49+
and then four file editing tools: write_file, ls, read_file, edit_file.
50+
51+
Args:
52+
tools: The additional tools the agent should have access to.
53+
instructions: The additional instructions the agent should have. Will go in
54+
the system prompt.
55+
model: The model to use.
56+
subagents: The subagents to use. Each subagent should be a dictionary with the
57+
following keys:
58+
- `name`
59+
- `description` (used by the main agent to decide whether to call the sub agent)
60+
- `prompt` (used as the system prompt in the subagent)
61+
- (optional) `tools`
62+
state_schema: The schema of the deep agent. Should subclass from template_langgraph.agents.demo_agents.deep_agent_coretate
63+
config_schema: The schema of the deep agent.
64+
checkpointer: Optional checkpointer for persisting agent state between runs.
65+
"""
66+
prompt = instructions + base_prompt
67+
built_in_tools = [write_todos, write_file, read_file, ls, edit_file]
68+
if model is None:
69+
model = AzureOpenAiWrapper().chat_model
70+
state_schema = state_schema or DeepAgentState
71+
task_tool = _create_task_tool(list(tools) + built_in_tools, instructions, subagents or [], model, state_schema)
72+
all_tools = built_in_tools + list(tools) + [task_tool]
73+
return create_react_agent(
74+
model,
75+
prompt=prompt,
76+
tools=all_tools,
77+
state_schema=state_schema,
78+
config_schema=config_schema,
79+
checkpointer=checkpointer,
80+
)

0 commit comments

Comments
 (0)