Module: LangsmithrbRails::Wrappers::Anthropic

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

Overview

Wrapper for Anthropic client

Class Method Summary collapse

Class Method Details

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

Wrap an Anthropic client with LangSmith tracing



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
90
91
92
93
94
95
96
97
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
# File 'lib/langsmithrb_rails/wrappers/anthropic.rb', line 14

def self.wrap(client, project_name: nil, tags: [])
  # Create a wrapper class that inherits from the client's class
  wrapper_class = Class.new(client.class) do
    attr_reader :original_client, :project_name, :tags

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

    # Wrap messages (Claude API)
    def messages(messages:, model: nil, max_tokens: nil, temperature: nil, **params)
      # Prepare inputs for tracing
      inputs = {
        messages: messages,
        model: model,
        max_tokens: max_tokens,
        temperature: temperature
      }.merge(params).compact

      # Create a run
      run_id = nil
      begin
        run = LangsmithrbRails::Wrappers::Base.create_run(
          "anthropic.messages",
          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 = original_client.messages(
          messages: messages,
          model: model,
          max_tokens: max_tokens,
          temperature: temperature,
          **params
        )

        # 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

    # Wrap completions (older Claude API)
    def completions(prompt:, model: nil, max_tokens_to_sample: nil, temperature: nil, **params)
      # Prepare inputs for tracing
      inputs = {
        prompt: prompt,
        model: model,
        max_tokens_to_sample: max_tokens_to_sample,
        temperature: temperature
      }.merge(params).compact

      # Create a run
      run_id = nil
      begin
        run = LangsmithrbRails::Wrappers::Base.create_run(
          "anthropic.completions",
          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 = original_client.completions(
          prompt: prompt,
          model: model,
          max_tokens_to_sample: max_tokens_to_sample,
          temperature: temperature,
          **params
        )

        # 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 client
    def method_missing(method_name, *args, **kwargs, &block)
      if original_client.respond_to?(method_name)
        original_client.send(method_name, *args, **kwargs, &block)
      else
        super
      end
    end

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

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