generated from ks6088ts/template-python
-
Notifications
You must be signed in to change notification settings - Fork 0
add deep agent #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
add deep agent #110
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
template_langgraph/agents/demo_agents/deep_agent_core/__init__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# ruff: noqa | ||
from template_langgraph.agents.demo_agents.deep_agent_core.graph import create_deep_agent | ||
from template_langgraph.agents.demo_agents.deep_agent_core.state import DeepAgentState | ||
from template_langgraph.agents.demo_agents.deep_agent_core.sub_agent import SubAgent |
80 changes: 80 additions & 0 deletions
80
template_langgraph/agents/demo_agents/deep_agent_core/graph.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# ruff: noqa: E501 | ||
from collections.abc import Callable, Sequence | ||
from typing import Any, TypeVar | ||
|
||
from langchain_core.language_models import LanguageModelLike | ||
from langchain_core.tools import BaseTool | ||
from langgraph.prebuilt import create_react_agent | ||
from langgraph.types import Checkpointer | ||
|
||
from template_langgraph.agents.demo_agents.deep_agent_core.state import DeepAgentState | ||
from template_langgraph.agents.demo_agents.deep_agent_core.sub_agent import SubAgent, _create_task_tool | ||
from template_langgraph.agents.demo_agents.deep_agent_core.tools import ( | ||
edit_file, | ||
ls, | ||
read_file, | ||
write_file, | ||
write_todos, | ||
) | ||
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper | ||
|
||
StateSchema = TypeVar("StateSchema", bound=DeepAgentState) | ||
StateSchemaType = type[StateSchema] | ||
|
||
base_prompt = """You have access to a number of standard tools | ||
|
||
## `write_todos` | ||
|
||
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. | ||
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. | ||
|
||
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. | ||
## `task` | ||
|
||
- When doing web search, prefer to use the `task` tool in order to reduce context usage.""" | ||
|
||
|
||
def create_deep_agent( | ||
tools: Sequence[BaseTool | Callable | dict[str, Any]], | ||
instructions: str, | ||
model: str | LanguageModelLike | None = None, | ||
subagents: list[SubAgent] = None, | ||
state_schema: StateSchemaType | None = None, | ||
config_schema: type[Any] | None = None, | ||
checkpointer: Checkpointer | None = None, | ||
): | ||
"""Create a deep agent. | ||
|
||
This agent will by default have access to a tool to write todos (write_todos), | ||
and then four file editing tools: write_file, ls, read_file, edit_file. | ||
|
||
Args: | ||
tools: The additional tools the agent should have access to. | ||
instructions: The additional instructions the agent should have. Will go in | ||
the system prompt. | ||
model: The model to use. | ||
subagents: The subagents to use. Each subagent should be a dictionary with the | ||
following keys: | ||
- `name` | ||
- `description` (used by the main agent to decide whether to call the sub agent) | ||
- `prompt` (used as the system prompt in the subagent) | ||
- (optional) `tools` | ||
state_schema: The schema of the deep agent. Should subclass from template_langgraph.agents.demo_agents.deep_agent_coretate | ||
config_schema: The schema of the deep agent. | ||
checkpointer: Optional checkpointer for persisting agent state between runs. | ||
""" | ||
prompt = instructions + base_prompt | ||
built_in_tools = [write_todos, write_file, read_file, ls, edit_file] | ||
if model is None: | ||
model = AzureOpenAiWrapper().chat_model | ||
state_schema = state_schema or DeepAgentState | ||
task_tool = _create_task_tool(list(tools) + built_in_tools, instructions, subagents or [], model, state_schema) | ||
all_tools = built_in_tools + list(tools) + [task_tool] | ||
return create_react_agent( | ||
model, | ||
prompt=prompt, | ||
tools=all_tools, | ||
state_schema=state_schema, | ||
config_schema=config_schema, | ||
checkpointer=checkpointer, | ||
) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a typo in the docstring: 'deep_agent_coretate' should be 'deep_agent_core.state'.
Copilot uses AI. Check for mistakes.