Skip to content
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

Accept Langchain's RunnableConfig to Agent.execute_task #483

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/core-concepts/Agents.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ description: What are crewAI Agents and how to use them.
| **Allow Delegation** *(optional)* | Agents can delegate tasks or questions to one another, ensuring that each task is handled by the most suitable agent. Default is `True`. |
| **Step Callback** *(optional)* | A function that is called after each step of the agent. This can be used to log the agent's actions or to perform other operations. It will overwrite the crew `step_callback`. |
| **Cache** *(optional)* | Indicates if the agent should use a cache for tool usage. Default is `True`. |
| **runnable_config** *(optional)* | A Langchain supported RunnableConfig to be used by the AgentExecutor. Default is `None`. |

## Creating an Agent

Expand Down Expand Up @@ -64,4 +65,4 @@ agent = Agent(
```

## Conclusion
Agents are the building blocks of the CrewAI framework. By understanding how to define and interact with agents, you can create sophisticated AI systems that leverage the power of collaborative intelligence.
Agents are the building blocks of the CrewAI framework. By understanding how to define and interact with agents, you can create sophisticated AI systems that leverage the power of collaborative intelligence.
11 changes: 10 additions & 1 deletion src/crewai/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from langchain.tools.render import render_text_description
from langchain_core.agents import AgentAction
from langchain_core.callbacks import BaseCallbackHandler
from langchain_core.runnables import RunnableConfig
from langchain_openai import ChatOpenAI
from pydantic import (
UUID4,
Expand Down Expand Up @@ -48,6 +49,7 @@ class Agent(BaseModel):
tools: Tools at agents disposal
step_callback: Callback to be executed after each step of the agent execution.
callbacks: A list of callback functions from the langchain library that are triggered during the agent's execution process
runnable_config: A runnable configuration to be used by the AgentExecutor
"""

__hash__ = object.__hash__ # type: ignore
Expand Down Expand Up @@ -130,6 +132,10 @@ class Agent(BaseModel):
response_template: Optional[str] = Field(
default=None, description="Response format for the agent."
)
runnable_config: Optional[RunnableConfig] = Field(
default=None,
description="A runnable configuration to be used by the AgentExecutor",
)

_original_role: str | None = None
_original_goal: str | None = None
Expand Down Expand Up @@ -192,13 +198,15 @@ def execute_task(
task: Any,
context: Optional[str] = None,
tools: Optional[List[Any]] = None,
runnable_config: Optional[RunnableConfig] = None,
) -> str:
"""Execute a task with the agent.

Args:
task: Task to execute.
context: Context to execute the task in.
tools: Tools to use for the task.
runnable_config: A runnable configuration to be used by the AgentExecutor. Overrides the runnable_config attribute.

Returns:
Output of the agent
Expand Down Expand Up @@ -238,7 +246,8 @@ def execute_task(
"input": task_prompt,
"tool_names": self.agent_executor.tools_names,
"tools": self.agent_executor.tools_description,
}
},
config=runnable_config or self.runnable_config,
)["output"]

if self.max_rpm:
Expand Down