@mymediset/sap-ai-provider
v2.1.0
Published
SAP AI Core provider for AI SDK (powered by @sap-ai-sdk/orchestration)
Readme
SAP AI Core Provider for Vercel AI SDK
A community provider for SAP AI Core that integrates seamlessly with the Vercel AI SDK. Built on top of the official @sap-ai-sdk/orchestration package, this provider enables you to use SAP's enterprise-grade AI models through the familiar Vercel AI SDK interface.
⚠️ Breaking Changes in v2.0
Version 2.0 is a complete rewrite using the official SAP AI SDK. Key changes:
- Authentication: Now uses
AICORE_SERVICE_KEYenvironment variable (SAP AI SDK standard) - Provider creation: Now synchronous -
createSAPAIProvider()instead ofawait createSAPAIProvider() - No more
serviceKeyoption: Authentication is handled automatically by the SAP AI SDK - New helper functions: Use
buildDpiMaskingProvider(),buildAzureContentSafetyFilter()etc. from the SDK
Table of Contents
- Features
- Quick Start
- Installation
- Authentication
- Basic Usage
- Supported Models
- Advanced Features
- Configuration Options
- Error Handling
- Examples
- Migration from v1
- Contributing
- License
Features
- 🔐 Automatic Authentication - Uses SAP AI SDK's built-in credential handling
- 🎯 Tool Calling Support - Full function calling capabilities
- 🖼️ Multi-modal Input - Support for text and image inputs
- 📡 Streaming Support - Real-time text generation
- 🔒 Data Masking - Built-in SAP DPI integration for privacy
- 🛡️ Content Filtering - Azure Content Safety and Llama Guard support
- 🔧 TypeScript Support - Full type safety and IntelliSense
- 🎨 Multiple Models - Support for GPT-4, Claude, Gemini, Nova, and more
Quick Start
npm install @mymediset/sap-ai-provider aiimport { createSAPAIProvider } from "@mymediset/sap-ai-provider";
import { generateText } from "ai";
// Create provider (authentication via AICORE_SERVICE_KEY env var)
const provider = createSAPAIProvider();
// Generate text with gpt-4o
const result = await generateText({
model: provider("gpt-4o"),
prompt: "Explain quantum computing in simple terms.",
});
console.log(result.text);Installation
Requirements: Node.js 18+ and Vercel AI SDK 6.0+
npm install @mymediset/sap-ai-provider aiOr with other package managers:
# Yarn
yarn add @mymediset/sap-ai-provider ai
# pnpm
pnpm add @mymediset/sap-ai-provider aiAuthentication
The SAP AI SDK handles authentication automatically. You need to provide credentials in one of these ways:
On SAP BTP (Recommended)
When running on SAP BTP, bind an AI Core service instance to your application. The SDK will automatically detect the service binding from VCAP_SERVICES.
Local Development
Set the AICORE_SERVICE_KEY environment variable with your service key JSON:
# .env
AICORE_SERVICE_KEY='{"serviceurls":{"AI_API_URL":"https://..."},"clientid":"...","clientsecret":"...","url":"..."}'Get your service key from SAP BTP:
- Go to your SAP BTP Cockpit
- Navigate to your AI Core instance
- Create a service key
- Copy the JSON and set it as the environment variable
Basic Usage
Text Generation
import { createSAPAIProvider } from "@mymediset/sap-ai-provider";
import { generateText } from "ai";
const provider = createSAPAIProvider();
const result = await generateText({
model: provider("gpt-4o"),
prompt: "Write a short story about a robot learning to paint.",
});
console.log(result.text);Chat Conversations
import { generateText } from "ai";
const result = await generateText({
model: provider("anthropic--claude-3.5-sonnet"),
messages: [
{ role: "system", content: "You are a helpful coding assistant." },
{
role: "user",
content: "How do I implement binary search in TypeScript?",
},
],
});Streaming Responses
import { streamText } from "ai";
const result = streamText({
model: provider("gpt-4o"),
prompt: "Explain machine learning concepts.",
});
for await (const delta of result.textStream) {
process.stdout.write(delta);
}Model Configuration
const model = provider("gpt-4o", {
modelParams: {
temperature: 0.3,
maxTokens: 2000,
topP: 0.9,
},
});
const result = await generateText({
model,
prompt: "Write a technical blog post about TypeScript.",
});Supported Models
Azure OpenAI Models
gpt-4o,gpt-4o-minigpt-4.1,gpt-4.1-mini,gpt-4.1-nanoo1,o3,o3-mini,o4-mini
Google Vertex AI Models
gemini-2.0-flash,gemini-2.0-flash-litegemini-2.5-flash,gemini-2.5-pro
AWS Bedrock Models
anthropic--claude-3-haiku,anthropic--claude-3-sonnet,anthropic--claude-3-opusanthropic--claude-3.5-sonnet,anthropic--claude-3.7-sonnetanthropic--claude-4-sonnet,anthropic--claude-4-opusamazon--nova-pro,amazon--nova-lite,amazon--nova-micro,amazon--nova-premier
AI Core Open Source Models
mistralai--mistral-large-instruct,mistralai--mistral-medium-instruct,mistralai--mistral-small-instructcohere--command-a-reasoning
Model availability depends on your SAP AI Core subscription and region.
Advanced Features
Tool Calling
import { generateText, tool } from "ai";
import { z } from "zod";
const weatherSchema = z.object({
location: z.string(),
});
const weatherTool = tool({
description: "Get weather for a location",
inputSchema: weatherSchema,
execute: (args: z.infer<typeof weatherSchema>) => {
const { location } = args;
return `Weather in ${location}: sunny, 72°F`;
},
});
const result = await generateText({
model: provider("gpt-4o"),
prompt: "What's the weather in Tokyo?",
tools: { getWeather: weatherTool },
maxSteps: 3,
});
console.log(result.text);Multi-modal Input (Images)
const result = await generateText({
model: provider("gpt-4o"),
messages: [
{
role: "user",
content: [
{ type: "text", text: "What do you see in this image?" },
{ type: "image", image: new URL("https://example.com/image.jpg") },
],
},
],
});Data Masking (SAP DPI)
Use SAP's Data Privacy Integration to mask sensitive data:
import {
createSAPAIProvider,
buildDpiMaskingProvider,
} from "@mymediset/sap-ai-provider";
const dpiConfig = buildDpiMaskingProvider({
method: "anonymization",
entities: [
"profile-email",
"profile-person",
{
type: "profile-phone",
replacement_strategy: { method: "constant", value: "REDACTED" },
},
],
});
const provider = createSAPAIProvider({
defaultSettings: {
masking: {
masking_providers: [dpiConfig],
},
},
});
const result = await generateText({
model: provider("gpt-4o"),
prompt: "Email [email protected] about the meeting.",
});Content Filtering
import {
createSAPAIProvider,
buildAzureContentSafetyFilter,
} from "@mymediset/sap-ai-provider";
const provider = createSAPAIProvider({
defaultSettings: {
filtering: {
input: {
filters: [
buildAzureContentSafetyFilter("input", {
hate: "ALLOW_SAFE",
violence: "ALLOW_SAFE_LOW_MEDIUM",
}),
],
},
},
},
});Configuration Options
Provider Settings
interface SAPAIProviderSettings {
resourceGroup?: string; // SAP AI Core resource group (default: 'default')
deploymentId?: string; // Specific deployment ID (auto-resolved if not set)
destination?: HttpDestinationOrFetchOptions; // Custom destination
defaultSettings?: SAPAISettings; // Default settings for all models
}Model Settings
interface SAPAISettings {
modelVersion?: string; // Model version (default: 'latest')
modelParams?: {
maxTokens?: number; // Maximum tokens to generate
temperature?: number; // Sampling temperature (0-2)
topP?: number; // Nucleus sampling (0-1)
frequencyPenalty?: number; // Frequency penalty (-2 to 2)
presencePenalty?: number; // Presence penalty (-2 to 2)
n?: number; // Number of completions
parallel_tool_calls?: boolean; // Enable parallel tool calls
};
masking?: MaskingModule; // Data masking configuration
filtering?: FilteringModule; // Content filtering configuration
}Error Handling
import { SAPAIError } from "@mymediset/sap-ai-provider";
try {
const result = await generateText({
model: provider("gpt-4o"),
prompt: "Hello world",
});
} catch (error) {
if (error instanceof SAPAIError) {
console.error("Code:", error.code);
console.error("Location:", error.location);
console.error("Request ID:", error.requestId);
}
}Examples
Check out the examples directory for complete working examples:
Migration from v1
Authentication
Before (v1):
const provider = await createSAPAIProvider({
serviceKey: process.env.SAP_AI_SERVICE_KEY,
});After (v2):
// Set AICORE_SERVICE_KEY env var instead
const provider = createSAPAIProvider();Masking Configuration
Before (v1):
const dpiMasking = {
type: "sap_data_privacy_integration",
method: "anonymization",
entities: [{ type: "profile-email" }],
};After (v2):
import { buildDpiMaskingProvider } from "@mymediset/sap-ai-provider";
const dpiMasking = buildDpiMaskingProvider({
method: "anonymization",
entities: ["profile-email"],
});Provider is now synchronous
Before (v1):
const provider = await createSAPAIProvider({ serviceKey });After (v2):
const provider = createSAPAIProvider();Important Note
Third-Party Provider: This SAP AI Core provider (
@mymediset/sap-ai-provider) is developed and maintained by mymediset, not by SAP SE. While it uses the official SAP AI SDK and integrates with SAP AI Core services, it is not an official SAP product.
Contributing
We welcome contributions! Please see our Contributing Guide for details.
License
Apache License 2.0 - see LICENSE for details.
Support
Related
- Vercel AI SDK - The AI SDK this provider extends
- SAP AI SDK - Official SAP Cloud SDK for AI
- SAP AI Core Documentation - Official SAP AI Core docs
