Skip to main content Link Search Menu Expand Document (external link)

AiLanguageModel.ts overview

Since v1.0.0


Exports Grouped by Category


Constructors

make

Signature

declare const make: <Config>(opts: {
  readonly generateText: (options: AiLanguageModelOptions) => Effect.Effect<AiResponse.AiResponse, AiError, Config>
  readonly streamText: (options: AiLanguageModelOptions) => Stream.Stream<AiResponse.AiResponse, AiError, Config>
}) => Effect.Effect<AiLanguageModel.Service<Config>>

Source

Since v1.0.0

Context

AiLanguageModel (class)

Signature

declare class AiLanguageModel

Source

Since v1.0.0

Functions

generateObject

Generate a structured object for the specified prompt and schema using a large language model.

When using a Schema that does not have an identifier or _tag property, you must specify a toolCallId to properly associate the output of the model.

Signature

declare const generateObject: <A, I extends Record<string, unknown>, R>(
  options: GenerateObjectOptions<A, I, R>
) => Effect.Effect<AiResponse.WithStructuredOutput<A>, AiError, AiLanguageModel | R>

Source

Since v1.0.0

generateText

Generate text using a large language model for the specified prompt.

If a toolkit is specified, the large language model will additionally be able to perform tool calls to augment its response.

Signature

declare const generateText: <
  Tools extends AiTool.Any,
  Options extends NoExcessProperties<GenerateTextOptions<any>, Options>
>(
  options: Options & GenerateTextOptions<Tools>
) => Effect.Effect<ExtractSuccess<Options>, ExtractError<Options>, AiLanguageModel | ExtractContext<Options>>

Source

Since v1.0.0

streamText

Generate text using a large language model for the specified prompt, streaming output from the model as soon as it is available.

If a toolkit is specified, the large language model will additionally be able to perform tool calls to augment its response.

Signature

declare const streamText: <
  Tools extends AiTool.Any,
  Options extends NoExcessProperties<GenerateTextOptions<any>, Options>
>(
  options: Options & GenerateTextOptions<Tools>
) => Stream.Stream<ExtractSuccess<Options>, ExtractError<Options>, AiLanguageModel | ExtractContext<Options>>

Source

Since v1.0.0

Models

AiLanguageModel (namespace)

Source

Since v1.0.0

Service (interface)

Signature

export interface Service<Config> {
  /**
   * Generate text using a large language model for the specified `prompt`.
   *
   * If a `toolkit` is specified, the large language model will additionally
   * be able to perform tool calls to augment its response.
   */
  readonly generateText: <
    Tools extends AiTool.Any,
    Options extends NoExcessProperties<GenerateTextOptions<any>, Options>
  >(
    options: Options & GenerateTextOptions<Tools>
  ) => Effect.Effect<ExtractSuccess<Options>, ExtractError<Options>, ExtractContext<Options> | Config>
  /**
   * Generate text using a large language model for the specified `prompt`,
   * streaming output from the model as soon as it is available.
   *
   * If a `toolkit` is specified, the large language model will additionally
   * be able to perform tool calls to augment its response.
   */
  readonly streamText: <
    Tools extends AiTool.Any,
    Options extends NoExcessProperties<GenerateTextOptions<any>, Options>
  >(
    options: Options & GenerateTextOptions<Tools>
  ) => Stream.Stream<ExtractSuccess<Options>, ExtractError<Options>, ExtractContext<Options> | Config>

  /**
   * Generate a structured object for the specified prompt and schema using a
   * large language model.
   */
  readonly generateObject: <A, I extends Record<string, unknown>, R>(
    options: GenerateObjectOptions<A, I, R>
  ) => Effect.Effect<AiResponse.WithStructuredOutput<A>, AiError, R | Config>
}

Source

Since v1.0.0

AiLanguageModelOptions (interface)

Signature

export interface AiLanguageModelOptions {
  /**
   * The prompt messages to use to generate text.
   */
  readonly prompt: AiInput.AiInput
  /**
   * An optional system message that will be part of the prompt.
   */
  readonly system: Option.Option<string>
  /**
   * The tools to use to generate text in an encoded format suitable for
   * incorporation into requests to the large language model.
   */
  readonly tools: Array<{
    readonly name: string
    readonly description: string
    readonly parameters: JsonSchema.JsonSchema7
    readonly structured: boolean
  }>
  /**
   * The tool choice mode for the language model.
   *
   * - `auto` (default): The model can decide whether or not to call tools, as well as which tools to call.
   * - `required`: The model **must** call a tool but can decide which tool will be called.
   * - `none`: The model **must not** call a tool.
   * - `{ tool: <tool_name> }`: The model must call the specified tool.
   */
  readonly toolChoice: ToolChoice<any>
  /**
   * The span to use to trace interactions with the large language model.
   */
  readonly span: Span
}

Source

Since v1.0.0

GenerateObjectOptions (interface)

Options for generating a structured object using a large language model.

Signature

export interface GenerateObjectOptions<A, I extends Record<string, unknown>, R> {
  /**
   * The prompt input to use to generate text.
   */
  readonly prompt: AiInput.Raw

  /**
   * An optional system message that will be part of the prompt.
   */
  readonly system?: string | undefined

  /**
   * The schema to be used to specify the structure of the object to generate.
   */
  readonly schema: Schema.Schema<A, I, R>

  /**
   * The identifier to use to associating the underlying tool call with the
   * generated output.
   */
  readonly toolCallId?: string | undefined
}

Source

Since v1.0.0

GenerateTextOptions (interface)

Options for generating text using a large language model.

Signature

export interface GenerateTextOptions<Tools extends AiTool.Any> {
  /**
   * The prompt input to use to generate text.
   */
  readonly prompt: AiInput.Raw

  /**
   * An optional system message that will be part of the prompt.
   */
  readonly system?: string | undefined

  /**
   * A toolkit containing both the tools and the tool call handler to use to
   * augment text generation.
   */
  readonly toolkit?: AiToolkit.ToHandler<Tools> | Effect.Effect<AiToolkit.ToHandler<Tools>, any, any>

  /**
   * The tool choice mode for the language model.
   *
   * - `auto` (default): The model can decide whether or not to call tools, as well as which tools to call.
   * - `required`: The model **must** call a tool but can decide which tool will be called.
   * - `none`: The model **must not** call a tool.
   * - `{ tool: <tool_name> }`: The model must call the specified tool.
   */
  readonly toolChoice?: ToolChoice<Tools>

  /**
   * The concurrency level for resolving tool calls.
   */
  readonly concurrency?: Concurrency | undefined
}

Source

Since v1.0.0

IdentifiedSchema (interface)

Signature

export interface IdentifiedSchema<A, I, R> extends Schema.Schema<A, I, R> {
  readonly identifier: string
}

Source

Since v1.0.0

StructuredSchema (type alias)

Signature

type StructuredSchema<A, I, R> = TaggedSchema<A, I, R> | IdentifiedSchema<A, I, R>

Source

Since v1.0.0

TaggedSchema (interface)

Signature

export interface TaggedSchema<A, I, R> extends Schema.Schema<A, I, R> {
  readonly _tag: string
}

Source

Since v1.0.0

ToolChoice (type alias)

The tool choice mode for the language model.

  • auto (default): The model can decide whether or not to call tools, as well as which tools to call.
  • required: The model must call a tool but can decide which tool will be called.
  • none: The model must not call a tool.
  • { tool: <tool_name> }: The model must call the specified tool.

Signature

type ToolChoice<Tool> =
  | "auto"
  | "none"
  | "required"
  | {
      readonly tool: Tool["name"]
    }

Source

Since v1.0.0

Utility Types

ExtractContext (type alias)

A utility type to extract the context type for the text generation methods of AiLanguageModel from the provided options.

Signature

type ExtractContext<Options> = Options extends {
  toolkit: AiToolkit.ToHandler<infer _Tools>
}
  ? AiTool.Context<_Tools>
  : Options extends {
        toolkit: Effect.Effect<AiToolkit.ToHandler<infer _Tools>, infer _E, infer _R>
      }
    ? AiTool.Context<_Tools> | _R
    : never

Source

Since v1.0.0

ExtractError (type alias)

A utility type to extract the error type for the text generation methods of AiLanguageModel from the provided options.

Signature

type ExtractError<Options> = Options extends {
  toolkit: AiToolkit.ToHandler<infer _Tools>
}
  ? AiError | AiTool.Failure<_Tools>
  : Options extends {
        toolkit: Effect.Effect<AiToolkit.ToHandler<infer _Tools>, infer _E, infer _R>
      }
    ? AiError | AiTool.Failure<_Tools> | _E
    : AiError

Source

Since v1.0.0

ExtractSuccess (type alias)

A utility type to extract the success type for the text generation methods of AiLanguageModel from the provided options.

Signature

type ExtractSuccess<Options> = Options extends {
  toolkit: AiToolkit.ToHandler<infer _Tools>
}
  ? AiResponse.WithToolCallResults<_Tools>
  : Options extends {
        toolkit: Effect.Effect<AiToolkit.ToHandler<infer _Tools>, infer _E, infer _R>
      }
    ? AiResponse.WithToolCallResults<_Tools>
    : AiResponse.AiResponse

Source

Since v1.0.0