Skip to content

Commit

Permalink
feat(credentials): drop API version prefix from the endpoint (#166)
Browse files Browse the repository at this point in the history
feat(credentials): drop API version prefix from the endpoint
Ref: #160

---------

Co-authored-by: Tomáš Dvořák <toomas2d@gmail.com>
Co-authored-by: David Kristek <David.Kristek@ibm.com>
  • Loading branch information
3 people committed Sep 22, 2023
1 parent c181afd commit d7cd7f7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
20 changes: 18 additions & 2 deletions src/genai/credentials.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import re
from warnings import warn


class Credentials:
DEFAULT_API = "https://workbench-api.res.ibm.com/v1"
DEFAULT_API = "https://workbench-api.res.ibm.com"

def __init__(
self,
Expand All @@ -18,4 +22,16 @@ def __init__(
self.api_key = api_key
if api_endpoint is None:
raise ValueError("api_endpoint must be provided")
self.api_endpoint = api_endpoint
self.api_endpoint = api_endpoint.rstrip("/")
self._remove_api_endpoint_version()

def _remove_api_endpoint_version(self) -> None:
[api, *version] = re.split(r"(/v\d+$)", self.api_endpoint, maxsplit=1)
if version:
warn(
DeprecationWarning(
f"The 'api_endpoint' property should not contain any explicit API version"
f"(rename it from '{self.api_endpoint}' to just '{api}')"
)
)
self.api_endpoint = api
10 changes: 5 additions & 5 deletions src/genai/services/service_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@


class ServiceInterface:
GENERATE = "/generate"
TOKENIZE = "/tokenize"
HISTORY = "/requests"
TOU = "/user"
MODELS = "/models"
GENERATE = "/v1/generate"
TOKENIZE = "/v1/tokenize"
HISTORY = "/v1/requests"
TOU = "/v1/user"
MODELS = "/v1/models"

def __init__(self, service_url: str, api_key: str) -> None:
"""Initialize ServiceInterface.
Expand Down
16 changes: 16 additions & 0 deletions tests/test_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

from genai import Credentials


@pytest.mark.unit
class TestCredentials:
@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_remove_version(self):
credentials = Credentials(api_key="GENAI_API_KEY", api_endpoint="https://workbench-api.res.ibm.com/v1/")

assert "/v1" not in credentials.api_endpoint

credentials = Credentials(api_key="GENAI_API_KEY", api_endpoint="https://workbench-api.res.ibm.com/v2/ai/v12")

assert credentials.api_endpoint == "https://workbench-api.res.ibm.com/v2/ai"

0 comments on commit d7cd7f7

Please sign in to comment.