Class: LangsmithrbRails::RunTree

Inherits:
Object
  • Object
show all
Defined in:
lib/langsmithrb_rails/run_trees.rb

Overview

Run trees for hierarchical tracing

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, run_type:, inputs:, run_id: nil, parent_run_id: nil, project_name: nil, tags: []) ⇒ RunTree

Initialize a new run tree

Parameters:

  • name (String)

    Name of the run

  • run_type (String)

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

  • inputs (Hash)

    Input data

  • run_id (String) (defaults to: nil)

    Optional run ID

  • parent_run_id (String) (defaults to: nil)

    Optional parent run ID

  • project_name (String) (defaults to: nil)

    Optional project name

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

    Optional tags



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/langsmithrb_rails/run_trees.rb', line 18

def initialize(name:, run_type:, inputs:, run_id: nil, parent_run_id: nil, project_name: nil, tags: [])
  @run_id = run_id || SecureRandom.uuid
  @name = name
  @run_type = run_type
  @inputs = inputs
  @parent_run_id = parent_run_id
  @start_time = Time.now.utc
  @children = []
  @project_name = project_name
  @tags = tags
  @client = LangsmithrbRails::Client.new
  @ended = false
  
  # Create the run in LangSmith
  create_run
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/langsmithrb_rails/run_trees.rb', line 8

def children
  @children
end

#inputsObject (readonly)

Returns the value of attribute inputs.



8
9
10
# File 'lib/langsmithrb_rails/run_trees.rb', line 8

def inputs
  @inputs
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/langsmithrb_rails/run_trees.rb', line 8

def name
  @name
end

#parent_run_idObject (readonly)

Returns the value of attribute parent_run_id.



8
9
10
# File 'lib/langsmithrb_rails/run_trees.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/langsmithrb_rails/run_trees.rb', line 8

def project_name
  @project_name
end

#run_idObject (readonly)

Returns the value of attribute run_id.



8
9
10
# File 'lib/langsmithrb_rails/run_trees.rb', line 8

def run_id
  @run_id
end

#run_typeObject (readonly)

Returns the value of attribute run_type.



8
9
10
# File 'lib/langsmithrb_rails/run_trees.rb', line 8

def run_type
  @run_type
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



8
9
10
# File 'lib/langsmithrb_rails/run_trees.rb', line 8

def start_time
  @start_time
end

#tagsObject (readonly)

Returns the value of attribute tags.



8
9
10
# File 'lib/langsmithrb_rails/run_trees.rb', line 8

def tags
  @tags
end

Instance Method Details

#create_child(name:, run_type:, inputs:, tags: []) ⇒ RunTree

Create a child run

Parameters:

  • name (String)

    Name of the child run

  • run_type (String)

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

  • inputs (Hash)

    Input data

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

    Optional tags

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/langsmithrb_rails/run_trees.rb', line 41

def create_child(name:, run_type:, inputs:, tags: [])
  child_run_id = SecureRandom.uuid
  
  child_run = RunTree.new(
    run_id: child_run_id,
    parent_run_id: @run_id,
    name: name,
    run_type: run_type,
    inputs: inputs,
    project_name: @project_name,
    tags: tags
  )
  
  @children << child_run
  child_run
end

#end(outputs: {}, error: nil) ⇒ Object

End the run with outputs

Parameters:

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

    Output data

  • error (String) (defaults to: nil)

    Optional error message



61
62
63
64
65
66
67
68
69
70
# File 'lib/langsmithrb_rails/run_trees.rb', line 61

def end(outputs: {}, error: nil)
  return if @ended

  # End all children first
  @children.each { |child| child.end unless child.ended? }

  # Update the run in LangSmith
  update_run(outputs, error)
  @ended = true
end

#ended?Boolean

Check if the run has ended

Returns:

  • (Boolean)

    True if the run has ended



74
75
76
# File 'lib/langsmithrb_rails/run_trees.rb', line 74

def ended?
  @ended
end