Class: DSPy::LM

Inherits:
Object
  • Object
show all
Defined in:
lib/dspy/lm.rb,
lib/dspy/lm/errors.rb,
lib/dspy/lm/adapter.rb,
lib/dspy/lm/response.rb,
lib/dspy/lm/adapter_factory.rb,
lib/dspy/lm/adapters/openai_adapter.rb,
lib/dspy/lm/adapters/anthropic_adapter.rb

Defined Under Namespace

Classes: Adapter, AdapterError, AdapterFactory, AnthropicAdapter, ConfigurationError, Error, OpenAIAdapter, Response, UnsupportedProviderError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_id, api_key: nil) ⇒ LM

Returns a new instance of LM.



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

def initialize(model_id, api_key: nil)
  @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
  @adapter = AdapterFactory.create(model_id, api_key: api_key)
end

Instance Attribute Details

#adapterObject (readonly)

Returns the value of attribute adapter.



19
20
21
# File 'lib/dspy/lm.rb', line 19

def adapter
  @adapter
end

#api_keyObject (readonly)

Returns the value of attribute api_key.



19
20
21
# File 'lib/dspy/lm.rb', line 19

def api_key
  @api_key
end

#modelObject (readonly)

Returns the value of attribute model.



19
20
21
# File 'lib/dspy/lm.rb', line 19

def model
  @model
end

#model_idObject (readonly)

Returns the value of attribute model_id.



19
20
21
# File 'lib/dspy/lm.rb', line 19

def model_id
  @model_id
end

#providerObject (readonly)

Returns the value of attribute provider.



19
20
21
# File 'lib/dspy/lm.rb', line 19

def provider
  @provider
end

Instance Method Details

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



32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/dspy/lm.rb', line 32

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)
  
  # Calculate input size for monitoring
  input_text = messages.map { |m| m[:content] }.join(' ')
  input_size = input_text.length
  
  # Use smart consolidation: emit LM events only when not in nested context
  response = nil
  token_usage = {}
  
  if should_emit_lm_events?
    # Emit all LM events when not in nested context
    response = Instrumentation.instrument('dspy.lm.request', {
      gen_ai_operation_name: 'chat',
      gen_ai_system: provider,
      gen_ai_request_model: model,
      signature_class: signature_class.name,
      provider: provider,
      adapter_class: adapter.class.name,
      input_size: input_size
    }) do
      adapter.chat(messages: messages, &block)
    end
    
    # Extract actual token usage from response (more accurate than estimation)
    token_usage = Instrumentation::TokenTracker.extract_token_usage(response, provider)
    
    # Emit token usage event if available
    if token_usage.any?
      Instrumentation.emit('dspy.lm.tokens', token_usage.merge({
        gen_ai_system: provider,
        gen_ai_request_model: model,
        signature_class: signature_class.name
      }))
    end
    
    # Instrument response parsing
    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
    # Consolidated mode: execute without nested instrumentation
    response = adapter.chat(messages: messages, &block)
    token_usage = Instrumentation::TokenTracker.extract_token_usage(response, provider)
    parsed_result = parse_response(response, input_values, signature_class)
  end
  
  parsed_result
end