Module: LangsmithrbRails::Wrappers::Base

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

Overview

Base module for LLM provider wrappers

Class Method Summary collapse

Class Method Details

.create_run(name, inputs, run_type: "llm", project_name: nil, tags: []) ⇒ Hash

Create a run for a provider operation

Parameters:

  • name (String)

    Name of the operation

  • inputs (Hash)

    Input data

  • run_type (String) (defaults to: "llm")

    Type of run (e.g., “llm”, “chain”)

  • project_name (String) (defaults to: nil)

    Optional project name

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

    Optional tags

Returns:

  • (Hash)

    The created run data



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/langsmithrb_rails/wrappers/base.rb', line 24

def self.create_run(name, inputs, run_type: "llm", project_name: nil, tags: [])
  client = LangsmithrbRails::Client.new
  
  run_data = {
    name: name,
    inputs: inputs,
    run_type: run_type,
    start_time: Time.now.utc.iso8601,
    execution_order: 1,
    serialized: { name: name },
    session_name: project_name,
    tags: tags
  }
  
  response = client.create_run(run_data)
  
  if response[:status] >= 200 && response[:status] < 300
    response[:body]
  else
    LangsmithrbRails.logger.error("Failed to create run: #{response[:error] || response[:body]}")
    nil
  end
end

.update_run(run_id, outputs, error: nil) ⇒ Hash

Update a run with outputs and end time

Parameters:

  • run_id (String)

    ID of the run to update

  • outputs (Hash)

    Output data

  • error (String) (defaults to: nil)

    Optional error message

Returns:

  • (Hash)

    The updated run data



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
# File 'lib/langsmithrb_rails/wrappers/base.rb', line 53

def self.update_run(run_id, outputs, error: nil)
  return unless run_id

  client = LangsmithrbRails::Client.new
  
  run_data = {
    outputs: outputs,
    end_time: Time.now.utc.iso8601
  }
  
  if error
    run_data[:error] = error
    run_data[:status] = "error"
  else
    run_data[:status] = "success"
  end
  
  response = client.update_run(run_id, run_data)
  
  if response[:status] >= 200 && response[:status] < 300
    response[:body]
  else
    LangsmithrbRails.logger.error("Failed to update run: #{response[:error] || response[:body]}")
    nil
  end
end

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

Wrap a provider client with LangSmith tracing

Parameters:

  • client (Object)

    The provider client 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 client

Raises:

  • (NotImplementedError)


12
13
14
15
# File 'lib/langsmithrb_rails/wrappers/base.rb', line 12

def self.wrap(client, project_name: nil, tags: [])
  # To be implemented by specific provider wrappers
  raise NotImplementedError, "This method should be implemented by provider-specific wrappers"
end