Skip to content

Commit

Permalink
fix: linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
csgulati09 committed Feb 9, 2024
1 parent b82a9dd commit bc9758f
Show file tree
Hide file tree
Showing 16 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion portkey_ai/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Prompts,
AsyncPrompts,
Portkey,
AsyncPortkey
AsyncPortkey,
)
from portkey_ai.version import VERSION
from portkey_ai.api_resources.global_constants import (
Expand Down
2 changes: 1 addition & 1 deletion portkey_ai/api_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,5 @@
"AsyncFeedback",
"createHeaders",
"Portkey",
"AsyncPortkey"
"AsyncPortkey",
]
2 changes: 1 addition & 1 deletion portkey_ai/api_resources/apis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
"Post",
"AsyncPost",
"Embeddings",
"AsyncEmbeddings"
"AsyncEmbeddings",
]
6 changes: 4 additions & 2 deletions portkey_ai/api_resources/apis/api_resource.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from portkey_ai.api_resources.base_client import APIClient, AsyncAPIClient
import asyncio


class APIResource:
_client: APIClient
# _get: Any
Expand All @@ -18,6 +19,7 @@ def __init__(self, client: APIClient) -> None:
def _post(self, *args, **kwargs):
return self._client._post(*args, **kwargs)


class AsyncAPIResource:
_client: AsyncAPIClient

Expand All @@ -30,6 +32,6 @@ def __init__(self, client: AsyncAPIClient) -> None:

async def _post(self, *args, **kwargs):
return await self._client._post(*args, **kwargs)

async def _sleep(self, seconds: float) -> None:
await asyncio.sleep(seconds)
await asyncio.sleep(seconds)
3 changes: 3 additions & 0 deletions portkey_ai/api_resources/apis/chat_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ def __init__(self, client: APIClient) -> None:
super().__init__(client)
self.completions = Completions(client)


class AsyncChatCompletion(AsyncAPIResource):
completions: AsyncCompletions

def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
self.completions = AsyncCompletions(client)


class Completions(APIResource):
def __init__(self, client: APIClient) -> None:
super().__init__(client)
Expand Down Expand Up @@ -114,6 +116,7 @@ def create(
def _get_config_string(self, config: Union[Mapping, str]) -> str:
return config if isinstance(config, str) else json.dumps(config)


class AsyncCompletions(AsyncAPIResource):
def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
Expand Down
1 change: 1 addition & 0 deletions portkey_ai/api_resources/apis/complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def create(
headers={},
)


class AsyncCompletion(AsyncAPIResource):
def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
Expand Down
3 changes: 2 additions & 1 deletion portkey_ai/api_resources/apis/embeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def create(
headers={},
)


class AsyncEmbeddings(AsyncAPIResource):
def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
Expand Down Expand Up @@ -68,4 +69,4 @@ async def create(
stream_cls=None,
stream=False,
headers={},
)
)
2 changes: 1 addition & 1 deletion portkey_ai/api_resources/apis/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def bulk_create(self, *, feedbacks: List[Dict[str, Any]]) -> GenericResponse:
headers={},
)


class AsyncFeedback(AsyncAPIResource):
def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
Expand Down Expand Up @@ -74,4 +75,3 @@ async def bulk_create(self, *, feedbacks: List[Dict[str, Any]]) -> GenericRespon
stream=False,
headers={},
)

5 changes: 5 additions & 0 deletions portkey_ai/api_resources/apis/generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ def create(
response["warning"] = warning_message
return response


class AsyncGenerations(AsyncAPIResource):
def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
Expand Down Expand Up @@ -75,20 +76,23 @@ async def create(
response["warning"] = warning_message
return response


class Prompts(APIResource):
completions: Completions

def __init__(self, client: APIClient) -> None:
super().__init__(client)
self.completions = Completions(client)


class AsyncPrompts(AsyncAPIResource):
completions: AsyncCompletions

def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
self.completions = AsyncCompletions(client)


class Completions(APIResource):
def __init__(self, client: APIClient) -> None:
super().__init__(client)
Expand Down Expand Up @@ -176,6 +180,7 @@ def create(
headers={},
)


class AsyncCompletions(AsyncAPIResource):
def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
Expand Down
3 changes: 2 additions & 1 deletion portkey_ai/api_resources/apis/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def create(
headers={},
)


class AsyncPost(AsyncAPIResource):
def __init__(self, client: AsyncAPIClient) -> None:
super().__init__(client)
Expand Down Expand Up @@ -107,4 +108,4 @@ async def create(
stream_cls=AsyncStream[GenericResponse],
stream=stream,
headers={},
)
)
12 changes: 5 additions & 7 deletions portkey_ai/api_resources/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ def __del__(self) -> None:
except Exception:
pass


class AsyncAPIClient:
_client: httpx.AsyncClient
_default_stream_cls: Union[type[AsyncStream[Any]], None] = None
Expand Down Expand Up @@ -420,7 +420,7 @@ def __init__(
metadata=metadata,
**kwargs,
)

self._client = AsyncHttpxClientWrapper(
base_url=self.base_url,
headers={
Expand All @@ -430,11 +430,11 @@ def __init__(

self.response_headers: httpx.Headers | None = None

async def _serialize_header_values(
def _serialize_header_values(
self, headers: Optional[Mapping[str, Any]]
) -> Dict[str, str]:
if headers is None:
return await {}
return {}
return {
f"{PORTKEY_HEADER_PREFIX}{k}": json.dumps(v)
if isinstance(v, (dict, list))
Expand All @@ -446,7 +446,6 @@ async def _serialize_header_values(
def custom_auth(self) -> Optional[httpx.Auth]:
return None


@overload
async def _post(
self,
Expand Down Expand Up @@ -697,7 +696,7 @@ async def _request(
cast_to(**res.json()),
)
if not isinstance(cast_to, httpx.Response)
else cast(ResponseT, res)
else cast(ResponseT, res)
)
response._headers = res.headers # type: ignore
return response
Expand Down Expand Up @@ -726,4 +725,3 @@ def _make_status_error_from_response(
err_msg = err_text or f"Error code: {response.status_code}"

return make_status_error(err_msg, body=body, request=request, response=response)

1 change: 1 addition & 0 deletions portkey_ai/api_resources/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def post(self, url: str, **kwargs):

with_options = copy


class AsyncPortkey(AsyncAPIClient):
completions: apis.AsyncCompletion
chat: apis.AsyncChatCompletion
Expand Down
2 changes: 1 addition & 1 deletion portkey_ai/api_resources/common_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
bound=AsyncStream[
Union[ChatCompletionChunk, TextCompletionChunk, GenericResponse, httpx.Response]
],
)
)
11 changes: 8 additions & 3 deletions portkey_ai/api_resources/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ def iter(self, iterator: Iterator[str]) -> Iterator[ServerSentEvent]:
if sse is not None:
yield sse

async def aiter(self, iterator: AsyncIterator[str]) -> AsyncIterator[ServerSentEvent]:
"""Given an async iterator that yields lines, iterate over it & yield every event encountered"""
async def aiter(
self, iterator: AsyncIterator[str]
) -> AsyncIterator[ServerSentEvent]:
"""Given an async iterator that yields lines,
iterate over it & yield every event encountered"""
async for line in iterator:
line = line.rstrip("\n")
sse = self.decode(line)
Expand Down Expand Up @@ -135,6 +138,7 @@ def decode(self, line: str) -> Union[ServerSentEvent, None]:

return None


class Stream(Generic[ResponseT]):
"""Provides the core interface to iterate over a synchronous stream response."""

Expand Down Expand Up @@ -185,6 +189,7 @@ def __stream__(self) -> Iterator[ResponseT]:
request=self.response.request,
)


class AsyncStream(Generic[ResponseT]):
"""Provides the core interface to iterate over a asynchronous stream response."""

Expand Down Expand Up @@ -235,4 +240,4 @@ async def __stream__(self) -> AsyncIterator[ResponseT]:
body=body,
response=self.response,
request=self.response.request,
)
)
16 changes: 12 additions & 4 deletions tests/test_async_chat_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ async def test_method_single_with_vk_and_provider(

@pytest.mark.asyncio
@pytest.mark.parametrize("client, config", t2_params)
async def test_method_single_with_basic_config(self, client: Any, config: Dict) -> None:
async def test_method_single_with_basic_config(
self, client: Any, config: Dict
) -> None:
"""
Test the creation of a chat completion with a virtual key using the specified
Portkey client.
Expand Down Expand Up @@ -200,7 +202,9 @@ async def test_method_loadbalance_with_two_apikeys(

@pytest.mark.asyncio
@pytest.mark.parametrize("client, config", t5_params)
async def test_method_loadbalance_and_fallback(self, client: Any, config: Dict) -> None:
async def test_method_loadbalance_and_fallback(
self, client: Any, config: Dict
) -> None:
portkey = client(
base_url=base_url,
api_key=api_key,
Expand Down Expand Up @@ -294,7 +298,9 @@ async def test_method_single_with_vk_and_provider(

@pytest.mark.asyncio
@pytest.mark.parametrize("client, config", t2_params)
async def test_method_single_with_basic_config(self, client: Any, config: Dict) -> None:
async def test_method_single_with_basic_config(
self, client: Any, config: Dict
) -> None:
"""
Test the creation of a chat completion with a virtual key using the specified
Portkey client.
Expand Down Expand Up @@ -421,7 +427,9 @@ async def test_method_loadbalance_with_two_apikeys(

@pytest.mark.asyncio
@pytest.mark.parametrize("client, config", t5_params)
async def test_method_loadbalance_and_fallback(self, client: Any, config: Dict) -> None:
async def test_method_loadbalance_and_fallback(
self, client: Any, config: Dict
) -> None:
portkey = client(
base_url=base_url,
api_key=api_key,
Expand Down
16 changes: 12 additions & 4 deletions tests/test_async_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ async def test_method_single_with_vk_and_provider(

@pytest.mark.asyncio
@pytest.mark.parametrize("client, config", t2_params)
async def test_method_single_with_basic_config(self, client: Any, config: Dict) -> None:
async def test_method_single_with_basic_config(
self, client: Any, config: Dict
) -> None:
"""
Test the creation of a chat completion with a virtual key using the specified
Portkey client.
Expand Down Expand Up @@ -194,7 +196,9 @@ async def test_method_loadbalance_with_two_apikeys(

@pytest.mark.asyncio
@pytest.mark.parametrize("client, config", t5_params)
async def test_method_loadbalance_and_fallback(self, client: Any, config: Dict) -> None:
async def test_method_loadbalance_and_fallback(
self, client: Any, config: Dict
) -> None:
portkey = client(
base_url=base_url,
api_key=api_key,
Expand Down Expand Up @@ -279,7 +283,9 @@ async def test_method_single_with_vk_and_provider(

@pytest.mark.asyncio
@pytest.mark.parametrize("client, config", t2_params)
async def test_method_single_with_basic_config(self, client: Any, config: Dict) -> None:
async def test_method_single_with_basic_config(
self, client: Any, config: Dict
) -> None:
"""
Test the creation of a chat completion with a virtual key using the specified
Portkey client.
Expand Down Expand Up @@ -391,7 +397,9 @@ async def test_method_loadbalance_with_two_apikeys(

@pytest.mark.asyncio
@pytest.mark.parametrize("client, config", t5_params)
async def test_method_loadbalance_and_fallback(self, client: Any, config: Dict) -> None:
async def test_method_loadbalance_and_fallback(
self, client: Any, config: Dict
) -> None:
portkey = client(
base_url=base_url,
api_key=api_key,
Expand Down

0 comments on commit bc9758f

Please sign in to comment.