Class: Langsmith::Dataset

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, data) ⇒ Dataset

Initialize a new Dataset instance

Parameters:

  • client (Langsmith::Client)

    The LangSmith client

  • data (Hash)

    Dataset data from the API



13
14
15
16
17
18
19
20
21
22
# File 'lib/langsmith/dataset.rb', line 13

def initialize(client, data)
  @client = client
  @id = data[:id] || data["id"]
  @name = data[:name] || data["name"]
  @description = data[:description] || data["description"]
  created_at_value = data[:created_at] || data["created_at"]
  @created_at = created_at_value ? Time.parse(created_at_value) : nil
  @tenant_id = data[:tenant_id] || data["tenant_id"]
  @data = data
end

Instance Attribute Details

#created_atObject (readonly)

Returns the value of attribute created_at.



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

def created_at
  @created_at
end

#dataObject (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#tenant_idObject (readonly)

Returns the value of attribute tenant_id.



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

def tenant_id
  @tenant_id
end

Instance Method Details

#create_evaluation_run(evaluator_name:, run_ids:, metadata: nil) ⇒ Langsmith::Evaluation

Create a new evaluation run on this dataset

Parameters:

  • evaluator_name (String)

    Name of the evaluator to use

  • run_ids (Array<String>)

    IDs of runs to evaluate

  • metadata (Hash, nil) (defaults to: nil)

    Additional metadata for the evaluation (optional)

Returns:



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/langsmith/dataset.rb', line 80

def create_evaluation_run(evaluator_name:, run_ids:, metadata: nil)
  data = {
    dataset_id: @id,
    evaluator_name: evaluator_name,
    run_ids: run_ids
  }
  data[:metadata] =  if 
  
  response = @client.post("/evaluations", data)
  Evaluation.new(@client, response)
end

#create_example(inputs:, outputs: nil, metadata: nil) ⇒ Langsmith::Example

Create a new example in this dataset

Parameters:

  • inputs (Hash)

    Input values for the example

  • outputs (Hash, nil) (defaults to: nil)

    Output values for the example (optional)

  • metadata (Hash, nil) (defaults to: nil)

    Additional metadata for the example (optional)

Returns:



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/langsmith/dataset.rb', line 30

def create_example(inputs:, outputs: nil, metadata: nil)
  data = {
    dataset_id: @id,
    inputs: inputs
  }
  data[:outputs] = outputs if outputs
  data[:metadata] =  if 
  
  response = @client.post("/examples", data)
  Example.new(@client, response)
end

#create_examples_batch(examples:) ⇒ Array<Example>

Create multiple examples in batch

Parameters:

  • examples (Array<Hash>)

    Array of example data, each containing :inputs and optionally :outputs and :metadata

Returns:

  • (Array<Example>)

    The created examples



70
71
72
# File 'lib/langsmith/dataset.rb', line 70

def create_examples_batch(examples:)
  @client.create_examples_batch(dataset_id: @id, examples: examples)
end

#deleteBoolean

Delete this dataset

Returns:

  • (Boolean)

    True if successful



127
128
129
130
# File 'lib/langsmith/dataset.rb', line 127

def delete
  @client.delete("/datasets/#{@id}")
  true
end

#get_example(example_id:) ⇒ Langsmith::Example

Get a specific example by ID

Parameters:

  • example_id (String)

    ID of the example to get

Returns:



46
47
48
49
# File 'lib/langsmith/dataset.rb', line 46

def get_example(example_id:)
  response = @client.get("/examples/#{example_id}")
  Example.new(@client, response)
end

#list_evaluation_runs(limit: 100, offset: 0) ⇒ Array<Langsmith::Evaluation>

List evaluation runs for this dataset

Parameters:

  • limit (Integer) (defaults to: 100)

    Maximum number of evaluation runs to return

  • offset (Integer) (defaults to: 0)

    Number of evaluation runs to skip

Returns:



97
98
99
100
101
102
103
104
105
# File 'lib/langsmith/dataset.rb', line 97

def list_evaluation_runs(limit: 100, offset: 0)
  params = {
    dataset_id: @id,
    limit: limit,
    offset: offset
  }
  response = @client.get("/evaluations", params)
  response.map { |eval_data| Evaluation.new(@client, eval_data) }
end

#list_examples(limit: 100, offset: 0) ⇒ Array<Langsmith::Example>

List examples in this dataset

Parameters:

  • limit (Integer) (defaults to: 100)

    Maximum number of examples to return

  • offset (Integer) (defaults to: 0)

    Number of examples to skip

Returns:



56
57
58
59
60
61
62
63
64
# File 'lib/langsmith/dataset.rb', line 56

def list_examples(limit: 100, offset: 0)
  params = {
    dataset_id: @id,
    limit: limit,
    offset: offset
  }
  response = @client.get("/examples", params)
  response.map { |example_data| Example.new(@client, example_data) }
end

#update(name: nil, description: nil) ⇒ Langsmith::Dataset

Update this dataset

Parameters:

  • name (String, nil) (defaults to: nil)

    New name for the dataset (optional)

  • description (String, nil) (defaults to: nil)

    New description for the dataset (optional)

Returns:



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/langsmith/dataset.rb', line 112

def update(name: nil, description: nil)
  data = {}
  data[:name] = name if name
  data[:description] = description if description
  
  response = @client.patch("/datasets/#{@id}", data)
  @name = response[:name] || response["name"] if name
  @description = response[:description] || response["description"] if description
  @data = response
  self
end