@cogniformai/instructor-stream
Version:
Streaming-first structured data extraction from LLMs with real-time updates
103 lines (100 loc) • 5.5 kB
TypeScript
import { S as SchemaSource, a as SchemaValidationMode, b as SnapshotChunk, c as StreamingPipelineError, C as CompletionMeta } from './snapshots-uXGVbqtJ.js';
export { A as ActivePath, B as BaseCompletionMeta, g as CompletedPaths, P as ProviderError, R as ResolvedSchema, e as SchemaResolutionError, f as SnapshotValidationError, d as StreamingError, h as StreamingValidationMode, T as TokenUsage, r as resolveSchema } from './snapshots-uXGVbqtJ.js';
export { S as SnapshotHydrator, a as SnapshotHydratorLayer } from './runtime-CCMvIy2V.js';
import { Stream, Effect } from 'effect';
import * as LanguageModel from '@effect/ai/LanguageModel';
import * as Prompt from '@effect/ai/Prompt';
import * as Tool from '@effect/ai/Tool';
export { FastAdapterOptions, LangGraphDelta, StreamLangGraphSpec, fastAdapter, iterableToReadableStream, streamLangGraph } from './adapters/langgraph/index.js';
import 'effect/Types';
import 'effect/Schema';
import 'zod';
/**
* Configuration object for streaming requests with schema validation.
*
* @template A - The type of the data structure that will be validated against the schema
* @template Tools - Record of available tools that can be used during generation, defaults to any tool record
*
* @property schema - The schema source used for validating the streamed response data
* @property prompt - Raw input prompt that will be processed by the language model
* @property validationMode - Optional mode specifying how schema validation should be performed
* @property options - Optional partial configuration for language model text generation, excluding the prompt property
*/
type StreamRequest<A, Tools extends Record<string, Tool.Any> = Record<string, Tool.Any>> = {
readonly schema: SchemaSource<A>;
readonly prompt: Prompt.RawInput;
readonly validationMode?: SchemaValidationMode;
readonly options?: Partial<Omit<LanguageModel.GenerateTextOptions<Tools>, 'prompt'>>;
};
/**
* Streams structured data extraction from a language model response.
*
* This function takes a stream request containing a prompt and schema, sends it to the language model,
* and returns a stream of hydrated objects that conform to the provided schema. The stream processes
* the language model's text output in real-time, validating and transforming it into structured data.
*
* @template A - The type of the structured data to extract, inferred from the schema
* @param request - The stream request configuration
* @param request.prompt - The prompt to send to the language model
* @param request.schema - The schema defining the structure of the expected output
* @param request.options - Optional additional options to pass to the language model
* @param request.validationMode - Optional validation mode for the hydrator
*
* @returns A Stream that emits hydrated objects conforming to the schema
*
* @example
* ```TypeScript
* const userStream = stream({
* prompt: "Extract user information",
* schema: z.object({ name: z.string(), age: z.number() })
* })
* ```
*/
declare const stream: <A>(request: StreamRequest<A>) => Stream.Stream<SnapshotChunk<A>, StreamingPipelineError, {
readonly stream: <A_1>(request: {
readonly schema: SchemaSource<A_1>;
readonly provider: {
readonly stream: ReadableStream<Uint8Array>;
readonly meta?: Partial<CompletionMeta> | (() => Partial<CompletionMeta>);
readonly channelType?: string;
};
readonly validationMode?: SchemaValidationMode;
}) => Stream.Stream<SnapshotChunk<A_1>, StreamingPipelineError, never>;
readonly stub: <A_1>(request: {
readonly schema: SchemaSource<A_1>;
readonly defaultData?: Partial<A_1>;
}) => Effect.Effect<Partial<A_1>, StreamingPipelineError, never>;
} | LanguageModel.LanguageModel>;
/**
* Creates a stub value based on the provided schema with optional default data.
*
* This function generates a stub instance by hydrating a schema through the SnapshotHydrator.
* The stub can be initialized with partial default data if provided.
*
* @template A - The type of the value to be created from the schema
* @param schema - The schema source defining the structure of the value
* @param defaultData - Optional partial data to initialize the stub with
* @returns An Effect that produces a stubbed value conforming to the schema
*
* @example
* ```TypeScript
* const userSchema = S.struct({ name: S.string, age: S.number });
* const userStub = stub(userSchema, { name: "John" });
* ```
*/
declare const stub: <A>(schema: SchemaSource<A>, defaultData?: Partial<A>) => Effect.Effect<Partial<A>, StreamingPipelineError, {
readonly stream: <A_1>(request: {
readonly schema: SchemaSource<A_1>;
readonly provider: {
readonly stream: ReadableStream<Uint8Array>;
readonly meta?: Partial<CompletionMeta> | (() => Partial<CompletionMeta>);
readonly channelType?: string;
};
readonly validationMode?: SchemaValidationMode;
}) => Stream.Stream<SnapshotChunk<A_1>, StreamingPipelineError, never>;
readonly stub: <A_1>(request: {
readonly schema: SchemaSource<A_1>;
readonly defaultData?: Partial<A_1>;
}) => Effect.Effect<Partial<A_1>, StreamingPipelineError, never>;
}>;
export { CompletionMeta, SchemaSource, SchemaValidationMode, SnapshotChunk, type StreamRequest, StreamingPipelineError, stream as instructorStream, stub as instructorStub, stream, stub };