Uniform valid suffixes and clarify exceptions (#9463)

**Description**:
- Uniformed the current valid suffixes (file formats) for loading agents
from hubs and files (to better handle future additions);
 - Clarified exception messages (also in unit test).
This commit is contained in:
Lorenzo 2023-08-19 06:35:53 +02:00 committed by GitHub
parent 9f545825b7
commit 5b3dbf12a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 6 deletions

View File

@ -97,8 +97,9 @@ def load_agent(
Returns: Returns:
An agent executor. An agent executor.
""" """
valid_suffixes = {"json", "yaml"}
if hub_result := try_load_from_hub( if hub_result := try_load_from_hub(
path, _load_agent_from_file, "agents", {"json", "yaml"} path, _load_agent_from_file, "agents", valid_suffixes
): ):
return hub_result return hub_result
else: else:
@ -109,19 +110,20 @@ def _load_agent_from_file(
file: Union[str, Path], **kwargs: Any file: Union[str, Path], **kwargs: Any
) -> Union[BaseSingleActionAgent, BaseMultiActionAgent]: ) -> Union[BaseSingleActionAgent, BaseMultiActionAgent]:
"""Load agent from file.""" """Load agent from file."""
valid_suffixes = {"json", "yaml"}
# Convert file to Path object. # Convert file to Path object.
if isinstance(file, str): if isinstance(file, str):
file_path = Path(file) file_path = Path(file)
else: else:
file_path = file file_path = file
# Load from either json or yaml. # Load from either json or yaml.
if file_path.suffix == ".json": if file_path.suffix[1:] == "json":
with open(file_path) as f: with open(file_path) as f:
config = json.load(f) config = json.load(f)
elif file_path.suffix == ".yaml": elif file_path.suffix[1:] == "yaml":
with open(file_path, "r") as f: with open(file_path, "r") as f:
config = yaml.safe_load(f) config = yaml.safe_load(f)
else: else:
raise ValueError("File type must be json or yaml") raise ValueError(f"Unsupported file type, must be one of {valid_suffixes}.")
# Load the agent from the config now. # Load the agent from the config now.
return load_agent_from_config(config, **kwargs) return load_agent_from_config(config, **kwargs)

View File

@ -35,7 +35,7 @@ def try_load_from_hub(
if remote_path.parts[0] != valid_prefix: if remote_path.parts[0] != valid_prefix:
return None return None
if remote_path.suffix[1:] not in valid_suffixes: if remote_path.suffix[1:] not in valid_suffixes:
raise ValueError("Unsupported file type.") raise ValueError(f"Unsupported file type, must be one of {valid_suffixes}.")
# Using Path with URLs is not recommended, because on Windows # Using Path with URLs is not recommended, because on Windows
# the backslash is used as the path separator, which can cause issues # the backslash is used as the path separator, which can cause issues

View File

@ -48,7 +48,9 @@ def test_invalid_suffix() -> None:
loader = Mock() loader = Mock()
valid_suffixes = {"json"} valid_suffixes = {"json"}
with pytest.raises(ValueError, match="Unsupported file type."): with pytest.raises(
ValueError, match=f"Unsupported file type, must be one of {valid_suffixes}."
):
try_load_from_hub(path, loader, "chains", valid_suffixes) try_load_from_hub(path, loader, "chains", valid_suffixes)
loader.assert_not_called() loader.assert_not_called()