|
| 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