Module: DSPy

Extended by:
Dry::Configurable
Defined in:
lib/dspy/prediction.rb,
lib/dspy.rb,
lib/dspy/lm.rb,
lib/dspy/field.rb,
lib/dspy/image.rb,
lib/dspy/tools.rb,
lib/dspy/errors.rb,
lib/dspy/events.rb,
lib/dspy/memory.rb,
lib/dspy/module.rb,
lib/dspy/prompt.rb,
lib/dspy/re_act.rb,
lib/dspy/context.rb,
lib/dspy/example.rb,
lib/dspy/predict.rb,
lib/dspy/version.rb,
lib/dspy/code_act.rb,
lib/dspy/evaluate.rb,
lib/dspy/lm/usage.rb,
lib/dspy/strategy.rb,
lib/dspy/lm/errors.rb,
lib/dspy/signature.rb,
lib/dspy/lm/adapter.rb,
lib/dspy/lm/message.rb,
lib/dspy/tools/base.rb,
lib/dspy/lm/response.rb,
lib/dspy/events/types.rb,
lib/dspy/observability.rb,
lib/dspy/tools/toolset.rb,
lib/dspy/error_formatter.rb,
lib/dspy/schema_adapters.rb,
lib/dspy/teleprompt/gepa.rb,
lib/dspy/type_serializer.rb,
lib/dspy/chain_of_thought.rb,
lib/dspy/few_shot_example.rb,
lib/dspy/lm/cache_manager.rb,
lib/dspy/lm/retry_handler.rb,
lib/dspy/lm/vision_models.rb,
lib/dspy/teleprompt/utils.rb,
lib/dspy/events/subscribers.rb,
lib/dspy/lm/adapter_factory.rb,
lib/dspy/lm/message_builder.rb,
lib/dspy/memory/memory_store.rb,
lib/dspy/teleprompt/mipro_v2.rb,
lib/dspy/lm/strategy_selector.rb,
lib/dspy/memory/memory_record.rb,
lib/dspy/mixins/type_coercion.rb,
lib/dspy/tools/memory_toolset.rb,
lib/dspy/memory/memory_manager.rb,
lib/dspy/mixins/struct_builder.rb,
lib/dspy/memory/in_memory_store.rb,
lib/dspy/events/subscriber_mixin.rb,
lib/dspy/memory/embedding_engine.rb,
lib/dspy/memory/memory_compactor.rb,
lib/dspy/storage/program_storage.rb,
lib/dspy/storage/storage_manager.rb,
lib/dspy/teleprompt/data_handler.rb,
lib/dspy/teleprompt/teleprompter.rb,
lib/dspy/propose/grounded_proposer.rb,
lib/dspy/registry/registry_manager.rb,
lib/dspy/lm/adapters/gemini_adapter.rb,
lib/dspy/lm/adapters/ollama_adapter.rb,
lib/dspy/lm/adapters/openai_adapter.rb,
lib/dspy/lm/strategies/base_strategy.rb,
lib/dspy/registry/signature_registry.rb,
lib/dspy/teleprompt/simple_optimizer.rb,
lib/dspy/lm/adapters/anthropic_adapter.rb,
lib/dspy/lm/structured_output_strategy.rb,
lib/dspy/memory/local_embedding_engine.rb,
lib/dspy/tools/text_processing_toolset.rb,
lib/dspy/lm/adapters/openai/schema_converter.rb,
lib/dspy/lm/strategies/anthropic_tool_use_strategy.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

Overview

typed: strict frozen_string_literal: true

Defined Under Namespace

Modules: Events, Memory, Metrics, Mixins, Propose, Registry, SchemaAdapters, Storage, Teleprompt, Tools Classes: ChainOfThought, CodeAct, CodeActHistoryEntry, CodeActNextStep, ConfigurationError, Context, DeserializationError, Error, ErrorFormatter, Evaluate, EventRegistry, Example, FewShotExample, HistoryEntry, Image, InputField, LM, Module, NextStep, Observability, OutputField, Predict, Prediction, PredictionInvalidError, Prompt, ReAct, ReActObservationBase, RubyCodeGeneration, RubyCodeObservation, Signature, Strategy, ThoughtBase, TypeSerializer, ValidationError

Constant Summary collapse

VERSION =
"0.24.2"

Class Method Summary collapse

Class Method Details

.event(event_name_or_object, attributes = {}) ⇒ Object



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
# File 'lib/dspy.rb', line 50

def self.event(event_name_or_object, attributes = {})
  # Handle typed event objects
  if event_name_or_object.respond_to?(:name) && event_name_or_object.respond_to?(:to_attributes)
    event_obj = event_name_or_object
    event_name = event_obj.name
    attributes = event_obj.to_attributes
    
    # For LLM events, use OpenTelemetry semantic conventions for spans
    if event_obj.is_a?(DSPy::Events::LLMEvent)
      otel_attributes = event_obj.to_otel_attributes
      create_event_span(event_name, otel_attributes)
    else
      create_event_span(event_name, attributes)
    end
  else
    # Handle string event names (backward compatibility)
    event_name = event_name_or_object
    raise ArgumentError, "Event name cannot be nil" if event_name.nil?
    
    # Handle nil attributes
    attributes = {} if attributes.nil?
    
    # Create OpenTelemetry span for the event if observability is enabled
    create_event_span(event_name, attributes)
  end
  
  # Perform the actual logging (original DSPy.log behavior)
  emit_log(event_name, attributes)
  
  # Notify event listeners
  events.notify(event_name, attributes)
end

.eventsObject



83
84
85
# File 'lib/dspy.rb', line 83

def self.events
  @event_registry ||= DSPy::EventRegistry.new
end

.log(event, **attributes) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dspy.rb', line 38

def self.log(event, **attributes)
  # Return nil early if logger is not configured (backward compatibility)
  return nil unless logger
  
  # Forward to event system - this maintains backward compatibility
  # while providing all new event system benefits
  event(event, attributes)
  
  # Return nil to maintain backward compatibility
  nil
end

.loggerObject



34
35
36
# File 'lib/dspy.rb', line 34

def self.logger
  @logger ||= create_logger
end