mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-05-03 08:12:39 +00:00
Using only summary memory for now and intial work on work delegation
This commit is contained in:
@@ -8,11 +8,7 @@ from langchain.chat_models import ChatOpenAI as OpenAI
|
|||||||
from langchain.tools.render import render_text_description
|
from langchain.tools.render import render_text_description
|
||||||
from langchain.agents.format_scratchpad import format_log_to_str
|
from langchain.agents.format_scratchpad import format_log_to_str
|
||||||
from langchain.agents.output_parsers import ReActSingleInputOutputParser
|
from langchain.agents.output_parsers import ReActSingleInputOutputParser
|
||||||
from langchain.memory import (
|
from langchain.memory import ConversationSummaryMemory
|
||||||
ConversationSummaryMemory,
|
|
||||||
ConversationEntityMemory,
|
|
||||||
CombinedMemory
|
|
||||||
)
|
|
||||||
|
|
||||||
from .prompts import Prompts
|
from .prompts import Prompts
|
||||||
|
|
||||||
@@ -22,6 +18,10 @@ class Agent(BaseModel):
|
|||||||
role: str = Field(description="Role of the agent")
|
role: str = Field(description="Role of the agent")
|
||||||
goal: str = Field(description="Objective of the agent")
|
goal: str = Field(description="Objective of the agent")
|
||||||
backstory: str = Field(description="Backstory of the agent")
|
backstory: str = Field(description="Backstory of the agent")
|
||||||
|
allow_delegation: bool = Field(
|
||||||
|
description="Allow delegation of tasks to agents",
|
||||||
|
default=True
|
||||||
|
)
|
||||||
tools: List[Any] = Field(
|
tools: List[Any] = Field(
|
||||||
description="Tools at agents disposal",
|
description="Tools at agents disposal",
|
||||||
default=[]
|
default=[]
|
||||||
@@ -30,8 +30,7 @@ class Agent(BaseModel):
|
|||||||
description="LLM that will run the agent",
|
description="LLM that will run the agent",
|
||||||
default=OpenAI(
|
default=OpenAI(
|
||||||
temperature=0.7,
|
temperature=0.7,
|
||||||
model_name="gpt-4",
|
model_name="gpt-4"
|
||||||
verbose=False
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -47,25 +46,25 @@ class Agent(BaseModel):
|
|||||||
inner_agent = {
|
inner_agent = {
|
||||||
"input": lambda x: x["input"],
|
"input": lambda x: x["input"],
|
||||||
"tools": lambda x: x["tools"],
|
"tools": lambda x: x["tools"],
|
||||||
"entities": lambda x: x["entities"],
|
|
||||||
"tool_names": lambda x: x["tool_names"],
|
"tool_names": lambda x: x["tool_names"],
|
||||||
"chat_history": lambda x: x["chat_history"],
|
"chat_history": lambda x: x["chat_history"],
|
||||||
"agent_scratchpad": lambda x: format_log_to_str(x['intermediate_steps']),
|
"agent_scratchpad": lambda x: format_log_to_str(x['intermediate_steps']),
|
||||||
} | execution_prompt | llm_with_bind | ReActSingleInputOutputParser()
|
} | execution_prompt | llm_with_bind | ReActSingleInputOutputParser()
|
||||||
|
|
||||||
summary_memory = ConversationSummaryMemory(llm=self.llm, memory_key='chat_history', input_key="input")
|
summary_memory = ConversationSummaryMemory(
|
||||||
entity_memory = ConversationEntityMemory(llm=self.llm, input_key="input")
|
llm=self.llm,
|
||||||
memory = CombinedMemory(memories=[entity_memory, summary_memory])
|
memory_key='chat_history',
|
||||||
|
input_key="input"
|
||||||
|
)
|
||||||
|
|
||||||
self.agent_executor = AgentExecutor(
|
self.agent_executor = AgentExecutor(
|
||||||
agent=inner_agent,
|
agent=inner_agent,
|
||||||
tools=self.tools,
|
tools=self.tools,
|
||||||
memory=memory,
|
memory=summary_memory,
|
||||||
verbose=False,
|
|
||||||
handle_parsing_errors=True
|
handle_parsing_errors=True
|
||||||
)
|
)
|
||||||
|
|
||||||
def execute_task(self, task: str, context: str = None) -> str:
|
def execute_task(self, task: str, context: str = None, tools: List[Any] = None) -> str:
|
||||||
"""
|
"""
|
||||||
Execute a task with the agent.
|
Execute a task with the agent.
|
||||||
Parameters:
|
Parameters:
|
||||||
@@ -80,12 +79,13 @@ class Agent(BaseModel):
|
|||||||
context
|
context
|
||||||
])
|
])
|
||||||
|
|
||||||
print(f"Executing task: {task}")
|
tools = tools or self.tools
|
||||||
|
self.agent_executor.tools = tools
|
||||||
return self.agent_executor.invoke({
|
return self.agent_executor.invoke({
|
||||||
"input": task,
|
"input": task,
|
||||||
"tool_names": self.__tools_names(),
|
"tool_names": self.__tools_names(tools),
|
||||||
"tools": render_text_description(self.tools),
|
"tools": render_text_description(tools),
|
||||||
})['output']
|
})['output']
|
||||||
|
|
||||||
def __tools_names(self) -> str:
|
def __tools_names(self, tools) -> str:
|
||||||
return ", ".join([t.name for t in self.tools])
|
return ", ".join([t.name for t in tools])
|
||||||
Reference in New Issue
Block a user