UNPKG

@varia-bly/variably-sdk

Version:

Official JavaScript/TypeScript SDK for Variably feature flags, experimentation, LLM experiments with React hooks, and real-time dynamic configurations

186 lines 4.9 kB
/** * React Hooks for Variably LLM Experiments * Easy-to-use React hooks for integrating LLM experiments into React applications */ import { VariablyLLMClient } from './llm-client'; import { PromptExecutionRequest, PromptExecutionResponse, LLMUserContext, LLMError, StreamChunk, EvaluationCriteria } from './llm-types'; /** * Hook for executing LLM prompts with automatic variant selection * * @example * ```tsx * const { execute, loading, response, error } = useLLMPrompt({ * client: llmClient, * experimentId: 'exp_123', * }); * * const handleSubmit = async () => { * const result = await execute({ * userContext: { userId: 'user_123' }, * variables: { query: userInput } * }); * }; * ``` */ export declare function useLLMPrompt(config: { client: VariablyLLMClient; experimentId: string; defaultUserContext?: LLMUserContext; includeEvaluation?: boolean; evaluationCriteria?: EvaluationCriteria; trackMetrics?: boolean; onSuccess?: (response: PromptExecutionResponse) => void; onError?: (error: LLMError) => void; }): { execute: (request: Omit<PromptExecutionRequest, "experimentId">) => Promise<PromptExecutionResponse>; cancel: () => void; loading: boolean; response: PromptExecutionResponse | null; error: LLMError | null; isSuccess: boolean; isError: boolean; }; /** * Hook for streaming LLM responses * * @example * ```tsx * const { stream, streaming, content, chunks, error } = useLLMStreaming({ * client: llmClient, * experimentId: 'exp_123', * }); * * const handleStream = async () => { * await stream({ * userContext: { userId: 'user_123' }, * variables: { query: userInput } * }); * }; * ``` */ export declare function useLLMStreaming(config: { client: VariablyLLMClient; experimentId: string; defaultUserContext?: LLMUserContext; onChunk?: (chunk: StreamChunk) => void; onComplete?: () => void; onError?: (error: LLMError) => void; }): { stream: (request: Omit<PromptExecutionRequest, "experimentId">) => Promise<void>; cancel: () => void; reset: () => void; streaming: boolean; content: string; chunks: StreamChunk[]; error: LLMError | null; isComplete: boolean; }; /** * Hook for managing conversation context across multiple LLM calls * * @example * ```tsx * const { * messages, * addMessage, * sendMessage, * loading, * clearHistory * } = useConversation({ * client: llmClient, * experimentId: 'exp_123', * userId: 'user_123', * }); * * const handleSend = async () => { * await sendMessage({ content: userInput, role: 'user' }); * }; * ``` */ export declare function useConversation(config: { client: VariablyLLMClient; experimentId: string; userId: string; maxMessages?: number; sessionId?: string; }): { messages: { role: "user" | "assistant" | "system"; content: string; }[]; addMessage: (message: { role: "user" | "assistant" | "system"; content: string; }) => void; sendMessage: (message: { content: string; role?: "user" | "assistant" | "system"; variables?: Record<string, any>; }) => Promise<PromptExecutionResponse>; clearHistory: () => void; loading: boolean; error: LLMError | null; sessionId: string; }; /** * Hook for evaluating LLM responses * * @example * ```tsx * const { evaluate, loading, result, error } = useLLMEvaluation({ * client: llmClient, * criteria: { * dimensions: [ * { name: 'accuracy', weight: 0.5, method: 'automated' }, * { name: 'clarity', weight: 0.3, method: 'automated' }, * { name: 'relevance', weight: 0.2, method: 'automated' } * ] * } * }); * * const handleEvaluate = async () => { * const evalResult = await evaluate({ * response: llmResponse, * prompt: originalPrompt * }); * }; * ``` */ export declare function useLLMEvaluation(config: { client: VariablyLLMClient; criteria: EvaluationCriteria; }): { evaluate: (request: { response: string; prompt?: string; expectedOutput?: string; context?: Record<string, any>; }) => Promise<import("./llm-types").ResponseEvaluationResponse>; loading: boolean; result: any; error: LLMError | null; }; /** * Hook for managing LLM client instance * * @example * ```tsx * const client = useLLMClient({ * apiKey: process.env.VARIABLY_API_KEY, * baseUrl: 'https://api.variably.tech', * llmConfig: { * provider: 'openai', * model: 'gpt-4', * temperature: 0.7 * } * }); * ``` */ export declare function useLLMClient(config: { apiKey: string; baseUrl?: string; llmConfig?: any; cacheConfig?: any; enableMetrics?: boolean; }): VariablyLLMClient; //# sourceMappingURL=react-hooks.d.ts.map