Class: Braintrust::API::Internal::Experiments

Inherits:
Object
  • Object
show all
Defined in:
lib/braintrust/api/internal/experiments.rb

Overview

Internal Experiments API Not part of the public API - use through Eval.run

Instance Method Summary collapse

Constructor Details

#initialize(state) ⇒ Experiments

Returns a new instance of Experiments.



14
15
16
# File 'lib/braintrust/api/internal/experiments.rb', line 14

def initialize(state)
  @state = state
end

Instance Method Details

#create(name:, project_id:, ensure_new: true, tags: nil, metadata: nil, dataset_id: nil, dataset_version: nil) ⇒ Hash

Create an experiment POST /v1/experiment

Parameters:

  • name (String)

    Experiment name

  • project_id (String)

    Project ID

  • ensure_new (Boolean) (defaults to: true)

    If true (default), fail if exists; if false, return existing

  • tags (Array<String>, nil) (defaults to: nil)

    Optional tags

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

    Optional metadata

Returns:

  • (Hash)

    Experiment data with “id”, “name”, “project_id”, etc.



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
# File 'lib/braintrust/api/internal/experiments.rb', line 26

def create(name:, project_id:, ensure_new: true, tags: nil, metadata: nil,
  dataset_id: nil, dataset_version: nil)
  uri = URI("#{@state.api_url}/v1/experiment")

  payload = {
    project_id: project_id,
    ensure_new: ensure_new
  }
  payload[:name] = name if name
  payload[:tags] = tags if tags
  payload[:metadata] =  if 
  payload[:dataset_id] = dataset_id if dataset_id
  payload[:dataset_version] = dataset_version if dataset_version

  request = Net::HTTP::Post.new(uri)
  request["Content-Type"] = "application/json"
  request["Authorization"] = "Bearer #{@state.api_key}"
  request.body = JSON.dump(payload)

  response = Braintrust::Internal::Http.with_redirects(uri, request)

  unless response.is_a?(Net::HTTPSuccess)
    raise Error, "HTTP #{response.code} for POST #{uri}: #{response.body}"
  end

  JSON.parse(response.body)
end

#delete(id:) ⇒ Hash

Delete an experiment DELETE /v1/experiment/:id

Parameters:

  • id (String)

    Experiment ID

Returns:

  • (Hash)

    Deleted experiment data



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/braintrust/api/internal/experiments.rb', line 58

def delete(id:)
  uri = URI("#{@state.api_url}/v1/experiment/#{id}")

  request = Net::HTTP::Delete.new(uri)
  request["Authorization"] = "Bearer #{@state.api_key}"

  response = Braintrust::Internal::Http.with_redirects(uri, request)

  unless response.is_a?(Net::HTTPSuccess)
    raise Error, "HTTP #{response.code} for DELETE #{uri}: #{response.body}"
  end

  JSON.parse(response.body)
end