Model.ts overview
The Model
module provides a unified interface for AI service providers.
This module enables creation of provider-specific AI models that can be used interchangeably within the Effect AI ecosystem. It combines Layer functionality with provider identification, allowing for seamless switching between different AI service providers while maintaining type safety.
Example
import { Model, LanguageModel } from "@effect/ai"
import { Effect, Layer } from "effect"
declare const myAnthropicLayer: Layer.Layer<LanguageModel.LanguageModel>
const anthropicModel = Model.make("anthropic", myAnthropicLayer)
const program = Effect.gen(function* () {
const response = yield* LanguageModel.generateText({
prompt: "Hello, world!"
})
return response.text
}).pipe(Effect.provide(anthropicModel))
Since v1.0.0
Exports Grouped by Category
Constructors
make
Creates a Model from a provider name and a Layer that constructs AI services.
Example
import { Model, LanguageModel } from "@effect/ai"
import { Effect, Layer } from "effect"
declare const bedrockLayer: Layer.Layer<LanguageModel.LanguageModel>
// Model automatically provides ProviderName service
const checkProviderAndGenerate = Effect.gen(function* () {
const provider = yield* Model.ProviderName
console.log(`Generating with: ${provider}`)
return yield* LanguageModel.generateText({
prompt: `Hello from ${provider}!`
})
})
const program = checkProviderAndGenerate.pipe(Effect.provide(Model.make("amazon-bedrock", bedrockLayer)))
// Will log: "Generating with: amazon-bedrock"
Signature
declare const make: <const Provider extends string, Provides, Requires>(
provider: Provider,
layer: Layer.Layer<Provides, never, Requires>
) => Model<Provider, Provides, Requires>
Since v1.0.0
Context
ProviderName (class)
Service tag that provides the current large language model provider name.
This tag is automatically provided by Model instances and can be used to access the name of the provider that is currently in use within a given Effect program.
Signature
declare class ProviderName
Since v1.0.0
Models
Model (interface)
A Model represents a provider-specific AI service.
A Model can be used directly as a Layer to provide a particular model implementation to an Effect program.
A Model can also be used as an Effect to “lift” dependencies of the Model constructor into the parent Effect. This is particularly useful when you want to use a Model from within an Effect service.
Signature
export interface Model<in out Provider, in out Provides, in out Requires>
extends Layer.Layer<Provides | ProviderName, never, Requires>,
Effect.Effect<Layer.Layer<Provides | ProviderName>, never, Requires> {
readonly [TypeId]: TypeId
/**
* The provider identifier (e.g., "openai", "anthropic", "amazon-bedrock").
*/
readonly provider: Provider
}
Since v1.0.0
Type Ids
TypeId
Unique identifier for Model instances.
Signature
declare const TypeId: "~@effect/ai/Model"
Since v1.0.0
TypeId (type alias)
Type-level representation of the Model identifier.
Signature
type TypeId = typeof TypeId
Since v1.0.0