@juspay/neurolink
Version:
The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002
217 lines (216 loc) • 7.59 kB
TypeScript
/**
* SageMaker Language Model Implementation
*
* This module implements the LanguageModel interface for Amazon SageMaker
* integration with the Vercel AI SDK.
*/
import type { ConnectivityResult, SageMakerAsLanguageModel, SageMakerConfig, SageMakerModelConfig } from "../../types/index.js";
/**
* SageMaker Language Model implementing LanguageModel interface
*
* Token Limit Behavior:
* - When maxTokens is undefined, SageMaker uses the model's default token limits
* - When maxTokens is specified, it sets max_new_tokens parameter explicitly
* - This aligns with the unlimited-by-default token policy across all providers
*/
export declare class SageMakerLanguageModel implements SageMakerAsLanguageModel {
/**
* Specification version for the AI SDK LanguageModel interface.
* Uses "v2" for structural compatibility with AI SDK v6's `LanguageModelV2`.
* The AI SDK checks this field to determine which interface version to use.
*/
readonly specificationVersion: "v2";
readonly provider = "sagemaker";
readonly modelId: string;
readonly supportsStreaming = true;
readonly defaultObjectGenerationMode: "json";
/**
* Supported URL patterns by media type.
* SageMaker endpoints do not natively download URLs, so this is empty.
* Required by the LanguageModelV2 interface.
*/
readonly supportedUrls: Record<string, RegExp[]>;
private client;
private config;
private modelConfig;
constructor(modelId: string, config: SageMakerConfig, modelConfig: SageMakerModelConfig);
/**
* Read the caller's abort signal out of the AI SDK's call options.
*
* `doGenerate`/`doStream` type `options` as `Record<string, unknown>`, so the
* value arrives as `unknown` even though `LanguageModelV2CallOptions`
* declares `abortSignal?: AbortSignal`. `instanceof` is a real runtime check
* rather than an assertion, which is what rule 14 asks for here.
*/
private readAbortSignal;
/**
* Generate text synchronously using SageMaker endpoint
*/
doGenerate(options: Record<string, unknown>): Promise<{
text?: string;
reasoning?: string | Array<{
type: "text";
text: string;
signature?: string;
} | {
type: "redacted";
data: string;
}>;
files?: Array<{
data: string | Uint8Array;
mimeType: string;
}>;
logprobs?: Array<{
token: string;
logprob: number;
topLogprobs: Array<{
token: string;
logprob: number;
}>;
}>;
usage: {
inputTokens: number;
outputTokens: number;
totalTokens?: number;
};
finishReason: "stop" | "length" | "content-filter" | "tool-calls" | "error" | "unknown";
warnings?: Array<{
type: "other";
message: string;
}>;
rawCall: {
rawPrompt: unknown;
rawSettings: Record<string, unknown>;
};
rawResponse?: {
headers?: Record<string, string>;
};
request?: {
body?: string;
};
}>;
/**
* Generate text with streaming using SageMaker endpoint
*/
doStream(options: Record<string, unknown>): Promise<{
stream: ReadableStream<Record<string, unknown>>;
rawCall: {
rawPrompt: unknown;
rawSettings: Record<string, unknown>;
};
rawResponse?: {
headers?: Record<string, string>;
};
request?: {
body?: string;
};
warnings?: Array<{
type: "other";
message: string;
}>;
}>;
/**
* Convert AI SDK options to SageMaker request format
*/
private convertToSageMakerRequest;
/**
* Convert AI SDK tools (`LanguageModelV3FunctionTool | LanguageModelV3ProviderTool`,
* the same union every other provider's tool converter accepts — see
* `v3ToolsToOpenAI` in openaiChatCompletionsClient.ts) into the SageMaker
* wire format, which mirrors the OpenAI Chat Completions nested
* `{ function: { name, description, parameters } }` convention.
*
* Function tools carry their fields flat (`tool.name`, `tool.inputSchema`,
* etc.) — there is no `tool.function` sub-object on the AI SDK side.
* `type: "provider"` tools (provider-defined tools like web search) have
* no SageMaker equivalent; they're dropped explicitly with a debug log
* rather than crashing or being sent malformed.
*/
private convertToolsToSageMakerFormat;
/**
* Convert an AI SDK tool choice (`LanguageModelV3ToolChoice`) into the
* SageMaker/OpenAI-style wire format. The AI SDK shape is
* `{ type: "auto" | "none" | "required" }` or `{ type: "tool", toolName }`
* — never the nested `{ type: "function", function: { name } }` shape.
*/
private convertToolChoiceToSageMakerFormat;
/**
* Convert Vercel AI SDK response format to SageMaker format (Phase 4)
*/
private convertResponseFormatToSageMakerFormat;
/**
* Extract text content from AI SDK prompt format
*/
private extractPromptText;
/**
* Extract generated text from SageMaker response
*/
private extractTextFromResponse;
/**
* Extract tool calls from SageMaker response (Phase 4)
*/
private extractToolCallsFromResponse;
/**
* Map SageMaker finish reason to standardized format
*/
private mapSageMakerFinishReason;
/**
* Get model configuration summary for debugging
*/
getModelInfo(): {
modelId: string;
provider: string;
specificationVersion: "v2";
endpointName: string;
modelType: "huggingface" | "mistral" | "custom" | "llama" | "claude" | "jumpstart" | undefined;
region: string;
};
/**
* Test basic connectivity to the SageMaker endpoint
*/
testConnectivity(): Promise<ConnectivityResult>;
/**
* Batch inference support (Phase 4)
* Process multiple prompts in a single request for efficiency
*/
doBatchGenerate(prompts: string[], options?: {
maxTokens?: number;
temperature?: number;
topP?: number;
}): Promise<Array<{
text: string;
usage: {
promptTokens: number;
completionTokens: number;
total: number;
};
finishReason: "stop" | "length" | "content-filter" | "tool-calls" | "error" | "unknown";
}>>;
/**
* Process prompts in parallel with advanced concurrency control and error handling
*/
private processPromptsInParallel;
/**
* Enhanced model information with batch capabilities
*/
getModelCapabilities(): {
capabilities: {
streaming: boolean;
toolCalling: boolean;
structuredOutput: boolean;
batchInference: boolean;
supportedResponseFormats: string[];
supportedToolTypes: string[];
maxBatchSize: number;
adaptiveConcurrency: boolean;
errorRecovery: boolean;
};
modelId: string;
provider: string;
specificationVersion: "v2";
endpointName: string;
modelType: "huggingface" | "mistral" | "custom" | "llama" | "claude" | "jumpstart" | undefined;
region: string;
};
}
export default SageMakerLanguageModel;