Class: LangsmithrbRails::Config

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/langsmithrb_rails/config.rb

Overview

Configuration class for LangsmithrbRails

Constant Summary collapse

DEFAULTS =

Default configuration values

{
  api_url: "https://api.smith.langchain.com",
  project_name: "default",
  api_key: ENV["LANGSMITH_API_KEY"],
  sampling: 1.0,
  redact_by_default: true,
  timeout_seconds: 3.0,
  open_timeout_seconds: 1.0,
  env: ENV["RAILS_ENV"] || "development",
  enabled: true,
  # Advanced tracing options
  trace_all: ENV.fetch("LANGSMITH_TRACE_ALL", "false") == "true",
  trace_level: ENV.fetch("LANGSMITH_TRACE_LEVEL", "info").to_sym,
  # OpenTelemetry options
  otel_enabled: ENV.fetch("LANGSMITH_OTEL_ENABLED", "false") == "true",
  otel_service_name: ENV.fetch("LANGSMITH_OTEL_SERVICE_NAME", "langsmithrb_rails"),
  # Evaluation options
  evaluation_enabled: ENV.fetch("LANGSMITH_EVALUATION_ENABLED", "false") == "true",
  # Logging options
  log_level: ENV.fetch("LANGSMITH_LOG_LEVEL", "info").to_sym,
  log_to_stdout: ENV.fetch("LANGSMITH_LOG_TO_STDOUT", "false") == "true"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfig

Initialize with default values



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/langsmithrb_rails/config.rb', line 44

def initialize
  # Set defaults first
  DEFAULTS.each do |key, value|
    instance_variable_set("@#{key}", value)
  end
  
  # Then override with environment variables if present
  load_from_env
  
  # Initialize logger
  @logger = create_logger(@log_level, @log_to_stdout)
end

Instance Attribute Details

#api_keyObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def api_key
  @api_key
end

#api_urlObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def api_url
  @api_url
end

#enabledObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def enabled
  @enabled
end

#evaluation_enabledObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def evaluation_enabled
  @evaluation_enabled
end

#log_levelObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def log_level
  @log_level
end

#log_to_stdoutObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def log_to_stdout
  @log_to_stdout
end

#loggerLogger (readonly)

Get logger instance

Returns:

  • (Logger)

    Logger instance



136
137
138
# File 'lib/langsmithrb_rails/config.rb', line 136

def logger
  @logger
end

#otel_enabledObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def otel_enabled
  @otel_enabled
end

#otel_service_nameObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def otel_service_name
  @otel_service_name
end

#project_nameObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def project_name
  @project_name
end

#samplingObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def sampling
  @sampling
end

#trace_allObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def trace_all
  @trace_all
end

#trace_levelObject

Configuration attributes



38
39
40
# File 'lib/langsmithrb_rails/config.rb', line 38

def trace_level
  @trace_level
end

Instance Method Details

#enabled?Boolean

Check if LangSmith is enabled

Returns:

  • (Boolean)

    Whether LangSmith is enabled



97
98
99
# File 'lib/langsmithrb_rails/config.rb', line 97

def enabled?
  @enabled
end

#evaluation_enabled?Boolean

Check if evaluation is enabled

Returns:

  • (Boolean)

    Whether evaluation is enabled



117
118
119
# File 'lib/langsmithrb_rails/config.rb', line 117

def evaluation_enabled?
  @evaluation_enabled
end

#load_from_envConfig

Load configuration from environment variables

Returns:

  • (Config)

    Self for chaining



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/langsmithrb_rails/config.rb', line 59

def load_from_env
  @api_key = ENV["LANGSMITH_API_KEY"] if ENV["LANGSMITH_API_KEY"]
  @api_url = ENV["LANGSMITH_API_URL"] if ENV["LANGSMITH_API_URL"]
  @project_name = ENV["LANGSMITH_PROJECT"] if ENV["LANGSMITH_PROJECT"]
  @enabled = ENV["LANGSMITH_ENABLED"] == "true" if ENV.key?("LANGSMITH_ENABLED")
  @sampling = ENV["LANGSMITH_SAMPLING"].to_f if ENV["LANGSMITH_SAMPLING"]
  @trace_all = ENV["LANGSMITH_TRACE_ALL"] == "true" if ENV.key?("LANGSMITH_TRACE_ALL")
  @trace_level = ENV["LANGSMITH_TRACE_LEVEL"].to_sym if ENV["LANGSMITH_TRACE_LEVEL"]
  @otel_enabled = ENV["LANGSMITH_OTEL_ENABLED"] == "true" if ENV.key?("LANGSMITH_OTEL_ENABLED")
  @otel_service_name = ENV["LANGSMITH_OTEL_SERVICE_NAME"] if ENV["LANGSMITH_OTEL_SERVICE_NAME"]
  @evaluation_enabled = ENV["LANGSMITH_EVALUATION_ENABLED"] == "true" if ENV.key?("LANGSMITH_EVALUATION_ENABLED")
  @log_level = ENV["LANGSMITH_LOG_LEVEL"].to_sym if ENV["LANGSMITH_LOG_LEVEL"]
  @log_to_stdout = ENV["LANGSMITH_LOG_TO_STDOUT"] == "true" if ENV.key?("LANGSMITH_LOG_TO_STDOUT")
  self
end

#load_from_yaml(path) ⇒ Config

Load configuration from YAML file

Parameters:

  • path (String)

    Path to YAML file

Returns:

  • (Config)

    Self for chaining



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/langsmithrb_rails/config.rb', line 78

def load_from_yaml(path)
  return self unless File.exist?(path)
  
  yml = YAML.load_file(path)
  env = (ENV["RAILS_ENV"] || "development").to_s
  config = yml.fetch(env, {})
  
  config.each do |key, value|
    send("#{key}=", value) if respond_to?("#{key}=")
  end
  
  # Reset logger if log settings changed
  @logger = create_logger(@log_level, @log_to_stdout)
  
  self
end

#otel_enabled?Boolean

Check if OpenTelemetry is enabled

Returns:

  • (Boolean)

    Whether OpenTelemetry is enabled



111
112
113
# File 'lib/langsmithrb_rails/config.rb', line 111

def otel_enabled?
  @otel_enabled
end

#should_sample?Boolean

Check if a trace should be sampled

Returns:

  • (Boolean)

    Whether to sample the trace



103
104
105
106
107
# File 'lib/langsmithrb_rails/config.rb', line 103

def should_sample?
  return true if @sampling >= 1.0
  return false if @sampling <= 0.0
  Random.rand < @sampling
end