Class: DSPy::LM
- Inherits:
-
Object
- Object
- DSPy::LM
- 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
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#model_id ⇒ Object
readonly
Returns the value of attribute model_id.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
Instance Method Summary collapse
- #chat(inference_module, input_values, &block) ⇒ Object
-
#initialize(model_id, api_key: nil) ⇒ LM
constructor
A new instance of LM.
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
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
19 20 21 |
# File 'lib/dspy/lm.rb', line 19 def adapter @adapter end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
19 20 21 |
# File 'lib/dspy/lm.rb', line 19 def api_key @api_key end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
19 20 21 |
# File 'lib/dspy/lm.rb', line 19 def model @model end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
19 20 21 |
# File 'lib/dspy/lm.rb', line 19 def model_id @model_id end |
#provider ⇒ Object (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 = (inference_module, input_values) # Calculate input size for monitoring input_text = .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: , &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: , &block) token_usage = Instrumentation::TokenTracker.extract_token_usage(response, provider) parsed_result = parse_response(response, input_values, signature_class) end parsed_result end |