Skip to content
This repository has been archived by the owner on Aug 13, 2024. It is now read-only.

Commit

Permalink
move local library to example
Browse files Browse the repository at this point in the history
otherwise the user is required to install all the contrib dependencies
  • Loading branch information
jordan-wu-97 committed Jan 6, 2024
1 parent 021a14a commit daa6dc8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 46 deletions.
47 changes: 47 additions & 0 deletions examples/fast-api-server/fast_api_server/local_library.py
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"
]
3 changes: 2 additions & 1 deletion packages/openassistants/openassistants/core/assistant.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import asyncio
from typing import Any, Dict, List, Optional, Tuple

from fast_api_server.local_library import LocalFunctionLibrary
from langchain.chat_models.base import BaseChatModel
from langchain.chat_models.openai import ChatOpenAI
from langchain.embeddings import OpenAIEmbeddings
Expand All @@ -19,7 +20,7 @@
IFunction,
IFunctionLibrary,
)
from openassistants.functions.crud import LocalFunctionLibrary, PythonLibrary
from openassistants.functions.crud import PythonLibrary
from openassistants.llm_function_calling.entity_resolution import resolve_entities
from openassistants.llm_function_calling.fallback import perform_general_qa
from openassistants.llm_function_calling.infilling import (
Expand Down
46 changes: 1 addition & 45 deletions packages/openassistants/openassistants/functions/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
import asyncio
import json
from json.decoder import JSONDecodeError
from pathlib import Path
from typing import (
Annotated,
Any,
Callable,
Dict,
Expand All @@ -17,35 +15,17 @@

from langchain.chains.openai_functions.openapi import openapi_spec_to_openai_fn
from langchain_community.utilities.openapi import OpenAPISpec
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_callable import PythonCallableFunction
from openassistants.contrib.python_eval import PythonEvalFunction
from openassistants.contrib.sqlalchemy_query import QueryFunction
from openassistants.contrib.text_response import TextResponseFunction
from openassistants.data_models.function_output import TextOutput
from openassistants.data_models.json_schema import JSONSchema
from openassistants.functions.base import (
BaseFunction,
BaseFunctionParameters,
IFunction,
IFunctionLibrary,
)
from openassistants.utils import yaml as yaml_utils
from pydantic import Field, TypeAdapter
from pydantic import TypeAdapter
from starlette.concurrency import run_in_threadpool

AllFunctionTypes = Annotated[
QueryFunction
| DuckDBQueryFunction
| PythonEvalFunction
| DuckDuckGoToolFunction
| TextResponseFunction
| AdvisorFunction,
Field(json_schema_extra={"discriminator": "type"}),
]


class BaseFileLibrary(IFunctionLibrary, abc.ABC):
@abc.abstractmethod
Expand All @@ -72,30 +52,6 @@ async def get_all_functions(self) -> Sequence[IFunction]:
return funcs # type: ignore


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"
]


class PythonLibrary(IFunctionLibrary):
def __init__(self, functions: Sequence[IFunction]):
self.functions = functions
Expand Down

0 comments on commit daa6dc8

Please sign in to comment.