This repository has been archived by the owner on Aug 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
otherwise the user is required to install all the contrib dependencies
- Loading branch information
1 parent
021a14a
commit daa6dc8
Showing
3 changed files
with
50 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from pathlib import Path | ||
from typing import Annotated, List, Optional | ||
|
||
from openassistants.contrib.advisor_function import AdvisorFunction | ||
from openassistants.contrib.duckdb_query import DuckDBQueryFunction | ||
from openassistants.contrib.langchain_ddg_tool import DuckDuckGoToolFunction | ||
from openassistants.contrib.python_eval import PythonEvalFunction | ||
from openassistants.contrib.sqlalchemy_query import QueryFunction | ||
from openassistants.contrib.text_response import TextResponseFunction | ||
from openassistants.functions.base import BaseFunction | ||
from openassistants.functions.crud import BaseFileLibrary | ||
from openassistants.utils import yaml as yaml_utils | ||
from pydantic import Field, TypeAdapter | ||
|
||
AllFunctionTypes = Annotated[ | ||
QueryFunction | ||
| DuckDBQueryFunction | ||
| PythonEvalFunction | ||
| DuckDuckGoToolFunction | ||
| TextResponseFunction | ||
| AdvisorFunction, | ||
Field(json_schema_extra={"discriminator": "type"}), | ||
] | ||
|
||
|
||
class LocalFunctionLibrary(BaseFileLibrary): | ||
def __init__(self, library_id: str, directory: str = "library"): | ||
self.library_id = library_id | ||
self.directory = Path(directory) / library_id | ||
|
||
def read(self, function_id: str) -> Optional[BaseFunction]: | ||
try: | ||
if (yaml_file := self.directory / f"{function_id}.yaml").exists(): | ||
with yaml_file.open() as f: | ||
parsed_yaml = yaml_utils.load(f) | ||
return TypeAdapter(AllFunctionTypes).validate_python( | ||
parsed_yaml | {"id": function_id} | ||
) # type: ignore | ||
else: | ||
return None | ||
except Exception as e: | ||
raise RuntimeError(f"Failed to load: {function_id}") from e | ||
|
||
def list_ids(self) -> List[str]: | ||
return [ | ||
file.stem for file in self.directory.iterdir() if file.suffix == ".yaml" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters