Class: LlmLogs::PromptsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/llm_logs/prompts_controller.rb

Constant Summary collapse

SORT_COLUMNS =
{ "name" => :name, "slug" => :slug, "updated" => :updated_at }.freeze

Instance Method Summary collapse

Instance Method Details

#createObject



26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/llm_logs/prompts_controller.rb', line 26

def create
  @prompt = Prompt.new(prompt_params)

  if @prompt.save
    if version_params[:messages].present?
      @prompt.update_content!(**version_params)
    end
    redirect_to prompt_path(@prompt), notice: "Prompt created."
  else
    render :new, status: :unprocessable_entity
  end
end

#destroyObject



57
58
59
60
61
# File 'app/controllers/llm_logs/prompts_controller.rb', line 57

def destroy
  @prompt = Prompt.find(params[:id])
  @prompt.destroy
  redirect_to prompts_path, notice: "Prompt deleted."
end

#editObject



39
40
41
42
# File 'app/controllers/llm_logs/prompts_controller.rb', line 39

def edit
  @prompt = Prompt.find(params[:id])
  @current_version = @prompt.current_version
end

#indexObject



5
6
7
8
9
10
11
12
13
14
# File 'app/controllers/llm_logs/prompts_controller.rb', line 5

def index
  tag = params[:tag].is_a?(String) ? params[:tag].presence : nil
  @sort      = SORT_COLUMNS.key?(params[:sort]) ? params[:sort] : "name"
  @direction = params[:direction] == "desc" ? "desc" : "asc"
  scope = Prompt.order(SORT_COLUMNS.fetch(@sort) => @direction.to_sym).includes(:versions)
  scope = scope.with_tag(tag) if tag
  @prompts    = scope.page(params[:page]).per(25)
  @active_tag = tag
  @all_tags   = Prompt.pluck(:tags).flatten.compact.uniq.sort
end

#newObject



22
23
24
# File 'app/controllers/llm_logs/prompts_controller.rb', line 22

def new
  @prompt = Prompt.new
end

#showObject



16
17
18
19
20
# File 'app/controllers/llm_logs/prompts_controller.rb', line 16

def show
  @prompt = Prompt.find(params[:id])
  @current_version = @prompt.current_version
  @versions = @prompt.versions.order(version_number: :desc).limit(5)
end

#updateObject



44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/controllers/llm_logs/prompts_controller.rb', line 44

def update
  @prompt = Prompt.find(params[:id])

  if @prompt.update(prompt_params)
    if version_params[:messages].present?
      @prompt.update_content!(**version_params)
    end
    redirect_to prompt_path(@prompt), notice: "Prompt updated."
  else
    render :edit, status: :unprocessable_entity
  end
end