mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-07-09 08:55:09 +00:00
Compare commits
1 Commits
devin/1746
...
devin/1744
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3efc5f67fb |
@@ -23,6 +23,7 @@ from crewai.utilities.constants import TRAINED_AGENTS_DATA_FILE, TRAINING_DATA_F
|
||||
from crewai.utilities.converter import generate_model_description
|
||||
from crewai.utilities.token_counter_callback import TokenCalcHandler
|
||||
from crewai.utilities.training_handler import CrewTrainingHandler
|
||||
from crewai.utilities.typing import AgentConfig
|
||||
|
||||
agentops = None
|
||||
|
||||
@@ -88,6 +89,7 @@ class Agent(BaseAgent):
|
||||
function_calling_llm: Optional[Any] = Field(
|
||||
description="Language model that will run the agent.", default=None
|
||||
)
|
||||
config: Optional[Union[Dict[str, Any], AgentConfig]] = Field(default=None)
|
||||
system_template: Optional[str] = Field(
|
||||
default=None, description="System format for the agent."
|
||||
)
|
||||
|
||||
@@ -6,12 +6,11 @@ import shutil
|
||||
import uuid
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
import numpy as np
|
||||
from chromadb.api import ClientAPI
|
||||
|
||||
from crewai.memory.storage.base_rag_storage import BaseRAGStorage
|
||||
from crewai.utilities import EmbeddingConfigurator
|
||||
from crewai.utilities.constants import MAX_FILE_NAME_LENGTH, MEMORY_CHUNK_SIZE, MEMORY_CHUNK_OVERLAP
|
||||
from crewai.utilities.constants import MAX_FILE_NAME_LENGTH
|
||||
from crewai.utilities.paths import db_storage_path
|
||||
|
||||
|
||||
@@ -139,57 +138,15 @@ class RAGStorage(BaseRAGStorage):
|
||||
logging.error(f"Error during {self.type} search: {str(e)}")
|
||||
return []
|
||||
|
||||
def _chunk_text(self, text: str) -> List[str]:
|
||||
"""
|
||||
Split text into chunks to avoid token limits.
|
||||
|
||||
Args:
|
||||
text: Input text to chunk.
|
||||
|
||||
Returns:
|
||||
List[str]: A list of chunked text segments, adhering to defined size and overlap.
|
||||
Empty list if input text is empty.
|
||||
"""
|
||||
if not text:
|
||||
return []
|
||||
|
||||
if len(text) <= MEMORY_CHUNK_SIZE:
|
||||
return [text]
|
||||
|
||||
chunks = []
|
||||
start_indices = range(0, len(text), MEMORY_CHUNK_SIZE - MEMORY_CHUNK_OVERLAP)
|
||||
for i in start_indices:
|
||||
chunk = text[i:i + MEMORY_CHUNK_SIZE]
|
||||
if chunk: # Only add non-empty chunks
|
||||
chunks.append(chunk)
|
||||
|
||||
return chunks
|
||||
|
||||
def _generate_embedding(self, text: str, metadata: Optional[Dict[str, Any]] = None) -> Optional[None]:
|
||||
"""
|
||||
Generate embeddings for text and add to collection.
|
||||
|
||||
Args:
|
||||
text: Input text to generate embeddings for.
|
||||
metadata: Optional metadata to associate with the embeddings.
|
||||
|
||||
Returns:
|
||||
None if successful, None if text is empty.
|
||||
"""
|
||||
def _generate_embedding(self, text: str, metadata: Dict[str, Any]) -> None: # type: ignore
|
||||
if not hasattr(self, "app") or not hasattr(self, "collection"):
|
||||
self._initialize_app()
|
||||
|
||||
chunks = self._chunk_text(text)
|
||||
|
||||
if not chunks:
|
||||
return None
|
||||
|
||||
for chunk in chunks:
|
||||
self.collection.add(
|
||||
documents=[chunk],
|
||||
metadatas=[metadata or {}],
|
||||
ids=[str(uuid.uuid4())],
|
||||
)
|
||||
self.collection.add(
|
||||
documents=[text],
|
||||
metadatas=[metadata or {}],
|
||||
ids=[str(uuid.uuid4())],
|
||||
)
|
||||
|
||||
def reset(self) -> None:
|
||||
try:
|
||||
|
||||
@@ -16,6 +16,12 @@ def after_kickoff(func):
|
||||
|
||||
|
||||
def task(func):
|
||||
"""Decorator to mark a method as a task creator.
|
||||
|
||||
When applied to a method in a class decorated with @CrewBase,
|
||||
this makes the method's return value accessible as an element
|
||||
of the self.tasks list.
|
||||
"""
|
||||
func.is_task = True
|
||||
|
||||
@wraps(func)
|
||||
@@ -29,6 +35,12 @@ def task(func):
|
||||
|
||||
|
||||
def agent(func):
|
||||
"""Decorator to mark a method as an agent creator.
|
||||
|
||||
When applied to a method in a class decorated with @CrewBase,
|
||||
this makes the method's return value accessible as an element
|
||||
of the self.agents list.
|
||||
"""
|
||||
func.is_agent = True
|
||||
func = memoize(func)
|
||||
return func
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import inspect
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Dict, TypeVar, cast
|
||||
from typing import Any, Callable, Dict, List, TypeVar, cast
|
||||
|
||||
import yaml
|
||||
from dotenv import load_dotenv
|
||||
@@ -66,6 +66,9 @@ def CrewBase(cls: T) -> T:
|
||||
self._kickoff = self._filter_functions(
|
||||
self._original_functions, "is_kickoff"
|
||||
)
|
||||
|
||||
self.agents = [] # type: List[Any]
|
||||
self.tasks = [] # type: List[Any]
|
||||
|
||||
@staticmethod
|
||||
def load_yaml(config_path: Path):
|
||||
|
||||
@@ -41,6 +41,7 @@ from crewai.tools.base_tool import BaseTool
|
||||
from crewai.utilities.config import process_config
|
||||
from crewai.utilities.converter import Converter, convert_to_model
|
||||
from crewai.utilities.i18n import I18N
|
||||
from crewai.utilities.typing import TaskConfig
|
||||
|
||||
|
||||
class Task(BaseModel):
|
||||
@@ -74,7 +75,7 @@ class Task(BaseModel):
|
||||
expected_output: str = Field(
|
||||
description="Clear definition of expected output for the task."
|
||||
)
|
||||
config: Optional[Dict[str, Any]] = Field(
|
||||
config: Optional[Union[Dict[str, Any], TaskConfig]] = Field(
|
||||
description="Configuration for the agent",
|
||||
default=None,
|
||||
)
|
||||
|
||||
@@ -4,5 +4,3 @@ DEFAULT_SCORE_THRESHOLD = 0.35
|
||||
KNOWLEDGE_DIRECTORY = "knowledge"
|
||||
MAX_LLM_RETRY = 3
|
||||
MAX_FILE_NAME_LENGTH = 255
|
||||
MEMORY_CHUNK_SIZE = 4000
|
||||
MEMORY_CHUNK_OVERLAP = 200
|
||||
|
||||
14
src/crewai/utilities/typing/__init__.py
Normal file
14
src/crewai/utilities/typing/__init__.py
Normal file
@@ -0,0 +1,14 @@
|
||||
from typing import Dict, List, Optional, Any, TypedDict, Union
|
||||
|
||||
class AgentConfig(TypedDict, total=False):
|
||||
"""TypedDict for agent configuration loaded from YAML."""
|
||||
role: str
|
||||
goal: str
|
||||
backstory: str
|
||||
verbose: bool
|
||||
|
||||
class TaskConfig(TypedDict, total=False):
|
||||
"""TypedDict for task configuration loaded from YAML."""
|
||||
description: str
|
||||
expected_output: str
|
||||
agent: str # Role of the agent to execute this task
|
||||
@@ -1,86 +0,0 @@
|
||||
import pytest
|
||||
import numpy as np
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
from crewai.memory.short_term.short_term_memory import ShortTermMemory
|
||||
from crewai.agent import Agent
|
||||
from crewai.crew import Crew
|
||||
from crewai.task import Task
|
||||
from crewai.utilities.constants import MEMORY_CHUNK_SIZE
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def short_term_memory():
|
||||
"""Fixture to create a ShortTermMemory instance"""
|
||||
agent = Agent(
|
||||
role="Researcher",
|
||||
goal="Search relevant data and provide results",
|
||||
backstory="You are a researcher at a leading tech think tank.",
|
||||
tools=[],
|
||||
verbose=True,
|
||||
)
|
||||
|
||||
task = Task(
|
||||
description="Perform a search on specific topics.",
|
||||
expected_output="A list of relevant URLs based on the search query.",
|
||||
agent=agent,
|
||||
)
|
||||
return ShortTermMemory(crew=Crew(agents=[agent], tasks=[task]))
|
||||
|
||||
|
||||
def test_memory_with_large_input(short_term_memory):
|
||||
"""Test that memory can handle large inputs without token limit errors"""
|
||||
large_input = "test value " * (MEMORY_CHUNK_SIZE + 1000)
|
||||
|
||||
with patch.object(
|
||||
short_term_memory.storage, '_chunk_text',
|
||||
return_value=["chunk1", "chunk2"]
|
||||
) as mock_chunk_text:
|
||||
with patch.object(
|
||||
short_term_memory.storage.collection, 'add'
|
||||
) as mock_add:
|
||||
short_term_memory.save(value=large_input, agent="test_agent")
|
||||
|
||||
assert mock_chunk_text.called
|
||||
|
||||
with patch.object(
|
||||
short_term_memory.storage, 'search',
|
||||
return_value=[{"context": large_input, "metadata": {"agent": "test_agent"}, "score": 0.95}]
|
||||
):
|
||||
result = short_term_memory.search(large_input[:100], score_threshold=0.01)
|
||||
assert result[0]["context"] == large_input
|
||||
assert result[0]["metadata"]["agent"] == "test_agent"
|
||||
|
||||
|
||||
def test_memory_with_empty_input(short_term_memory):
|
||||
"""Test that memory correctly handles empty input strings"""
|
||||
empty_input = ""
|
||||
|
||||
with patch.object(
|
||||
short_term_memory.storage, '_chunk_text',
|
||||
return_value=[]
|
||||
) as mock_chunk_text:
|
||||
with patch.object(
|
||||
short_term_memory.storage.collection, 'add'
|
||||
) as mock_add:
|
||||
short_term_memory.save(value=empty_input, agent="test_agent")
|
||||
|
||||
mock_chunk_text.assert_called_with(empty_input)
|
||||
mock_add.assert_not_called()
|
||||
|
||||
|
||||
def test_memory_with_exact_chunk_size_input(short_term_memory):
|
||||
"""Test that memory correctly handles inputs that match chunk size exactly"""
|
||||
exact_size_input = "x" * MEMORY_CHUNK_SIZE
|
||||
|
||||
with patch.object(
|
||||
short_term_memory.storage, '_chunk_text',
|
||||
return_value=[exact_size_input]
|
||||
) as mock_chunk_text:
|
||||
with patch.object(
|
||||
short_term_memory.storage.collection, 'add'
|
||||
) as mock_add:
|
||||
short_term_memory.save(value=exact_size_input, agent="test_agent")
|
||||
|
||||
mock_chunk_text.assert_called_with(exact_size_input)
|
||||
assert mock_add.call_count == 1
|
||||
55
tests/typing_test.py
Normal file
55
tests/typing_test.py
Normal file
@@ -0,0 +1,55 @@
|
||||
from typing import Dict, Any
|
||||
|
||||
import pytest
|
||||
|
||||
from crewai.agent import Agent
|
||||
from crewai.task import Task
|
||||
from crewai.utilities.typing import AgentConfig, TaskConfig
|
||||
|
||||
|
||||
def test_agent_with_config_dict():
|
||||
config: AgentConfig = {
|
||||
"role": "Test Agent",
|
||||
"goal": "Test Goal",
|
||||
"backstory": "Test Backstory",
|
||||
"verbose": True
|
||||
}
|
||||
|
||||
agent = Agent(config=config)
|
||||
|
||||
assert agent.role == "Test Agent"
|
||||
assert agent.goal == "Test Goal"
|
||||
assert agent.backstory == "Test Backstory"
|
||||
assert agent.verbose is True
|
||||
|
||||
|
||||
def test_agent_with_yaml_config():
|
||||
config: Dict[str, Any] = {
|
||||
"researcher": {
|
||||
"role": "Researcher",
|
||||
"goal": "Research Goal",
|
||||
"backstory": "Researcher Backstory",
|
||||
"verbose": True
|
||||
}
|
||||
}
|
||||
|
||||
agent = Agent(config=config["researcher"])
|
||||
|
||||
assert agent.role == "Researcher"
|
||||
assert agent.goal == "Research Goal"
|
||||
assert agent.backstory == "Researcher Backstory"
|
||||
|
||||
|
||||
def test_task_with_config_dict():
|
||||
config: TaskConfig = {
|
||||
"description": "Test Task",
|
||||
"expected_output": "Test Output",
|
||||
"agent": "researcher"
|
||||
}
|
||||
|
||||
agent = Agent(role="Researcher", goal="Goal", backstory="Backstory")
|
||||
task = Task(config=config, agent=agent)
|
||||
|
||||
assert task.description == "Test Task"
|
||||
assert task.expected_output == "Test Output"
|
||||
assert task.agent == agent
|
||||
Reference in New Issue
Block a user