@mymediset/sap-ai-provider
Version:
SAP AI Core provider for AI SDK
282 lines (276 loc) • 9.98 kB
text/typescript
import { LanguageModelV2, LanguageModelV2CallOptions, LanguageModelV2Content, LanguageModelV2FinishReason, LanguageModelV2Usage, LanguageModelV2CallWarning, LanguageModelV2StreamPart, ProviderV2 } from '@ai-sdk/provider';
import { FetchFunction } from '@ai-sdk/provider-utils';
import { z } from 'zod';
/**
* Settings for configuring SAP AI Core model behavior.
*/
interface SAPAISettings {
/**
* Specific version of the model to use.
* If not provided, the latest version will be used.
*/
modelVersion?: string;
/**
* Model generation parameters that control the output.
*/
modelParams?: {
/**
* Maximum number of tokens to generate.
* Higher values allow for longer responses but increase latency and cost.
* @default 1000
*/
maxTokens?: number;
/**
* Sampling temperature between 0 and 2.
* Higher values make output more random, lower values more deterministic.
* @default 0.7
*/
temperature?: number;
/**
* Nucleus sampling parameter between 0 and 1.
* Controls diversity via cumulative probability cutoff.
* @default 1
*/
topP?: number;
/**
* Frequency penalty between -2.0 and 2.0.
* Positive values penalize tokens based on their frequency.
* @default 0
*/
frequencyPenalty?: number;
/**
* Presence penalty between -2.0 and 2.0.
* Positive values penalize tokens that have appeared in the text.
* @default 0
*/
presencePenalty?: number;
/**
* Number of completions to generate.
* Multiple completions provide alternative responses.
* @default 1
*/
n?: number;
};
/**
* Enable safe prompt filtering.
* When enabled, prompts are checked for harmful content.
* @default true
*/
safePrompt?: boolean;
/**
* Enable structured outputs.
* When enabled, responses will be formatted according to provided schemas.
* @default false
*/
structuredOutputs?: boolean;
}
/**
* Supported model IDs in SAP AI Core.
* Note: Model availability depends on your subscription and region.
*/
type SAPAIModelId = "amazon--nova-premier" | "amazon--nova-pro" | "amazon--nova-lite" | "amazon--nova-micro" | "gpt-4" | "gpt-4o" | "gpt-4o-mini" | "gpt-4.1" | "gpt-4.1-mini" | "gpt-4.1-nano" | "o1" | "o1-mini" | "o3" | "o3-mini" | "o4-mini" | "gemini-1.5-pro" | "gemini-1.5-flash" | "gemini-2.0-pro" | "gemini-2.0-flash" | "gemini-2.0-flash-thinking" | "gemini-2.0-flash-lite" | "gemini-2.5-pro" | "gemini-2.5-flash" | "anthropic--claude-3-haiku" | "anthropic--claude-3-sonnet" | "anthropic--claude-3-opus" | "anthropic--claude-3.5-sonnet" | "anthropic--claude-3.7-sonnet" | "anthropic--claude-4-sonnet" | "anthropic--claude-4-opus" | "amazon--titan-text-lite" | "amazon--titan-text-express" | "alephalpha-pharia-1-7b-control" | "meta--llama3-70b-instruct" | "meta--llama3.1-70b-instruct" | "mistralai--mistral-large-instruct" | "mistralai--mistral-small-instruct" | "mistralai--pixtral-large-instruct" | "ibm--granite-13b-chat" | (string & {});
type SAPAIConfig = {
provider: string;
baseURL: string;
headers: () => Record<string, string | undefined>;
fetch?: FetchFunction;
};
declare class SAPAIChatLanguageModel implements LanguageModelV2 {
readonly specificationVersion = "v2";
readonly defaultObjectGenerationMode = "json";
readonly supportsImageUrls = true;
readonly modelId: SAPAIModelId;
readonly supportsStructuredOutputs = true;
private readonly config;
private readonly settings;
constructor(modelId: SAPAIModelId, settings: SAPAISettings, config: SAPAIConfig);
supportsUrl(url: URL): boolean;
get supportedUrls(): Record<string, RegExp[]>;
get provider(): string;
private getArgs;
doGenerate(options: LanguageModelV2CallOptions): Promise<{
content: LanguageModelV2Content[];
finishReason: LanguageModelV2FinishReason;
usage: LanguageModelV2Usage;
rawCall: {
rawPrompt: unknown;
rawSettings: Record<string, unknown>;
};
warnings: LanguageModelV2CallWarning[];
}>;
doStream(options: LanguageModelV2CallOptions): Promise<{
stream: ReadableStream<LanguageModelV2StreamPart>;
rawCall: {
rawPrompt: unknown;
rawSettings: Record<string, unknown>;
};
}>;
}
interface SAPAIServiceKey {
serviceurls: {
AI_API_URL: string;
};
clientid: string;
clientsecret: string;
url: string;
identityzone?: string;
identityzoneid?: string;
appname?: string;
"credential-type"?: string;
}
interface SAPAIProvider extends ProviderV2 {
(modelId: SAPAIModelId, settings?: SAPAISettings): SAPAIChatLanguageModel;
chat(modelId: SAPAIModelId, settings?: SAPAISettings): SAPAIChatLanguageModel;
}
interface SAPAIProviderSettings {
/**
* SAP AI Core service key (JSON string or parsed object).
* This is what you get from SAP BTP - just copy/paste it here.
* If provided, OAuth2 will be handled automatically.
*/
serviceKey?: string | SAPAIServiceKey;
/**
* Alternative: provide token directly (for advanced users)
*/
token?: string;
/**
* SAP AI Core deployment ID.
* @default 'd65d81e7c077e583'
*/
deploymentId?: string;
/**
* SAP AI Core resource group.
* @default 'default'
*/
resourceGroup?: string;
/**
* Custom base URL (optional)
*/
baseURL?: string;
/**
* Custom headers (optional)
*/
headers?: Record<string, string>;
/**
* Custom fetch implementation (optional)
*/
fetch?: typeof fetch;
}
/**
* Main SAP AI provider factory function.
*
* @example
* // With service key (recommended)
* const provider = await createSAPAIProvider({
* serviceKey: '{"serviceurls":{"AI_API_URL":"..."},"clientid":"...","clientsecret":"...","url":"..."}'
* });
*
* // With token (advanced)
* const provider = createSAPAIProvider({
* token: 'your-oauth-token'
* });
*
* // Use with Vercel AI SDK
* const model = provider('gpt-4o');
*/
declare function createSAPAIProvider(options?: SAPAIProviderSettings): Promise<SAPAIProvider>;
declare const sapai: SAPAIProvider;
/**
* Schema for SAP AI Core error responses.
* This matches the error format returned by the SAP AI Core API.
*/
declare const sapAIErrorSchema: z.ZodOptional<z.ZodObject<{
/** Unique identifier for tracking the request */
request_id: z.ZodOptional<z.ZodString>;
/** HTTP status code or custom error code */
code: z.ZodOptional<z.ZodNumber>;
/** Human-readable error message */
message: z.ZodOptional<z.ZodString>;
/** Where the error occurred (e.g., endpoint, function) */
location: z.ZodOptional<z.ZodString>;
/** Detailed error information */
error: z.ZodOptional<z.ZodObject<{
/** Specific error message */
message: z.ZodOptional<z.ZodString>;
/** Error type code */
code: z.ZodOptional<z.ZodString>;
/** Parameter that caused the error */
param: z.ZodOptional<z.ZodString>;
/** Error category (e.g., 'validation', 'auth') */
type: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
code?: string | undefined;
message?: string | undefined;
param?: string | undefined;
type?: string | undefined;
}, {
code?: string | undefined;
message?: string | undefined;
param?: string | undefined;
type?: string | undefined;
}>>;
/** Additional error context or debugging information */
details: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
request_id?: string | undefined;
code?: number | undefined;
message?: string | undefined;
location?: string | undefined;
error?: {
code?: string | undefined;
message?: string | undefined;
param?: string | undefined;
type?: string | undefined;
} | undefined;
details?: string | undefined;
}, {
request_id?: string | undefined;
code?: number | undefined;
message?: string | undefined;
location?: string | undefined;
error?: {
code?: string | undefined;
message?: string | undefined;
param?: string | undefined;
type?: string | undefined;
} | undefined;
details?: string | undefined;
}>>;
type SAPAIErrorData = z.infer<typeof sapAIErrorSchema>;
/**
* Custom error class for SAP AI Core errors.
* Provides structured access to error details returned by the API.
*
* @example
* ```typescript
* try {
* await model.generate();
* } catch (error) {
* if (error instanceof SAPAIError) {
* console.error('Error Code:', error.code);
* console.error('Request ID:', error.requestId);
* console.error('Location:', error.location);
* }
* }
* ```
*/
declare class SAPAIError extends Error {
/** Raw error data from the API response */
readonly data?: SAPAIErrorData;
/** Original HTTP response object */
readonly response?: Response | undefined;
/** HTTP status code or custom error code */
readonly code?: number;
/** Where the error occurred (e.g., endpoint, function) */
readonly location?: string;
/** Unique identifier for tracking the request */
readonly requestId?: string;
/** Additional error context or debugging information */
readonly details?: string;
constructor(message: string,
/** Raw error data from the API response */
data?: SAPAIErrorData,
/** Original HTTP response object */
response?: Response | undefined);
}
export { SAPAIError, type SAPAIProvider, type SAPAIProviderSettings, type SAPAIServiceKey, type SAPAISettings, createSAPAIProvider, sapai };