Skip to content

Commit

Permalink
chore: limiting the scope of modules; removed dead code
Browse files Browse the repository at this point in the history
  • Loading branch information
adubovik committed Sep 5, 2024
1 parent c75256c commit 7bb7781
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 237 deletions.
14 changes: 6 additions & 8 deletions aidial_interceptors_sdk/chat_completion/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
ChatCompletionInterceptor,
)
from aidial_interceptors_sdk.dial_client import DialClient
from aidial_interceptors_sdk.utils.debug import debug_logging
from aidial_interceptors_sdk.utils.exceptions import (
EarlyStreamExit,
dial_exception_decorator,
)
from aidial_interceptors_sdk.utils.reflection import call_with_extra_body
from aidial_interceptors_sdk.error import EarlyStreamExit
from aidial_interceptors_sdk.utils._debug import debug_logging
from aidial_interceptors_sdk.utils._exceptions import dial_exception_decorator
from aidial_interceptors_sdk.utils._reflection import call_with_extra_body
from aidial_interceptors_sdk.utils.streaming import (
block_to_chunk,
block_response_to_streaming_chunk,
handle_streaming_errors,
map_stream,
singleton_stream,
Expand Down Expand Up @@ -72,7 +70,7 @@ async def call_upstream(
f"upstream response[{call_context}]: {json.dumps(resp)}"
)

chunk = block_to_chunk(resp)
chunk = block_response_to_streaming_chunk(resp)
stream = singleton_stream(chunk)
else:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from aidial_interceptors_sdk.chat_completion.response_message_handler import (
ResponseMessageHandler,
)
from aidial_interceptors_sdk.utils.dial_sdk import send_chunk_to_response
from aidial_interceptors_sdk.utils._dial_sdk import send_chunk_to_response
from aidial_interceptors_sdk.utils.not_given import NotGiven


Expand Down
4 changes: 2 additions & 2 deletions aidial_interceptors_sdk/dial_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from aidial_sdk.pydantic_v1 import BaseModel
from openai import AsyncAzureOpenAI

from aidial_interceptors_sdk.utils.env import get_env
from aidial_interceptors_sdk.utils.http_client import get_http_client
from aidial_interceptors_sdk.utils._env import get_env
from aidial_interceptors_sdk.utils._http_client import get_http_client
from aidial_interceptors_sdk.utils.storage import FileStorage

DIAL_URL = get_env("DIAL_URL")
Expand Down
6 changes: 3 additions & 3 deletions aidial_interceptors_sdk/embeddings/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

from aidial_interceptors_sdk.dial_client import DialClient
from aidial_interceptors_sdk.embeddings.base import EmbeddingsInterceptor
from aidial_interceptors_sdk.utils.debug import debug_logging
from aidial_interceptors_sdk.utils.exceptions import dial_exception_decorator
from aidial_interceptors_sdk.utils.reflection import call_with_extra_body
from aidial_interceptors_sdk.utils._debug import debug_logging
from aidial_interceptors_sdk.utils._exceptions import dial_exception_decorator
from aidial_interceptors_sdk.utils._reflection import call_with_extra_body


def interceptor_to_embeddings_handler(cls: Type[EmbeddingsInterceptor]):
Expand Down
4 changes: 4 additions & 0 deletions aidial_interceptors_sdk/error.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class EarlyStreamExit(Exception):
"""
Thrown when one needs to end the stream prematurely.
"""
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
from aidial_interceptors_sdk.chat_completion.base import (
ChatCompletionInterceptor,
)
from aidial_interceptors_sdk.error import EarlyStreamExit
from aidial_interceptors_sdk.examples.utils.lru_cache import LRUCache
from aidial_interceptors_sdk.utils.exceptions import EarlyStreamExit

_log = logging.getLogger(__name__)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
SpacyAnonymizer,
)
from aidial_interceptors_sdk.examples.utils.markdown import MarkdownTable
from aidial_interceptors_sdk.utils.env import get_env_list
from aidial_interceptors_sdk.utils._env import get_env_list

PII_ANONYMIZER_LABELS_TO_REDACT = get_env_list(
"PII_ANONYMIZER_LABELS_TO_REDACT", DEFAULT_LABELS_TO_REDACT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
ChatCompletionInterceptor,
)
from aidial_interceptors_sdk.chat_completion.element_path import ElementPath
from aidial_interceptors_sdk.examples.utils.dict import collect_at_path
from aidial_interceptors_sdk.examples.utils.markdown import MarkdownTable
from aidial_interceptors_sdk.utils.dict import collect_at_path
from aidial_interceptors_sdk.utils.not_given import NotGiven


Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,4 @@
from typing import Any, Callable, Iterable, List, Mapping, TypeVar


def censor_ci_dict(d: Mapping[str, str], keys: List[str]) -> dict:
key_set = {k.lower() for k in keys}
return {
k: v if k.lower() not in key_set else "**********" for k, v in d.items()
}


def get_keys(d: dict, keys: Iterable[str]) -> dict:
return {k: d[k] for k in keys if k in d}

from typing import Any, Callable, List, TypeVar

Path = List[str | int]
PathLike = Path | str
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ def get_env(name: str, err_msg: Optional[str] = None) -> str:
raise Exception(err_msg or f"{name} env variable is not set")


def get_env_bool(name: str, default: bool = False) -> bool:
return os.getenv(name, str(default)).lower() == "true"


def get_env_list(name: str, default: List[str] = []) -> List[str]:
value = os.getenv(name)
if value is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@

import openai
from aidial_sdk.exceptions import HTTPException as DialException
from aidial_sdk.exceptions import internal_server_error
from fastapi.responses import Response

_log = logging.getLogger(__name__)


class EarlyStreamExit(Exception):
"""
Thrown when one needs to end the stream prematurely.
"""


def to_dial_exception(e: Exception) -> Exception:
def _to_dial_exception(e: Exception) -> Exception:
"""
Converting certain interceptor-specific exceptions into DialException.
Expand All @@ -39,31 +31,6 @@ def to_dial_exception(e: Exception) -> Exception:
return e


def to_error_response(e: Exception) -> Response:
if isinstance(e, DialException):
return e.to_fastapi_response()
else:
return internal_server_error(str(e)).to_fastapi_response()


def dial_exception_decorator_response(func):
@wraps(func)
async def wrapper(*args, **kwargs):
try:
return await func(*args, **kwargs)
except Exception as e:
_log.exception(
f"caught exception: {type(e).__module__}.{type(e).__name__}"
)

dial_exception = to_dial_exception(e)
error_response = to_error_response(dial_exception)
_log.debug(f"error response: {error_response}")
return error_response

return wrapper


def dial_exception_decorator(func):
@wraps(func)
async def wrapper(*args, **kwargs):
Expand All @@ -73,6 +40,6 @@ async def wrapper(*args, **kwargs):
_log.exception(
f"caught exception: {type(e).__module__}.{type(e).__name__}"
)
raise to_dial_exception(e) from e
raise _to_dial_exception(e) from e

return wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
async def call_with_extra_body(
func: Callable[..., Coroutine[Any, Any, T]], arg: dict
) -> T:
if has_kwargs_argument(func):
if _has_kwargs_argument(func):
return await func(**arg)

expected_args = set(inspect.signature(func).parameters.keys())
Expand All @@ -31,7 +31,7 @@ async def call_with_extra_body(
return await func(**arg)


def has_kwargs_argument(func: Callable[..., Coroutine[Any, Any, Any]]) -> bool:
def _has_kwargs_argument(func: Callable[..., Coroutine[Any, Any, Any]]) -> bool:
"""
Determines if the given function accepts a variable keyword argument (**kwargs).
"""
Expand Down
106 changes: 6 additions & 100 deletions aidial_interceptors_sdk/utils/storage.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import io
import logging
import mimetypes
from typing import Mapping, Optional, TypedDict
from typing import Mapping
from urllib.parse import urljoin

import aiohttp
Expand All @@ -10,50 +9,14 @@
_log = logging.getLogger(__name__)


class FileMetadata(TypedDict):
name: str
parentPath: str
bucket: str
url: str


class Bucket(TypedDict):
bucket: str
appdata: str | None # Users do not have appdata


class AccessDeniedError(Exception):
def __init__(self, url: str):
super().__init__(f"Access denied: {url!r}")
self.url = url


class FileStorage(BaseModel):
dial_url: str
api_key: str

bucket: Optional[Bucket] = None

@property
def headers(self) -> Mapping[str, str]:
return {"api-key": self.api_key}

async def get_bucket(self, session: aiohttp.ClientSession) -> Bucket:
if self.bucket is not None:
return self.bucket

_log.debug(f"retrieving bucket for {self.dial_url!r}")

async with session.get(
f"{self.dial_url}/v1/bucket",
headers=self.headers,
) as response:
response.raise_for_status()
bucket = await response.json()
self.bucket = bucket
_log.debug(f"bucket: {self.bucket}")
return bucket

@staticmethod
def _to_form_data(
filename: str, content_type: str | None, content: bytes
Expand All @@ -67,68 +30,12 @@ def _to_form_data(
)
return data

async def is_accessible(
self, url: str, session: aiohttp.ClientSession
) -> bool:
try:
await self._get_metadata(url, session)
_log.debug(f"file is accessible: url={url!r}")
return True
except AccessDeniedError:
_log.debug(f"file isn't accessible: url={url!r}")
return False

def to_metadata_url(self, url: str) -> str:
"""
The file metadata URL given url like: files/BUCKET/foo/baz/document.pdf
"""
metadata_url = f"{self.dial_url}/v1/metadata/"
return urljoin(metadata_url, url, allow_fragments=True)

async def _get_metadata(
self,
url: str,
session: aiohttp.ClientSession,
) -> dict:
metadata_url = self.to_metadata_url(url)

_log.debug(f"retrieving metadata: file={url!r}, url={metadata_url!r}")

async with session.get(metadata_url, headers=self.headers) as response:
if not response.ok:
match response.status:
case 403:
raise AccessDeniedError(url)
case _:
response.raise_for_status()

metadata = await response.json()

_log.debug(
f"retrieved metadata: file={url!r}, url={metadata_url!r}, metadata={metadata}"
)

return metadata

async def upload_file(
self,
filename: str,
content_type: str | None,
content: bytes,
session: aiohttp.ClientSession,
) -> FileMetadata:
bucket = await self.get_bucket(session)
appdata = bucket["appdata"]
ext = (content_type and mimetypes.guess_extension(content_type)) or ""
url = f"{self.dial_url}/v1/files/{appdata}/{filename}{ext}"
return await self.upload(url, content_type, content)

async def upload(
self, url: str, content_type: str | None, content: bytes
) -> FileMetadata:
) -> None:
if self.to_dial_url(url) is None:
raise ValueError(f"URL isn't DIAL url: {url!r}")
url = self.to_abs_url(url)
url = self._to_abs_url(url)

data = FileStorage._to_form_data(url, content_type, content)
async with aiohttp.ClientSession() as session:
Expand All @@ -140,24 +47,23 @@ async def upload(
response.raise_for_status()
meta = await response.json()
_log.debug(f"uploaded file: url={url!r}, metadata={meta}")
return meta

def to_dial_url(self, link: str) -> str | None:
url = self.to_abs_url(link)
url = self._to_abs_url(link)
base_url = f"{self.dial_url}/v1/"
if url.startswith(base_url):
return url.removeprefix(base_url)
return None

def to_abs_url(self, link: str) -> str:
def _to_abs_url(self, link: str) -> str:
base_url = f"{self.dial_url}/v1/"
ret = urljoin(base_url, link)
return ret

async def download(self, url: str) -> bytes:
if self.to_dial_url(url) is None:
raise ValueError(f"URL isn't DIAL url: {url!r}")
url = self.to_abs_url(url)
url = self._to_abs_url(url)

async with aiohttp.ClientSession() as session:
async with session.get(url, headers=self.headers) as response:
Expand Down
Loading

0 comments on commit 7bb7781

Please sign in to comment.