Module: LangsmithrbRails::Wrappers::LLM

Defined in:
lib/langsmithrb_rails/wrappers/llm.rb

Overview

Wrapper for generic LLM providers

Class Method Summary collapse

Class Method Details

.traceable(object, method_name, run_name: nil, project_name: nil, tags: []) ⇒ Object

Create a traceable decorator for any method

Parameters:

  • object (Object)

    The object with the method to trace

  • method_name (Symbol)

    The name of the method to trace

  • run_name (String) (defaults to: nil)

    Name for the run

  • project_name (String) (defaults to: nil)

    Optional project name for traces

  • tags (Array<String>) (defaults to: [])

    Optional tags for traces

Returns:

  • (Object)

    The object with the traced method



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/langsmithrb_rails/wrappers/llm.rb', line 98

def self.traceable(object, method_name, run_name: nil, project_name: nil, tags: [])
  run_name ||= "#{object.class.name}.#{method_name}"
  
  # Store the original method
  original_method = object.method(method_name)
  
  # Define a new method that wraps the original
  object.define_singleton_method(method_name) do |*args, **kwargs, &block|
    # Prepare inputs for tracing
    inputs = {
      args: args,
      kwargs: kwargs
    }.compact
    
    # Create a run
    run_id = nil
    begin
      run = LangsmithrbRails::Wrappers::Base.create_run(
        run_name,
        inputs,
        run_type: "chain",
        project_name: project_name,
        tags: tags
      )
      run_id = run&.dig("id")
    rescue => e
      LangsmithrbRails.logger.error("Failed to create LangSmith run: #{e.message}")
    end
    
    # Call the original method
    begin
      result = original_method.call(*args, **kwargs, &block)
      
      # Update the run with the result
      if run_id
        outputs = { result: result }
        LangsmithrbRails::Wrappers::Base.update_run(run_id, outputs)
      end
      
      result
    rescue => e
      # Update the run with the error
      if run_id
        LangsmithrbRails::Wrappers::Base.update_run(run_id, {}, error: e.message)
      end
      raise e
    end
  end
  
  object
end

.wrap(llm, project_name: nil, tags: []) ⇒ Object

Wrap an LLM with LangSmith tracing

Parameters:

  • llm (Object)

    The LLM to wrap

  • project_name (String) (defaults to: nil)

    Optional project name for traces

  • tags (Array<String>) (defaults to: [])

    Optional tags for traces

Returns:

  • (Object)

    The wrapped LLM



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
89
# File 'lib/langsmithrb_rails/wrappers/llm.rb', line 14

def self.wrap(llm, project_name: nil, tags: [])
  # Create a wrapper class
  wrapper_class = Class.new do
    attr_reader :llm, :project_name, :tags

    def initialize(llm, project_name, tags)
      @llm = llm
      @project_name = project_name
      @tags = tags
    end

    # Wrap the call method
    def call(prompt, **params)
      # Prepare inputs for tracing
      inputs = {
        prompt: prompt
      }.merge(params).compact

      # Create a run
      run_id = nil
      begin
        run = LangsmithrbRails::Wrappers::Base.create_run(
          "llm.call",
          inputs,
          run_type: "llm",
          project_name: project_name,
          tags: tags
        )
        run_id = run&.dig("id")
      rescue => e
        LangsmithrbRails.logger.error("Failed to create LangSmith run: #{e.message}")
      end

      # Call the original method
      begin
        result = if llm.respond_to?(:call)
          llm.call(prompt, **params)
        elsif llm.is_a?(Proc)
          llm.call(prompt, **params)
        else
          raise ArgumentError, "LLM must respond to 'call' or be a Proc"
        end

        # Update the run with the result
        if run_id
          outputs = { response: result }
          LangsmithrbRails::Wrappers::Base.update_run(run_id, outputs)
        end

        result
      rescue => e
        # Update the run with the error
        if run_id
          LangsmithrbRails::Wrappers::Base.update_run(run_id, {}, error: e.message)
        end
        raise e
      end
    end

    # Forward other methods to the original LLM
    def method_missing(method_name, *args, **kwargs, &block)
      if llm.respond_to?(method_name)
        llm.send(method_name, *args, **kwargs, &block)
      else
        super
      end
    end

    def respond_to_missing?(method_name, include_private = false)
      llm.respond_to?(method_name, include_private) || super
    end
  end

  # Create and return an instance of the wrapper class
  wrapper_class.new(llm, project_name, tags)
end