Class: DSPy::LM::AdapterFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/dspy/lm/adapter_factory.rb

Overview

Factory for creating appropriate adapters based on model_id

Constant Summary collapse

ADAPTER_MAP =

Maps provider prefixes to adapter classes

{
  'openai' => 'OpenAIAdapter',
  'anthropic' => 'AnthropicAdapter',
  'ollama' => 'OllamaAdapter',
  'gemini' => 'GeminiAdapter'
}.freeze

Class Method Summary collapse

Class Method Details

.create(model_id, api_key:, **options) ⇒ DSPy::LM::Adapter

Creates an adapter instance based on model_id

Parameters:

  • model_id (String)

    Full model identifier (e.g., “openai/gpt-4”)

  • api_key (String)

    API key for the provider

  • options (Hash)

    Additional adapter-specific options

Returns:



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/dspy/lm/adapter_factory.rb', line 21

def create(model_id, api_key:, **options)
  provider, model = parse_model_id(model_id)
  adapter_class = get_adapter_class(provider)
  
  # Pass provider-specific options
  adapter_options = { model: model, api_key: api_key }
  # Both OpenAI and Ollama accept additional options
  adapter_options.merge!(options) if %w[openai ollama].include?(provider)
  
  adapter_class.new(**adapter_options)
end