Compare commits

..

2 Commits

8 changed files with 275 additions and 227 deletions

View File

@@ -9,6 +9,7 @@ from crewai.agents import CacheHandler
from crewai.agents.agent_builder.base_agent import BaseAgent
from crewai.agents.crew_agent_executor import CrewAgentExecutor
from crewai.cli.constants import ENV_VARS, LITELLM_PARAMS
from crewai.utilities import Logger
from crewai.knowledge.knowledge import Knowledge
from crewai.knowledge.source.base_knowledge_source import BaseKnowledgeSource
from crewai.knowledge.utils.knowledge_utils import extract_knowledge_context
@@ -62,8 +63,12 @@ class Agent(BaseAgent):
tools: Tools at agents disposal
step_callback: Callback to be executed after each step of the agent execution.
knowledge_sources: Knowledge sources for the agent.
allow_feedback: Whether the agent can receive and process feedback during execution.
allow_conflict: Whether the agent can handle conflicts with other agents during execution.
allow_iteration: Whether the agent can iterate on its solutions based on feedback and validation.
"""
_logger = PrivateAttr(default_factory=lambda: Logger(verbose=False))
_times_executed: int = PrivateAttr(default=0)
max_execution_time: Optional[int] = Field(
default=None,
@@ -123,6 +128,18 @@ class Agent(BaseAgent):
default="safe",
description="Mode for code execution: 'safe' (using Docker) or 'unsafe' (direct execution).",
)
allow_feedback: bool = Field(
default=False,
description="Enable agent to receive and process feedback during execution.",
)
allow_conflict: bool = Field(
default=False,
description="Enable agent to handle conflicts with other agents during execution.",
)
allow_iteration: bool = Field(
default=False,
description="Enable agent to iterate on its solutions based on feedback and validation.",
)
embedder_config: Optional[Dict[str, Any]] = Field(
default=None,
description="Embedder configuration for the agent.",
@@ -139,6 +156,19 @@ class Agent(BaseAgent):
def post_init_setup(self):
self._set_knowledge()
self.agent_ops_agent_name = self.role
if self.allow_feedback:
self._logger.log("info", "Feedback mode enabled for agent.", color="bold_green")
if self.allow_conflict:
self._logger.log("info", "Conflict handling enabled for agent.", color="bold_green")
if self.allow_iteration:
self._logger.log("info", "Iteration mode enabled for agent.", color="bold_green")
# Validate boolean parameters
for param in ['allow_feedback', 'allow_conflict', 'allow_iteration']:
if not isinstance(getattr(self, param), bool):
raise ValueError(f"Parameter '{param}' must be a boolean value.")
unaccepted_attributes = [
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
@@ -400,6 +430,9 @@ class Agent(BaseAgent):
step_callback=self.step_callback,
function_calling_llm=self.function_calling_llm,
respect_context_window=self.respect_context_window,
allow_feedback=self.allow_feedback,
allow_conflict=self.allow_conflict,
allow_iteration=self.allow_iteration,
request_within_rpm_limit=(
self._rpm_controller.check_or_wait if self._rpm_controller else None
),

View File

@@ -31,6 +31,34 @@ class ToolResult:
class CrewAgentExecutor(CrewAgentExecutorMixin):
"""CrewAgentExecutor class for managing agent execution.
This class is responsible for executing agent tasks, handling tools,
managing agent interactions, and processing the results.
Parameters:
llm: The language model to use for generating responses.
task: The task to be executed.
crew: The crew that the agent belongs to.
agent: The agent to execute the task.
prompt: The prompt to use for generating responses.
max_iter: Maximum number of iterations for the agent execution.
tools: The tools available to the agent.
tools_names: The names of the tools available to the agent.
stop_words: Words that signal the end of agent execution.
tools_description: Description of the tools available to the agent.
tools_handler: Handler for tool operations.
step_callback: Callback function for each step of execution.
original_tools: Original list of tools before processing.
function_calling_llm: LLM specifically for function calling.
respect_context_window: Whether to respect the context window size.
request_within_rpm_limit: Function to check if request is within RPM limit.
callbacks: List of callback functions.
allow_feedback: Controls feedback processing during execution.
allow_conflict: Enables conflict handling between agents.
allow_iteration: Allows solution iteration based on feedback.
"""
_logger: Logger = Logger()
def __init__(
@@ -52,6 +80,9 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
respect_context_window: bool = False,
request_within_rpm_limit: Any = None,
callbacks: List[Any] = [],
allow_feedback: bool = False,
allow_conflict: bool = False,
allow_iteration: bool = False,
):
self._i18n: I18N = I18N()
self.llm = llm
@@ -73,6 +104,9 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
self.function_calling_llm = function_calling_llm
self.respect_context_window = respect_context_window
self.request_within_rpm_limit = request_within_rpm_limit
self.allow_feedback = allow_feedback
self.allow_conflict = allow_conflict
self.allow_iteration = allow_iteration
self.ask_for_human_input = False
self.messages: List[Dict[str, str]] = []
self.iterations = 0
@@ -487,3 +521,56 @@ class CrewAgentExecutor(CrewAgentExecutorMixin):
self.ask_for_human_input = False
return formatted_answer
def process_feedback(self, feedback: str) -> bool:
"""
Process feedback for the agent if feedback mode is enabled.
Parameters:
feedback (str): The feedback to process.
Returns:
bool: True if the feedback was processed successfully, False otherwise.
"""
if not self.allow_feedback:
self._logger.log("warning", "Feedback processing skipped (allow_feedback=False).", color="yellow")
return False
self._logger.log("info", f"Processing feedback: {feedback}", color="green")
# Add feedback to messages
self.messages.append(self._format_msg(f"Feedback: {feedback}"))
return True
def handle_conflict(self, other_agent: 'CrewAgentExecutor') -> bool:
"""
Handle conflict with another agent if conflict handling is enabled.
Parameters:
other_agent (CrewAgentExecutor): The other agent involved in the conflict.
Returns:
bool: True if the conflict was handled successfully, False otherwise.
"""
if not self.allow_conflict:
self._logger.log("warning", "Conflict handling skipped (allow_conflict=False).", color="yellow")
return False
self._logger.log("info", f"Handling conflict with agent: {other_agent.agent.role}", color="green")
return True
def process_iteration(self, result: Any) -> bool:
"""
Process iteration based on result if iteration mode is enabled.
Parameters:
result (Any): The result to iterate on.
Returns:
bool: True if the iteration was processed successfully, False otherwise.
"""
if not self.allow_iteration:
self._logger.log("warning", "Iteration processing skipped (allow_iteration=False).", color="yellow")
return False
self._logger.log("info", "Processing iteration on result.", color="green")
return True

View File

@@ -28,7 +28,7 @@ def create_flow(name):
(project_root / "tests").mkdir(exist_ok=True)
# Create .env file
with open(project_root / ".env", "w", encoding="utf-8", newline="\n") as file:
with open(project_root / ".env", "w") as file:
file.write("OPENAI_API_KEY=YOUR_API_KEY")
package_dir = Path(__file__).parent
@@ -58,7 +58,7 @@ def create_flow(name):
content = content.replace("{{flow_name}}", class_name)
content = content.replace("{{folder_name}}", folder_name)
with open(dst_file, "w", encoding="utf-8", newline="\n") as file:
with open(dst_file, "w") as file:
file.write(content)
# Copy and process root template files

View File

@@ -138,22 +138,17 @@ def load_provider_data(cache_file, cache_expiry):
def read_cache_file(cache_file):
"""
Reads and returns the JSON content from a cache file. Returns None if the file contains invalid JSON
or if there's an encoding error.
Reads and returns the JSON content from a cache file. Returns None if the file contains invalid JSON.
Args:
- cache_file (Path): The path to the cache file.
Returns:
- dict or None: The JSON content of the cache file or None if the JSON is invalid or there's an encoding error.
- dict or None: The JSON content of the cache file or None if the JSON is invalid.
"""
try:
with open(cache_file, "r", encoding="utf-8") as f:
with open(cache_file, "r") as f:
return json.load(f)
except UnicodeDecodeError as e:
click.secho(f"Error reading cache file: Unicode decode error - {e}", fg="red")
click.secho("This may be due to file encoding issues. Try deleting the cache file and trying again.", fg="yellow")
return None
except json.JSONDecodeError:
return None
@@ -172,16 +167,13 @@ def fetch_provider_data(cache_file):
response = requests.get(JSON_URL, stream=True, timeout=60)
response.raise_for_status()
data = download_data(response)
with open(cache_file, "w", encoding="utf-8", newline="\n") as f:
with open(cache_file, "w") as f:
json.dump(data, f)
return data
except requests.RequestException as e:
click.secho(f"Error fetching provider data: {e}", fg="red")
except json.JSONDecodeError:
click.secho("Error parsing provider data. Invalid JSON format.", fg="red")
except UnicodeDecodeError as e:
click.secho(f"Unicode decode error when processing provider data: {e}", fg="red")
click.secho("This may be due to encoding issues with the downloaded data.", fg="yellow")
return None

View File

@@ -18,24 +18,19 @@ console = Console()
def copy_template(src, dst, name, class_name, folder_name):
"""Copy a file from src to dst."""
try:
with open(src, "r", encoding="utf-8") as file:
content = file.read()
with open(src, "r") as file:
content = file.read()
# Interpolate the content
content = content.replace("{{name}}", name)
content = content.replace("{{crew_name}}", class_name)
content = content.replace("{{folder_name}}", folder_name)
# Interpolate the content
content = content.replace("{{name}}", name)
content = content.replace("{{crew_name}}", class_name)
content = content.replace("{{folder_name}}", folder_name)
# Write the interpolated content to the new file
with open(dst, "w", encoding="utf-8", newline="\n") as file:
file.write(content)
# Write the interpolated content to the new file
with open(dst, "w") as file:
file.write(content)
click.secho(f" - Created {dst}", fg="green")
except UnicodeDecodeError as e:
click.secho(f"Error reading template file {src}: Unicode decode error - {e}", fg="red")
click.secho("This may be due to file encoding issues. Please ensure all template files use UTF-8 encoding.", fg="yellow")
raise
click.secho(f" - Created {dst}", fg="green")
def read_toml(file_path: str = "pyproject.toml"):
@@ -83,7 +78,7 @@ def _get_project_attribute(
attribute = None
try:
with open(pyproject_path, "r", encoding="utf-8") as f:
with open(pyproject_path, "r") as f:
pyproject_content = parse_toml(f.read())
dependencies = (
@@ -124,7 +119,7 @@ def fetch_and_json_env_file(env_file_path: str = ".env") -> dict:
"""Fetch the environment variables from a .env file and return them as a dictionary."""
try:
# Read the .env file
with open(env_file_path, "r", encoding="utf-8") as f:
with open(env_file_path, "r") as f:
env_content = f.read()
# Parse the .env file content to a dictionary
@@ -138,9 +133,6 @@ def fetch_and_json_env_file(env_file_path: str = ".env") -> dict:
except FileNotFoundError:
print(f"Error: {env_file_path} not found.")
except UnicodeDecodeError as e:
click.secho(f"Error reading .env file: Unicode decode error - {e}", fg="red")
click.secho("This may be due to file encoding issues. Please ensure the .env file uses UTF-8 encoding.", fg="yellow")
except Exception as e:
print(f"Error reading the .env file: {e}")
@@ -166,15 +158,10 @@ def tree_find_and_replace(directory, find, replace):
for filename in files:
filepath = os.path.join(path, filename)
try:
with open(filepath, "r", encoding="utf-8") as file:
contents = file.read()
with open(filepath, "w", encoding="utf-8", newline="\n") as file:
file.write(contents.replace(find, replace))
except UnicodeDecodeError as e:
click.secho(f"Error processing file {filepath}: Unicode decode error - {e}", fg="red")
click.secho("This may be due to file encoding issues. Skipping this file.", fg="yellow")
continue
with open(filepath, "r") as file:
contents = file.read()
with open(filepath, "w") as file:
file.write(contents.replace(find, replace))
if find in filename:
new_filename = filename.replace(find, replace)
@@ -202,15 +189,11 @@ def load_env_vars(folder_path):
env_file_path = folder_path / ".env"
env_vars = {}
if env_file_path.exists():
try:
with open(env_file_path, "r", encoding="utf-8") as file:
for line in file:
key, _, value = line.strip().partition("=")
if key and value:
env_vars[key] = value
except UnicodeDecodeError as e:
click.secho(f"Error reading .env file: Unicode decode error - {e}", fg="red")
click.secho("This may be due to file encoding issues. Please ensure the .env file uses UTF-8 encoding.", fg="yellow")
with open(env_file_path, "r") as file:
for line in file:
key, _, value = line.strip().partition("=")
if key and value:
env_vars[key] = value
return env_vars
@@ -261,11 +244,6 @@ def write_env_file(folder_path, env_vars):
- env_vars (dict): A dictionary of environment variables to write.
"""
env_file_path = folder_path / ".env"
try:
with open(env_file_path, "w", encoding="utf-8", newline="\n") as file:
for key, value in env_vars.items():
file.write(f"{key}={value}\n")
except Exception as e:
click.secho(f"Error writing .env file: {e}", fg="red")
click.secho("This may be due to file system permissions or other issues.", fg="yellow")
raise
with open(env_file_path, "w") as file:
for key, value in env_vars.items():
file.write(f"{key}={value}\n")

View File

@@ -1625,3 +1625,127 @@ def test_agent_with_knowledge_sources():
# Assert that the agent provides the correct information
assert "red" in result.raw.lower()
def test_agent_with_feedback_conflict_iteration_params():
"""Test that the agent correctly handles the allow_feedback, allow_conflict, and allow_iteration parameters."""
agent = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
allow_feedback=True,
allow_conflict=True,
allow_iteration=True,
)
assert agent.allow_feedback is True
assert agent.allow_conflict is True
assert agent.allow_iteration is True
# Create another agent with default values
default_agent = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
)
assert default_agent.allow_feedback is False
assert default_agent.allow_conflict is False
assert default_agent.allow_iteration is False
def test_agent_feedback_processing():
"""Test that the agent correctly processes feedback when allow_feedback is enabled."""
from unittest.mock import patch, MagicMock
# Create a mock CrewAgentExecutor
mock_executor = MagicMock()
mock_executor.allow_feedback = True
mock_executor.process_feedback.return_value = True
# Mock the create_agent_executor method at the module level
with patch('crewai.agent.Agent.create_agent_executor', return_value=mock_executor):
# Create an agent with allow_feedback=True
agent = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
allow_feedback=True,
llm=MagicMock() # Mock LLM to avoid API calls
)
executor = agent.create_agent_executor()
assert executor.allow_feedback is True
result = executor.process_feedback("Test feedback")
assert result is True
executor.process_feedback.assert_called_once_with("Test feedback")
def test_agent_conflict_handling():
"""Test that the agent correctly handles conflicts when allow_conflict is enabled."""
from unittest.mock import patch, MagicMock
mock_executor1 = MagicMock()
mock_executor1.allow_conflict = True
mock_executor1.handle_conflict.return_value = True
mock_executor2 = MagicMock()
mock_executor2.allow_conflict = True
with patch('crewai.agent.Agent.create_agent_executor', return_value=mock_executor1):
# Create agents with allow_conflict=True
agent1 = Agent(
role="role1",
goal="goal1",
backstory="backstory1",
allow_conflict=True,
llm=MagicMock() # Mock LLM to avoid API calls
)
agent2 = Agent(
role="role2",
goal="goal2",
backstory="backstory2",
allow_conflict=True,
llm=MagicMock() # Mock LLM to avoid API calls
)
# Get the executors
executor1 = agent1.create_agent_executor()
executor2 = agent2.create_agent_executor()
assert executor1.allow_conflict is True
assert executor2.allow_conflict is True
result = executor1.handle_conflict(executor2)
assert result is True
executor1.handle_conflict.assert_called_once_with(executor2)
def test_agent_iteration_processing():
"""Test that the agent correctly processes iterations when allow_iteration is enabled."""
from unittest.mock import patch, MagicMock
# Create a mock CrewAgentExecutor
mock_executor = MagicMock()
mock_executor.allow_iteration = True
mock_executor.process_iteration.return_value = True
# Mock the create_agent_executor method at the module level
with patch('crewai.agent.Agent.create_agent_executor', return_value=mock_executor):
# Create an agent with allow_iteration=True
agent = Agent(
role="test role",
goal="test goal",
backstory="test backstory",
allow_iteration=True,
llm=MagicMock() # Mock LLM to avoid API calls
)
executor = agent.create_agent_executor()
assert executor.allow_iteration is True
result = executor.process_iteration("Test result")
assert result is True
executor.process_iteration.assert_called_once_with("Test result")

View File

@@ -1,77 +0,0 @@
import os
import tempfile
import unittest
from pathlib import Path
from unittest.mock import MagicMock, patch
import click
from click.testing import CliRunner
from crewai.cli.cli import create
from crewai.cli.create_crew import create_crew
class TestCreateCrew(unittest.TestCase):
def setUp(self):
self.runner = CliRunner()
self.temp_dir = tempfile.TemporaryDirectory()
self.test_dir = Path(self.temp_dir.name)
def tearDown(self):
self.temp_dir.cleanup()
@patch("crewai.cli.create_crew.get_provider_data")
@patch("crewai.cli.create_crew.select_provider")
@patch("crewai.cli.create_crew.select_model")
@patch("crewai.cli.create_crew.write_env_file")
@patch("crewai.cli.create_crew.load_env_vars")
@patch("click.confirm")
def test_create_crew_handles_unicode(self, mock_confirm, mock_load_env,
mock_write_env, mock_select_model,
mock_select_provider, mock_get_provider_data):
"""Test that create_crew command handles Unicode properly."""
mock_confirm.return_value = True
mock_load_env.return_value = {}
mock_get_provider_data.return_value = {"openai": ["gpt-4"]}
mock_select_provider.return_value = "openai"
mock_select_model.return_value = "gpt-4"
templates_dir = Path("src/crewai/cli/templates/crew")
templates_dir.mkdir(parents=True, exist_ok=True)
template_content = """
Hello {{name}}! Unicode test: 你好, こんにちは, Привет 🚀
Class: {{crew_name}}
Folder: {{folder_name}}
"""
(templates_dir / "tools").mkdir(exist_ok=True)
(templates_dir / "config").mkdir(exist_ok=True)
for file_name in [".gitignore", "pyproject.toml", "README.md", "__init__.py", "main.py", "crew.py"]:
with open(templates_dir / file_name, "w", encoding="utf-8") as f:
f.write(template_content)
(templates_dir / "knowledge").mkdir(exist_ok=True)
with open(templates_dir / "knowledge" / "user_preference.txt", "w", encoding="utf-8") as f:
f.write(template_content)
for file_path in ["tools/custom_tool.py", "tools/__init__.py", "config/agents.yaml", "config/tasks.yaml"]:
(templates_dir / file_path).parent.mkdir(exist_ok=True, parents=True)
with open(templates_dir / file_path, "w", encoding="utf-8") as f:
f.write(template_content)
with patch("crewai.cli.create_crew.Path") as mock_path:
mock_path.return_value = self.test_dir
mock_path.side_effect = lambda x: self.test_dir / x if isinstance(x, str) else x
create_crew("test_crew", skip_provider=True)
crew_dir = self.test_dir / "test_crew"
for root, _, files in os.walk(crew_dir):
for file in files:
file_path = os.path.join(root, file)
with open(file_path, "r", encoding="utf-8") as f:
content = f.read()
self.assertIn("你好", content, f"Unicode characters not preserved in {file_path}")
self.assertIn("🚀", content, f"Emoji not preserved in {file_path}")

View File

@@ -1,89 +0,0 @@
import os
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch
from crewai.cli.provider import fetch_provider_data, read_cache_file
from crewai.cli.utils import (
copy_template,
load_env_vars,
tree_find_and_replace,
write_env_file,
)
class TestEncoding(unittest.TestCase):
def setUp(self):
self.temp_dir = tempfile.TemporaryDirectory()
self.test_dir = Path(self.temp_dir.name)
self.unicode_content = "Hello Unicode: 你好, こんにちは, Привет, مرحبا, 안녕하세요 🚀"
self.src_file = self.test_dir / "src_file.txt"
self.dst_file = self.test_dir / "dst_file.txt"
with open(self.src_file, "w", encoding="utf-8") as f:
f.write(self.unicode_content)
def tearDown(self):
self.temp_dir.cleanup()
def test_copy_template_handles_unicode(self):
"""Test that copy_template handles Unicode characters properly in all environments."""
copy_template(
self.src_file,
self.dst_file,
"test_name",
"TestClass",
"test_folder"
)
with open(self.dst_file, "r", encoding="utf-8") as f:
content = f.read()
self.assertIn("你好", content)
self.assertIn("こんにちは", content)
self.assertIn("🚀", content)
def test_env_vars_handle_unicode(self):
"""Test that environment variable functions handle Unicode characters properly."""
test_env_path = self.test_dir / ".env"
test_env_vars = {
"KEY1": "Value with Unicode: 你好",
"KEY2": "More Unicode: こんにちは 🚀"
}
write_env_file(self.test_dir, test_env_vars)
loaded_vars = load_env_vars(self.test_dir)
self.assertEqual(loaded_vars["KEY1"], "Value with Unicode: 你好")
self.assertEqual(loaded_vars["KEY2"], "More Unicode: こんにちは 🚀")
def test_tree_find_and_replace_handles_unicode(self):
"""Test that tree_find_and_replace handles Unicode characters properly."""
test_file = self.test_dir / "replace_test.txt"
with open(test_file, "w", encoding="utf-8") as f:
f.write("Replace this: PLACEHOLDER with Unicode: 你好")
tree_find_and_replace(self.test_dir, "PLACEHOLDER", "🚀")
with open(test_file, "r", encoding="utf-8") as f:
content = f.read()
self.assertIn("Replace this: 🚀 with Unicode: 你好", content)
@patch("crewai.cli.provider.requests.get")
def test_provider_functions_handle_unicode(self, mock_get):
"""Test that provider data functions handle Unicode properly."""
mock_response = unittest.mock.Mock()
mock_response.iter_content.return_value = [self.unicode_content.encode("utf-8")]
mock_response.headers.get.return_value = str(len(self.unicode_content))
mock_get.return_value = mock_response
cache_file = self.test_dir / "cache.json"
with open(cache_file, "w", encoding="utf-8") as f:
f.write('{"model": "Unicode test: 你好 🚀"}')
cache_data = read_cache_file(cache_file)
self.assertEqual(cache_data["model"], "Unicode test: 你好 🚀")