Providers

Providers in AINU are responsible for connecting agents to AI models. They act as the bridge between the framework and external AI services, such as Anthropic, OpenAI, or other supported providers. The Provider class in AINU provides a base implementation that can be extended to integrate specific AI services.


Base Provider Class

The Provider class is an abstract base class that defines the core functionality for all providers. It is not meant to be used directly. Instead, each specific provider (e.g., Anthropic, OpenAI) extends this class and implements its methods.

Key Responsibilities:

  • Language Models: Provides access to language models via the languageModel method.

  • Image Models: Provides access to image models via the imageModel method.

  • Default Model IDs: Defines default model IDs for language and image models.

Key Methods:

Method
Description

languageModel(model?)

Returns a LanguageModelV1 instance for the specified or default model ID.

imageModel(model?)

Returns an ImageModelV1 instance for the specified or default model ID.

defaultLanguageModelId()

Returns the default language model ID for the provider.

defaultImageModelId()

Returns the default image model ID for the provider.


Implementing a Provider

To implement a provider, you extend the Provider class and instantiate the appropriate provider from the @ai-sdk package. The Anthropic provider is a good example of this.

Example: Anthropic Provider

The Anthropic class extends the Provider base class and integrates the Anthropic AI service:

import { AnthropicProvider, createAnthropic } from "@ai-sdk/anthropic";
import { Provider } from "./base";

export class Anthropic extends Provider<AnthropicProvider, AnthropicProviderSettings, AnthropicMessagesModelId> {
  constructor(options: AnthropicProviderSettings) {
    super({ ...options });
    this.provider = createAnthropic(this.options); // Instantiates the Anthropic provider
  }

  defaultLanguageModelId(): AnthropicMessagesModelId {
    return "claude-3-5-haiku-latest"; // Specifies the default model ID
  }
}

Key Points:

  1. Instantiation: The Anthropic provider uses the createAnthropic method from @ai-sdk/anthropic to instantiate the provider.

  2. Default Model ID: The defaultLanguageModelId method specifies the default model ID for the provider.

  3. Base Class Methods: The languageModel and imageModel methods from the base class are inherited and can be used directly.


Using a Provider

To use a provider, you simply create an instance of the provider class and pass it to the agent:

import { Agent, Anthropic } from "@ainulabs/ainu";

const provider = new Anthropic({
  apiKey: "your-api-key", // Replace with your actual API key
});

const agent = new Agent({
  provider,
});

This connects the agent to the Anthropic AI service, allowing it to generate text or perform other tasks.


Summary

Providers are essential for connecting agents to external AI services. The Provider base class defines the core functionality, while specific providers like Anthropic extend it to integrate with their respective services. By using providers, you can easily switch between AI services or add support for new ones with minimal effort.

Last updated