Class: LangsmithrbRails::Generators::LangsmithGenerator

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

Overview

Generator for adding LangSmith support to a Rails application

Instance Method Summary collapse

Instance Method Details

#create_initializerObject



12
13
14
# File 'lib/langsmithrb_rails/generators/langsmithrb_rails/langsmith_generator.rb', line 12

def create_initializer
  template "langsmith_initializer.rb", "config/initializers/langsmith.rb"
end

#display_post_install_messageObject



51
52
53
54
55
56
57
58
# File 'lib/langsmithrb_rails/generators/langsmithrb_rails/langsmith_generator.rb', line 51

def display_post_install_message
  say "\nšŸŽ‰ LangSmith support has been added to your Rails application!", :green
  say "\nTo enable LangSmith tracing, add the following to your .env file:", :yellow
  say "LANGSMITH_API_KEY=your_api_key", :yellow
  say "LANGSMITH_PROJECT=your_project_name (optional)", :yellow
  say "\nYou can get your API key from https://smith.langchain.com/", :yellow
  say "\nRestart your Rails server to apply the changes.", :yellow
end

#update_env_exampleObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/langsmithrb_rails/generators/langsmithrb_rails/langsmith_generator.rb', line 16

def update_env_example
  create_file ".env.example" unless File.exist?(".env.example")
  
  # Check if LangSmith config is already in the file
  env_content = File.exist?(".env.example") ? File.read(".env.example") : ""
  return if env_content.include?("LANGSMITH_API_KEY")
  
  append_to_file ".env.example" do
    # Add a newline if the file doesn't end with one
    (env_content.end_with?("\n") ? "" : "\n") + 
    <<~ENV
      # LangSmith configuration
      LANGSMITH_API_KEY=
      LANGSMITH_PROJECT=
    ENV
  end
end

#update_gitignoreObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/langsmithrb_rails/generators/langsmithrb_rails/langsmith_generator.rb', line 34

def update_gitignore
  create_file ".gitignore" unless File.exist?(".gitignore")
  
  # Check if .env is already in gitignore
  gitignore_content = File.exist?(".gitignore") ? File.read(".gitignore") : ""
  return if gitignore_content.match?(/^\.env\s*$/)
  
  append_to_file ".gitignore" do
    # Add a newline if the file doesn't end with one
    (gitignore_content.end_with?("\n") ? "" : "\n") + 
    <<~GITIGNORE
      # LangSmith
      .env
    GITIGNORE
  end
end