SAP Cloud SDK for AI (Python) - generative
The SDK formerly known as generative AI Hub SDK was rebranded.
With this SDK you can leverage the power of generative models available in the generative AI Hub of SAP AI Core. This SDK provides LLM access by wrapping the native SDKs of the model providers (OpenAI, Amazon, Google), through langchain, or through the orchestration service.
Installation
Use the package name to install the SDK with support for all models (OpenAI, Amazon, Google) including langchain support:
pip install "sap-ai-sdk-gen[all]"
With the name rebranding, class names have not changed i.e., you can continue to use existing code.
The default installation only includes OpenAI models (with langchain support):
pip install sap-ai-sdk-gen
You can install a subset of the extra libraries (with langchain support) by specifying them in square brackets:
pip install "sap-ai-sdk-gen[google, amazon]"
In the table below, you can see which models and vendor specific langchain packages are installed when using different installation parameters.
| Install Parameter | OpenAI | AWS | LangChain | OpenAI-LangChain | Google-LangChain | AWS-LangChain | |
|---|---|---|---|---|---|---|---|
| yes | no | no | yes | yes | no | no | |
| [google] | yes | yes | no | yes | yes | yes | no |
| [amazon] | yes | no | yes | yes | yes | no | yes |
| [amazon, google] / [google, amazon] | yes | yes | yes | yes | yes | yes | yes |
| [all] | yes | yes | yes | yes | yes | yes | yes |
Configuration
There are different ways to configure the SAP AI Core access (listed in order of precedence):
- environment variables
- (profile) configuration file
- from VCAP_SERVICES environment variable, if it exists
These methods automatically initialize an authenticated client.
For custom authentication, you can provide a proxy_client parameter when instantiating SDK classes to use your own
GenAIHubProxyClient with direct credential configuration.
We recommend setting these values as environment variables or via config file. The default path for the configuration file
is ~/.aicore/config.json.
Environment variables
AICORE_CLIENT_ID: This represents the client ID.AICORE_CLIENT_SECRET: This stands for the client secret.AICORE_AUTH_URL: This is the URL used to retrieve a token using the client ID and secret.AICORE_BASE_URL: This is the URL of the service (with suffix /v2).AICORE_RESOURCE_GROUP: This represents the resource group that should be used.AI_CLIENT_TYPE(optional): Specify client type in request headers. Default is 'GenAI Hub SDK (Python)'. Note: This cannot be set in the config file.
For using X.509 credentials, you can set the file paths to certificate and key files, or certificate and key strings, as an alternative to client secret.
AICORE_CERT_FILE_PATH: This is the path to the file which holds the X.509 certificate.AICORE_KEY_FILE_PATH: This is the path to the file which holds the X.509 key.AICORE_CERT_STR: This is the content of the X.509 certificate as a string.AICORE_KEY_STR: This is the content of the X.509 key as a string.
Configuration files
By default, the configuration file is located at ~/.aicore/config.json. You can change the directory where the config file is located by setting the AICORE_HOME environment variable.
Note: tilde (~) is not supported, so use the full path to the directory.
A profile is a json file residing in a config directory. With profile names one can switch easily between profiles e.g., for different (sub)accounts. The profile name can be passed also as a keyword. If no profile is specified, the default profile is used. Specify the profile via environment variable AICORE_PROFILE. The associated configuration file then needs to have file name config_{profile}.json.
The command aicore configure --help can be used to generate a profile.
The following list explains which environment variables can be used to control which configuration file will be used:
AICORE_HOME: This variable represents a directory path. Within this directory, various configuration files can be stored and the SDK will automatically load them from there based on theAICORE_PROFILEenvironment variable.AICORE_PROFILE: This variable allows users to switch between different configurations stored in theAICORE_HOMEdirectory. It is important to note thatAICORE_PROFILEdoes not represent the complete name of a configuration file. Instead, it refers to a profile name, which corresponds to a file namedconfig_{profile}.json. IfAICORE_PROFILEis empty,$AICORE_HOME/config.jsonis used.AICORE_CONFIG: This variable overrides bothAICORE_HOMEandAICORE_PROFILE. It specifies the direct absolute path to a configuration file that will be used.
The configuration file should be:
{
"AICORE_AUTH_URL": "https://***.authentication.sap.hana.ondemand.com/oauth/token",
"AICORE_CLIENT_ID": "***",
"AICORE_CLIENT_SECRET": "***",
"AICORE_RESOURCE_GROUP": "***",
"AICORE_BASE_URL": "https://api.ai.***.cfapps.sap.hana.ondemand.com/v2"
}
or
{
"AICORE_AUTH_URL": "https://***.authentication.cert.sap.hana.ondemand.com",
"AICORE_CLIENT_ID": "***",
"AICORE_CERT_FILE_PATH": "***/cert.pem",
"AICORE_KEY_FILE_PATH": "***/key.pem",
"AICORE_RESOURCE_GROUP": "***",
"AICORE_BASE_URL": "https://api.ai.***.cfapps.sap.hana.ondemand.com/v2"
}
or
{
"AICORE_AUTH_URL": "https://***.authentication.cert.sap.hana.ondemand.com",
"AICORE_CLIENT_ID": "***",
"AICORE_CERT_STR": "***",
"AICORE_KEY_STR": "***",
"AICORE_RESOURCE_GROUP": "***",
"AICORE_BASE_URL": "https://api.ai.***.cfapps.sap.hana.ondemand.com/v2"
}
Usage
Prerequisite
For direct model access, you need to create a deployment for each desired model according to the help documentation for model deployments.
For model access through the orchestration service, you need to create a deployment of the orchestration service according to the help documentation for orchestration service deployments.
Examples
In section Examples there are code snippets for each Large Language and Embedding model as well as for the orchestration service usage.
Supported Models
The list of models in the Generative AI Hub of SAP AI Core can be found in SAP note 3437766.