Class: LangsmithrbRails::Generators::DemoGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/langsmithrb_rails/demo/demo_generator.rb

Overview

Generator for adding a demo LLM application with LangSmith tracing

Instance Method Summary collapse

Instance Method Details

#check_dependenciesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 14

def check_dependencies
  # Check if the required gems are installed
  unless options[:provider] == "mock"
    gem_name = options[:provider] == "anthropic" ? "anthropic" : "ruby-openai"
    
    begin
      require options[:provider] == "anthropic" ? "anthropic" : "openai"
    rescue LoadError
      say_status :error, "#{gem_name} gem is required for this generator", :red
      say "Please add the following to your Gemfile and run bundle install:", :yellow
      say "gem '#{gem_name}'", :yellow
      exit 1
    end
  end
end

#create_controllerObject



34
35
36
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 34

def create_controller
  template "chat_controller.rb", "app/controllers/chat_controller.rb"
end

#create_javascriptObject



55
56
57
58
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 55

def create_javascript
  empty_directory "app/javascript/controllers"
  template "chat_controller.js", "app/javascript/controllers/chat_controller.js"
end

#create_migrationObject



47
48
49
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 47

def create_migration
  migration_template "create_chat_messages.rb", "db/migrate/create_chat_messages.rb"
end

#create_modelObject



43
44
45
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 43

def create_model
  template "chat_message.rb", "app/models/chat_message.rb"
end

#create_serviceObject



30
31
32
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 30

def create_service
  template "llm_service.rb", "app/services/llm_service.rb"
end

#create_viewsObject



38
39
40
41
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 38

def create_views
  empty_directory "app/views/chat"
  template "index.html.erb", "app/views/chat/index.html.erb"
end

#display_post_install_messageObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 60

def display_post_install_message
  say "\n"
  say "LangSmith demo application has been added to your Rails application! 🎉", :green
  say "\n"
  say "This adds:", :yellow
  say "  1. LLM service with LangSmith tracing", :yellow
  say "  2. Chat controller and views", :yellow
  say "  3. Chat message model and migration", :yellow
  say "\n"
  say "To set up the demo:", :yellow
  say "  1. Run the migration: bin/rails db:migrate", :yellow
  say "  2. Configure your LLM provider API key in your environment", :yellow
  say "  3. Start your Rails server: bin/rails server", :yellow
  say "  4. Visit http://localhost:3000/chat", :yellow
  say "\n"
  say "For OpenAI, set OPENAI_API_KEY in your environment", :yellow
  say "For Anthropic, set ANTHROPIC_API_KEY in your environment", :yellow
  say "\n"
end

#update_routesObject



51
52
53
# File 'lib/generators/langsmithrb_rails/demo/demo_generator.rb', line 51

def update_routes
  route "resources :chat, only: [:index, :create]"
end