SAP Cloud SDK for AI
Generative AI development on SAP BTP — LLM orchestration, prompt management, RAG.
Python SDK
from gen_ai_hub.orchestration import OrchestrationService
# Orchestration with templating and grounding
service = OrchestrationService(
api_url="https://api.ai.core.prod.<region>.aws.cloud.sap/v2",
client_id="...", client_secret="..."
)
# Define a prompt template
template = '''
System: You are an SAP procurement expert. Answer using only provided context.
Context: {{?grounding_context}}
User: {{?user_input}}
'''
result = service.invoke(
template=template,
grounding={"type": "document", "source": "sap-procurement-docs"},
model={"name": "gpt-4"},
input={"user_input": "How do I create a purchase order?"}
)
print(result.content)
Document Grounding
# Upload SAP documents for RAG
service.upload_document(
name="sap-procurement-docs",
content=open("procurement_guide.pdf", "rb"),
metadata={"category": "SAP_MM", "language": "EN"}
)
# Query with grounding
response = service.query_with_grounding(
question="What BAPI creates a purchase order?",
document_names=["sap-procurement-docs"],
top_k=3
)
Model Selection
| Model |
Provider |
Best For |
| GPT-4 / GPT-4o |
OpenAI |
Complex reasoning, code generation |
| Codex Opus / Sonnet |
Anthropic |
Analysis, structured output |
| Llama 3 |
Meta (self-hosted) |
Cost-sensitive, offline scenarios |
| Gemini Pro |
Google |
Multimodal, long context |
Multi-Step Orchestration
# Chain: classify → route → generate → validate
orchestrator = OrchestrationService()
steps = [
{"name": "classify", "template": classify_template, "model": "gpt-4o-mini"},
{"name": "generate", "template": generate_template, "model": "gpt-4"},
{"name": "validate", "template": validate_template, "model": "gpt-4o-mini"}
]
result = orchestrator.run_pipeline(steps=steps, input={"question": user_input})
Gotchas
- GenAI Hub requires AI Core entitlement on BTP subaccount
- Rate limits vary by model provider (OpenAI vs Anthropic vs Meta)
- Document grounding documents must be in supported format (PDF, TXT, MD)
- Always validate LLM output — hallucination risk with SAP technical content
1---2name: sap-cloud-sdk-ai3description: SAP Cloud SDK for AI — generative AI hub SDK, LLM orchestration, prompt templating, model selection, orchestration service for multi-step AI workflows, document grounding, retrieval augmented generation (RAG). Use when building AI agents with SAP Cloud SDK, calling LLMs via SAP BTP GenAI Hub, or implementing RAG patterns with SAP data.4---56# SAP Cloud SDK for AI78Generative AI development on SAP BTP — LLM orchestration, prompt management, RAG.910## Python SDK1112```python13from gen_ai_hub.orchestration import OrchestrationService1415# Orchestration with templating and grounding16service = OrchestrationService(17 api_url="https://api.ai.core.prod.<region>.aws.cloud.sap/v2",18 client_id="...", client_secret="..."19)2021# Define a prompt template22template = '''23System: You are an SAP procurement expert. Answer using only provided context.2425Context: {{?grounding_context}}2627User: {{?user_input}}28'''2930result = service.invoke(31 template=template,32 grounding={"type": "document", "source": "sap-procurement-docs"},33 model={"name": "gpt-4"},34 input={"user_input": "How do I create a purchase order?"}35)36print(result.content)37```3839## Document Grounding4041```python42# Upload SAP documents for RAG43service.upload_document(44 name="sap-procurement-docs",45 content=open("procurement_guide.pdf", "rb"),46 metadata={"category": "SAP_MM", "language": "EN"}47)4849# Query with grounding50response = service.query_with_grounding(51 question="What BAPI creates a purchase order?",52 document_names=["sap-procurement-docs"],53 top_k=354)55```5657## Model Selection5859| Model | Provider | Best For |60|---|---|---|61| GPT-4 / GPT-4o | OpenAI | Complex reasoning, code generation |62| Codex Opus / Sonnet | Anthropic | Analysis, structured output |63| Llama 3 | Meta (self-hosted) | Cost-sensitive, offline scenarios |64| Gemini Pro | Google | Multimodal, long context |6566## Multi-Step Orchestration6768```python69# Chain: classify → route → generate → validate70orchestrator = OrchestrationService()7172steps = [73 {"name": "classify", "template": classify_template, "model": "gpt-4o-mini"},74 {"name": "generate", "template": generate_template, "model": "gpt-4"},75 {"name": "validate", "template": validate_template, "model": "gpt-4o-mini"}76]7778result = orchestrator.run_pipeline(steps=steps, input={"question": user_input})79```8081## Gotchas82- GenAI Hub requires AI Core entitlement on BTP subaccount83- Rate limits vary by model provider (OpenAI vs Anthropic vs Meta)84- Document grounding documents must be in supported format (PDF, TXT, MD)85- Always validate LLM output — hallucination risk with SAP technical content