From c79ee6547e275dc283c944c0045e8b0759d0161a Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Tue, 26 Mar 2024 11:56:46 +0530 Subject: [PATCH 1/6] fix: prompt completion response type --- portkey_ai/api_resources/apis/generation.py | 41 +++++++++++++++++---- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/portkey_ai/api_resources/apis/generation.py b/portkey_ai/api_resources/apis/generation.py index cc0b217..28290fa 100644 --- a/portkey_ai/api_resources/apis/generation.py +++ b/portkey_ai/api_resources/apis/generation.py @@ -2,6 +2,11 @@ import warnings from typing import Literal, Optional, Union, Mapping, Any, overload from portkey_ai.api_resources.base_client import APIClient, AsyncAPIClient +from portkey_ai.api_resources.types.chat_complete_type import ( + ChatCompletionChunk, + ChatCompletions, +) +from portkey_ai.api_resources.types.complete_type import TextChoice, TextCompletionChunk from portkey_ai.api_resources.utils import ( retrieve_config, GenericResponse, @@ -172,7 +177,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Stream[GenericResponse]: + ) -> Union[Stream[ChatCompletionChunk], Stream[TextCompletionChunk]]: ... @overload @@ -188,7 +193,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> GenericResponse: + ) -> Union[ChatCompletions, TextChoice]: ... @overload @@ -204,7 +209,12 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[GenericResponse, Stream[GenericResponse]]: + ) -> Union[ + ChatCompletions, + TextChoice, + Stream[ChatCompletionChunk], + Stream[TextCompletionChunk], + ]: ... def create( @@ -219,7 +229,12 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[GenericResponse, Stream[GenericResponse]]: + ) -> Union[ + ChatCompletions, + TextChoice, + Stream[ChatCompletionChunk], + Stream[TextCompletionChunk], + ]: """Prompt completions Method""" if config is None: config = retrieve_config() @@ -260,7 +275,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> AsyncStream[GenericResponse]: + ) -> Union[AsyncStream[ChatCompletionChunk], AsyncStream[TextCompletionChunk]]: ... @overload @@ -276,7 +291,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> GenericResponse: + ) -> Union[ChatCompletions, TextChoice]: ... @overload @@ -292,7 +307,12 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[GenericResponse, AsyncStream[GenericResponse]]: + ) -> Union[ + ChatCompletions, + TextChoice, + AsyncStream[ChatCompletionChunk], + AsyncStream[TextCompletionChunk], + ]: ... async def create( @@ -307,7 +327,12 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[GenericResponse, AsyncStream[GenericResponse]]: + ) -> Union[ + ChatCompletions, + TextChoice, + AsyncStream[ChatCompletionChunk], + AsyncStream[TextCompletionChunk], + ]: """Prompt completions Method""" if config is None: config = retrieve_config() From 69a4e48e334b5fde3c2fd03dbecbc84c65055f68 Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Tue, 26 Mar 2024 12:02:10 +0530 Subject: [PATCH 2/6] fix: cast_to for prompt completion --- portkey_ai/api_resources/apis/generation.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/portkey_ai/api_resources/apis/generation.py b/portkey_ai/api_resources/apis/generation.py index 28290fa..183379c 100644 --- a/portkey_ai/api_resources/apis/generation.py +++ b/portkey_ai/api_resources/apis/generation.py @@ -251,8 +251,8 @@ def create( f"/prompts/{prompt_id}/completions", body=body, params=None, - cast_to=GenericResponse, - stream_cls=Stream[GenericResponse], + cast_to=Union[ChatCompletions, TextChoice], + stream_cls=Union[Stream[ChatCompletionChunk], Stream[TextCompletionChunk]], stream=stream, headers={}, ) @@ -349,8 +349,10 @@ async def create( f"/prompts/{prompt_id}/completions", body=body, params=None, - cast_to=GenericResponse, - stream_cls=AsyncStream[GenericResponse], + cast_to=Union[ChatCompletions, TextChoice], + stream_cls=Union[ + AsyncStream[ChatCompletionChunk], AsyncStream[TextCompletionChunk] + ], stream=stream, headers={}, ) From 21e868b58ceda9de0e0af7e68024cfbca634ba74 Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Tue, 26 Mar 2024 14:06:59 +0530 Subject: [PATCH 3/6] feat: prompt class for types created --- portkey_ai/api_resources/apis/generation.py | 53 +++++------------ portkey_ai/api_resources/common_types.py | 18 +++++- .../api_resources/types/generation_type.py | 59 +++++++++++++++++++ portkey_ai/api_resources/utils.py | 6 +- 4 files changed, 95 insertions(+), 41 deletions(-) create mode 100644 portkey_ai/api_resources/types/generation_type.py diff --git a/portkey_ai/api_resources/apis/generation.py b/portkey_ai/api_resources/apis/generation.py index 183379c..b8ae024 100644 --- a/portkey_ai/api_resources/apis/generation.py +++ b/portkey_ai/api_resources/apis/generation.py @@ -2,11 +2,10 @@ import warnings from typing import Literal, Optional, Union, Mapping, Any, overload from portkey_ai.api_resources.base_client import APIClient, AsyncAPIClient -from portkey_ai.api_resources.types.chat_complete_type import ( - ChatCompletionChunk, - ChatCompletions, +from portkey_ai.api_resources.types.generation_type import ( + PromptCreate, + PromptCreateChunk, ) -from portkey_ai.api_resources.types.complete_type import TextChoice, TextCompletionChunk from portkey_ai.api_resources.utils import ( retrieve_config, GenericResponse, @@ -177,7 +176,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[Stream[ChatCompletionChunk], Stream[TextCompletionChunk]]: + ) -> Stream[PromptCreateChunk]: ... @overload @@ -193,7 +192,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[ChatCompletions, TextChoice]: + ) -> PromptCreate: ... @overload @@ -209,12 +208,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[ - ChatCompletions, - TextChoice, - Stream[ChatCompletionChunk], - Stream[TextCompletionChunk], - ]: + ) -> Union[PromptCreate, Stream[PromptCreateChunk]]: ... def create( @@ -229,12 +223,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[ - ChatCompletions, - TextChoice, - Stream[ChatCompletionChunk], - Stream[TextCompletionChunk], - ]: + ) -> Union[PromptCreate, Stream[PromptCreateChunk],]: """Prompt completions Method""" if config is None: config = retrieve_config() @@ -251,8 +240,8 @@ def create( f"/prompts/{prompt_id}/completions", body=body, params=None, - cast_to=Union[ChatCompletions, TextChoice], - stream_cls=Union[Stream[ChatCompletionChunk], Stream[TextCompletionChunk]], + cast_to=PromptCreate, + stream_cls=Stream[PromptCreateChunk], stream=stream, headers={}, ) @@ -275,7 +264,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[AsyncStream[ChatCompletionChunk], AsyncStream[TextCompletionChunk]]: + ) -> AsyncStream[PromptCreateChunk]: ... @overload @@ -291,7 +280,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[ChatCompletions, TextChoice]: + ) -> PromptCreate: ... @overload @@ -307,12 +296,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[ - ChatCompletions, - TextChoice, - AsyncStream[ChatCompletionChunk], - AsyncStream[TextCompletionChunk], - ]: + ) -> Union[PromptCreate, AsyncStream[PromptCreateChunk]]: ... async def create( @@ -327,12 +311,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[ - ChatCompletions, - TextChoice, - AsyncStream[ChatCompletionChunk], - AsyncStream[TextCompletionChunk], - ]: + ) -> Union[PromptCreate, AsyncStream[PromptCreateChunk]]: """Prompt completions Method""" if config is None: config = retrieve_config() @@ -349,10 +328,8 @@ async def create( f"/prompts/{prompt_id}/completions", body=body, params=None, - cast_to=Union[ChatCompletions, TextChoice], - stream_cls=Union[ - AsyncStream[ChatCompletionChunk], AsyncStream[TextCompletionChunk] - ], + cast_to=PromptCreate, + stream_cls=AsyncStream[PromptCreateChunk], stream=stream, headers={}, ) diff --git a/portkey_ai/api_resources/common_types.py b/portkey_ai/api_resources/common_types.py index ddb765a..427fa0d 100644 --- a/portkey_ai/api_resources/common_types.py +++ b/portkey_ai/api_resources/common_types.py @@ -1,6 +1,8 @@ from typing import TypeVar, Union import httpx + +from portkey_ai.api_resources.types.generation_type import PromptCreateChunk from .streaming import Stream, AsyncStream from .utils import GenericResponse from .types.chat_complete_type import ChatCompletionChunk @@ -9,13 +11,25 @@ StreamT = TypeVar( "StreamT", bound=Stream[ - Union[ChatCompletionChunk, TextCompletionChunk, GenericResponse, httpx.Response] + Union[ + ChatCompletionChunk, + TextCompletionChunk, + GenericResponse, + PromptCreateChunk, + httpx.Response, + ] ], ) AsyncStreamT = TypeVar( "AsyncStreamT", bound=AsyncStream[ - Union[ChatCompletionChunk, TextCompletionChunk, GenericResponse, httpx.Response] + Union[ + ChatCompletionChunk, + TextCompletionChunk, + GenericResponse, + PromptCreateChunk, + httpx.Response, + ] ], ) diff --git a/portkey_ai/api_resources/types/generation_type.py b/portkey_ai/api_resources/types/generation_type.py new file mode 100644 index 0000000..771a475 --- /dev/null +++ b/portkey_ai/api_resources/types/generation_type.py @@ -0,0 +1,59 @@ +import json +from typing import Dict, Optional, Union +import httpx + +from portkey_ai.api_resources.types.chat_complete_type import ( + Choice, + StreamChoice, + Usage, +) +from portkey_ai.api_resources.types.complete_type import Logprobs, TextChoice + +from .utils import parse_headers +from typing import List, Any +from pydantic import BaseModel + + +class PromptCreate(BaseModel): + id: Optional[str] + choices: List[Choice] + created: Optional[int] + model: Optional[str] + object: Optional[str] + system_fingerprint: Optional[str] = None + usage: Optional[Usage] = None + index: Optional[int] = None + text: Optional[str] = None + logprobs: Optional[Logprobs] = None + finish_reason: Optional[str] = None + _headers: Optional[httpx.Headers] = None + + def __str__(self): + return json.dumps(self.dict(), indent=4) + + def __getitem__(self, key): + return getattr(self, key, None) + + def get(self, key: str, default: Optional[Any] = None): + return getattr(self, key, None) or default + + def get_headers(self) -> Optional[Dict[str, str]]: + return parse_headers(self._headers) + + +class PromptCreateChunk(BaseModel): + id: Optional[str] = None + object: Optional[str] = None + created: Optional[int] = None + model: Optional[str] = None + provider: Optional[str] = None + choices: Optional[Union[List[TextChoice], List[StreamChoice]]] + + def __str__(self): + return json.dumps(self.dict(), indent=4) + + def __getitem__(self, key): + return getattr(self, key, None) + + def get(self, key: str, default: Optional[Any] = None): + return getattr(self, key, None) or default diff --git a/portkey_ai/api_resources/utils.py b/portkey_ai/api_resources/utils.py index 2308e5e..ede5e40 100644 --- a/portkey_ai/api_resources/utils.py +++ b/portkey_ai/api_resources/utils.py @@ -15,6 +15,10 @@ TextCompletionChunk, TextCompletion, ) +from portkey_ai.api_resources.types.generation_type import ( + PromptCreate, + PromptCreateChunk, +) from .exceptions import ( APIStatusError, BadRequestError, @@ -56,7 +60,7 @@ class CacheType(str, Enum, metaclass=MetaEnum): ResponseT = TypeVar( "ResponseT", - bound="Union[ChatCompletionChunk, ChatCompletions, TextCompletion, TextCompletionChunk, GenericResponse, httpx.Response]", # noqa: E501 + bound="Union[ChatCompletionChunk, ChatCompletions, TextCompletion, TextCompletionChunk, GenericResponse, PromptCreate, PromptCreateChunk, httpx.Response]", # noqa: E501 ) From 66e6be257399638765bbfa1b0888eeb735fd28dc Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Tue, 26 Mar 2024 14:08:57 +0530 Subject: [PATCH 4/6] fix: renaming class --- portkey_ai/api_resources/apis/generation.py | 28 +++++++++---------- portkey_ai/api_resources/common_types.py | 6 ++-- .../api_resources/types/generation_type.py | 4 +-- portkey_ai/api_resources/utils.py | 6 ++-- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/portkey_ai/api_resources/apis/generation.py b/portkey_ai/api_resources/apis/generation.py index b8ae024..8f2df4b 100644 --- a/portkey_ai/api_resources/apis/generation.py +++ b/portkey_ai/api_resources/apis/generation.py @@ -3,8 +3,8 @@ from typing import Literal, Optional, Union, Mapping, Any, overload from portkey_ai.api_resources.base_client import APIClient, AsyncAPIClient from portkey_ai.api_resources.types.generation_type import ( - PromptCreate, - PromptCreateChunk, + PromptCompletion, + PromptCompletionChunk, ) from portkey_ai.api_resources.utils import ( retrieve_config, @@ -176,7 +176,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Stream[PromptCreateChunk]: + ) -> Stream[PromptCompletionChunk]: ... @overload @@ -192,7 +192,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> PromptCreate: + ) -> PromptCompletion: ... @overload @@ -208,7 +208,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[PromptCreate, Stream[PromptCreateChunk]]: + ) -> Union[PromptCompletion, Stream[PromptCompletionChunk]]: ... def create( @@ -223,7 +223,7 @@ def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[PromptCreate, Stream[PromptCreateChunk],]: + ) -> Union[PromptCompletion, Stream[PromptCompletionChunk],]: """Prompt completions Method""" if config is None: config = retrieve_config() @@ -240,8 +240,8 @@ def create( f"/prompts/{prompt_id}/completions", body=body, params=None, - cast_to=PromptCreate, - stream_cls=Stream[PromptCreateChunk], + cast_to=PromptCompletion, + stream_cls=Stream[PromptCompletionChunk], stream=stream, headers={}, ) @@ -264,7 +264,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> AsyncStream[PromptCreateChunk]: + ) -> AsyncStream[PromptCompletionChunk]: ... @overload @@ -280,7 +280,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> PromptCreate: + ) -> PromptCompletion: ... @overload @@ -296,7 +296,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[PromptCreate, AsyncStream[PromptCreateChunk]]: + ) -> Union[PromptCompletion, AsyncStream[PromptCompletionChunk]]: ... async def create( @@ -311,7 +311,7 @@ async def create( top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> Union[PromptCreate, AsyncStream[PromptCreateChunk]]: + ) -> Union[PromptCompletion, AsyncStream[PromptCompletionChunk]]: """Prompt completions Method""" if config is None: config = retrieve_config() @@ -328,8 +328,8 @@ async def create( f"/prompts/{prompt_id}/completions", body=body, params=None, - cast_to=PromptCreate, - stream_cls=AsyncStream[PromptCreateChunk], + cast_to=PromptCompletion, + stream_cls=AsyncStream[PromptCompletionChunk], stream=stream, headers={}, ) diff --git a/portkey_ai/api_resources/common_types.py b/portkey_ai/api_resources/common_types.py index 427fa0d..d7e066c 100644 --- a/portkey_ai/api_resources/common_types.py +++ b/portkey_ai/api_resources/common_types.py @@ -2,7 +2,7 @@ import httpx -from portkey_ai.api_resources.types.generation_type import PromptCreateChunk +from portkey_ai.api_resources.types.generation_type import PromptCompletionChunk from .streaming import Stream, AsyncStream from .utils import GenericResponse from .types.chat_complete_type import ChatCompletionChunk @@ -15,7 +15,7 @@ ChatCompletionChunk, TextCompletionChunk, GenericResponse, - PromptCreateChunk, + PromptCompletionChunk, httpx.Response, ] ], @@ -28,7 +28,7 @@ ChatCompletionChunk, TextCompletionChunk, GenericResponse, - PromptCreateChunk, + PromptCompletionChunk, httpx.Response, ] ], diff --git a/portkey_ai/api_resources/types/generation_type.py b/portkey_ai/api_resources/types/generation_type.py index 771a475..4d67b9c 100644 --- a/portkey_ai/api_resources/types/generation_type.py +++ b/portkey_ai/api_resources/types/generation_type.py @@ -14,7 +14,7 @@ from pydantic import BaseModel -class PromptCreate(BaseModel): +class PromptCompletion(BaseModel): id: Optional[str] choices: List[Choice] created: Optional[int] @@ -41,7 +41,7 @@ def get_headers(self) -> Optional[Dict[str, str]]: return parse_headers(self._headers) -class PromptCreateChunk(BaseModel): +class PromptCompletionChunk(BaseModel): id: Optional[str] = None object: Optional[str] = None created: Optional[int] = None diff --git a/portkey_ai/api_resources/utils.py b/portkey_ai/api_resources/utils.py index ede5e40..221e481 100644 --- a/portkey_ai/api_resources/utils.py +++ b/portkey_ai/api_resources/utils.py @@ -16,8 +16,8 @@ TextCompletion, ) from portkey_ai.api_resources.types.generation_type import ( - PromptCreate, - PromptCreateChunk, + PromptCompletion, + PromptCompletionChunk, ) from .exceptions import ( APIStatusError, @@ -60,7 +60,7 @@ class CacheType(str, Enum, metaclass=MetaEnum): ResponseT = TypeVar( "ResponseT", - bound="Union[ChatCompletionChunk, ChatCompletions, TextCompletion, TextCompletionChunk, GenericResponse, PromptCreate, PromptCreateChunk, httpx.Response]", # noqa: E501 + bound="Union[ChatCompletionChunk, ChatCompletions, TextCompletion, TextCompletionChunk, GenericResponse, PromptCompletion, PromptCompletionChunk, httpx.Response]", # noqa: E501 ) From dd0170264e5865ccb8ae0732403a2f839ddaf7b8 Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Wed, 27 Mar 2024 13:55:53 +0530 Subject: [PATCH 5/6] feat: response type for prompt.render --- portkey_ai/api_resources/apis/generation.py | 19 +++--- .../api_resources/types/generation_type.py | 59 +++++++++++++++++++ 2 files changed, 70 insertions(+), 8 deletions(-) diff --git a/portkey_ai/api_resources/apis/generation.py b/portkey_ai/api_resources/apis/generation.py index 8f2df4b..a723fb2 100644 --- a/portkey_ai/api_resources/apis/generation.py +++ b/portkey_ai/api_resources/apis/generation.py @@ -5,6 +5,7 @@ from portkey_ai.api_resources.types.generation_type import ( PromptCompletion, PromptCompletionChunk, + PromptRender, ) from portkey_ai.api_resources.utils import ( retrieve_config, @@ -92,14 +93,14 @@ def render( self, *, prompt_id: str, - variables: Optional[Mapping[str, Any]] = None, + variables: Mapping[str, Any], stream: bool = False, temperature: Optional[float] = None, max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> GenericResponse: + ) -> PromptRender: """Prompt render Method""" body = { "variables": variables, @@ -114,8 +115,8 @@ def render( f"/prompts/{prompt_id}/render", body=body, params=None, - cast_to=GenericResponse, - stream_cls=Stream[GenericResponse], + cast_to=PromptRender, + stream_cls=Stream[PromptRender], stream=False, headers={}, ) @@ -132,13 +133,14 @@ async def render( self, *, prompt_id: str, - variables: Optional[Mapping[str, Any]] = None, + variables: Mapping[str, Any], + stream: bool = False, temperature: Optional[float] = None, max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, **kwargs, - ) -> GenericResponse: + ) -> PromptRender: """Prompt render Method""" body = { "variables": variables, @@ -146,15 +148,16 @@ async def render( "max_tokens": max_tokens, "top_k": top_k, "top_p": top_p, + "stream": stream, **kwargs, } return await self._post( f"/prompts/{prompt_id}/render", body=body, params=None, - cast_to=GenericResponse, + cast_to=PromptRender, stream=False, - stream_cls=AsyncStream[GenericResponse], + stream_cls=AsyncStream[PromptRender], headers={}, ) diff --git a/portkey_ai/api_resources/types/generation_type.py b/portkey_ai/api_resources/types/generation_type.py index 4d67b9c..71fa9cf 100644 --- a/portkey_ai/api_resources/types/generation_type.py +++ b/portkey_ai/api_resources/types/generation_type.py @@ -3,6 +3,7 @@ import httpx from portkey_ai.api_resources.types.chat_complete_type import ( + ChatCompletionMessage, Choice, StreamChoice, Usage, @@ -57,3 +58,61 @@ def __getitem__(self, key): def get(self, key: str, default: Optional[Any] = None): return getattr(self, key, None) or default + +FunctionParameters = Dict[str, object] + +class Function(BaseModel): + name: Optional[str] + description: Optional[str] = None + parameters: Optional[FunctionParameters] = None + +class Tool(BaseModel): + function: Function + type: Optional[str] + +class PromptRenderData(BaseModel): + messages: Optional[List[ChatCompletionMessage]] = None + prompt: Optional[str] = None + model: Optional[str] = None + suffix: Optional[str] = None + max_tokens: Optional[int] = None + temperature: Optional[float] = None + top_k: Optional[int] = None + top_p: Optional[float] = None + n: Optional[int] = None + stop_sequences: Optional[List[str]] = None + timeout: Union[float, None] = None + functions: Optional[List[Function]] = None + function_call: Optional[Union[None, str, Function]] = None + logprobs: Optional[bool] = None + top_logprobs: Optional[int] = None + echo: Optional[bool] = None + stop: Optional[Union[str, List[str]]] = None + presence_penalty: Optional[int] = None + frequency_penalty: Optional[int] = None + best_of: Optional[int] = None + logit_bias: Optional[Dict[str, int]] = None + user: Optional[str] = None + organization: Optional[str] = None + tool_choice: Optional[Union[None, str]] = None + tools: Optional[List[Tool]] = None + + +class PromptRender(BaseModel): + success: Optional[bool] = True + data: PromptRenderData + + def __str__(self): + return json.dumps(self.dict(), indent=4) + + def __getitem__(self, key): + return getattr(self, key, None) + + def get(self, key: str, default: Optional[Any] = None): + return getattr(self, key, None) or default + + + + + + From a3f93b0b4ed886dc475f081bf96fbe25b879efc0 Mon Sep 17 00:00:00 2001 From: csgulati09 Date: Wed, 27 Mar 2024 13:58:04 +0530 Subject: [PATCH 6/6] fix: linting issues --- portkey_ai/api_resources/common_types.py | 7 ++++++- portkey_ai/api_resources/types/generation_type.py | 12 +++++------- portkey_ai/api_resources/utils.py | 3 ++- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/portkey_ai/api_resources/common_types.py b/portkey_ai/api_resources/common_types.py index d7e066c..f35a842 100644 --- a/portkey_ai/api_resources/common_types.py +++ b/portkey_ai/api_resources/common_types.py @@ -2,7 +2,10 @@ import httpx -from portkey_ai.api_resources.types.generation_type import PromptCompletionChunk +from portkey_ai.api_resources.types.generation_type import ( + PromptCompletionChunk, + PromptRender, +) from .streaming import Stream, AsyncStream from .utils import GenericResponse from .types.chat_complete_type import ChatCompletionChunk @@ -16,6 +19,7 @@ TextCompletionChunk, GenericResponse, PromptCompletionChunk, + PromptRender, httpx.Response, ] ], @@ -29,6 +33,7 @@ TextCompletionChunk, GenericResponse, PromptCompletionChunk, + PromptRender, httpx.Response, ] ], diff --git a/portkey_ai/api_resources/types/generation_type.py b/portkey_ai/api_resources/types/generation_type.py index 71fa9cf..417e4a8 100644 --- a/portkey_ai/api_resources/types/generation_type.py +++ b/portkey_ai/api_resources/types/generation_type.py @@ -59,17 +59,21 @@ def __getitem__(self, key): def get(self, key: str, default: Optional[Any] = None): return getattr(self, key, None) or default + FunctionParameters = Dict[str, object] + class Function(BaseModel): name: Optional[str] description: Optional[str] = None parameters: Optional[FunctionParameters] = None + class Tool(BaseModel): function: Function type: Optional[str] + class PromptRenderData(BaseModel): messages: Optional[List[ChatCompletionMessage]] = None prompt: Optional[str] = None @@ -97,7 +101,7 @@ class PromptRenderData(BaseModel): tool_choice: Optional[Union[None, str]] = None tools: Optional[List[Tool]] = None - + class PromptRender(BaseModel): success: Optional[bool] = True data: PromptRenderData @@ -110,9 +114,3 @@ def __getitem__(self, key): def get(self, key: str, default: Optional[Any] = None): return getattr(self, key, None) or default - - - - - - diff --git a/portkey_ai/api_resources/utils.py b/portkey_ai/api_resources/utils.py index 221e481..860952e 100644 --- a/portkey_ai/api_resources/utils.py +++ b/portkey_ai/api_resources/utils.py @@ -18,6 +18,7 @@ from portkey_ai.api_resources.types.generation_type import ( PromptCompletion, PromptCompletionChunk, + PromptRender, ) from .exceptions import ( APIStatusError, @@ -60,7 +61,7 @@ class CacheType(str, Enum, metaclass=MetaEnum): ResponseT = TypeVar( "ResponseT", - bound="Union[ChatCompletionChunk, ChatCompletions, TextCompletion, TextCompletionChunk, GenericResponse, PromptCompletion, PromptCompletionChunk, httpx.Response]", # noqa: E501 + bound="Union[ChatCompletionChunk, ChatCompletions, TextCompletion, TextCompletionChunk, GenericResponse, PromptCompletion, PromptCompletionChunk, PromptRender, httpx.Response]", # noqa: E501 )