diff --git a/CHANGELOG.md b/CHANGELOG.md index 6164b854..ba854b8e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,14 +1,33 @@ ### Changelog +#### Unreleased -All notable changes to this project will be documented in this file. Dates are displayed in UTC. +#### [v0.1.51](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.51...v0.1.52) +> 21 September 2023 +- feat: added openschema for all integrations + anyscale integration -Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). +#### [v0.1.50](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.50...v0.1.51) +> 21 September 2023 +- fix: Issue with the str comparison on the api path + +#### [v0.1.50](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.49...v0.1.50) +> 19 September 2023 + +- feat: introducing a "Generate" API for prompt-based content generation with saved prompts + + + +#### [v0.1.49](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.48...v0.1.49) + +> 16 September 2023 + +- fix: Fixing the issue with the capturing of env variables [`#13`](https://github.com/Portkey-AI/portkey-python-sdk/pull/13) #### [v0.1.48](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.45...v0.1.48) > 14 September 2023 - fix: cache age type fix [`#11`](https://github.com/Portkey-AI/portkey-python-sdk/pull/11) +- docs: updated change log [`#10`](https://github.com/Portkey-AI/portkey-python-sdk/pull/10) #### [v0.1.45](https://github.com/Portkey-AI/portkey-python-sdk/compare/v0.1.44...v0.1.45) diff --git a/README.md b/README.md index fa261b5d..619f4224 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,8 @@ You can find a comprehensive [list of Portkey features here](#📔-list-of-portk Portkey is designed to be flexible. All the features you're familiar with from your LLM provider, like `top_p`, `top_k`, and `temperature`, can be used seamlessly. Check out the [complete list of provider features here](https://github.com/Portkey-AI/portkey-python-sdk/blob/af0814ebf4f1961b5dfed438918fe68b26ef5f1e/portkey/api_resources/utils.py#L137). **Setting the Prompt Input**: -You can set the input in two ways. For models like Claude and GPT3, use `prompt` = `(str)`, and for models like GPT3.5 & GPT4, use `messages` = `[array]`. +This param lets you override any prompt that is passed during the completion call - set a model-specific prompt here to optimise the model performance. You can set the input in two ways. For models like Claude and GPT3, use `prompt` = `(str)`, and for models like GPT3.5 & GPT4, use `messages` = `[array]`. + Here's how you can combine everything: @@ -73,7 +74,7 @@ temperature = 1 messages = [{"role": "user", "content": "Who are you?"}] # Construct LLM -llm = LLMOptions(provider=provider, virtual_key=virtual_key, trace_id=trace_id, model=model, temperature=temperature, messages=messages) +llm = LLMOptions(provider=provider, virtual_key=virtual_key, trace_id=trace_id, model=model, temperature=temperature) ``` ### **Steo 3️⃣ : Construct the Portkey Client** @@ -101,8 +102,12 @@ The Portkey client can do `ChatCompletions` and `Completions`. Since our LLM is GPT4, we will use ChatCompletions: ```py -response = portkey.ChatCompletions.create() - +response = portkey.ChatCompletions.create( + messages=[{ + "role": "user", + "content": "Who are you ?" + }] +) print(response.choices[0].message) ``` diff --git a/portkey/__init__.py b/portkey/__init__.py index ee5269c4..df640785 100644 --- a/portkey/__init__.py +++ b/portkey/__init__.py @@ -19,6 +19,7 @@ ChatCompletionChunk, TextCompletion, TextCompletionChunk, + Generations, ) from portkey.version import VERSION from portkey.api_resources.global_constants import ( @@ -50,6 +51,7 @@ "ChatCompletionChunk", "TextCompletion", "TextCompletionChunk", + "Generations", "Config", "api_key", "base_url", diff --git a/portkey/api_resources/__init__.py b/portkey/api_resources/__init__.py index 663e5295..8e7b5160 100644 --- a/portkey/api_resources/__init__.py +++ b/portkey/api_resources/__init__.py @@ -1,5 +1,5 @@ """""" -from .apis import ChatCompletions, Completions +from .apis import ChatCompletions, Completions, Generations from .utils import ( Modes, ModesLiteral, @@ -41,4 +41,5 @@ "ChatCompletionChunk", "TextCompletion", "TextCompletionChunk", + "Generations", ] diff --git a/portkey/api_resources/apis.py b/portkey/api_resources/apis.py index f26ec790..772b0c9a 100644 --- a/portkey/api_resources/apis.py +++ b/portkey/api_resources/apis.py @@ -1,4 +1,4 @@ -from typing import Optional, Union, overload, Literal, List +from typing import Optional, Union, overload, Literal, List, Mapping, Any from portkey.api_resources.base_client import APIClient from .utils import ( Modes, @@ -10,7 +10,7 @@ ChatCompletion, TextCompletion, TextCompletionChunk, - ApiType, + GenericResponse, ) from .streaming import Stream @@ -49,7 +49,7 @@ def create( max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, - **kwargs + **kwargs, ) -> Stream[TextCompletionChunk]: ... @@ -65,7 +65,7 @@ def create( max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, - **kwargs + **kwargs, ) -> TextCompletion: ... @@ -81,7 +81,7 @@ def create( max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, - **kwargs + **kwargs, ) -> Union[TextCompletion, Stream[TextCompletionChunk]]: ... @@ -96,7 +96,7 @@ def create( max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, - **kwargs + **kwargs, ) -> Union[TextCompletion, Stream[TextCompletionChunk]]: if config is None: config = retrieve_config() @@ -107,7 +107,7 @@ def create( max_tokens=max_tokens, top_k=top_k, top_p=top_p, - **kwargs + **kwargs, ) if config.mode == Modes.SINGLE.value: return cls(_client)._post( @@ -118,7 +118,6 @@ def create( cast_to=TextCompletion, stream_cls=Stream[TextCompletionChunk], stream=stream, - _type=ApiType.COMPLETIONS, ) if config.mode == Modes.FALLBACK.value: return cls(_client)._post( @@ -129,7 +128,6 @@ def create( cast_to=TextCompletion, stream_cls=Stream[TextCompletionChunk], stream=stream, - _type=ApiType.COMPLETIONS, ) if config.mode == Modes.AB_TEST.value: return cls(_client)._post( @@ -140,7 +138,6 @@ def create( cast_to=TextCompletion, stream_cls=Stream[TextCompletionChunk], stream=stream, - _type=ApiType.COMPLETIONS, ) raise NotImplementedError("Mode not implemented.") @@ -158,7 +155,7 @@ def create( max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, - **kwargs + **kwargs, ) -> Stream[ChatCompletionChunk]: ... @@ -174,7 +171,7 @@ def create( max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, - **kwargs + **kwargs, ) -> ChatCompletion: ... @@ -190,7 +187,7 @@ def create( max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, - **kwargs + **kwargs, ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]: ... @@ -205,7 +202,7 @@ def create( max_tokens: Optional[int] = None, top_k: Optional[int] = None, top_p: Optional[float] = None, - **kwargs + **kwargs, ) -> Union[ChatCompletion, Stream[ChatCompletionChunk]]: if config is None: config = retrieve_config() @@ -216,7 +213,7 @@ def create( max_tokens=max_tokens, top_k=top_k, top_p=top_p, - **kwargs + **kwargs, ) if config.mode == Modes.SINGLE.value: return cls(_client)._post( @@ -227,7 +224,6 @@ def create( cast_to=ChatCompletion, stream_cls=Stream[ChatCompletionChunk], stream=stream, - _type=ApiType.CHAT_COMPLETION, ) if config.mode == Modes.FALLBACK.value: return cls(_client)._post( @@ -238,7 +234,6 @@ def create( cast_to=ChatCompletion, stream_cls=Stream[ChatCompletionChunk], stream=stream, - _type=ApiType.CHAT_COMPLETION, ) if config.mode == Modes.AB_TEST.value: return cls(_client)._post( @@ -249,6 +244,29 @@ def create( cast_to=ChatCompletion, stream_cls=Stream[ChatCompletionChunk], stream=stream, - _type=ApiType.CHAT_COMPLETION, ) raise NotImplementedError("Mode not implemented.") + + +class Generations(APIResource): + @classmethod + def create( + cls, + *, + prompt_id: str, + config: Optional[Config] = None, + variables: Optional[Mapping[str, Any]] = None, + ) -> Union[GenericResponse, Stream[GenericResponse]]: + if config is None: + config = retrieve_config() + _client = APIClient(api_key=config.api_key, base_url=config.base_url) + body = {"variables": variables} + return cls(_client)._post( + f"/v1/prompts/{prompt_id}/generate", + body=body, + mode=None, + params=None, + cast_to=GenericResponse, + stream_cls=Stream[GenericResponse], + stream=False, + ) diff --git a/portkey/api_resources/base_client.py b/portkey/api_resources/base_client.py index 667367f3..9b4963fb 100644 --- a/portkey/api_resources/base_client.py +++ b/portkey/api_resources/base_client.py @@ -13,6 +13,7 @@ Type, overload, Literal, + get_args, ) import httpx import platform @@ -26,7 +27,7 @@ ProviderOptions, Params, Constructs, - ApiType, + PortkeyApiPaths, ) from .exceptions import ( APIStatusError, @@ -91,7 +92,6 @@ def post( stream: Literal[True], stream_cls: type[StreamT], params: Params, - _type: ApiType, ) -> StreamT: ... @@ -106,7 +106,6 @@ def post( stream: Literal[False], stream_cls: type[StreamT], params: Params, - _type: ApiType, ) -> ResponseT: ... @@ -121,7 +120,6 @@ def post( stream: bool, stream_cls: type[StreamT], params: Params, - _type: ApiType, ) -> Union[ResponseT, StreamT]: ... @@ -129,27 +127,61 @@ def post( self, path: str, *, - body: List[Body], + body: Union[List[Body], Any], mode: str, cast_to: Type[ResponseT], stream: bool, stream_cls: type[StreamT], params: Params, - _type: ApiType, ) -> Union[ResponseT, StreamT]: - body = cast(List[Body], body) - opts = self._construct( - method="post", url=path, body=body, mode=mode, stream=stream, params=params - ) + if path in [PortkeyApiPaths.CHAT_COMPLETION, PortkeyApiPaths.COMPLETION]: + body = cast(List[Body], body) + opts = self._construct( + method="post", + url=path, + body=body, + mode=mode, + stream=stream, + params=params, + ) + elif path.endswith("/generate"): + opts = self._construct_generate_options( + method="post", + url=path, + body=body, + mode=mode, + stream=stream, + params=params, + ) + else: + raise NotImplementedError(f"This API path `{path}` is not implemented.") + res = self._request( options=opts, stream=stream, cast_to=cast_to, stream_cls=stream_cls, - _type=_type, ) return res + def _construct_generate_options( + self, + *, + method: str, + url: str, + body: Any, + mode: str, + stream: bool, + params: Params, + ) -> Options: + opts = Options.construct() + opts.method = method + opts.url = url + json_body = body + opts.json_body = remove_empty_values(json_body) + opts.headers = None + return opts + def _construct( self, *, @@ -252,7 +284,6 @@ def _request( stream: Literal[False], cast_to: Type[ResponseT], stream_cls: Type[StreamT], - _type: ApiType, ) -> ResponseT: ... @@ -264,7 +295,6 @@ def _request( stream: Literal[True], cast_to: Type[ResponseT], stream_cls: Type[StreamT], - _type: ApiType, ) -> StreamT: ... @@ -276,7 +306,6 @@ def _request( stream: bool, cast_to: Type[ResponseT], stream_cls: Type[StreamT], - _type: ApiType, ) -> Union[ResponseT, StreamT]: ... @@ -287,7 +316,6 @@ def _request( stream: bool, cast_to: Type[ResponseT], stream_cls: Type[StreamT], - _type: ApiType, ) -> Union[ResponseT, StreamT]: request = self._build_request(options) try: @@ -302,18 +330,28 @@ def _request( raise APITimeoutError(request=request) from err except Exception as err: raise APIConnectionError(request=request) from err - if stream: - # stream_cls = stream_cls + if stream or res.headers["content-type"] == "text/event-stream": if stream_cls is None: raise MissingStreamClassError() - stream_response = stream_cls(response=res, _type=_type) + stream_response = stream_cls( + response=res, cast_to=self._extract_stream_chunk_type(stream_cls) + ) return stream_response + response = cast( ResponseT, cast_to(**res.json()), ) return response + def _extract_stream_chunk_type(self, stream_cls: Type) -> type: + args = get_args(stream_cls) + if not args: + raise TypeError( + f"Expected stream_cls to have been given a generic type argument, e.g. Stream[Foo] but received {stream_cls}", + ) + return cast(type, args[0]) + def _make_status_error_from_response( self, request: httpx.Request, diff --git a/portkey/api_resources/common_types.py b/portkey/api_resources/common_types.py index 77bfbfa1..317ecf2c 100644 --- a/portkey/api_resources/common_types.py +++ b/portkey/api_resources/common_types.py @@ -1,8 +1,8 @@ -from typing import TypeVar, Union +from typing import TypeVar, Union, Any from .streaming import Stream -from .utils import ChatCompletionChunk, TextCompletionChunk +from .utils import ChatCompletionChunk, TextCompletionChunk, GenericResponse StreamT = TypeVar( "StreamT", - bound=Stream[Union[ChatCompletionChunk, TextCompletionChunk]], + bound=Stream[Union[ChatCompletionChunk, TextCompletionChunk, GenericResponse]], ) diff --git a/portkey/api_resources/streaming.py b/portkey/api_resources/streaming.py index 4b562341..1edfd598 100644 --- a/portkey/api_resources/streaming.py +++ b/portkey/api_resources/streaming.py @@ -1,7 +1,7 @@ from __future__ import annotations import json -from typing import Any, Iterator, Generic, cast, Union +from typing import Any, Iterator, Generic, cast, Union, Type import httpx @@ -140,12 +140,8 @@ class Stream(Generic[ResponseT]): response: httpx.Response - def __init__(self, *, response: httpx.Response, _type: str) -> None: - self.response_cls = ( - ChatCompletionChunk - if _type == ApiType.CHAT_COMPLETION - else TextCompletionChunk - ) + def __init__(self, *, response: httpx.Response, cast_to: Type[ResponseT]) -> None: + self._cast_to = cast_to self.response = response self._decoder = SSEDecoder() self._iterator = self.__stream__() @@ -164,7 +160,7 @@ def __stream__(self) -> Iterator[ResponseT]: response = self.response for sse in self._iter_events(): if sse.event is None: - yield cast(ResponseT, self.response_cls(**sse.json())) + yield cast(ResponseT, self._cast_to(**sse.json())) if sse.event == "ping": continue diff --git a/portkey/api_resources/utils.py b/portkey/api_resources/utils.py index 4b131c00..a0753490 100644 --- a/portkey/api_resources/utils.py +++ b/portkey/api_resources/utils.py @@ -48,7 +48,7 @@ class CacheType(str, Enum, metaclass=MetaEnum): ResponseT = TypeVar( "ResponseT", - bound="Union[ChatCompletionChunk, ChatCompletion, TextCompletionChunk, TextCompletion]", + bound="Union[ChatCompletionChunk, ChatCompletion, TextCompletionChunk, TextCompletion, GenericResponse]", ) @@ -95,9 +95,10 @@ class ApiType(str, Enum, metaclass=MetaEnum): ModesLiteral = Literal["fallback", "ab_test", "single", "proxy"] -class PortkeyApiPaths(Enum): +class PortkeyApiPaths(str, Enum, metaclass=MetaEnum): CHAT_COMPLETION = "/v1/chatComplete" COMPLETION = "/v1/complete" + GENERATION = "/v1/prompts/{prompt_id}/generate" class Options(BaseModel): @@ -186,7 +187,7 @@ def remove_empty_values( class Constructs(BaseModel): - provider: Union[ProviderTypes, ProviderTypesLiteral] + provider: Union[ProviderTypes, ProviderTypesLiteral, str] api_key: Optional[str] = None virtual_key: Optional[str] = None cache: Optional[bool] = None @@ -400,7 +401,12 @@ def get(self, key: str, default: Optional[Any] = None): return getattr(self, key, None) or default -def apikey_from_env(provider: Union[ProviderTypes, ProviderTypesLiteral]) -> str: +class GenericResponse(BaseModel, extra="allow"): + success: Optional[bool] + data: Optional[Mapping[str, Any]] + + +def apikey_from_env(provider: Union[ProviderTypes, ProviderTypesLiteral, str]) -> str: env_key = f"{provider.upper().replace('-', '_')}_API_KEY" if provider is None: return "" @@ -451,8 +457,8 @@ def make_status_error( class Config(BaseModel): api_key: Optional[str] = None base_url: Optional[str] = None - mode: Optional[Union[Modes, ModesLiteral]] = None - llms: Union[List[LLMOptions], LLMOptions] + mode: Optional[Union[Modes, ModesLiteral, str]] = None + llms: Optional[Union[List[LLMOptions], LLMOptions]] = None @validator("mode", always=True) @classmethod @@ -460,8 +466,6 @@ def check_mode(cls, mode): if mode is None: # You can access other fields' values via the 'values' dictionary mode = retrieve_mode() - if mode not in Modes: - raise ValueError(INVALID_PORTKEY_MODE.format(mode)) return mode @@ -498,7 +502,7 @@ def retrieve_config() -> Config: raise ValueError(MISSING_CONFIG_MESSAGE) -def retrieve_mode() -> Union[Modes, ModesLiteral]: +def retrieve_mode() -> Union[Modes, ModesLiteral, str]: if portkey.mode: return portkey.mode raise ValueError(MISSING_MODE_MESSAGE) diff --git a/portkey/version.py b/portkey/version.py index 08ce86ea..eb01087e 100644 --- a/portkey/version.py +++ b/portkey/version.py @@ -1 +1 @@ -VERSION = "0.1.49" +VERSION = "0.1.52" diff --git a/tests/tests.py b/pytest.ini similarity index 100% rename from tests/tests.py rename to pytest.ini diff --git a/tests/anyscale_tests/test_anyscale_CodeLlama-34b-Instruct-hf.py b/tests/anyscale_tests/test_anyscale_CodeLlama-34b-Instruct-hf.py new file mode 100644 index 00000000..5b75b395 --- /dev/null +++ b/tests/anyscale_tests/test_anyscale_CodeLlama-34b-Instruct-hf.py @@ -0,0 +1,204 @@ +from __future__ import annotations + +import os +from typing import Any +import pytest +import portkey +from portkey import TextCompletion, TextCompletionChunk, Config, LLMOptions +from dotenv import load_dotenv + +# from tests.utils import assert_matches_type +load_dotenv() +base_url = os.environ.get("PORTKEY_BASE_URL") +api_key = os.environ.get("PORTKEY_API_KEY") +anyscale_api_key = os.environ.get("ANYSCALE_API_KEY") + + +class TestAnyscaleCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="codellama/CodeLlama-34b-Instruct-hf", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="codellama/CodeLlama-34b-Instruct-hf", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="codellama/CodeLlama-34b-Instruct-hf", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="codellama/CodeLlama-34b-Instruct-hf", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestAnyscaleChatCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="codellama/CodeLlama-34b-Instruct-hf", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="codellama/CodeLlama-34b-Instruct-hf", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="codellama/CodeLlama-34b-Instruct-hf", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="codellama/CodeLlama-34b-Instruct-hf", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") diff --git a/tests/anyscale_tests/test_anyscale_Llama-2-13b-chat-hf.py b/tests/anyscale_tests/test_anyscale_Llama-2-13b-chat-hf.py new file mode 100644 index 00000000..26ccaf86 --- /dev/null +++ b/tests/anyscale_tests/test_anyscale_Llama-2-13b-chat-hf.py @@ -0,0 +1,204 @@ +from __future__ import annotations + +import os +from typing import Any +import pytest +import portkey +from portkey import TextCompletion, TextCompletionChunk, Config, LLMOptions +from dotenv import load_dotenv + +# from tests.utils import assert_matches_type +load_dotenv() +base_url = os.environ.get("PORTKEY_BASE_URL") +api_key = os.environ.get("PORTKEY_API_KEY") +anyscale_api_key = os.environ.get("ANYSCALE_API_KEY") + + +class TestAnyscaleCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-13b-chat-hf", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-13b-chat-hf", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-13b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-13b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestAnyscaleChatCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-13b-chat-hf", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-13b-chat-hf", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-13b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-13b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") diff --git a/tests/anyscale_tests/test_anyscale_Llama-2-70b-chat-hf.py b/tests/anyscale_tests/test_anyscale_Llama-2-70b-chat-hf.py new file mode 100644 index 00000000..5c2dd6b0 --- /dev/null +++ b/tests/anyscale_tests/test_anyscale_Llama-2-70b-chat-hf.py @@ -0,0 +1,204 @@ +from __future__ import annotations + +import os +from typing import Any +import pytest +import portkey +from portkey import TextCompletion, TextCompletionChunk, Config, LLMOptions +from dotenv import load_dotenv + +# from tests.utils import assert_matches_type +load_dotenv() +base_url = os.environ.get("PORTKEY_BASE_URL") +api_key = os.environ.get("PORTKEY_API_KEY") +anyscale_api_key = os.environ.get("ANYSCALE_API_KEY") + + +class TestAnyscaleCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-70b-chat-hf", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-70b-chat-hf", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-70b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-70b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestAnyscaleChatCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-70b-chat-hf", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-70b-chat-hf", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-70b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-70b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") diff --git a/tests/anyscale_tests/test_anyscale_Llama-2-7b-chat-hf.py b/tests/anyscale_tests/test_anyscale_Llama-2-7b-chat-hf.py new file mode 100644 index 00000000..bc8ef0a9 --- /dev/null +++ b/tests/anyscale_tests/test_anyscale_Llama-2-7b-chat-hf.py @@ -0,0 +1,204 @@ +from __future__ import annotations + +import os +from typing import Any +import pytest +import portkey +from portkey import TextCompletion, TextCompletionChunk, Config, LLMOptions +from dotenv import load_dotenv + +# from tests.utils import assert_matches_type +load_dotenv() +base_url = os.environ.get("PORTKEY_BASE_URL") +api_key = os.environ.get("PORTKEY_API_KEY") +anyscale_api_key = os.environ.get("ANYSCALE_API_KEY") + + +class TestAnyscaleCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-7b-chat-hf", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-7b-chat-hf", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-7b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-7b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestAnyscaleChatCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-7b-chat-hf", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-7b-chat-hf", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-7b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + api_key=anyscale_api_key, + provider="anyscale", + metadata={"_user": "portkey-python-sdk"}, + model="meta-llama/Llama-2-7b-chat-hf", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") diff --git a/tests/test_anthropic.py b/tests/test_anthropic.py new file mode 100644 index 00000000..1a1466ad --- /dev/null +++ b/tests/test_anthropic.py @@ -0,0 +1,218 @@ +from __future__ import annotations + +import os +from typing import Any +import pytest +import portkey +from portkey import TextCompletion, TextCompletionChunk, Config, LLMOptions +from dotenv import load_dotenv + +# from tests.utils import assert_matches_type +load_dotenv() +base_url = os.environ.get("PORTKEY_BASE_URL") +api_key = os.environ.get("PORTKEY_API_KEY") +virtual_api_key = os.environ.get("ANTHROPIC_VIRTUAL_KEY") + + +class TestAnthropicCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="anthropic", + metadata={"_user": "portkey-python-sdk"}, + model="claude-2", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="anthropic", + metadata={"_user": "portkey-python-sdk"}, + model="claude-2", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="anthropic", + metadata={"_user": "portkey-python-sdk"}, + model="claude-2", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="anthropic", + metadata={"_user": "portkey-python-sdk"}, + model="claude-2", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestAnthropicChatCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="anthropic", + metadata={"_user": "portkey-python-sdk"}, + model="claude-2", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="anthropic", + metadata={"_user": "portkey-python-sdk"}, + model="claude-2", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="anthropic", + metadata={"_user": "portkey-python-sdk"}, + model="claude-2", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="anthropic", + metadata={"_user": "portkey-python-sdk"}, + model="claude-2", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestOpenaiGenerations: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_stream(self, client: Any) -> None: + config = Config() + client.config = config + completion = client.Generations.create( + prompt_id="", + ) diff --git a/tests/test_azure_openai.py b/tests/test_azure_openai.py new file mode 100644 index 00000000..43a30681 --- /dev/null +++ b/tests/test_azure_openai.py @@ -0,0 +1,135 @@ +from __future__ import annotations + +import os +from typing import Any +import pytest +import portkey +from portkey import TextCompletion, TextCompletionChunk, Config, LLMOptions +from dotenv import load_dotenv + +# from tests.utils import assert_matches_type +load_dotenv() +base_url = os.environ.get("PORTKEY_BASE_URL") +api_key = os.environ.get("PORTKEY_API_KEY") +virtual_api_key = os.environ.get("AZURE_OPENAI_VIRTUAL_KEY") + + +class TestAzureChatCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="azure-openai", + metadata={"_user": "portkey-python-sdk"}, + api_version="2023-03-15-preview", + resource_name="portkey", + deployment_id="turbo-16k", + retry={"attempts": 5, "on_status_codes": [429]}, + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="azure-openai", + metadata={"_user": "portkey-python-sdk"}, + api_version="2023-03-15-preview", + resource_name="portkey", + deployment_id="turbo-16k", + retry={"attempts": 5, "on_status_codes": [429]}, + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="azure-openai", + metadata={"_user": "portkey-python-sdk"}, + api_version="2023-03-15-preview", + resource_name="portkey", + deployment_id="turbo-16k", + retry={"attempts": 5, "on_status_codes": [429]}, + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="azure-openai", + metadata={"_user": "portkey-python-sdk"}, + api_version="2023-03-15-preview", + resource_name="portkey", + deployment_id="turbo-16k", + retry={"attempts": 5, "on_status_codes": [429]}, + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestOpenaiGenerations: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_stream(self, client: Any) -> None: + config = Config() + client.config = config + completion = client.Generations.create( + prompt_id="", + ) diff --git a/tests/test_cohere.py b/tests/test_cohere.py new file mode 100644 index 00000000..c0ad060a --- /dev/null +++ b/tests/test_cohere.py @@ -0,0 +1,127 @@ +from __future__ import annotations + +import os +from typing import Any +import pytest +import portkey +from portkey import TextCompletion, TextCompletionChunk, Config, LLMOptions +from dotenv import load_dotenv + +# from tests.utils import assert_matches_type +load_dotenv() +base_url = os.environ.get("PORTKEY_BASE_URL") +api_key = os.environ.get("PORTKEY_API_KEY") +virtual_api_key = os.environ.get("COHERE_VIRTUAL_KEY") + + +class TestCohereCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="cohere", + metadata={"_user": "portkey-python-sdk"}, + model="command-nightly", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + model="text-davinci-003", + prompt="why is the sky blue ?", + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="cohere", + metadata={"_user": "portkey-python-sdk"}, + model="command-nightly", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + model="text-davinci-003", + prompt="why is the sky blue ?", + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="cohere", + metadata={"_user": "portkey-python-sdk"}, + model="command-nightly", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + model="text-davinci-003", + prompt="why is the sky blue ?", + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="cohere", + metadata={"_user": "portkey-python-sdk"}, + model="command-nightly", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + model="text-davinci-003", + prompt="why is the sky blue ?", + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestOpenaiGenerations: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_stream(self, client: Any) -> None: + config = Config() + client.config = config + completion = client.Generations.create( + prompt_id="", + ) diff --git a/tests/test_openai.py b/tests/test_openai.py new file mode 100644 index 00000000..9418d8bd --- /dev/null +++ b/tests/test_openai.py @@ -0,0 +1,218 @@ +from __future__ import annotations + +import os +from typing import Any +import pytest +import portkey +from portkey import TextCompletion, TextCompletionChunk, Config, LLMOptions +from dotenv import load_dotenv + +# from tests.utils import assert_matches_type +load_dotenv() +base_url = os.environ.get("PORTKEY_BASE_URL") +api_key = os.environ.get("PORTKEY_API_KEY") +virtual_api_key = os.environ.get("OPENAI_VIRTUAL_KEY") + + +class TestOpenaiCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="openai", + metadata={"_user": "portkey-python-sdk"}, + model="text-davinci-003", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="openai", + metadata={"_user": "portkey-python-sdk"}, + model="text-davinci-003", + ), + ) + client.config = config + completion = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="openai", + metadata={"_user": "portkey-python-sdk"}, + model="text-davinci-003", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="openai", + metadata={"_user": "portkey-python-sdk"}, + model="text-davinci-003", + ), + ) + client.config = config + completion_streaming = client.Completions.create( + max_tokens=256, + prompt="why is the sky blue ?", + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestOpenaiChatCompletions: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="openai", + metadata={"_user": "portkey-python-sdk"}, + model="gpt-3.5-turbo", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + ) + # assert("True", "True") + + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_with_all_params_non_stream(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="openai", + metadata={"_user": "portkey-python-sdk"}, + model="gpt-3.5-turbo", + ), + ) + client.config = config + completion = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stop_sequences=["string", "string", "string"], + stream=False, + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + # assert_matches_type(TextCompletion, completion, path=["response"]) + + @parametrize + def test_method_create_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="openai", + metadata={"_user": "portkey-python-sdk"}, + model="gpt-3.5-turbo", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + ) + # assert("True", "True") + + # for chunk in completion_streaming: + # assert_matches_type(TextCompletionChunk, chunk, path=["response"]) + + @parametrize + def test_method_create_with_all_params_streaming(self, client: Any) -> None: + config = Config( + mode="single", + llms=LLMOptions( + virtual_key=virtual_api_key, + provider="openai", + metadata={"_user": "portkey-python-sdk"}, + model="gpt-3.5-turbo", + ), + ) + client.config = config + completion_streaming = client.ChatCompletions.create( + max_tokens=256, + messages=[{"role": "user", "content": "why is the sky blue ?"}], + stream=True, + stop_sequences=["string", "string", "string"], + temperature=1, + top_k=5, + top_p=0.7, + ) + # assert("True", "True") + + +class TestOpenaiGenerations: + client = portkey + client.api_key = api_key + parametrize = pytest.mark.parametrize("client", [client], ids=["strict"]) + + @parametrize + def test_method_create_stream(self, client: Any) -> None: + config = Config(mode="single") + client.config = config + completion = client.Generations.create( + prompt_id="", + ) diff --git a/tests/utils.py b/tests/utils.py new file mode 100644 index 00000000..e69de29b