Sample code to demonstrate the usage of the SAP Cloud SDK for AI. Parts of the sample code are also used in E2E tests.
-
Run
pnpm installto install dependencies. -
Create a
.envfile in thesample-codedirectory with the complete content of your AI core service key by adding the following line:AICORE_SERVICE_KEY='{ "clientid": "...", ... }'
-
Run
pnpm localto start the server.
GET /ai-api/deployments
Get all deployments in resource group default.
GET /ai-api/deployments-with-destination
Get all deployments targeting a custom destination.
Provide a destination when calling the execute() method.
To create a custom destination, the following template can be used in the .env file:
destinations='[{
"name": "<destination name>",
"url": "<destination url>",
"authentication": "OAuth2ClientCredentials",
"authTokens": [{
"http_header": {
"key": "Authorization",
"value": "Bearer <your token>" }
}]
}]'Refer to our guide for more details on testing locally with custom destinations.
POST /ai-api/deployment/create
{
"configurationId": "CONFIGURATION_ID"
}Create a deployment with certain configuration ID in resource group default.
PATCH /ai-api/deployment/batch-stop
{
"configurationId": "CONFIGURATION_ID"
}Stop all deployments with a certain configuration ID in resource group default.
DELETE /ai-api/deployment/batch-delete
Delete all deployments with status STOPPED or UNKNOWN in resource group default.
GET /ai-api/scenarios
Get all scenarios in resource group default.
GET /ai-api/models
Get all foundation models in resource group default.
GET /azure-openai/chat-completion
Get chat completion response.
GET /azure-openai/chat-completion-with-destination
Get chat completion response targeting a custom destination.
Provide a destination when initializing the AzureOpenAiChatClient.
To reference a custom destination check out: Get all Deployments with Custom Destination
GET /azure-openai/chat-completion-stream
Get chat completion response with streaming.
The response header is set with Content-Type: text/event-stream to stream the text.
AbortController is used to cancel the request in case user closes or refreshes the page, or there is an error.
The toContentStream() method is called to extract the content of the chunk for convenience.
Once the streaming is done, finish reason and token usage are printed out.
GET /azure-openai/embedding
Get embedding vector for the input.
GET /orchestration/simple
Get chat completion response for a given static input.
GET /orchestration/template
Get chat completion response with template and input parameters.
Define variable by wrapping it with {{?...}}.
GET /orchestration/templateRef
Get chat completion response with template reference and input parameters.
GET /orchestration/messageHistory
Get chat completion response and use it as input for subsequent chat completion requests through message history.
GET /orchestration/inputFiltering
Get chat completion response with Azure content filter for the input.
For example, use buildAzureContentSafetyFilter() function to build Azure content filter.
GET /orchestration/outputFiltering
Get chat completion response with Azure content filter for the output.
For example, use buildAzureContentSafetyFilter() function to build Azure content filter.
GET /orchestration/requestConfig
Send chat completion request with a custom header as the custom request configuration.
GET /orchestration/fromJson
Get chat completion response with a JSON config initialized orchestration client.
GET /orchestration/image
Get chat completion response with image input.
GET /orchestration/responseFormat
Get chat completion response with structured response format.
GET /orchestration/maskGroundingInput
Get chat completion response with masked grounding input.
POST /orchestration-stream/chat-completion-stream
Get a chat completion response with streaming.
You can set the streaming options in the body of the request.
An example for setting the chunk size would look like this:
curl -X POST http://localhost:8080/orchestration-stream/chat-completion-stream \
-H "Content-Type: application/json" \
-d '{
"global": {
"chunk_size": 10
}
}'
The response header is set with Content-Type: text/event-stream to stream the text.
AbortController is used to cancel the request in case user closes or refreshes the page, or there is an error.
The toContentStream() method is called to extract the content of the chunk for convenience.
Once the streaming is done, finish reason and token usage are printed out.
GET /orchestration-stream/chat-completion-stream-json
Get a chat completion response with streaming with a JSON Module Config initalized client.
The response header is set with Content-Type: text/event-stream to stream the text.
AbortController is used to cancel the request in case user closes or refreshes the page, or there is an error.
The toContentStream() method is called to extract the content of the chunk for convenience.
Once the streaming is done, finish reason and token usage are printed out.
GET /orchestration/translation
Get chat completion response with input and output translation.
For example, use buildTranslationConfig() function to build a SAP Document Translation config.
GET /langchain/invoke
Invoke LangChain Azure OpenAI client with a simple input to get chat completion response.
GET /langchain/invoke-chain
Invoke chain to get chat completion response from Azure OpenAI. The chain contains a template and a string parser.
GET /langchain/invoke-chain-orchestration
Invoke a chain to get a orchestration response from the orchestration service. The chain has a built-in template and is chained with a string parser.
GET /langchain/invoke-chain-orchestration-input-filter
Invoke a chain to trigger input filtering of orchestration service.
GET /langchain/invoke-chain-orchestration-output-filter
Invoke a chain to trigger output filtering of orchestration service.
GET /langchain/invoke-chain-orchestration-masking
Invoke a chain to trigger data masking of orchestration service.
GET /langchain/invoke-rag-chain
Invoke a chain to embed documents and get chat completion response with context from Azure OpenAI. The chain performs RAG with the chat and embedding client.
GET /langchain/stream-azure-openai
Stream chat completion response with Azure OpenAI using LangChain.
GET /langchain/stream-orchestration
Stream chat completion response with orchestration service using LangChain.
GET /document-grounding/orchestration-grounding-vector
This scenario demonstrates the end-to-end flow for creating collections and documents using the document grounding service, and then using the orchestration grounding module to get a chat completion response with a retrieved context based on vector data repository.
The flow will first create an empty collection and then add a document to it. The document contains a statement with the current timestamp. It will be retrieved with a user question asking for the latest timestamp by orchestration grounding module. Then, orchestration service will send a chat completion request with the context to LLM. The response should contain the same timestamp.
The created collection will be deleted at the end of the flow.
GET /document-grounding/retrieve-documents
This scenario demonstrates the end-to-end flow for creating collections and documents using the document grounding service, and then retrieving the documents with a query.
The response should contain chunks of documents similar to the query.
The created collection will be deleted at the end of the flow.
GET /document-grounding/orchestration-grounding-help-sap-com
This scenario uses the orchestration grounding module to get a chat completion response with a retrieved context based on help.sap.com data repository.
Orchestration service will send a chat completion request with the context to LLM. The response should contain the same timestamp.
The created collection will be deleted at the end of the flow.
GET /prompt-registry/template
Create a prompt template and delete it.
This tutorial will guide you through the process of building an agent using the LangChain OpenAI Client and the Model Context Protocol (MCP).
To run the tutorial, execute the following command in the sample-code directory:
pnpm agent-tutorial