revert back to old create_executor

Former-commit-id: 62006f114949ceaddca8c25003bedf257eda207b
pull/160/head
Kye 2 years ago
parent 00353b387b
commit 6b0da5eb8e

@ -36,72 +36,52 @@ class AgentManager:
self.memories[session] = self.create_memory() self.memories[session] = self.create_memory()
return self.memories[session] return self.memories[session]
def create_executor(self, session_id: str, task: Optional[Task] = None, openai_api_key: str = None) -> AgentExecutor: def create_executor(self, session: str, execution: Optional[Task] = None, openai_api_key: str = None) -> AgentExecutor:
try: try:
# Create an agent builder with the provided toolsets builder = AgentBuilder(self.toolsets)
agent_builder = AgentBuilder(self.toolsets) builder.build_parser()
# Setup the parser for the agent
agent_builder.build_parser()
# Initialize an empty list for callbacks
callbacks = [] callbacks = []
eval_callback = EVALCallbackHandler()
# Create and setup an evaluation callback, then add it to the callbacks list eval_callback.set_parser(builder.get_parser())
eval_callback = EvaluationCallbackHandler()
eval_callback.set_parser(agent_builder.get_parser())
callbacks.append(eval_callback) callbacks.append(eval_callback)
# If a task is provided, create and setup an execution tracing callback, then add it to the callbacks list if execution:
if task: execution_callback = ExecutionTracingCallbackHandler(execution)
execution_trace_callback = ExecutionTracingCallbackHandler(task) execution_callback.set_parser(builder.get_parser())
execution_trace_callback.set_parser(agent_builder.get_parser()) callbacks.append(execution_callback)
callbacks.append(execution_trace_callback)
# Create a callback manager with the callbacks
callback_manager = CallbackManager(callbacks) callback_manager = CallbackManager(callbacks)
# Setup the language model with the callback manager and OpenAI API key builder.build_llm(callback_manager, openai_api_key)
agent_builder.build_llm(callback_manager, openai_api_key)
# Setup the global tools for the agent
agent_builder.build_global_tools()
# Get or create a memory for the session builder.build_global_tools()
chat_memory = self.get_or_create_memory(session_id)
# Create a list of tools by combining global tools and per session tools memory: BaseChatMemory = self.get_or_create_memory(session)
tools = [ tools = [
*agent_builder.get_global_tools(), *builder.get_global_tools(),
*ToolsFactory.create_per_session_tools( *ToolsFactory.create_per_session_tools(
self.toolsets, self.toolsets,
get_session=lambda: (session_id, self.executors[session_id]), get_session=lambda: (session, self.executors[session]),
), ),
] ]
# Set the callback manager for each tool
for tool in tools: for tool in tools:
tool.callback_manager = callback_manager tool.callback_manager = callback_manager
# Create an executor from the agent and tools
executor = AgentExecutor.from_agent_and_tools( executor = AgentExecutor.from_agent_and_tools(
agent=agent_builder.get_agent(), agent=builder.get_agent(),
tools=tools, tools=tools,
memory=chat_memory, memory=memory,
callback_manager=callback_manager, callback_manager=callback_manager,
verbose=True, verbose=True,
) )
self.executors[session] = executor
# Store the executor in the executors dictionary
self.executors[session_id] = executor
# Return the executor
return executor return executor
except Exception as e: except Exception as e:
logging.error(f"Error while creating executor: {str(e)}") logging.error(f"Error while creating executor: {str(e)}")
raise e raise e
@staticmethod @staticmethod
def create(toolsets: list[BaseToolSet]) -> "AgentManager": def create(toolsets: list[BaseToolSet]) -> "AgentManager":
if not isinstance(toolsets, list): if not isinstance(toolsets, list):

Loading…
Cancel
Save