Repository navigation
fix: use Portkey native Python SDK #1307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
c68d532
30f5e05
1c43648
29f4574
a05f9d5
328ba4f
5ba45f9
eb973e7
4e8b123
9cbdbcd
ad949c1
60f203a
ffcb420
aee3e4d
e3e597c
3c0d193
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,16 @@ | |
| from any_llm.types.completion import ChatCompletion, ChatCompletionChunk, CompletionParams | ||
| from any_llm.utils.structured_output import get_json_schema, is_structured_output_type | ||
|
|
||
| try: | ||
| import portkey_ai # type: ignore[import-not-found] | ||
|
|
||
| AsyncPortkey: Any = portkey_ai.AsyncPortkey | ||
| except ImportError as e: # pragma: no cover - exercised by the package guard | ||
| AsyncPortkey = None | ||
| MISSING_PACKAGES_ERROR: ImportError | None = e | ||
| else: | ||
| MISSING_PACKAGES_ERROR = None | ||
|
|
||
|
|
||
| class PortkeyProvider(XMLReasoningOpenAIProvider): | ||
| """Portkey provider for accessing 200+ LLMs through Portkey's AI Gateway.""" | ||
|
|
@@ -30,6 +40,17 @@ class PortkeyProvider(XMLReasoningOpenAIProvider): | |
| SUPPORTS_LIST_MODELS = True | ||
|
|
||
| _DEFAULT_REASONING_EFFORT = None | ||
| MISSING_PACKAGES_ERROR = MISSING_PACKAGES_ERROR | ||
| client: Any | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] |
||
|
|
||
| @override | ||
| def _init_client(self, api_key: str | None = None, api_base: str | None = None, **kwargs: Any) -> None: | ||
| """Initialize Portkey's native async client.""" | ||
| self.client = AsyncPortkey( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [major] Requests now go out with no timeout at all. Portkey's SDK pops |
||
| api_key=api_key, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] The request on the wire changed. Portkey's client sends |
||
| base_url=api_base or self.API_BASE, | ||
| **kwargs, | ||
| ) | ||
|
Comment on lines
+62
to
+66
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| @staticmethod | ||
| @override | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,26 @@ | ||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| import pytest | ||
|
|
||
| from any_llm.providers.portkey.portkey import PortkeyProvider | ||
| from any_llm.types.completion import CompletionParams | ||
|
|
||
|
|
||
| def test_portkey_uses_native_async_client(monkeypatch: pytest.MonkeyPatch) -> None: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] This change has no CI signal. Every workflow run on the branch is |
||
| """The provider initializes Portkey's native SDK instead of OpenAI's client.""" | ||
| calls: dict[str, Any] = {} | ||
|
|
||
| class FakeAsyncPortkey: | ||
| def __init__(self, **kwargs: Any) -> None: | ||
| calls.update(kwargs) | ||
|
|
||
| monkeypatch.setattr("any_llm.providers.portkey.portkey.AsyncPortkey", FakeAsyncPortkey) | ||
| PortkeyProvider(api_key="test-key", api_base="https://example.test/v1") | ||
|
|
||
| assert calls == {"api_key": "test-key", "base_url": "https://example.test/v1"} | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
|
|
||
| def test_convert_completion_params_with_dataclass_response_format() -> None: | ||
| """Test that dataclass response_format is converted to JSON schema format.""" | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[minor]
portkey-aihas no lower bound. The code needsportkey_ai.AsyncPortkeyand the response shapes of a recent release, so a resolver that picks an old version would fail in a confusing way. Add a floor such asportkey-ai>=2.3.4, as other extras here do.