-
Notifications
You must be signed in to change notification settings - Fork 6
Add Rails engine for remote eval server #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
320334b
Added: Rails engine for remote eval server
Qard f448225
Address Rails engine review feedback
Qard 98c49e9
Fix Rails engine lint
Qard 2a1e0e2
Add Rails eval server generator
Qard 763799c
Namespace Rails eval server generator
Qard d7a8424
Changed: Move some files around for namespacing server
delner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Braintrust Rails Engine — mount example | ||
| # | ||
| # This file shows one conventional setup for the Braintrust eval server in Rails: | ||
| # 1. Define evaluator classes under app/evaluators/ | ||
| # 2. Generate the initializer with: | ||
| # bin/rails generate braintrust:server | ||
| # 3. Mount the engine in config/routes.rb | ||
| # | ||
| # Requirements: | ||
| # gem 'actionpack', '~> 8.0' | ||
| # gem 'railties', '~> 8.0' | ||
| # gem 'activesupport', '~> 8.0' | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # app/evaluators/my_classifier.rb | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| class MyClassifier < Braintrust::Eval::Evaluator | ||
| def task | ||
| ->(input:) { classify(input) } | ||
| end | ||
|
|
||
| def scorers | ||
| [Braintrust::Scorer.new("accuracy") { |expected:, output:| (output == expected) ? 1.0 : 0.0 }] | ||
| end | ||
| end | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # config/initializers/braintrust_server.rb | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| # Generated by: bin/rails generate braintrust:server | ||
| # | ||
| # require "braintrust/contrib/rails/server" | ||
| # | ||
| # Braintrust::Contrib::Rails::Server::Engine.configure do |config| | ||
| # config.evaluators = { | ||
| # "my-classifier" => MyClassifier.new | ||
| # } | ||
| # | ||
| # # Default is :clerk_token. Use :none only for local development without | ||
| # # incoming request authentication. Outgoing Braintrust API calls still need | ||
| # # normal Braintrust credentials. | ||
| # config.auth = :clerk_token | ||
| # end | ||
|
|
||
| # --------------------------------------------------------------------------- | ||
| # config/routes.rb | ||
| # --------------------------------------------------------------------------- | ||
|
|
||
| # Rails.application.routes.draw do | ||
| # mount Braintrust::Contrib::Rails::Server::Engine, at: "/braintrust" | ||
| # end | ||
|
|
||
| puts "Braintrust Rails Engine example — see comments for usage" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # This file was generated by Appraisal | ||
|
|
||
| source "https://rubygems.org" | ||
|
|
||
| gem "minitest-reporters", "~> 1.6" | ||
| gem "minitest-stub-const", "~> 0.6" | ||
| gem "climate_control", "~> 1.2" | ||
| gem "actionpack", "~> 8.0" | ||
| gem "railties", "~> 8.0" | ||
| gem "activesupport", "~> 8.0" | ||
| gem "rack", "~> 3.0" | ||
| gem "rack-test", "~> 2.1" | ||
|
|
||
| gemspec path: "../" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| begin | ||
| require "action_controller" | ||
| require "rails/engine" | ||
| rescue LoadError | ||
| raise LoadError, | ||
| "Rails (actionpack + railties) is required for the Braintrust Rails server engine. " \ | ||
| "Add `gem 'rails'` or `gem 'actionpack'` and `gem 'railties'` to your Gemfile." | ||
| end | ||
|
|
||
| require "json" | ||
| require_relative "../../eval" | ||
| require_relative "../../server/sse" | ||
| require_relative "../../server/auth/no_auth" | ||
| require_relative "../../server/auth/clerk_token" | ||
| require_relative "../../server/middleware/cors" | ||
| require_relative "../../server/services/list_service" | ||
| require_relative "../../server/services/eval_service" | ||
| require_relative "server/engine" |
34 changes: 34 additions & 0 deletions
34
lib/braintrust/contrib/rails/server/application_controller.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Braintrust | ||
| module Contrib | ||
| module Rails | ||
| module Server | ||
| class ApplicationController < ActionController::API | ||
| before_action :authenticate! | ||
|
|
||
| private | ||
|
|
||
| def authenticate! | ||
| auth_result = Engine.auth_strategy.authenticate(request.env) | ||
| unless auth_result | ||
| render json: {"error" => "Unauthorized"}, status: :unauthorized | ||
| return | ||
| end | ||
|
|
||
| request.env["braintrust.auth"] = auth_result | ||
| @braintrust_auth = auth_result | ||
| end | ||
|
|
||
| def parse_json_body | ||
| body = request.body.read | ||
| return nil if body.nil? || body.empty? | ||
| JSON.parse(body) | ||
| rescue JSON::ParserError | ||
| nil | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Braintrust | ||
| module Contrib | ||
| module Rails | ||
| module Server | ||
| class Engine < ::Rails::Engine | ||
| isolate_namespace Braintrust::Contrib::Rails::Server | ||
|
|
||
| config.evaluators = {} | ||
| config.auth = :clerk_token | ||
|
|
||
| # Register the engine's routes file so Rails loads it during initialization. | ||
| paths["config/routes.rb"] << File.expand_path("routes.rb", __dir__) | ||
|
|
||
| initializer "braintrust.server.cors" do |app| | ||
| app.middleware.use Braintrust::Server::Middleware::Cors | ||
| end | ||
|
|
||
| # Class-level helpers that read from engine config. | ||
|
|
||
| def self.evaluators | ||
| config.evaluators | ||
| end | ||
|
|
||
| def self.auth_strategy | ||
| resolve_auth(config.auth) | ||
| end | ||
|
|
||
| def self.list_service | ||
| Braintrust::Server::Services::List.new(-> { config.evaluators }) | ||
| end | ||
|
|
||
| # Long-lived so the state cache persists across requests. | ||
| def self.eval_service | ||
| @eval_service ||= Braintrust::Server::Services::Eval.new(-> { config.evaluators }) | ||
| end | ||
|
|
||
| # Support the explicit `|config|` style used by this integration while | ||
| # still delegating zero-arity DSL blocks to Rails' native implementation. | ||
| def self.configure(&block) | ||
| return super if block&.arity == 0 | ||
| yield config if block | ||
| end | ||
|
|
||
| def self.resolve_auth(auth) | ||
| case auth | ||
| when :none | ||
| Braintrust::Server::Auth::NoAuth.new | ||
| when :clerk_token | ||
| Braintrust::Server::Auth::ClerkToken.new | ||
| when Symbol, String | ||
| raise ArgumentError, "Unknown auth strategy #{auth.inspect}. Expected :none, :clerk_token, or an auth object." | ||
| else | ||
| auth | ||
| end | ||
| end | ||
| private_class_method :resolve_auth | ||
|
|
||
| generators do | ||
| require "braintrust/contrib/rails/server/generator" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| require_relative "application_controller" | ||
| require_relative "health_controller" | ||
| require_relative "list_controller" | ||
| require_relative "eval_controller" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Braintrust | ||
| module Contrib | ||
| module Rails | ||
| module Server | ||
| class EvalController < ApplicationController | ||
| include ActionController::Live | ||
|
|
||
| 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 | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.