Class: Braintrust::Contrib::Rails::Server::EvalController

Inherits:
ApplicationController show all
Includes:
ActionController::Live
Defined in:
lib/braintrust/contrib/rails/server/eval_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/braintrust/contrib/rails/server/eval_controller.rb', line 10

def create
  body = parse_json_body
  unless body
    render json: {"error" => "Invalid JSON body"}, status: :bad_request
    return
  end

  result = Engine.eval_service.validate(body)
  if result[:error]
    render json: {"error" => result[:error]}, status: result[:status]
    return
  end

  response.headers["Content-Type"] = "text/event-stream"
  response.headers["Cache-Control"] = "no-cache"
  response.headers["Connection"] = "keep-alive"

  sse = Braintrust::Server::SSEWriter.new { |chunk| response.stream.write(chunk) }
  Engine.eval_service.stream(result, auth: @braintrust_auth, sse: sse)
ensure
  response.stream.close
end