mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 16:18:30 +00:00
feat: Add inject_date flag to Agent for automatic date injection
Co-Authored-By: Joe Moura <joao@crewai.com>
This commit is contained in:
22
docs/usage_examples/inject_date_example.py
Normal file
22
docs/usage_examples/inject_date_example.py
Normal file
@@ -0,0 +1,22 @@
|
||||
from crewai import Agent, Task, Crew
|
||||
|
||||
agent = Agent(
|
||||
role="research_analyst",
|
||||
goal="Provide timely and accurate research",
|
||||
backstory="You are a research analyst who always provides up-to-date information.",
|
||||
inject_date=True, # Enable automatic date injection
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Research market trends and provide analysis",
|
||||
expected_output="A comprehensive report on current market trends",
|
||||
agent=agent,
|
||||
)
|
||||
|
||||
crew = Crew(
|
||||
agents=[agent],
|
||||
tasks=[task],
|
||||
)
|
||||
|
||||
result = crew.kickoff()
|
||||
print(result)
|
||||
@@ -115,6 +115,10 @@ class Agent(BaseAgent):
|
||||
default=False,
|
||||
description="Whether the agent is multimodal.",
|
||||
)
|
||||
inject_date: bool = Field(
|
||||
default=False,
|
||||
description="Whether to automatically inject the current date into tasks.",
|
||||
)
|
||||
code_execution_mode: Literal["safe", "unsafe"] = Field(
|
||||
default="safe",
|
||||
description="Mode for code execution: 'safe' (using Docker) or 'unsafe' (direct execution).",
|
||||
@@ -248,6 +252,11 @@ class Agent(BaseAgent):
|
||||
else:
|
||||
print(f"Error during reasoning process: {str(e)}")
|
||||
|
||||
if self.inject_date:
|
||||
from datetime import datetime
|
||||
current_date = datetime.now().strftime("%Y-%m-%d")
|
||||
task.description += f"\n\nCurrent Date: {current_date}"
|
||||
|
||||
if self.tools_handler:
|
||||
self.tools_handler.last_used_tool = {} # type: ignore # Incompatible types in assignment (expression has type "dict[Never, Never]", variable has type "ToolCalling")
|
||||
|
||||
|
||||
55
tests/test_agent_inject_date.py
Normal file
55
tests/test_agent_inject_date.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import re
|
||||
from datetime import datetime
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from crewai.agent import Agent
|
||||
from crewai.task import Task
|
||||
|
||||
|
||||
def test_agent_inject_date():
|
||||
"""Test that the inject_date flag injects the current date into the task."""
|
||||
agent = Agent(
|
||||
role="test_agent",
|
||||
goal="test_goal",
|
||||
backstory="test_backstory",
|
||||
inject_date=True,
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Test task",
|
||||
expected_output="Test output",
|
||||
agent=agent,
|
||||
)
|
||||
|
||||
with patch.object(Agent, 'execute_task', return_value="Task executed") as mock_execute:
|
||||
agent.execute_task(task)
|
||||
|
||||
called_task = mock_execute.call_args[0][0]
|
||||
|
||||
current_date = datetime.now().strftime("%Y-%m-%d")
|
||||
assert f"Current Date: {current_date}" in called_task.description
|
||||
|
||||
|
||||
def test_agent_without_inject_date():
|
||||
"""Test that without inject_date flag, no date is injected."""
|
||||
agent = Agent(
|
||||
role="test_agent",
|
||||
goal="test_goal",
|
||||
backstory="test_backstory",
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Test task",
|
||||
expected_output="Test output",
|
||||
agent=agent,
|
||||
)
|
||||
|
||||
original_description = task.description
|
||||
|
||||
with patch.object(Agent, 'execute_task', return_value="Task executed") as mock_execute:
|
||||
agent.execute_task(task)
|
||||
|
||||
called_task = mock_execute.call_args[0][0]
|
||||
|
||||
assert "Current Date:" not in called_task.description
|
||||
assert called_task.description == original_description
|
||||
Reference in New Issue
Block a user