ai-sdk-ollama
Version:
Vercel AI SDK Provider for Ollama using official ollama-js library
82 lines • 3.29 kB
TypeScript
/**
* Object Generation Reliability Utilities for Ollama
*
* This module provides utilities to make Ollama object generation more reliable
* and deterministic. It addresses common issues like:
* - Schema validation failures
* - Inconsistent results across multiple attempts
* - Timeout and fetch errors
* - Malformed JSON responses
* - Type mismatches (strings vs numbers)
*/
import type { JSONSchema7 } from '@ai-sdk/provider';
import { type RepairTextFunction } from './json-text-repair.js';
export interface ObjectGenerationOptions {
/**
* Maximum number of retry attempts for object generation
*/
maxRetries?: number;
/**
* Whether to attempt schema recovery when validation fails
*/
attemptRecovery?: boolean;
/**
* Whether to use fallback values for failed generations
*/
useFallbacks?: boolean;
/**
* Custom fallback values for specific fields
*/
fallbackValues?: Record<string, unknown>;
/**
* Timeout for object generation in milliseconds
*/
generationTimeout?: number;
/**
* Whether to validate and fix type mismatches
*/
fixTypeMismatches?: boolean;
/**
* Custom repair function for malformed JSON or validation errors.
* If provided, replaces the default cascade (jsonrepair then enhancedRepairText).
*/
repairText?: RepairTextFunction;
/**
* Whether to enable automatic JSON repair for malformed LLM outputs.
* Default: true. Uses jsonrepair then enhancedRepairText for edge cases.
* Set to false to disable.
*/
enableTextRepair?: boolean;
}
export interface ReliableObjectGenerationResult<T> {
object: T;
success: boolean;
retryCount: number;
errors?: string[];
recoveryMethod?: 'retry' | 'fallback' | 'type_fix' | 'text_repair' | 'natural';
}
declare const DEFAULT_OBJECT_GENERATION_OPTIONS: Required<Pick<ObjectGenerationOptions, 'maxRetries' | 'attemptRecovery' | 'useFallbacks' | 'fixTypeMismatches' | 'enableTextRepair'>>;
export declare function resolveObjectGenerationOptions(options?: ObjectGenerationOptions): ObjectGenerationOptions & typeof DEFAULT_OBJECT_GENERATION_OPTIONS;
/**
* Validate and attempt to recover from schema validation failures
*/
export declare function attemptSchemaRecovery(rawObject: unknown, schema: JSONSchema7 | unknown, options?: ObjectGenerationOptions): Promise<{
success: boolean;
object?: unknown;
error?: string;
repaired?: boolean;
}>;
/**
* Create a reliable object generation wrapper
*/
export declare function createReliableObjectGeneration<T>(generateObjectFunction: (options: Record<string, unknown>) => Promise<{
object: T;
}>, schema: JSONSchema7 | unknown, options?: ObjectGenerationOptions): (generationOptions: Record<string, unknown>) => Promise<ReliableObjectGenerationResult<T>>;
/**
* Enhanced object generation with reliability features
*/
export declare function reliableGenerateObject<T>(generateObjectFunction: (options: Record<string, unknown>) => Promise<{
object: T;
}>, options: Record<string, unknown>, schema: JSONSchema7 | unknown, reliabilityOptions?: ObjectGenerationOptions): Promise<ReliableObjectGenerationResult<T>>;
export {};
//# sourceMappingURL=object-generation-reliability.d.ts.map