Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update feedback #118

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions portkey_ai/api_resources/apis/api_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ def __init__(self, client: APIClient) -> None:
def _post(self, *args, **kwargs):
return self._client._post(*args, **kwargs)

def _put(self, *args, **kwargs):
return self._client._put(*args, **kwargs)


class AsyncAPIResource:
_client: AsyncAPIClient
Expand All @@ -32,6 +35,9 @@ def __init__(self, client: AsyncAPIClient) -> None:

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

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

async def _sleep(self, seconds: float) -> None:
await asyncio.sleep(seconds)
66 changes: 53 additions & 13 deletions portkey_ai/api_resources/apis/feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from portkey_ai.api_resources.apis.api_resource import APIResource, AsyncAPIResource
from portkey_ai.api_resources.base_client import APIClient, AsyncAPIClient
from portkey_ai.api_resources.streaming import AsyncStream, Stream
from portkey_ai.api_resources.utils import GenericResponse, PortkeyApiPaths
from portkey_ai.api_resources.types.feedback_type import FeedbackResponse
from portkey_ai.api_resources.utils import PortkeyApiPaths


class Feedback(APIResource):
Expand All @@ -16,26 +17,46 @@ def create(
value: Optional[int] = None,
weight: Optional[float] = None,
metadata: Optional[Dict[str, Any]] = None
) -> GenericResponse:
) -> FeedbackResponse:
body = dict(trace_id=trace_id, value=value, weight=weight, metadata=metadata)
return self._post(
PortkeyApiPaths.FEEDBACK_API,
body=body,
params=None,
cast_to=GenericResponse,
stream_cls=Stream[GenericResponse],
cast_to=FeedbackResponse,
stream_cls=Stream[FeedbackResponse],
stream=False,
headers={},
)

def bulk_create(self, *, feedbacks: List[Dict[str, Any]]) -> GenericResponse:
def bulk_create(self, *, feedbacks: List[Dict[str, Any]]) -> FeedbackResponse:
body = feedbacks
return self._post(
PortkeyApiPaths.FEEDBACK_API,
body=body,
params=None,
cast_to=GenericResponse,
stream_cls=Stream[GenericResponse],
cast_to=FeedbackResponse,
stream_cls=Stream[FeedbackResponse],
stream=False,
headers={},
)

def update(
self,
*,
feedback_id: Optional[str] = None,
value: Optional[int] = None,
weight: Optional[float] = None,
metadata: Optional[Dict[str, Any]] = None
) -> FeedbackResponse:
body = dict(value=value, weight=weight, metadata=metadata)

return self._put(
f"{PortkeyApiPaths.FEEDBACK_API}/{feedback_id}",
body=body,
params=None,
cast_to=FeedbackResponse,
stream_cls=Stream[FeedbackResponse],
stream=False,
headers={},
)
Expand All @@ -52,26 +73,45 @@ async def create(
value: Optional[int] = None,
weight: Optional[float] = None,
metadata: Optional[Dict[str, Any]] = None
) -> GenericResponse:
) -> FeedbackResponse:
body = dict(trace_id=trace_id, value=value, weight=weight, metadata=metadata)
return await self._post(
PortkeyApiPaths.FEEDBACK_API,
body=body,
params=None,
cast_to=GenericResponse,
stream_cls=AsyncStream[GenericResponse],
cast_to=FeedbackResponse,
stream_cls=AsyncStream[FeedbackResponse],
stream=False,
headers={},
)

async def bulk_create(self, *, feedbacks: List[Dict[str, Any]]) -> GenericResponse:
async def bulk_create(self, *, feedbacks: List[Dict[str, Any]]) -> FeedbackResponse:
body = feedbacks
return await self._post(
PortkeyApiPaths.FEEDBACK_API,
body=body,
params=None,
cast_to=GenericResponse,
stream_cls=AsyncStream[GenericResponse],
cast_to=FeedbackResponse,
stream_cls=AsyncStream[FeedbackResponse],
stream=False,
headers={},
)

async def update(
self,
*,
feedback_id: Optional[str] = None,
value: Optional[int] = None,
weight: Optional[float] = None,
metadata: Optional[Dict[str, Any]] = None
) -> FeedbackResponse:
body = dict(value=value, weight=weight, metadata=metadata)
return await self._put(
f"{PortkeyApiPaths.FEEDBACK_API}/{feedback_id}",
body=body,
params=None,
cast_to=FeedbackResponse,
stream_cls=Stream[FeedbackResponse],
stream=False,
headers={},
)
140 changes: 140 additions & 0 deletions portkey_ai/api_resources/base_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,76 @@ def _post(
)
return res

@overload
def _put(
self,
path: str,
*,
body: Mapping[str, Any],
cast_to: Type[ResponseT],
stream: Literal[True],
stream_cls: type[StreamT],
params: Mapping[str, str],
headers: Mapping[str, str],
) -> StreamT:
...

@overload
def _put(
self,
path: str,
*,
body: Mapping[str, Any],
cast_to: Type[ResponseT],
stream: Literal[False],
stream_cls: type[StreamT],
params: Mapping[str, str],
headers: Mapping[str, str],
) -> ResponseT:
...

@overload
def _put(
self,
path: str,
*,
body: Mapping[str, Any],
cast_to: Type[ResponseT],
stream: bool,
stream_cls: type[StreamT],
params: Mapping[str, str],
headers: Mapping[str, str],
) -> Union[ResponseT, StreamT]:
...

def _put(
self,
path: str,
*,
body: Mapping[str, Any],
cast_to: Type[ResponseT],
stream: bool,
stream_cls: type[StreamT],
params: Mapping[str, str],
headers: Mapping[str, str],
) -> Union[ResponseT, StreamT]:

opts = self._construct(
method="put",
url=path,
body=body,
stream=stream,
params=params,
headers=headers,
)
res = self._request(
options=opts,
stream=stream,
cast_to=cast_to,
stream_cls=stream_cls,
)
return res

def _construct_generate_options(
self,
*,
Expand Down Expand Up @@ -530,6 +600,76 @@ async def _post(
)
return res

@overload
async def _put(
self,
path: str,
*,
cast_to: Type[ResponseT],
body: Mapping[str, Any],
stream: Literal[False],
stream_cls: type[AsyncStreamT],
params: Mapping[str, str],
headers: Mapping[str, str],
) -> ResponseT:
...

@overload
async def _put(
self,
path: str,
*,
cast_to: Type[ResponseT],
body: Mapping[str, Any],
stream: Literal[True],
stream_cls: type[AsyncStreamT],
params: Mapping[str, str],
headers: Mapping[str, str],
) -> AsyncStreamT:
...

@overload
async def _put(
self,
path: str,
*,
cast_to: Type[ResponseT],
body: Mapping[str, Any],
stream: bool,
stream_cls: type[AsyncStreamT],
params: Mapping[str, str],
headers: Mapping[str, str],
) -> Union[ResponseT, AsyncStreamT]:
...

async def _put(
self,
path: str,
*,
cast_to: Type[ResponseT],
body: Mapping[str, Any],
stream: bool,
stream_cls: type[AsyncStreamT],
params: Mapping[str, str],
headers: Mapping[str, str],
) -> Union[ResponseT, AsyncStreamT]:

opts = await self._construct(
method="put",
url=path,
body=body,
stream=stream,
params=params,
headers=headers,
)
res = await self._request(
options=opts,
stream=stream,
cast_to=cast_to,
stream_cls=stream_cls,
)
return res

async def _construct_generate_options(
self,
*,
Expand Down
2 changes: 2 additions & 0 deletions portkey_ai/api_resources/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Portkey(APIClient):
generations: apis.Generations
prompts: apis.Prompts
embeddings: apis.Embeddings
feedback: apis.Feedback
images: apis.Images
files: apis.MainFiles
models: apis.Models
Expand Down Expand Up @@ -114,6 +115,7 @@ class AsyncPortkey(AsyncAPIClient):
generations: apis.AsyncGenerations
prompts: apis.AsyncPrompts
embeddings: apis.AsyncEmbeddings
feedback: apis.AsyncFeedback
images: apis.AsyncImages
files: apis.AsyncMainFiles
models: apis.AsyncModels
Expand Down
12 changes: 12 additions & 0 deletions portkey_ai/api_resources/types/feedback_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import json
from typing import Optional

from .utils import parse_headers
from typing import List
from pydantic import BaseModel


class FeedbackResponse(BaseModel):
status: Optional[str]
message: Optional[str]
feedback_ids: Optional[List[str]]
3 changes: 2 additions & 1 deletion portkey_ai/api_resources/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
TextCompletionChunk,
TextCompletion,
)
from portkey_ai.api_resources.types.feedback_type import FeedbackResponse
from portkey_ai.api_resources.types.generation_type import (
PromptCompletion,
PromptCompletionChunk,
Expand Down Expand Up @@ -61,7 +62,7 @@ class CacheType(str, Enum, metaclass=MetaEnum):

ResponseT = TypeVar(
"ResponseT",
bound="Union[ChatCompletionChunk, ChatCompletions, TextCompletion, TextCompletionChunk, GenericResponse, PromptCompletion, PromptCompletionChunk, PromptRender, httpx.Response]", # noqa: E501
bound="Union[ChatCompletionChunk, ChatCompletions, TextCompletion, TextCompletionChunk, GenericResponse, PromptCompletion, PromptCompletionChunk, PromptRender, FeedbackResponse, httpx.Response]", # noqa: E501
)


Expand Down
Loading