mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-26 16:48:13 +00:00
Run pre-commit hooks
In the title !
This commit is contained in:
204
crewai/agent.py
204
crewai/agent.py
@@ -1,128 +1,120 @@
|
||||
"""Generic agent."""
|
||||
|
||||
from typing import List, Any, Optional
|
||||
from pydantic.v1 import BaseModel, PrivateAttr, Field, root_validator
|
||||
from typing import Any, List, Optional
|
||||
|
||||
from langchain.agents import AgentExecutor
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.tools.render import render_text_description
|
||||
from langchain.agents.format_scratchpad import format_log_to_str
|
||||
from langchain.agents.output_parsers import ReActSingleInputOutputParser
|
||||
from langchain.chat_models import ChatOpenAI
|
||||
from langchain.memory import ConversationSummaryMemory
|
||||
from langchain.tools.render import render_text_description
|
||||
from pydantic.v1 import BaseModel, Field, PrivateAttr, root_validator
|
||||
|
||||
from .prompts import Prompts
|
||||
|
||||
|
||||
class Agent(BaseModel):
|
||||
"""
|
||||
Represents an agent in a system.
|
||||
"""
|
||||
Represents an agent in a system.
|
||||
|
||||
Each agent has a role, a goal, a backstory, and an optional language model (llm).
|
||||
The agent can also have memory, can operate in verbose mode, and can delegate tasks to other agents.
|
||||
Each agent has a role, a goal, a backstory, and an optional language model (llm).
|
||||
The agent can also have memory, can operate in verbose mode, and can delegate tasks to other agents.
|
||||
|
||||
Attributes:
|
||||
agent_executor: An instance of the AgentExecutor class.
|
||||
role: The role of the agent.
|
||||
goal: The objective of the agent.
|
||||
backstory: The backstory of the agent.
|
||||
llm: The language model that will run the agent.
|
||||
memory: Whether the agent should have memory or not.
|
||||
verbose: Whether the agent execution should be in verbose mode.
|
||||
allow_delegation: Whether the agent is allowed to delegate tasks to other agents.
|
||||
"""
|
||||
agent_executor: AgentExecutor = None
|
||||
role: str = Field(description="Role of the agent")
|
||||
goal: str = Field(description="Objective of the agent")
|
||||
backstory: str = Field(description="Backstory of the agent")
|
||||
llm: Optional[Any] = Field(description="LLM that will run the agent")
|
||||
memory: bool = Field(
|
||||
description="Whether the agent should have memory or not",
|
||||
default=True
|
||||
)
|
||||
verbose: bool = Field(
|
||||
description="Verbose mode for the Agent Execution",
|
||||
default=False
|
||||
)
|
||||
allow_delegation: bool = Field(
|
||||
description="Allow delegation of tasks to agents",
|
||||
default=True
|
||||
)
|
||||
tools: List[Any] = Field(
|
||||
description="Tools at agents disposal",
|
||||
default=[]
|
||||
)
|
||||
_task_calls: List[Any] = PrivateAttr()
|
||||
Attributes:
|
||||
agent_executor: An instance of the AgentExecutor class.
|
||||
role: The role of the agent.
|
||||
goal: The objective of the agent.
|
||||
backstory: The backstory of the agent.
|
||||
llm: The language model that will run the agent.
|
||||
memory: Whether the agent should have memory or not.
|
||||
verbose: Whether the agent execution should be in verbose mode.
|
||||
allow_delegation: Whether the agent is allowed to delegate tasks to other agents.
|
||||
"""
|
||||
|
||||
@root_validator(pre=True)
|
||||
def check_llm(_cls, values):
|
||||
if not values.get('llm'):
|
||||
values['llm'] = ChatOpenAI(
|
||||
temperature=0.7,
|
||||
model_name="gpt-4"
|
||||
)
|
||||
return values
|
||||
agent_executor: AgentExecutor = None
|
||||
role: str = Field(description="Role of the agent")
|
||||
goal: str = Field(description="Objective of the agent")
|
||||
backstory: str = Field(description="Backstory of the agent")
|
||||
llm: Optional[Any] = Field(description="LLM that will run the agent")
|
||||
memory: bool = Field(
|
||||
description="Whether the agent should have memory or not", default=True
|
||||
)
|
||||
verbose: bool = Field(
|
||||
description="Verbose mode for the Agent Execution", default=False
|
||||
)
|
||||
allow_delegation: bool = Field(
|
||||
description="Allow delegation of tasks to agents", default=True
|
||||
)
|
||||
tools: List[Any] = Field(description="Tools at agents disposal", default=[])
|
||||
_task_calls: List[Any] = PrivateAttr()
|
||||
|
||||
def __init__(self, **data):
|
||||
super().__init__(**data)
|
||||
agent_args = {
|
||||
"input": lambda x: x["input"],
|
||||
"tools": lambda x: x["tools"],
|
||||
"tool_names": lambda x: x["tool_names"],
|
||||
"agent_scratchpad": lambda x: format_log_to_str(x['intermediate_steps']),
|
||||
}
|
||||
executor_args = {
|
||||
"tools": self.tools,
|
||||
"verbose": self.verbose,
|
||||
"handle_parsing_errors": True,
|
||||
}
|
||||
@root_validator(pre=True)
|
||||
def check_llm(_cls, values):
|
||||
if not values.get("llm"):
|
||||
values["llm"] = ChatOpenAI(temperature=0.7, model_name="gpt-4")
|
||||
return values
|
||||
|
||||
if self.memory:
|
||||
summary_memory = ConversationSummaryMemory(
|
||||
llm=self.llm,
|
||||
memory_key='chat_history',
|
||||
input_key="input"
|
||||
)
|
||||
executor_args['memory'] = summary_memory
|
||||
agent_args['chat_history'] = lambda x: x["chat_history"]
|
||||
prompt = Prompts.TASK_EXECUTION_WITH_MEMORY_PROMPT
|
||||
else:
|
||||
prompt = Prompts.TASK_EXECUTION_PROMPT
|
||||
def __init__(self, **data):
|
||||
super().__init__(**data)
|
||||
agent_args = {
|
||||
"input": lambda x: x["input"],
|
||||
"tools": lambda x: x["tools"],
|
||||
"tool_names": lambda x: x["tool_names"],
|
||||
"agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]),
|
||||
}
|
||||
executor_args = {
|
||||
"tools": self.tools,
|
||||
"verbose": self.verbose,
|
||||
"handle_parsing_errors": True,
|
||||
}
|
||||
|
||||
execution_prompt = prompt.partial(
|
||||
goal=self.goal,
|
||||
role=self.role,
|
||||
backstory=self.backstory,
|
||||
)
|
||||
if self.memory:
|
||||
summary_memory = ConversationSummaryMemory(
|
||||
llm=self.llm, memory_key="chat_history", input_key="input"
|
||||
)
|
||||
executor_args["memory"] = summary_memory
|
||||
agent_args["chat_history"] = lambda x: x["chat_history"]
|
||||
prompt = Prompts.TASK_EXECUTION_WITH_MEMORY_PROMPT
|
||||
else:
|
||||
prompt = Prompts.TASK_EXECUTION_PROMPT
|
||||
|
||||
bind = self.llm.bind(stop=["\nObservation"])
|
||||
inner_agent = agent_args | execution_prompt | bind | ReActSingleInputOutputParser()
|
||||
execution_prompt = prompt.partial(
|
||||
goal=self.goal,
|
||||
role=self.role,
|
||||
backstory=self.backstory,
|
||||
)
|
||||
|
||||
self.agent_executor = AgentExecutor(
|
||||
agent=inner_agent,
|
||||
**executor_args
|
||||
)
|
||||
bind = self.llm.bind(stop=["\nObservation"])
|
||||
inner_agent = (
|
||||
agent_args | execution_prompt | bind | ReActSingleInputOutputParser()
|
||||
)
|
||||
|
||||
def execute_task(self, task: str, context: str = None, tools: List[Any] = None) -> str:
|
||||
"""
|
||||
Execute a task with the agent.
|
||||
Parameters:
|
||||
task (str): Task to execute
|
||||
Returns:
|
||||
output (str): Output of the agent
|
||||
"""
|
||||
if context:
|
||||
task = "\n".join([
|
||||
task,
|
||||
"\nThis is the context you are working with:",
|
||||
context
|
||||
])
|
||||
self.agent_executor = AgentExecutor(agent=inner_agent, **executor_args)
|
||||
|
||||
tools = tools or self.tools
|
||||
self.agent_executor.tools = tools
|
||||
return self.agent_executor.invoke({
|
||||
"input": task,
|
||||
"tool_names": self.__tools_names(tools),
|
||||
"tools": render_text_description(tools),
|
||||
})['output']
|
||||
def execute_task(
|
||||
self, task: str, context: str = None, tools: List[Any] = None
|
||||
) -> str:
|
||||
"""
|
||||
Execute a task with the agent.
|
||||
Parameters:
|
||||
task (str): Task to execute
|
||||
Returns:
|
||||
output (str): Output of the agent
|
||||
"""
|
||||
if context:
|
||||
task = "\n".join(
|
||||
[task, "\nThis is the context you are working with:", context]
|
||||
)
|
||||
|
||||
def __tools_names(self, tools) -> str:
|
||||
return ", ".join([t.name for t in tools])
|
||||
tools = tools or self.tools
|
||||
self.agent_executor.tools = tools
|
||||
return self.agent_executor.invoke(
|
||||
{
|
||||
"input": task,
|
||||
"tool_names": self.__tools_names(tools),
|
||||
"tools": render_text_description(tools),
|
||||
}
|
||||
)["output"]
|
||||
|
||||
def __tools_names(self, tools) -> str:
|
||||
return ", ".join([t.name for t in tools])
|
||||
|
||||
Reference in New Issue
Block a user