Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Formatted entire codebase with Ruff
ae8c0da038f49a72e761b3247859c68d7a4c9bed
4 changes: 3 additions & 1 deletion .github/scripts/update_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,6 @@ def matches(section: str) -> bool:

# Keep the generated header line, replace only the body with the published notes.
header = match.split("\n", 1)[0]
open(path, "w").write(content.replace(f"## {match}", f"## {header}\n\n{body.strip()}\n\n"))
open(path, "w").write(
content.replace(f"## {match}", f"## {header}\n\n{body.strip()}\n\n")
)
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ jobs:
steps:
- uses: SAP/ai-sdk-python/.github/actions/setup@main

- name: Check formatting
run: uv run ruff format --check --output-format=github

- name: Run pylint
run: |
uv run pylint ai_api_client_sdk ai_core_sdk gen_ai_hub \
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ docs:
packages/gen/gen_ai_hub/evaluations/helpers
uv run sphinx-build -b html docs/sphinx api-docs/

format:
uv run ruff format

format-check:
uv run ruff format --check

lint:
uv run pylint ai_api_client_sdk ai_core_sdk gen_ai_hub --errors-only --output-format=colorized

Expand Down
3 changes: 3 additions & 0 deletions adr/0007-gen-package-split.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Simple user example:

```python
from gen_ai_hub.proxy.native.openai import chat

chat.completions.create(model_name="gpt-5.4-nano", messages=[...])
```

Expand All @@ -69,6 +70,7 @@ Simple user example:

```python
from gen_ai_hub.proxy.native.openai import chat

chat.completions.create(model_name="gpt-5.4-nano", messages=[...])
```

Expand All @@ -95,5 +97,6 @@ Simple user example:

```python
from sap_ai_sdk_openai import chat

chat.completions.create(model_name="gpt-5.4-nano", messages=[...])
```
5 changes: 1 addition & 4 deletions docs/sphinx/conf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
project = "SAP AI SDK Python"
extensions = [
"sphinx.ext.autodoc",
"sphinx.ext.napoleon",
]
extensions = ["sphinx.ext.autodoc", "sphinx.ext.napoleon"]
html_theme = "furo"
exclude_patterns = ["_autogen/*/modules.rst"]
autodoc_default_options = {
Expand Down
9 changes: 4 additions & 5 deletions packages/base/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,14 @@ Each resource client has these functions (if supported for that resource) to sen
Example:

```python

from ai_api_client_sdk.ai_api_v2_client import AIAPIV2Client

ai_api_v2_client = AIAPIV2Client(
base_url="<BASE_URL>",
auth_url="<AUTH_URL>",
base_url="<BASE_URL>",
auth_url="<AUTH_URL>",
client_id="<CLIENT_ID>",
client_secret="<CLIENT_SECRET>",
resource_group="<RESOURCE_GROUP_ID>"
client_secret="<CLIENT_SECRET>",
resource_group="<RESOURCE_GROUP_ID>",
)

scenario = ai_api_v2_client.scenario.get(scenario_id="<SCENARIO_ID>")
Expand Down
122 changes: 92 additions & 30 deletions packages/base/ai_api_client_sdk/ai_api_v2_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,51 +74,113 @@ class AIAPIV2Client:
"""

@staticmethod
def _create_token_creator_if_does_not_exist(token_creator, auth_url, client_id, client_secret, cert_str, key_str,
cert_file_path, key_file_path):
def _create_token_creator_if_does_not_exist(
token_creator,
auth_url,
client_id,
client_secret,
cert_str,
key_str,
cert_file_path,
key_file_path,
):
if token_creator:
if not _is_all_none(auth_url, client_id, client_secret, cert_str, key_str, cert_file_path, key_file_path):
raise AIAPIAuthenticatorException(error_message=AUTH_PARAM_ERROR_MESSAGE)
if not _is_all_none(
auth_url,
client_id,
client_secret,
cert_str,
key_str,
cert_file_path,
key_file_path,
):
raise AIAPIAuthenticatorException(
error_message=AUTH_PARAM_ERROR_MESSAGE
)
return token_creator
try:
return Authenticator(auth_url=auth_url, client_id=client_id, client_secret=client_secret, cert_str=cert_str,
key_str=key_str, cert_file_path=cert_file_path, key_file_path=key_file_path).get_token
return Authenticator(
auth_url=auth_url,
client_id=client_id,
client_secret=client_secret,
cert_str=cert_str,
key_str=key_str,
cert_file_path=cert_file_path,
key_file_path=key_file_path,
).get_token
except TypeError as te:
if any(x in te.__str__() for x in ['auth_url', 'client_id']):
raise AIAPIAuthenticatorException(error_message=AUTH_PARAM_ERROR_MESSAGE)
if any(x in te.__str__() for x in ["auth_url", "client_id"]):
raise AIAPIAuthenticatorException(
error_message=AUTH_PARAM_ERROR_MESSAGE
)
raise te
except AIAPIAuthenticatorException:
raise AIAPIAuthenticatorException(error_message=AUTH_PARAM_ERROR_MESSAGE)

def __init__(self, base_url: str, auth_url: str = None, client_id: str = None, client_secret: str = None,
cert_str: str = None, key_str: str = None, cert_file_path: str = None, key_file_path: str = None,
token_creator: Callable[[], str] = None, resource_group: str = None,
connect_timeout=Timeouts.CONNECT_TIMEOUT.value,
num_request_retries=Timeouts.NUM_REQUEST_RETRIES.value,
**kwargs):
read_timeout = kwargs.get('read_timeout', Timeouts.READ_TIMEOUT.value)
client_type = kwargs.get('client_type', 'AI API Python SDK')
def __init__(
self,
base_url: str,
auth_url: str = None,
client_id: str = None,
client_secret: str = None,
cert_str: str = None,
key_str: str = None,
cert_file_path: str = None,
key_file_path: str = None,
token_creator: Callable[[], str] = None,
resource_group: str = None,
connect_timeout=Timeouts.CONNECT_TIMEOUT.value,
num_request_retries=Timeouts.NUM_REQUEST_RETRIES.value,
**kwargs,
):
read_timeout = kwargs.get("read_timeout", Timeouts.READ_TIMEOUT.value)
client_type = kwargs.get("client_type", "AI API Python SDK")

self.base_url: str = base_url
if os.environ.get(SKIP_AUTH_ENV_VAR, '').lower() != 'true':
if os.environ.get(SKIP_AUTH_ENV_VAR, "").lower() != "true":
token_creator = self._create_token_creator_if_does_not_exist(
token_creator=token_creator, auth_url=auth_url, client_id=client_id, client_secret=client_secret,
cert_str=cert_str, key_str=key_str, cert_file_path=cert_file_path, key_file_path=key_file_path)
self.rest_client: RestClient = RestClient(base_url=base_url, get_token=token_creator,
resource_group=resource_group, read_timeout=read_timeout,
connect_timeout=connect_timeout,
num_request_retries=num_request_retries,
client_type=client_type)
token_creator=token_creator,
auth_url=auth_url,
client_id=client_id,
client_secret=client_secret,
cert_str=cert_str,
key_str=key_str,
cert_file_path=cert_file_path,
key_file_path=key_file_path,
)
self.rest_client: RestClient = RestClient(
base_url=base_url,
get_token=token_creator,
resource_group=resource_group,
read_timeout=read_timeout,
connect_timeout=connect_timeout,
num_request_retries=num_request_retries,
client_type=client_type,
)
self.artifact: ArtifactClient = ArtifactClient(rest_client=self.rest_client)
self.configuration: ConfigurationClient = ConfigurationClient(rest_client=self.rest_client)
self.deployment: DeploymentClient = DeploymentClient(rest_client=self.rest_client)
self.executable: ExecutableClient = ExecutableClient(rest_client=self.rest_client)
self.configuration: ConfigurationClient = ConfigurationClient(
rest_client=self.rest_client
)
self.deployment: DeploymentClient = DeploymentClient(
rest_client=self.rest_client
)
self.executable: ExecutableClient = ExecutableClient(
rest_client=self.rest_client
)
self.execution: ExecutionClient = ExecutionClient(rest_client=self.rest_client)
self.execution_schedule: ExecutionScheduleClient = ExecutionScheduleClient(rest_client=self.rest_client)
self.execution_schedule: ExecutionScheduleClient = ExecutionScheduleClient(
rest_client=self.rest_client
)
self.healthz: HealthzClient = HealthzClient(rest_client=self.rest_client)
self.metrics: MetricsClient = MetricsClient(rest_client=self.rest_client)
self.model: ModelClient = ModelClient(rest_client=self.rest_client)
self.scenario: ScenarioClient = ScenarioClient(rest_client=self.rest_client)
self.meta: MetaClient = MetaClient(rest_client=self.rest_client)
admin_rest_client = RestClient(base_url=base_url[:-3], get_token=token_creator, resource_group=resource_group)
self.resource_groups: ResourceGroupsClient = ResourceGroupsClient(rest_client=admin_rest_client)
admin_rest_client = RestClient(
base_url=base_url[:-3],
get_token=token_creator,
resource_group=resource_group,
)
self.resource_groups: ResourceGroupsClient = ResourceGroupsClient(
rest_client=admin_rest_client
)
108 changes: 87 additions & 21 deletions packages/base/ai_api_client_sdk/exception.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
class AIAPIClientSDKException(Exception):
"""Base Exception class for AI API Client SDK exceptions"""

def __init__(self, description: str, status_code: int = None, error_message: str = None):
msg = f'{description}: {error_message}' if error_message else description
def __init__(
self, description: str, status_code: int = None, error_message: str = None
):
msg = f"{description}: {error_message}" if error_message else description
super().__init__(msg)
self.description = description
self.status_code = status_code
Expand All @@ -11,6 +13,7 @@ def __init__(self, description: str, status_code: int = None, error_message: str

class AIAPIInvalidInputException(AIAPIClientSDKException):
"""Exception type raised, when the provided input is invalid"""

def __init__(self, description: str):
super().__init__(description=description)

Expand All @@ -19,8 +22,11 @@ class AIAPIAuthenticatorException(AIAPIClientSDKException):
"""Exception type that is raised by the :class:`ai_api_client_sdk.helpers.authenticator.Authenticator`"""

def __init__(self, status_code: int = None, error_message: str = None):
super().__init__(description='Could not retrieve Authorization token', status_code=status_code,
error_message=error_message)
super().__init__(
description="Could not retrieve Authorization token",
status_code=status_code,
error_message=error_message,
)


class AIAPIAuthenticatorInvalidRequestException(AIAPIAuthenticatorException):
Expand Down Expand Up @@ -89,8 +95,15 @@ class AIAPIServerException(Exception):
:type details: dict, optional
"""

def __init__(self, description: str, status_code: int, error_message: str, error_code: str = None,
request_id: str = None, details: dict = None):
def __init__(
self,
description: str,
status_code: int,
error_message: str,
error_code: str = None,
request_id: str = None,
details: dict = None,
):
super().__init__()
self.description = description
self.status_code = status_code
Expand All @@ -100,8 +113,10 @@ def __init__(self, description: str, status_code: int, error_message: str, error
self.details = details

def __str__(self):
debug_msg = f'{self.description}: {self.error_message} \n ' \
f'Status Code: {self.status_code}, Request ID:{self.request_id}'
debug_msg = (
f"{self.description}: {self.error_message} \n "
f"Status Code: {self.status_code}, Request ID:{self.request_id}"
)
return debug_msg


Expand All @@ -111,10 +126,22 @@ class AIAPIAuthorizationException(AIAPIServerException):
for object definition
"""

def __init__(self, description: str, error_message: str, error_code: str = None, request_id: str = None,
details: dict = None):
super().__init__(description=description, status_code=401, error_code=error_code, error_message=error_message,
request_id=request_id, details=details)
def __init__(
self,
description: str,
error_message: str,
error_code: str = None,
request_id: str = None,
details: dict = None,
):
super().__init__(
description=description,
status_code=401,
error_code=error_code,
error_message=error_message,
request_id=request_id,
details=details,
)


class AIAPIInvalidRequestException(AIAPIServerException):
Expand All @@ -123,9 +150,22 @@ class AIAPIInvalidRequestException(AIAPIServerException):
for object definition
"""

def __init__(self, description: str, error_code: str, error_message: str, request_id: str, details: dict = None):
super().__init__(description=description, status_code=400, error_code=error_code, error_message=error_message,
request_id=request_id, details=details)
def __init__(
self,
description: str,
error_code: str,
error_message: str,
request_id: str,
details: dict = None,
):
super().__init__(
description=description,
status_code=400,
error_code=error_code,
error_message=error_message,
request_id=request_id,
details=details,
)


class AIAPINotFoundException(AIAPIServerException):
Expand All @@ -134,9 +174,22 @@ class AIAPINotFoundException(AIAPIServerException):
for object definition
"""

def __init__(self, description: str, error_code: str, error_message: str, request_id: str, details: dict = None):
super().__init__(description=description, status_code=404, error_code=error_code, error_message=error_message,
request_id=request_id, details=details)
def __init__(
self,
description: str,
error_code: str,
error_message: str,
request_id: str,
details: dict = None,
):
super().__init__(
description=description,
status_code=404,
error_code=error_code,
error_message=error_message,
request_id=request_id,
details=details,
)


class AIAPIPreconditionFailedException(AIAPIServerException):
Expand All @@ -145,6 +198,19 @@ class AIAPIPreconditionFailedException(AIAPIServerException):
for object definition
"""

def __init__(self, description: str, error_code: str, error_message: str, request_id: str, details: dict = None):
super().__init__(description=description, status_code=412, error_code=error_code, error_message=error_message,
request_id=request_id, details=details)
def __init__(
self,
description: str,
error_code: str,
error_message: str,
request_id: str,
details: dict = None,
):
super().__init__(
description=description,
status_code=412,
error_code=error_code,
error_message=error_message,
request_id=request_id,
details=details,
)
Loading
Loading