Class: DSPy::LM
- Inherits:
-
Object
- Object
- DSPy::LM
- 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/message.rb,
lib/dspy/lm/response.rb,
lib/dspy/lm/chat_strategy.rb,
lib/dspy/lm/json_strategy.rb,
lib/dspy/lm/vision_models.rb,
lib/dspy/lm/adapter_factory.rb,
lib/dspy/lm/message_builder.rb
Defined Under Namespace
Modules: MessageFactory, ResponseMetadataFactory, UsageFactory, VisionModels Classes: Adapter, AdapterError, AdapterFactory, AnthropicResponseMetadata, AnthropicUsage, ChatStrategy, ConfigurationError, Error, GeminiResponseMetadata, IncompatibleDocumentFeatureError, IncompatibleImageFeatureError, JSONStrategy, Message, MessageBuilder, MissingAPIKeyError, MissingAdapterError, OpenAIResponseMetadata, OpenAIUsage, Response, ResponseMetadata, UnsupportedProviderError, Usage
Instance Attribute Summary collapse
-
#adapter ⇒ Object
readonly
Returns the value of attribute adapter.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#data_format ⇒ Object
readonly
Returns the value of attribute data_format.
-
#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.
-
#schema_format ⇒ Object
readonly
Returns the value of attribute schema_format.
Instance Method Summary collapse
- #chat(inference_module, input_values, &block) ⇒ Object
-
#initialize(model_id, api_key: nil, schema_format: :json, data_format: :json, **options) ⇒ LM
constructor
A new instance of LM.
- #raw_chat(messages = nil, &block) ⇒ Object
Constructor Details
#initialize(model_id, api_key: nil, schema_format: :json, data_format: :json, **options) ⇒ LM
Returns a new instance of LM.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/dspy/lm.rb', line 30 def initialize(model_id, api_key: nil, schema_format: :json, data_format: :json, **) @model_id = model_id @api_key = api_key @schema_format = schema_format @data_format = data_format # 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, **) end |
Instance Attribute Details
#adapter ⇒ Object (readonly)
Returns the value of attribute adapter.
28 29 30 |
# File 'lib/dspy/lm.rb', line 28 def adapter @adapter end |
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
28 29 30 |
# File 'lib/dspy/lm.rb', line 28 def api_key @api_key end |
#data_format ⇒ Object (readonly)
Returns the value of attribute data_format.
28 29 30 |
# File 'lib/dspy/lm.rb', line 28 def data_format @data_format end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
28 29 30 |
# File 'lib/dspy/lm.rb', line 28 def model @model end |
#model_id ⇒ Object (readonly)
Returns the value of attribute model_id.
28 29 30 |
# File 'lib/dspy/lm.rb', line 28 def model_id @model_id end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
28 29 30 |
# File 'lib/dspy/lm.rb', line 28 def provider @provider end |
#schema_format ⇒ Object (readonly)
Returns the value of attribute schema_format.
28 29 30 |
# File 'lib/dspy/lm.rb', line 28 def schema_format @schema_format end |
Instance Method Details
#chat(inference_module, input_values, &block) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/dspy/lm.rb', line 43 def chat(inference_module, input_values, &block) # Capture the current DSPy context before entering Sync block parent_context = DSPy::Context.current Sync do # Isolate fiber context while preserving trace/module ancestry Fiber[:dspy_context] = DSPy::Context.fork_context(parent_context) signature_class = inference_module.signature_class # Build messages from inference module = (inference_module, input_values) # Execute with instrumentation response = instrument_lm_request(, signature_class.name) do chat_with_strategy(, signature_class, &block) end # Parse response (no longer needs separate instrumentation) parsed_result = parse_response(response, input_values, signature_class) parsed_result end end |
#raw_chat(messages = nil, &block) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/dspy/lm.rb', line 68 def raw_chat( = nil, &block) # Support both array format and builder DSL if block_given? && .nil? # DSL mode - block is for building messages builder = MessageBuilder.new yield builder = builder. streaming_block = nil else # Array mode - block is for streaming ||= [] streaming_block = block end # Normalize and validate messages = () # Execute with instrumentation execute_raw_chat(, &streaming_block) end |