Class: Langsmith::Run

Inherits:
Object
  • Object
show all
Defined in:
lib/langsmith/run.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data) ⇒ Run

Initialize a new Run instance

Parameters:

  • client (Langsmith::Client)

    The LangSmith client

  • data (Hash)

    Run data from the API



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/langsmith/run.rb', line 14

def initialize(client, data)
  @client = client
  @id = data["id"]
  @name = data["name"]
  @run_type = data["run_type"]
  @start_time = data["start_time"] ? Time.parse(data["start_time"]) : Time.now
  @end_time = data["end_time"] ? Time.parse(data["end_time"]) : nil
  @status = data["status"] || "in_progress"
  @inputs = data["inputs"] || {}
  @outputs = data["outputs"] || {}
  @error = data["error"]
  @project_name = data["project_name"]
  @trace_id = data["trace_id"]
  @parent_run_id = data["parent_run_id"]
  @extra = data["extra"] || {}
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



7
8
9
# File 'lib/langsmith/run.rb', line 7

def end_time
  @end_time
end

#errorObject (readonly)

Returns the value of attribute error.



8
9
10
# File 'lib/langsmith/run.rb', line 8

def error
  @error
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/langsmith/run.rb', line 7

def id
  @id
end

#inputsObject (readonly)

Returns the value of attribute inputs.



8
9
10
# File 'lib/langsmith/run.rb', line 8

def inputs
  @inputs
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/langsmith/run.rb', line 7

def name
  @name
end

#outputsObject (readonly)

Returns the value of attribute outputs.



8
9
10
# File 'lib/langsmith/run.rb', line 8

def outputs
  @outputs
end

#parent_run_idObject (readonly)

Returns the value of attribute parent_run_id.



8
9
10
# File 'lib/langsmith/run.rb', line 8

def parent_run_id
  @parent_run_id
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



8
9
10
# File 'lib/langsmith/run.rb', line 8

def project_name
  @project_name
end

#run_typeObject (readonly)

Returns the value of attribute run_type.



7
8
9
# File 'lib/langsmith/run.rb', line 7

def run_type
  @run_type
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



7
8
9
# File 'lib/langsmith/run.rb', line 7

def start_time
  @start_time
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/langsmith/run.rb', line 7

def status
  @status
end

#trace_idObject (readonly)

Returns the value of attribute trace_id.



8
9
10
# File 'lib/langsmith/run.rb', line 8

def trace_id
  @trace_id
end

Instance Method Details

#add_feedback(key:, score:, comment: nil) ⇒ Langsmith::Feedback

Add feedback to this run

Parameters:

  • key (String)

    Feedback key (e.g., “correctness”, “helpfulness”)

  • score (Float)

    Feedback score (typically 0.0 to 1.0)

  • comment (String) (defaults to: nil)

    Optional comment with the feedback

Returns:



73
74
75
# File 'lib/langsmith/run.rb', line 73

def add_feedback(key:, score:, comment: nil)
  @client.create_feedback(run_id: @id, key: key, score: score, comment: comment)
end

#completed?Boolean

Check if the run is completed

Returns:

  • (Boolean)

    True if the run is completed



95
96
97
# File 'lib/langsmith/run.rb', line 95

def completed?
  !@end_time.nil?
end

#create_child_run(name:, run_type:, inputs: {}, extra: {}) ⇒ Langsmith::Run

Create a child run for this run

Parameters:

  • name (String)

    Name of the child run

  • run_type (String)

    Type of the child run

  • inputs (Hash) (defaults to: {})

    Input values for the child run

  • extra (Hash) (defaults to: {})

    Additional metadata for the child run

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/langsmith/run.rb', line 52

def create_child_run(name:, run_type:, inputs: {}, extra: {})
  child_extra = extra.merge(parent_run_id: @id, trace_id: @trace_id)
  
  data = {
    name: name,
    run_type: run_type,
    inputs: inputs,
    extra: child_extra
  }
  data[:project_name] = @project_name if @project_name

  response = @client.post("/runs", data)
  Langsmith::Run.new(@client, response)
end

#durationFloat

Get the duration of the run in seconds

Returns:

  • (Float)

    Duration in seconds, or nil if the run is not completed



109
110
111
112
# File 'lib/langsmith/run.rb', line 109

def duration
  return nil unless @end_time
  @end_time - @start_time
end

#end(outputs: nil, error: nil) ⇒ Langsmith::Run

Update the run with outputs and mark it as completed

Parameters:

  • outputs (Hash) (defaults to: nil)

    Output values from the run

Returns:



35
36
37
38
39
40
41
42
43
# File 'lib/langsmith/run.rb', line 35

def end(outputs: nil, error: nil)
  end_time = Time.now
  
  if error
    @client.update_run(run_id: @id, end_time: end_time, error: error)
  else
    @client.update_run(run_id: @id, outputs: outputs, end_time: end_time)
  end
end

#error?Boolean

Check if the run has an error

Returns:

  • (Boolean)

    True if the run has an error



102
103
104
# File 'lib/langsmith/run.rb', line 102

def error?
  !@error.nil?
end

#get_feedbackArray<Langsmith::Feedback>

Get all feedback for this run

Returns:



80
81
82
# File 'lib/langsmith/run.rb', line 80

def get_feedback
  @client.get_feedback(run_id: @id)
end

#get_metadata(key) ⇒ Object

Get metadata from the run

Parameters:

  • key (String)

    The metadata key to retrieve

Returns:

  • (Object)

    The metadata value



88
89
90
# File 'lib/langsmith/run.rb', line 88

def (key)
  @extra[key]
end