mirror of
https://github.com/crewAIInc/crewAI.git
synced 2025-12-16 04:18:35 +00:00
Update to Pydantic v2: Transitioned all references from pydantic.v1 to pydantic (v2), ensuring compatibility with the latest Pydantic features and improvements. Affected components include agent tools, prompts, crew, and task modules. Refactoring & Alignment with Pydantic Standards: Refactored the agent module away from traditional __init__ to align more closely with Pydantic best practices. Updated the crew module to Pydantic v2 and enhanced configurations, allowing JSON and dictionary inputs. Additionally, some (not all) exceptions have been migrated to leverage Pydantic's error-handling capabilities. Enhancements to Validators and Typings: Improved validators and type annotations across multiple modules, enhancing code readability and maintainability. Streamlined the validation process in line with Pydantic v2's methodologies. Import and Configuration Adjustments: Updated to test-related absolute imports due to issues with Pytest finding packages through relative imports.
75 lines
2.6 KiB
Python
75 lines
2.6 KiB
Python
from textwrap import dedent
|
|
from typing import List
|
|
|
|
from langchain.tools import Tool
|
|
from pydantic import BaseModel, Field
|
|
|
|
from ..agent import Agent
|
|
|
|
|
|
class AgentTools(BaseModel):
|
|
"""Tools for generic agent."""
|
|
|
|
agents: List[Agent] = Field(description="List of agents in this crew.")
|
|
|
|
def tools(self):
|
|
return [
|
|
Tool.from_function(
|
|
func=self.delegate_work,
|
|
name="Delegate Work to Co-Worker",
|
|
description=dedent(
|
|
f"""Useful to delegate a specific task to one of the
|
|
following co-workers: [{', '.join([agent.role for agent in self.agents])}].
|
|
The input to this tool should be a pipe (|) separated text of length
|
|
three, representing the role you want to delegate it to, the task and
|
|
information necessary. For example, `coworker|task|information`.
|
|
"""
|
|
),
|
|
),
|
|
Tool.from_function(
|
|
func=self.ask_question,
|
|
name="Ask Question to Co-Worker",
|
|
description=dedent(
|
|
f"""Useful to ask a question, opinion or take from on
|
|
of the following co-workers: [{', '.join([agent.role for agent in self.agents])}].
|
|
The input to this tool should be a pipe (|) separated text of length
|
|
three, representing the role you want to ask it to, the question and
|
|
information necessary. For example, `coworker|question|information`.
|
|
"""
|
|
),
|
|
),
|
|
]
|
|
|
|
def delegate_work(self, command):
|
|
"""Useful to delegate a specific task to a coworker."""
|
|
return self.__execute(command)
|
|
|
|
def ask_question(self, command):
|
|
"""Useful to ask a question, opinion or take from a coworker."""
|
|
return self.__execute(command)
|
|
|
|
def __execute(self, command):
|
|
"""Execute the command."""
|
|
try:
|
|
agent, task, information = command.split("|")
|
|
except ValueError:
|
|
return "Error executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|task|information`."
|
|
|
|
if not agent or not task or not information:
|
|
return "Error executing tool. Missing exact 3 pipe (|) separated values. For example, `coworker|question|information`."
|
|
|
|
agent = [
|
|
available_agent
|
|
for available_agent in self.agents
|
|
if available_agent.role == agent
|
|
]
|
|
|
|
if len(agent) == 0:
|
|
return (
|
|
"Error executing tool. Co-worker not found, double check the co-worker."
|
|
)
|
|
|
|
agent = agent[0]
|
|
result = agent.execute_task(task, information)
|
|
return result
|