/** * LLM-based guardrail content checking. * * This module enables the creation and registration of content moderation guardrails * using Large Language Models (LLMs). It provides configuration and output schemas, * prompt helpers, a utility for executing LLM-based checks, and a factory for generating * guardrail check functions leveraging LLMs. */ import type { OpenAI } from 'openai'; import { type ZodTypeAny, z } from 'zod'; import { type CheckFn, type GuardrailLLMContext, type GuardrailResult, type TokenUsage } from '../types'; import type { NormalizedConversationEntry } from '../utils/conversation'; /** * Default maximum number of conversation turns to include for multi-turn analysis. */ export declare const DEFAULT_MAX_TURNS = 10; /** * Configuration schema for LLM-based content checks. * * Used to specify the LLM model and confidence threshold for triggering a tripwire. */ export declare const LLMConfig: z.ZodObject<{ model: z.ZodString; confidence_threshold: z.ZodDefault; system_prompt_details: z.ZodOptional; include_reasoning: z.ZodDefault; max_turns: z.ZodDefault; }, z.core.$strip>; export type LLMConfig = z.infer; /** * Output schema for LLM content checks. * * Used for structured results returned by LLM-based moderation guardrails. */ export declare const LLMOutput: z.ZodObject<{ flagged: z.ZodBoolean; confidence: z.ZodNumber; }, z.core.$strip>; export type LLMOutput = z.infer; /** * Extended LLM output schema with reasoning. * * Extends LLMOutput to include a reason field explaining the decision. * Used when include_reasoning is enabled in the config. */ export declare const LLMReasoningOutput: z.ZodObject<{ flagged: z.ZodBoolean; confidence: z.ZodNumber; reason: z.ZodString; }, z.core.$strip>; export type LLMReasoningOutput = z.infer; /** * Extended LLM output schema with error information. * * Extends LLMOutput to include additional information about errors that occurred * during LLM processing, such as content filter triggers. */ export declare const LLMErrorOutput: z.ZodObject<{ flagged: z.ZodBoolean; confidence: z.ZodNumber; info: z.ZodRecord; }, z.core.$strip>; export type LLMErrorOutput = z.infer; /** * Create a standardized error result for LLM-based guardrails. * * This helper provides a consistent way to handle errors across all LLM-based checks, * ensuring uniform error reporting and preventing tripwire triggers on execution failures. * Sets executionFailed=true to enable raiseGuardrailErrors handling. * * @param guardrailName - Name of the guardrail that encountered the error. * @param analysis - LLMErrorOutput containing error information. * @param additionalInfo - Optional additional information to include in the result. * @returns GuardrailResult with tripwireTriggered=false, executionFailed=true, and error information. */ export declare function createErrorResult(guardrailName: string, analysis: LLMErrorOutput, additionalInfo?: Record, tokenUsage?: TokenUsage): GuardrailResult; export declare function buildFullPrompt(systemPrompt: string, outputModel?: ZodTypeAny): string; /** * Extract conversation history from context if available. * * Safely attempts to retrieve conversation history from context objects * that implement the GuardrailLLMContextWithHistory interface. * * @param ctx - Context object that may contain conversation history. * @returns Array of conversation entries, or empty array if unavailable. */ export declare function extractConversationHistory(ctx: GuardrailLLMContext): NormalizedConversationEntry[]; /** * Build analysis payload for multi-turn conversation analysis. * * Creates a JSON string containing the recent conversation history and the * latest input text for LLM analysis. * * @param conversationHistory - Array of conversation entries. * @param latestInput - The latest text input to analyze. * @param maxTurns - Maximum number of conversation turns to include. * @returns JSON string containing conversation and latest_input. */ export declare function buildAnalysisPayload(conversationHistory: NormalizedConversationEntry[], latestInput: string, maxTurns: number): string; /** * Run an LLM analysis for a given prompt and user input. * * Invokes the OpenAI LLM, enforces prompt/response contract, parses the LLM's * output, and returns a validated result. * * When conversation history is provided, the analysis includes recent conversation * context for multi-turn detection capabilities. * * @param text - Text to analyze. * @param systemPrompt - Prompt instructions for the LLM. * @param client - OpenAI client for LLM inference. * @param model - Identifier for which LLM model to use. * @param outputModel - Model for parsing and validating the LLM's response. * @param conversationHistory - Optional array of conversation entries for multi-turn analysis. * @param maxTurns - Maximum number of conversation turns to include. Defaults to DEFAULT_MAX_TURNS. * @returns Structured output containing the detection decision and confidence. */ export declare function runLLM(text: string, systemPrompt: string, client: OpenAI, model: string, outputModel: TOutput, conversationHistory?: NormalizedConversationEntry[] | null, maxTurns?: number): Promise<[z.infer | LLMErrorOutput, TokenUsage]>; export declare function createLLMCheckFn(name: string, description: string, systemPrompt: string, outputModel?: typeof LLMOutput, configModel?: typeof LLMConfig): CheckFn>; /** * Context requirements for LLM-based guardrails. */ export declare const LLMContext: z.ZodType; //# sourceMappingURL=llm-base.d.ts.map