Class: DSPy::LM

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dspy/lm.rb,
lib/dspy/lm/usage.rb,
lib/dspy/lm/errors.rb,
lib/dspy/lm/adapter.rb,
lib/dspy/lm/response.rb,
lib/dspy/lm/cache_manager.rb,
lib/dspy/lm/retry_handler.rb,
lib/dspy/lm/adapter_factory.rb,
lib/dspy/lm/message_builder.rb,
lib/dspy/lm/strategy_selector.rb,
lib/dspy/lm/adapters/openai_adapter.rb,
lib/dspy/lm/strategies/base_strategy.rb,
lib/dspy/lm/adapters/anthropic_adapter.rb,
lib/dspy/lm/structured_output_strategy.rb,
lib/dspy/lm/adapters/openai/schema_converter.rb,
lib/dspy/lm/strategies/enhanced_prompting_strategy.rb,
lib/dspy/lm/strategies/anthropic_extraction_strategy.rb,
lib/dspy/lm/strategies/openai_structured_output_strategy.rb

Defined Under Namespace

Modules: Adapters, Strategies, UsageFactory Classes: Adapter, AdapterError, AdapterFactory, AnthropicAdapter, CacheManager, ConfigurationError, Error, MessageBuilder, MissingAPIKeyError, OpenAIAdapter, OpenAIUsage, Response, RetryHandler, StrategySelector, StructuredOutputStrategy, UnsupportedProviderError, Usage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_id, api_key: nil, **options) ⇒ LM

Returns a new instance of LM.



28
29
30
31
32
33
34
35
36
37
# File 'lib/dspy/lm.rb', line 28

def initialize(model_id, api_key: nil, **options)
  @model_id = model_id
  @api_key = api_key
  
  # Parse provider and model from model_id
  @provider, @model = parse_model_id(model_id)
  
  # Create appropriate adapter with options
  @adapter = AdapterFactory.create(model_id, api_key: api_key, **options)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



26
27
28
# File 'lib/dspy/lm.rb', line 26

def adapter
  @adapter
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



26
27
28
# File 'lib/dspy/lm.rb', line 26

def api_key
  @api_key
end

#modelObject (readonly)

Returns the value of attribute model.



26
27
28
# File 'lib/dspy/lm.rb', line 26

def model
  @model
end

#model_idObject (readonly)

Returns the value of attribute model_id.



26
27
28
# File 'lib/dspy/lm.rb', line 26

def model_id
  @model_id
end

#providerObject (readonly)

Returns the value of attribute provider.



26
27
28
# File 'lib/dspy/lm.rb', line 26

def provider
  @provider
end

Class Method Details

.cache_managerObject



146
147
148
# File 'lib/dspy/lm/cache_manager.rb', line 146

def cache_manager
  @cache_manager ||= CacheManager.new
end

Instance Method Details

#chat(inference_module, input_values, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dspy/lm.rb', line 39

def chat(inference_module, input_values, &block)
  signature_class = inference_module.signature_class
  
  # Build messages from inference module
  messages = build_messages(inference_module, input_values)
  
  # Execute with instrumentation
  response = instrument_lm_request(messages, signature_class.name) do
    chat_with_strategy(messages, signature_class, &block)
  end
  
  # Instrument response parsing
  if should_emit_lm_events?
    parsed_result = Instrumentation.instrument('dspy.lm.response.parsed', {
      signature_class: signature_class.name,
      provider: provider,
      response_length: response.content&.length || 0
    }) do
      parse_response(response, input_values, signature_class)
    end
  else
    parsed_result = parse_response(response, input_values, signature_class)
  end
  
  parsed_result
end

#raw_chat(messages = nil, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/dspy/lm.rb', line 66

def raw_chat(messages = nil, &block)
  # Support both array format and builder DSL
  if block_given? && messages.nil?
    # DSL mode - block is for building messages
    builder = MessageBuilder.new
    yield builder
    messages = builder.messages
    streaming_block = nil
  else
    # Array mode - block is for streaming
    messages ||= []
    streaming_block = block
  end
  
  # Validate messages format
  validate_messages!(messages)
  
  # Execute with instrumentation
  execute_raw_chat(messages, &streaming_block)
end