Skip to content

Commit

Permalink
Merge branch 'main' into feat/render
Browse files Browse the repository at this point in the history
  • Loading branch information
csgulati09 committed Mar 22, 2024
2 parents ece9a96 + 72f3ea8 commit d1bbd74
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 90 deletions.
2 changes: 1 addition & 1 deletion portkey_ai/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
VERSION = "1.1.7"
VERSION = "1.2.1"
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ install_requires =
black==23.7.0
typing_extensions>=4.7.1,<5.0
pydantic>=1.10.12
openai>=1.0,<2.0

[options.entry_points]
console_scripts =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"strategy": {
"mode": "loadbalance"
},
"targets": [
{
"virtual_key": "stability-virtual-key",
"override_params": {
"model": "stable-diffusion-v1-6"
}
},
{
"strategy": {
"mode": "fallback",
"on_status_codes": [
429,
241
]
},
"targets": [
{
"virtual_key": "openai-virtual-key",
"override_params": {
"model": "dall-e-2"
}
},
{
"virtual_key": "stability-virtual-key",
"override_params": {
"model": "stable-diffusion-v1-6"
}
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"strategy": {
"mode": "loadbalance"
},
"targets": [
{
"virtual_key": "openai-virtual-key",
"override_params": {
"model": "dall-e-2"
}
},
{
"virtual_key": "stability-virtual-key",
"override_params": {
"model": "stable-diffusion-v1-6"
}
}
]
}
81 changes: 51 additions & 30 deletions tests/test_assistants.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,35 +39,56 @@ def get_metadata(self):
"random_id": str(uuid4()),
}

# # --------------------------
# # Test-1

# t1_params = []
# t = []
# for k, v in models.items():
# if k == "openai":
# for i in v["chat"]:
# if "vision" not in i:
# t.append((client, k, os.environ.get(v["env_variable"]), i))

# t1_params.extend(t)

# @pytest.mark.parametrize("client, provider, auth, model", t1_params)
# def test_method_single_with_vk_and_provider(
# self, client: Any, provider: str, auth: str, model
# ) -> None:
# portkey = client(
# base_url=base_url,
# api_key=api_key,
# provider=f"{provider}",
# Authorization=f"{auth}",
# trace_id=str(uuid4()),
# metadata=self.get_metadata(),
# )
# assistant = portkey.beta.assistants.create(
# model=model,
# )
# print(assistant)
# --------------------------
# Test-1

t1_params = []
t = []
for k, v in models.items():
if k == "openai":
for i in v["chat"]:
if "vision" not in i:
t.append((client, k, os.environ.get(v["env_variable"]), i))

t1_params.extend(t)

@pytest.mark.parametrize("client, provider, auth, model", t1_params)
def test_method_single_with_vk_and_provider(
self, client: Any, provider: str, auth: str, model
) -> None:
portkey = client(
base_url=base_url,
api_key=api_key,
provider=f"{provider}",
Authorization=f"{auth}",
trace_id=str(uuid4()),
metadata=self.get_metadata(),
)
assistant = portkey.beta.assistants.create(
model=model,
)

assert isinstance(assistant.id, str) is True
assert assistant.object == "assistant"
assert assistant.model == model

update_assistant = portkey.beta.assistants.update(
assistant.id, description="updated string"
)

assert update_assistant.description == "updated string"

retrieve_assistant = portkey.beta.assistants.retrieve(assistant.id)

assert retrieve_assistant.id == assistant.id
assert retrieve_assistant.object == "assistant"
assert retrieve_assistant.model == model

delete_assistant = portkey.beta.assistants.delete(assistant.id)

assert delete_assistant.id == assistant.id
assert delete_assistant.object == "assistant.deleted"
assert delete_assistant.deleted is True

# --------------------------
# Test-3
Expand Down Expand Up @@ -99,7 +120,7 @@ def test_method_all_params(self, client: Any, virtual_key: str) -> None:
tools=[{"type": "code_interpreter"}],
)

assert type(assistant.id) is str
assert isinstance(assistant.id, str) is True
assert assistant.object == "assistant"
assert assistant.model == model
assert assistant.name == "Math Tutor"
Expand Down
16 changes: 7 additions & 9 deletions tests/test_async_chat_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def test_method_single_with_vk_and_provider(
max_tokens=245,
)

assert type(completion.choices[0].message.content) is str
assert isinstance(completion.choices[0].message.content, str) is True

# --------------------------
# Test -2
Expand Down Expand Up @@ -120,7 +120,7 @@ async def test_method_single_with_basic_config(
model="gpt-3.5-turbo",
)

assert type(completion.choices[0].message.content) is str
assert isinstance(completion.choices[0].message.content, str) is True

# --------------------------
# Test-3
Expand Down Expand Up @@ -162,11 +162,11 @@ async def test_method_single_provider_with_vk_retry_cache(
config=config,
)

cached_completion = portkey_2.chat.completions.create(
cached_completion = await portkey_2.chat.completions.create(
messages=[{"role": "user", "content": "Say this is a test"}],
model="gpt-3.5-turbo",
)
assert type(cached_completion.choices[0].message.content) is str
assert isinstance(cached_completion.choices[0].message.content, str) is True

# --------------------------
# Test-4
Expand All @@ -182,7 +182,6 @@ async def test_method_loadbalance_with_two_apikeys(
portkey = client(
base_url=base_url,
api_key=api_key,
# virtual_key=virtual_api_key,
trace_id=str(uuid4()),
metadata=self.get_metadata(),
config=config,
Expand All @@ -192,7 +191,7 @@ async def test_method_loadbalance_with_two_apikeys(
messages=[{"role": "user", "content": "Say this is a test"}], max_tokens=245
)

assert type(completion.choices[0].message.content) is str
assert isinstance(completion.choices[0].message.content, str) is True

# --------------------------
# Test-5
Expand Down Expand Up @@ -221,7 +220,7 @@ async def test_method_loadbalance_and_fallback(
],
)

assert type(completion.choices[0].message.content) is str
assert isinstance(completion.choices[0].message.content, str) is True

# --------------------------
# Test-6
Expand All @@ -244,7 +243,7 @@ async def test_method_single_provider(self, client: Any, config: Dict) -> None:
model="gpt-3.5-turbo",
)

assert type(completion.choices[0].message.content) is str
assert isinstance(completion.choices[0].message.content, str) is True


class TestChatCompletionsStreaming:
Expand Down Expand Up @@ -409,7 +408,6 @@ async def test_method_loadbalance_with_two_apikeys(
portkey = client(
base_url=base_url,
api_key=api_key,
# virtual_key=virtual_api_key,
trace_id=str(uuid4()),
metadata=self.get_metadata(),
config=config,
Expand Down
14 changes: 6 additions & 8 deletions tests/test_async_complete.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def test_method_single_with_vk_and_provider(
max_tokens=245,
)

assert type(completion.choices[0].text) is str
assert isinstance(completion.choices[0].text, str) is True

# --------------------------
# Test -2
Expand Down Expand Up @@ -118,7 +118,7 @@ async def test_method_single_with_basic_config(
prompt="Say this is a test",
)

assert type(completion.choices[0].text) is str
assert isinstance(completion.choices[0].text, str) is True

# --------------------------
# Test-3
Expand Down Expand Up @@ -163,7 +163,7 @@ async def test_method_single_provider_with_vk_retry_cache(
prompt="Say this is a test"
)

assert type(cached_completion.choices[0].text) is str
assert isinstance(cached_completion.choices[0].text, str) is True

# --------------------------
# Test-4
Expand All @@ -179,7 +179,6 @@ async def test_method_loadbalance_with_two_apikeys(
portkey = client(
base_url=base_url,
api_key=api_key,
# virtual_key=virtual_api_key,
trace_id=str(uuid4()),
metadata=self.get_metadata(),
config=config,
Expand All @@ -189,7 +188,7 @@ async def test_method_loadbalance_with_two_apikeys(
prompt="Say this is a test", max_tokens=245
)

assert type(completion.choices[0].text) is str
assert isinstance(completion.choices[0].text, str) is True

# --------------------------
# Test-5
Expand All @@ -213,7 +212,7 @@ async def test_method_loadbalance_and_fallback(
prompt="Say this is just a loadbalance and fallback test test"
)

assert type(completion.choices[0].text) is str
assert isinstance(completion.choices[0].text, str) is True

# --------------------------
# Test-6
Expand All @@ -235,7 +234,7 @@ async def test_method_single_provider(self, client: Any, config: Dict) -> None:
prompt="Say this is a test",
)

assert type(completion.choices[0].text) is str
assert isinstance(completion.choices[0].text, str) is True


class TestChatCompletionsStreaming:
Expand Down Expand Up @@ -388,7 +387,6 @@ async def test_method_loadbalance_with_two_apikeys(
portkey = client(
base_url=base_url,
api_key=api_key,
# virtual_key=virtual_api_key,
trace_id=str(uuid4()),
metadata=self.get_metadata(),
config=config,
Expand Down
27 changes: 17 additions & 10 deletions tests/test_async_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def test_method_single_with_vk_and_provider(
model=model, prompt="A cute baby sea otter", n=1, size="1024x1024"
)

assert type(generation.data[0].url) is str
assert isinstance(generation.data[0].url, str) is True

# --------------------------
# Test -2
Expand All @@ -95,7 +95,7 @@ async def test_method_single_with_basic_config(
model="dall-e-3", prompt="A cute baby sea otter", n=1, size="1024x1024"
)

assert type(generation.data[0].url) is str
assert isinstance(generation.data[0].url, str) is True

# --------------------------
# Test-3
Expand Down Expand Up @@ -141,7 +141,7 @@ async def test_method_single_provider_with_vk_retry_cache(
model="dall-e-3", prompt="A cute baby sea otter", n=1, size="1024x1024"
)

assert type(cached_generation.data[0].url) is str
assert isinstance(cached_generation.data[0].url, str) is True

# --------------------------
# Test-4
Expand All @@ -157,17 +157,20 @@ async def test_method_loadbalance_with_two_apikeys(
portkey = client(
base_url=base_url,
api_key=api_key,
# virtual_key=virtual_api_key,
trace_id=str(uuid4()),
metadata=self.get_metadata(),
config=config,
)

image = await portkey.images.generate(
model="dall-e-3", prompt="A cute baby sea otter", n=1, size="1024x1024"
model="dall-e-3",
prompt="A cute baby sea otter",
n=1,
size="1024x1024",
response_format="b64_json",
)

assert type(image.data[0].url) is str
assert isinstance(image.data[0].b64_json, str) is True

# --------------------------
# Test-5
Expand All @@ -188,10 +191,14 @@ async def test_method_loadbalance_and_fallback(
)

image = await portkey.images.generate(
model="dall-e-3", prompt="A cute baby sea otter", n=1, size="1024x1024"
model="dall-e-3",
prompt="A cute baby sea otter",
n=1,
size="1024x1024",
response_format="b64_json",
)

assert type(image.data[0].url) is str
assert isinstance(image.data[0].b64_json, str) is True

# --------------------------
# Test-6
Expand All @@ -213,7 +220,7 @@ async def test_method_single_provider(self, client: Any, config: Dict) -> None:
model="dall-e-3", prompt="A cute baby sea otter", n=1, size="1024x1024"
)

assert type(image.data[0].url) is str
assert isinstance(image.data[0].url, str) is True

# --------------------------
# Test-7
Expand Down Expand Up @@ -242,4 +249,4 @@ async def test_method_all_params(self, client: Any, config: Dict) -> None:
user="user-1234",
)

assert type(image.data[0].url) is str
assert isinstance(image.data[0].url, str) is True
Loading

0 comments on commit d1bbd74

Please sign in to comment.