sap-cloud-sdk-ai-python
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSAP Cloud SDK for AI (Python)
SAP Cloud SDK for AI(Python版)
Package rename: The PyPI packageis deprecated (v4.12.4 is the last release). Its successor isgenerative-ai-hub-sdk(currently v6.10.0 per public PyPI registry evidence from 2026-06-15). Code and tutorials referencingsap-ai-sdk-genshould migrate togenerative-ai-hub-sdk; the import name remainssap-ai-sdk-gen.gen_ai_hub
The official Python SDK for SAP Generative AI Hub and Orchestration Service. It wraps
the native SDKs of model providers (OpenAI, Amazon Bedrock, Google GenAI) and offers
a harmonised LangChain integration and a full Orchestration client — all routed through
SAP AI Core with unified authentication. Package freshness is registry-verified; AI Core runtime behavior and exact model availability still require target-tenant validation.
包重命名说明:PyPI包已被弃用(v4.12.4为最后一个版本)。其替代包为**generative-ai-hub-sdk**(根据2026年6月15日公开PyPI注册表信息,当前版本为v6.10.0)。所有引用sap-ai-sdk-gen的代码和教程应迁移至generative-ai-hub-sdk;导入名称仍为sap-ai-sdk-gen。gen_ai_hub
这是SAP Generative AI Hub和编排服务的官方Python SDK。它封装了模型提供商(OpenAI、Amazon Bedrock、Google GenAI)的原生SDK,提供统一的LangChain集成和完整的编排客户端——所有请求均通过SAP AI Core路由并使用统一认证。包的更新状态已通过注册表验证;AI Core运行时行为和具体模型可用性仍需目标租户验证。
Related Skills
相关技能
- sap-ai-core: Platform setup, deployments, resource groups, and model management in SAP AI Core
- sap-cloud-sdk-ai: JavaScript/TypeScript and Java equivalents of this SDK
- sap-hana-ml: HANA-side machine learning in Python
- sap-dependency-security: Pip dependency hygiene and upgrade patterns
- sap-ai-core:SAP AI Core中的平台设置、部署、资源组和模型管理
- sap-cloud-sdk-ai:本SDK对应的JavaScript/TypeScript和Java版本
- sap-hana-ml:Python中的HANA端机器学习
- sap-dependency-security:Pip依赖管理和升级模式
Related external skills
相关外部技能
If your task involves working inside Databricks (notebooks, Unity Catalog, Spark,
SAP Databricks in SAP Business Data Cloud), consider installing the
Databricks agent skills plugin.
Ask whether you would like help installing it — never install unprompted.
如果您的任务涉及在Databricks(笔记本、Unity Catalog、Spark、SAP Business Data Cloud中的SAP Databricks)内操作,建议安装Databricks agent skills插件。如需帮助安装可提出请求——请勿未经提示自行安装。
When to Use This Skill
适用场景
Use this skill when:
- Building Python applications that call LLMs through SAP AI Core / Generative AI Hub
- Using the Python package (installed as
gen_ai_hub)sap-ai-sdk-gen - Integrating OpenAI, Amazon Bedrock, or Google GenAI models via SAP's proxy
- Implementing LangChain chains with SAP AI Core as the backend
- Using the Orchestration Service from Python (templating, filtering, masking, grounding)
- Migrating code from the deprecated to
generative-ai-hub-sdksap-ai-sdk-gen - Generating embeddings through SAP AI Core
- Working with SAP RPT-1 (Relational Pretrained Transformer) for tabular predictions
在以下场景中使用本技能:
- 构建通过SAP AI Core / Generative AI Hub调用大语言模型(LLM)的Python应用
- 使用Python包(通过安装
gen_ai_hub获取)sap-ai-sdk-gen - 通过SAP代理集成OpenAI、Amazon Bedrock或Google GenAI模型
- 以SAP AI Core为后端实现LangChain链
- 从Python中使用编排服务(模板化、过滤、掩码、检索增强生成)
- 将代码从已弃用的迁移至
generative-ai-hub-sdksap-ai-sdk-gen - 通过SAP AI Core生成嵌入向量
- 使用SAP RPT-1(关系预训练Transformer)进行表格预测
Table of Contents
目录
Quick Start
快速开始
Native OpenAI Chat Completion
原生OpenAI聊天补全
python
from gen_ai_hub.proxy.native.openai import chat
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is SAP BTP?"}
]
response = chat.completions.create(
model_name="gpt-4o-mini",
messages=messages
)
print(response.choices[0].message.content)python
from gen_ai_hub.proxy.native.openai import chat
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is SAP BTP?"}
]
response = chat.completions.create(
model_name="gpt-4o-mini",
messages=messages
)
print(response.choices[0].message.content)Orchestration Service
编排服务
python
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, OrchestrationService,
ModuleConfig, PromptTemplatingModuleConfig,
Template, UserMessage, LLMModelDetails
)
config = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(
template=[UserMessage(role="user", content="{{?question}}")]
),
model=LLMModelDetails(name="gpt-4o-mini")
)
)
)
service = OrchestrationService(config=config)
response = service.run(placeholder_values={"question": "What is SAP?"})
print(response.final_result.choices[0].message.content)python
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, OrchestrationService,
ModuleConfig, PromptTemplatingModuleConfig,
Template, UserMessage, LLMModelDetails
)
config = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(
template=[UserMessage(role="user", content="{{?question}}")]
),
model=LLMModelDetails(name="gpt-4o-mini")
)
)
)
service = OrchestrationService(config=config)
response = service.run(placeholder_values={"question": "What is SAP?"})
print(response.final_result.choices[0].message.content)Installation
安装
bash
undefinedbash
undefinedAll providers + LangChain support
所有提供商 + LangChain支持
pip install "sap-ai-sdk-gen[all]"
pip install "sap-ai-sdk-gen[all]"
Default (OpenAI only, no LangChain)
默认安装(仅OpenAI,无LangChain)
pip install sap-ai-sdk-gen
pip install sap-ai-sdk-gen
Specific providers (without LangChain)
指定提供商(无LangChain)
pip install "sap-ai-sdk-gen[google, amazon]"
undefinedpip install "sap-ai-sdk-gen[google, amazon]"
undefinedAuthentication
认证
The SDK reads credentials via , which resolves
credentials in this order:
AICoreV2Client.from_env()- Keyword arguments passed to
GenAIHubProxyClient(...) - Environment variables — ,
AICORE_CLIENT_ID,AICORE_CLIENT_SECRET,AICORE_AUTH_URL,AICORE_BASE_URLAICORE_RESOURCE_GROUP - Config file — (or path set by
$AICORE_HOME/config.json); useAICORE_CONFIGto select a named profileAICORE_PROFILE - VCAP_SERVICES — automatic on Cloud Foundry/Kyma when the AI Core service is bound
SDK通过读取凭证,凭证解析顺序如下:
AICoreV2Client.from_env()- 关键字参数:传递给的参数
GenAIHubProxyClient(...) - 环境变量——、
AICORE_CLIENT_ID、AICORE_CLIENT_SECRET、AICORE_AUTH_URL、AICORE_BASE_URLAICORE_RESOURCE_GROUP - 配置文件——(或由
$AICORE_HOME/config.json设置的路径);使用AICORE_CONFIG选择命名配置文件AICORE_PROFILE - VCAP_SERVICES——在Cloud Foundry/Kyma上绑定AI Core服务时自动读取
Local Development (Environment Variables)
本地开发(环境变量)
bash
export AICORE_CLIENT_ID="sb-..."
export AICORE_CLIENT_SECRET="..."
export AICORE_AUTH_URL="https://<tenant>.authentication.sap.hana.ondemand.com/oauth/token"
export AICORE_BASE_URL="https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2"
export AICORE_RESOURCE_GROUP="default"bash
export AICORE_CLIENT_ID="sb-..."
export AICORE_CLIENT_SECRET="..."
export AICORE_AUTH_URL="https://<tenant>.authentication.sap.hana.ondemand.com/oauth/token"
export AICORE_BASE_URL="https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2"
export AICORE_RESOURCE_GROUP="default"Config File Profile
配置文件配置
bash
undefinedbash
undefined~/.aicore/config.json
~/.aicore/config.json
{
"AICORE_CLIENT_ID": "sb-...",
"AICORE_CLIENT_SECRET": "...",
"AICORE_AUTH_URL": "https://<tenant>.authentication.sap.hana.ondemand.com/oauth/token",
"AICORE_BASE_URL": "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2",
"AICORE_RESOURCE_GROUP": "default"
}
For detailed auth setup and troubleshooting, see `references/getting-started-auth.md`.{
"AICORE_CLIENT_ID": "sb-...",
"AICORE_CLIENT_SECRET": "...",
"AICORE_AUTH_URL": "https://<tenant>.authentication.sap.hana.ondemand.com/oauth/token",
"AICORE_BASE_URL": "https://api.ai.prod.eu-central-1.aws.ml.hana.ondemand.com/v2",
"AICORE_RESOURCE_GROUP": "default"
}
如需详细的认证设置和故障排除,请参阅`references/getting-started-auth.md`。Available Modules
可用模块
| Module | Import Path | Purpose |
|---|---|---|
| Proxy (native clients) | | Direct model access per provider |
| LangChain integration | | |
| Orchestration | | Templating, filtering, masking, grounding |
| Document Grounding | | Pipeline, Vector, Retrieval APIs |
| Prompt Registry | | Template management and config storage |
| Evaluations | | Model evaluation runs and metrics |
| SAP RPT-1 | | Tabular prediction (classification, regression) |
| 模块 | 导入路径 | 用途 |
|---|---|---|
| 代理(原生客户端) | | 按提供商直接访问模型 |
| LangChain集成 | | |
| 编排服务 | | 模板化、过滤、掩码、检索增强生成 |
| 文档检索增强生成 | | 流水线、向量、检索API |
| 提示词注册表 | | 模板管理和配置存储 |
| 评估模块 | | 模型评估运行和指标 |
| SAP RPT-1 | | 表格预测(分类、回归) |
Native Clients by Provider
各提供商原生客户端
| Provider | Import | Key Classes |
|---|---|---|
| OpenAI | | |
| Amazon Bedrock | | |
| Google GenAI | | |
| SAP RPT-1 | | |
| 提供商 | 导入路径 | 核心类 |
|---|---|---|
| OpenAI | | |
| Amazon Bedrock | | |
| Google GenAI | | |
| SAP RPT-1 | | |
Supported Models
支持的模型
The Generative AI Hub catalog includes models from multiple providers. Check
SAP's model catalog
and the target tenant catalog for the authoritative model IDs. Example families:
| Provider | Example Families |
|---|---|
| OpenAI | GPT-family chat, multimodal, reasoning, and embedding models |
| Anthropic (via Bedrock) | Claude-family models |
| Amazon | Nova/Titan-family models |
| Gemini-family models | |
| Mistral | Mistral-family models |
| SAP | RPT-family tabular prediction models where enabled |
Generative AI Hub目录包含来自多个提供商的模型。请查看SAP模型目录和目标租户目录获取权威模型ID。示例模型系列:
| 提供商 | 示例模型系列 |
|---|---|
| OpenAI | GPT系列聊天、多模态、推理和嵌入模型 |
| Anthropic(通过Bedrock) | Claude系列模型 |
| Amazon | Nova/Titan系列模型 |
| Gemini系列模型 | |
| Mistral | Mistral系列模型 |
| SAP | RPT系列表格预测模型(已启用时) |
Core Features
核心功能
Chat Completion with OpenAI Client
使用OpenAI客户端实现聊天补全
python
from gen_ai_hub.proxy.native.openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Explain CAP in one paragraph."}]
)
print(response.choices[0].message.content)python
from gen_ai_hub.proxy.native.openai import OpenAI
client = OpenAI()
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Explain CAP in one paragraph."}]
)
print(response.choices[0].message.content)Streaming
流式传输
python
from gen_ai_hub.proxy.native.openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Explain SAP CAP."}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")python
from gen_ai_hub.proxy.native.openai import OpenAI
client = OpenAI()
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Explain SAP CAP."}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")Embeddings
嵌入向量生成
python
from gen_ai_hub.proxy.native.openai import embeddings
response = embeddings.create(
input="Every decoding is another encoding.",
model_name="text-embedding-3-small"
)
print(response.data[0].embedding)python
from gen_ai_hub.proxy.native.openai import embeddings
response = embeddings.create(
input="Every decoding is another encoding.",
model_name="text-embedding-3-small"
)
print(response.data[0].embedding)LangChain Integration
LangChain集成
python
from gen_ai_hub.proxy.langchain import init_llm, init_embedding_model
llm = init_llm("gpt-4o-mini", max_tokens=300)
result = llm.invoke("What is SAP BTP?")
print(result.content)
embeddings = init_embedding_model("text-embedding-3-small")
vector = embeddings.embed_query("SAP Business Technology Platform")python
from gen_ai_hub.proxy.langchain import init_llm, init_embedding_model
llm = init_llm("gpt-4o-mini", max_tokens=300)
result = llm.invoke("What is SAP BTP?")
print(result.content)
embeddings = init_embedding_model("text-embedding-3-small")
vector = embeddings.embed_query("SAP Business Technology Platform")Content Filtering (via Orchestration)
内容过滤(通过编排服务)
python
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, OrchestrationService,
ModuleConfig, PromptTemplatingModuleConfig,
Template, UserMessage, LLMModelDetails,
FilteringModuleConfig, InputFiltering, OutputFiltering,
AzureContentSafetyInput, AzureContentSafetyOutput, AzureThreshold
)
config = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(template=[UserMessage(role="user", content="{{?question}}")]),
model=LLMModelDetails(name="gpt-4o-mini")
),
filtering=FilteringModuleConfig(
input=InputFiltering(filters=[
AzureContentSafetyInput(hate=AzureThreshold.ALLOW_SAFE, violence=AzureThreshold.ALLOW_SAFE)
]),
output=OutputFiltering(filters=[
AzureContentSafetyOutput(hate=AzureThreshold.ALLOW_SAFE, violence=AzureThreshold.ALLOW_SAFE)
])
)
)
)
service = OrchestrationService(config=config)
response = service.run(placeholder_values={"question": "Explain SAP."})python
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, OrchestrationService,
ModuleConfig, PromptTemplatingModuleConfig,
Template, UserMessage, LLMModelDetails,
FilteringModuleConfig, InputFiltering, OutputFiltering,
AzureContentSafetyInput, AzureContentSafetyOutput, AzureThreshold
)
config = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(template=[UserMessage(role="user", content="{{?question}}")]),
model=LLMModelDetails(name="gpt-4o-mini")
),
filtering=FilteringModuleConfig(
input=InputFiltering(filters=[
AzureContentSafetyInput(hate=AzureThreshold.ALLOW_SAFE, violence=AzureThreshold.ALLOW_SAFE)
]),
output=OutputFiltering(filters=[
AzureContentSafetyOutput(hate=AzureThreshold.ALLOW_SAFE, violence=AzureThreshold.ALLOW_SAFE)
])
)
)
)
service = OrchestrationService(config=config)
response = service.run(placeholder_values={"question": "Explain SAP."})Data Masking (via Orchestration)
数据掩码(通过编排服务)
python
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, OrchestrationService,
ModuleConfig, PromptTemplatingModuleConfig,
Template, UserMessage, LLMModelDetails,
MaskingModuleConfig, MaskingProviderConfig,
DPIStandardEntity, MaskingMethod, DataMaskingProviderName
)
config = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(template=[UserMessage(role="user", content="{{?text}}")]),
model=LLMModelDetails(name="gpt-4o-mini")
),
masking=MaskingModuleConfig(
masking_providers=[
MaskingProviderConfig(
type=DataMaskingProviderName.SAP_DATA_PRIVACY_INTEGRATION,
method=MaskingMethod.ANONYMIZATION,
entities=[
DPIStandardEntity(type="profile-email"),
DPIStandardEntity(type="profile-person")
]
)
]
)
)
)
service = OrchestrationService(config=config)
response = service.run(placeholder_values={"text": "Contact john@example.com for details."})python
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, OrchestrationService,
ModuleConfig, PromptTemplatingModuleConfig,
Template, UserMessage, LLMModelDetails,
MaskingModuleConfig, MaskingProviderConfig,
DPIStandardEntity, MaskingMethod, DataMaskingProviderName
)
config = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(template=[UserMessage(role="user", content="{{?text}}")]),
model=LLMModelDetails(name="gpt-4o-mini")
),
masking=MaskingModuleConfig(
masking_providers=[
MaskingProviderConfig(
type=DataMaskingProviderName.SAP_DATA_PRIVACY_INTEGRATION,
method=MaskingMethod.ANONYMIZATION,
entities=[
DPIStandardEntity(type="profile-email"),
DPIStandardEntity(type="profile-person")
]
)
]
)
)
)
service = OrchestrationService(config=config)
response = service.run(placeholder_values={"text": "Contact john@example.com for details."})Document Grounding (via Orchestration)
文档检索增强生成(通过编排服务)
python
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, OrchestrationService,
ModuleConfig, PromptTemplatingModuleConfig,
Template, UserMessage, LLMModelDetails,
GroundingModuleConfig, DocumentGroundingConfig,
DocumentGroundingFilter, DocumentGroundingPlaceholders,
GroundingSearchConfig, DataRepositoryType, GroundingType
)
config = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(template=[UserMessage(role="user", content="{{?question}}")]),
model=LLMModelDetails(name="gpt-4o-mini")
),
grounding=GroundingModuleConfig(
type=GroundingType.DOCUMENT_GROUNDING_SERVICE,
config=DocumentGroundingConfig(
placeholders=DocumentGroundingPlaceholders(
input=["{{?question}}"],
output="{{?context}}"
),
filters=[
DocumentGroundingFilter(
id="my-vector-repo-id",
data_repository_type=DataRepositoryType.VECTOR,
search_config=GroundingSearchConfig(max_chunk_count=5)
)
]
)
)
)
)
service = OrchestrationService(config=config)
response = service.run(placeholder_values={"question": "What is the refund policy?"})python
from gen_ai_hub.orchestration_v2 import (
OrchestrationConfig, OrchestrationService,
ModuleConfig, PromptTemplatingModuleConfig,
Template, UserMessage, LLMModelDetails,
GroundingModuleConfig, DocumentGroundingConfig,
DocumentGroundingFilter, DocumentGroundingPlaceholders,
GroundingSearchConfig, DataRepositoryType, GroundingType
)
config = OrchestrationConfig(
modules=ModuleConfig(
prompt_templating=PromptTemplatingModuleConfig(
prompt=Template(template=[UserMessage(role="user", content="{{?question}}")]),
model=LLMModelDetails(name="gpt-4o-mini")
),
grounding=GroundingModuleConfig(
type=GroundingType.DOCUMENT_GROUNDING_SERVICE,
config=DocumentGroundingConfig(
placeholders=DocumentGroundingPlaceholders(
input=["{{?question}}"],
output="{{?context}}"
),
filters=[
DocumentGroundingFilter(
id="my-vector-repo-id",
data_repository_type=DataRepositoryType.VECTOR,
search_config=GroundingSearchConfig(max_chunk_count=5)
)
]
)
)
)
)
service = OrchestrationService(config=config)
response = service.run(placeholder_values={"question": "What is the refund policy?"})Common Errors
常见错误
| Error | Cause | Solution |
|---|---|---|
| Missing AI Core service key/env vars | Set all |
| Model not deployed in AI Core | Deploy the model in your resource group, or use |
| Missing resource group | Set |
| Wrong package installed | Install |
Import from | Using deprecated package name | The package was renamed; import from |
| Incomplete credentials | Verify all four required env vars: |
| 错误信息 | 原因 | 解决方案 |
|---|---|---|
| 缺少AI Core服务密钥/环境变量 | 设置所有 |
| 模型未在AI Core中部署 | 在您的资源组中部署模型,或直接使用 |
| 缺少资源组 | 设置 |
| 安装了错误的包 | 安装 |
从 | 使用了已弃用的包名 | 包已重命名;从 |
代理客户端初始化时出现 | 凭证不完整 | 验证四个必填环境变量: |
Bundled Resources
捆绑资源
Reference Documentation
参考文档
- - Installation, authentication, and config setup
references/getting-started-auth.md - - Native client usage for OpenAI, Amazon, Google, and SAP RPT-1
references/native-clients-guide.md - - Orchestration service: templating, filtering, masking, grounding, embeddings
references/orchestration-guide.md - - LangChain integration: LLM/embedding init, chains, structured outputs
references/langchain-guide.md - - Common errors, version compatibility, migration from
references/troubleshooting.mdgenerative-ai-hub-sdk
- - 安装、认证和配置设置
references/getting-started-auth.md - - OpenAI、Amazon、Google和SAP RPT-1的原生客户端使用指南
references/native-clients-guide.md - - 编排服务:模板化、过滤、掩码、检索增强生成、嵌入向量
references/orchestration-guide.md - - LangChain集成:LLM/嵌入模型初始化、链、结构化输出
references/langchain-guide.md - - 常见错误、版本兼容性、从
references/troubleshooting.md迁移指南generative-ai-hub-sdk
Documentation Sources
文档来源
Keep this skill updated using these sources:
- PyPI: https://pypi.org/pypi/sap-ai-sdk-gen/json — package metadata and README
- SDK Reference: https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/gen_ai_hub.html
- SAP Samples: https://github.com/SAP-samples/btp-gen-ai-hub-sdk-samples
- AI Core Help: https://help.sap.com/docs/sap-ai-core
- Deprecated Package: https://pypi.org/pypi/generative-ai-hub-sdk/json (for migration notes)
通过以下来源保持本技能更新:
- PyPI:https://pypi.org/pypi/sap-ai-sdk-gen/json — 包元数据和README
- SDK参考文档:https://help.sap.com/doc/generative-ai-hub-sdk/CLOUD/en-US/_reference/gen_ai_hub.html
- SAP示例:https://github.com/SAP-samples/btp-gen-ai-hub-sdk-samples
- AI Core帮助文档:https://help.sap.com/docs/sap-ai-core
- 已弃用包:https://pypi.org/pypi/generative-ai-hub-sdk/json(用于迁移说明)