mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-28 17:48:13 +00:00
Compare commits
4 Commits
devin/1742
...
63028e1b20
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
63028e1b20 | ||
|
|
81759e8c72 | ||
|
|
27472ba69e | ||
|
|
25aa774d8c |
@@ -14,13 +14,13 @@ class Knowledge(BaseModel):
|
||||
Knowledge is a collection of sources and setup for the vector store to save and query relevant context.
|
||||
Args:
|
||||
sources: List[BaseKnowledgeSource] = Field(default_factory=list)
|
||||
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
|
||||
storage: Optional[KnowledgeStorage] = Field(default=None)
|
||||
embedder_config: Optional[Dict[str, Any]] = None
|
||||
"""
|
||||
|
||||
sources: List[BaseKnowledgeSource] = Field(default_factory=list)
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
|
||||
storage: Optional[KnowledgeStorage] = Field(default=None)
|
||||
embedder_config: Optional[Dict[str, Any]] = None
|
||||
collection_name: Optional[str] = None
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
|
||||
default_factory=list, description="The path to the file"
|
||||
)
|
||||
content: Dict[Path, str] = Field(init=False, default_factory=dict)
|
||||
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
|
||||
storage: Optional[KnowledgeStorage] = Field(default=None)
|
||||
safe_file_paths: List[Path] = Field(default_factory=list)
|
||||
|
||||
@field_validator("file_path", "file_paths", mode="before")
|
||||
@@ -62,7 +62,10 @@ class BaseFileKnowledgeSource(BaseKnowledgeSource, ABC):
|
||||
|
||||
def _save_documents(self):
|
||||
"""Save the documents to the storage."""
|
||||
self.storage.save(self.chunks)
|
||||
if self.storage:
|
||||
self.storage.save(self.chunks)
|
||||
else:
|
||||
raise ValueError("No storage found to save documents.")
|
||||
|
||||
def convert_to_path(self, path: Union[Path, str]) -> Path:
|
||||
"""Convert a path to a Path object."""
|
||||
|
||||
@@ -16,7 +16,7 @@ class BaseKnowledgeSource(BaseModel, ABC):
|
||||
chunk_embeddings: List[np.ndarray] = Field(default_factory=list)
|
||||
|
||||
model_config = ConfigDict(arbitrary_types_allowed=True)
|
||||
storage: KnowledgeStorage = Field(default_factory=KnowledgeStorage)
|
||||
storage: Optional[KnowledgeStorage] = Field(default=None)
|
||||
metadata: Dict[str, Any] = Field(default_factory=dict) # Currently unused
|
||||
collection_name: Optional[str] = Field(default=None)
|
||||
|
||||
@@ -46,4 +46,7 @@ class BaseKnowledgeSource(BaseModel, ABC):
|
||||
Save the documents to the storage.
|
||||
This method should be called after the chunks and embeddings are generated.
|
||||
"""
|
||||
self.storage.save(self.chunks)
|
||||
if self.storage:
|
||||
self.storage.save(self.chunks)
|
||||
else:
|
||||
raise ValueError("No storage found to save documents.")
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import inspect
|
||||
from pathlib import Path
|
||||
from typing import Any, Callable, Dict, List, TypeVar, Union, cast
|
||||
from typing import Any, Callable, Dict, TypeVar, cast
|
||||
|
||||
import yaml
|
||||
from dotenv import load_dotenv
|
||||
@@ -116,33 +116,13 @@ def CrewBase(cls: T) -> T:
|
||||
def _map_agent_variables(
|
||||
self,
|
||||
agent_name: str,
|
||||
agent_info: Union[Dict[str, Any], List[Dict[str, Any]]],
|
||||
agent_info: Dict[str, Any],
|
||||
agents: Dict[str, Callable],
|
||||
llms: Dict[str, Callable],
|
||||
tool_functions: Dict[str, Callable],
|
||||
cache_handler_functions: Dict[str, Callable],
|
||||
callbacks: Dict[str, Callable],
|
||||
) -> None:
|
||||
"""Maps agent variables from configuration to internal state.
|
||||
|
||||
Args:
|
||||
agent_name: Name of the agent.
|
||||
agent_info: Configuration as a dictionary or list of configurations.
|
||||
agents: Dictionary of agent functions.
|
||||
llms: Dictionary of LLM functions.
|
||||
tool_functions: Dictionary of tool functions.
|
||||
cache_handler_functions: Dictionary of cache handler functions.
|
||||
callbacks: Dictionary of callback functions.
|
||||
|
||||
Raises:
|
||||
ValueError: When an empty list is provided as agent_info.
|
||||
"""
|
||||
# If agent_info is a list, use the first item as the configuration
|
||||
if isinstance(agent_info, list):
|
||||
if not agent_info:
|
||||
raise ValueError(f"Empty agent configuration list for agent {agent_name}")
|
||||
agent_info = agent_info[0]
|
||||
|
||||
if llm := agent_info.get("llm"):
|
||||
try:
|
||||
self.agents_config[agent_name]["llm"] = llms[llm]()
|
||||
|
||||
@@ -1,115 +0,0 @@
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
import yaml
|
||||
|
||||
|
||||
class TestYamlConfig:
|
||||
"""Tests for YAML configuration handling."""
|
||||
|
||||
def test_list_format_in_yaml(self):
|
||||
"""Test that list format in YAML is handled correctly."""
|
||||
# Create a test YAML content with list format
|
||||
yaml_content = """
|
||||
test_agent:
|
||||
- name: test_agent
|
||||
role: Test Agent
|
||||
goal: Test Goal
|
||||
"""
|
||||
|
||||
# Parse the YAML content
|
||||
data = yaml.safe_load(yaml_content)
|
||||
|
||||
# Get the agent_info which should be a list
|
||||
agent_name = "test_agent"
|
||||
agent_info = data[agent_name]
|
||||
|
||||
# Verify it's a list
|
||||
assert isinstance(agent_info, list)
|
||||
|
||||
# Create a function that simulates the behavior of _map_agent_variables
|
||||
# with our fix applied
|
||||
def map_agent_variables(agent_name, agent_info):
|
||||
# This is the fix we implemented
|
||||
if isinstance(agent_info, list):
|
||||
if not agent_info:
|
||||
raise ValueError(f"Empty agent configuration list for agent {agent_name}")
|
||||
agent_info = agent_info[0]
|
||||
|
||||
# Try to access a dictionary method on agent_info
|
||||
# This would fail with AttributeError if agent_info is still a list
|
||||
value = agent_info.get("name")
|
||||
return value
|
||||
|
||||
# Call the function - this would raise AttributeError before the fix
|
||||
result = map_agent_variables(agent_name, agent_info)
|
||||
|
||||
def test_empty_list_in_yaml(self):
|
||||
"""Test that empty list in YAML raises appropriate error."""
|
||||
# Create a test YAML content with empty list
|
||||
yaml_content = """
|
||||
test_agent: []
|
||||
"""
|
||||
|
||||
# Parse the YAML content
|
||||
data = yaml.safe_load(yaml_content)
|
||||
|
||||
# Get the agent_info which should be an empty list
|
||||
agent_name = "test_agent"
|
||||
agent_info = data[agent_name]
|
||||
|
||||
# Verify it's a list
|
||||
assert isinstance(agent_info, list)
|
||||
assert len(agent_info) == 0
|
||||
|
||||
# Create a function that simulates the behavior of _map_agent_variables
|
||||
def map_agent_variables(agent_name, agent_info):
|
||||
if isinstance(agent_info, list):
|
||||
if not agent_info:
|
||||
raise ValueError(f"Empty agent configuration list for agent {agent_name}")
|
||||
agent_info = agent_info[0]
|
||||
return agent_info
|
||||
|
||||
# Call the function - should raise ValueError
|
||||
with pytest.raises(ValueError, match=f"Empty agent configuration list for agent {agent_name}"):
|
||||
map_agent_variables(agent_name, agent_info)
|
||||
def test_multiple_items_in_list(self):
|
||||
"""Test that when multiple items are in the list, the first one is used."""
|
||||
# Create a test YAML content with multiple items in the list
|
||||
yaml_content = """
|
||||
test_agent:
|
||||
- name: first_agent
|
||||
role: First Agent
|
||||
goal: First Goal
|
||||
- name: second_agent
|
||||
role: Second Agent
|
||||
goal: Second Goal
|
||||
"""
|
||||
|
||||
# Parse the YAML content
|
||||
data = yaml.safe_load(yaml_content)
|
||||
|
||||
# Get the agent_info which should be a list
|
||||
agent_name = "test_agent"
|
||||
agent_info = data[agent_name]
|
||||
|
||||
# Verify it's a list with multiple items
|
||||
assert isinstance(agent_info, list)
|
||||
assert len(agent_info) > 1
|
||||
|
||||
# Create a function that simulates the behavior of _map_agent_variables
|
||||
def map_agent_variables(agent_name, agent_info):
|
||||
if isinstance(agent_info, list):
|
||||
if not agent_info:
|
||||
raise ValueError(f"Empty agent configuration list for agent {agent_name}")
|
||||
agent_info = agent_info[0]
|
||||
return agent_info.get("name")
|
||||
|
||||
# Call the function - should return name from the first item
|
||||
result = map_agent_variables(agent_name, agent_info)
|
||||
|
||||
# Verify only the first item was used
|
||||
assert result == "first_agent"
|
||||
Reference in New Issue
Block a user