Module: LangsmithrbRails::Wrappers::OpenAI
- Defined in:
- lib/langsmithrb_rails/wrappers/openai.rb
Overview
Wrapper for OpenAI client
Class Method Summary collapse
-
.wrap(client, project_name: nil, tags: []) ⇒ Object
Wrap an OpenAI client with LangSmith tracing.
Class Method Details
.wrap(client, project_name: nil, tags: []) ⇒ Object
Wrap an OpenAI 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/langsmithrb_rails/wrappers/openai.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, ) @original_client = original_client @project_name = project_name @tags = end # Wrap chat completions def chat(messages:, model: nil, temperature: nil, max_tokens: nil, **params) # Prepare inputs for tracing inputs = { messages: , model: model, temperature: temperature, max_tokens: max_tokens }.merge(params).compact # Create a run run_id = nil begin run = LangsmithrbRails::Wrappers::Base.create_run( "openai.chat", inputs, run_type: "llm", project_name: project_name, tags: ) run_id = run&.dig("id") rescue => e LangsmithrbRails.logger.error("Failed to create LangSmith run: #{e.}") end # Call the original method begin result = original_client.chat( messages: , model: model, temperature: temperature, max_tokens: max_tokens, **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.) end raise e end end # Wrap completions def completions(prompt:, model: nil, temperature: nil, max_tokens: nil, **params) # Prepare inputs for tracing inputs = { prompt: prompt, model: model, temperature: temperature, max_tokens: max_tokens }.merge(params).compact # Create a run run_id = nil begin run = LangsmithrbRails::Wrappers::Base.create_run( "openai.completions", inputs, run_type: "llm", project_name: project_name, tags: ) run_id = run&.dig("id") rescue => e LangsmithrbRails.logger.error("Failed to create LangSmith run: #{e.}") end # Call the original method begin result = original_client.completions( prompt: prompt, model: model, temperature: temperature, max_tokens: max_tokens, **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.) end raise e end end # Wrap embeddings def (input:, model: nil, **params) # Prepare inputs for tracing inputs = { input: input, model: model }.merge(params).compact # Create a run run_id = nil begin run = LangsmithrbRails::Wrappers::Base.create_run( "openai.embeddings", inputs, run_type: "embedding", project_name: project_name, tags: ) run_id = run&.dig("id") rescue => e LangsmithrbRails.logger.error("Failed to create LangSmith run: #{e.}") end # Call the original method begin result = original_client.( input: input, model: model, **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.) 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, ) end |