-
-
Notifications
You must be signed in to change notification settings - Fork 197
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
Admin insights #8460
Draft
gbp
wants to merge
4
commits into
develop
Choose a base branch
from
admin-insights
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Admin insights #8460
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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 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 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,48 @@ | ||
## | ||
# Controller for running AI insights tasks from the admin UI | ||
# | ||
class Admin::InsightsController < AdminController | ||
before_action :find_info_request | ||
before_action :find_insight, only: [:show, :destroy] | ||
|
||
def show | ||
end | ||
|
||
def new | ||
last = Insight.last | ||
@insight = @info_request.insights.new( | ||
model: last&.model, temperature: last&.temperature || 0.5, | ||
template: last&.template | ||
) | ||
end | ||
|
||
def create | ||
@insight = @info_request.insights.new(insight_params) | ||
if @insight.save | ||
redirect_to admin_info_request_insight_path(@info_request, @insight), | ||
notice: 'Insight was successfully created.' | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def destroy | ||
@insight.destroy | ||
redirect_to admin_request_path(@info_request), | ||
notice: 'Insight was successfully deleted.' | ||
end | ||
|
||
private | ||
|
||
def find_info_request | ||
@info_request = InfoRequest.find(params[:info_request_id]) | ||
end | ||
|
||
def find_insight | ||
@insight = @info_request.insights.find(params[:id]) | ||
end | ||
|
||
def insight_params | ||
params.require(:insight).permit(:model, :temperature, :template) | ||
end | ||
end |
This file contains 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,30 @@ | ||
## | ||
# InsightJob is responsible for generating InfoRequest insights using an AI | ||
# model run via Ollama. | ||
# | ||
class InsightJob < ApplicationJob | ||
queue_as :insights | ||
|
||
delegate :model, :temperature, :prompt, to: :@insight | ||
|
||
def perform(insight) | ||
@insight = insight | ||
|
||
insight.update(output: results.first) | ||
end | ||
|
||
private | ||
|
||
def results | ||
client.generate( | ||
{ model: model, prompt: prompt, temperature: temperature, stream: false } | ||
) | ||
end | ||
|
||
def client | ||
Ollama.new( | ||
credentials: { address: ENV['OLLAMA_URL'] }, | ||
options: { server_sent_events: true } | ||
) | ||
end | ||
end |
This file contains 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 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,52 @@ | ||
# == Schema Information | ||
# Schema version: 20241024140606 | ||
# | ||
# Table name: insights | ||
# | ||
# id :bigint not null, primary key | ||
# info_request_id :bigint | ||
# model :string | ||
# temperature :decimal(8, 2) | ||
# template :text | ||
# output :jsonb | ||
# created_at :datetime not null | ||
# updated_at :datetime not null | ||
# | ||
class Insight < ApplicationRecord | ||
admin_columns exclude: [:template, :output], | ||
include: [:duration, :prompt, :response] | ||
|
||
after_commit :queue, on: :create | ||
|
||
belongs_to :info_request, optional: false | ||
has_many :outgoing_messages, through: :info_request | ||
|
||
serialize :output, type: Hash, coder: JSON, default: {} | ||
|
||
validates :model, presence: true | ||
validates :temperature, presence: true | ||
validates :template, presence: true | ||
|
||
def duration | ||
return unless output['total_duration'] | ||
|
||
seconds = output['total_duration'].to_f / 1_000_000_000 | ||
ActiveSupport::Duration.build(seconds.to_i).inspect | ||
end | ||
|
||
def prompt | ||
template.gsub('[initial_request]') do | ||
outgoing_messages.first.body[0...500] | ||
end | ||
end | ||
|
||
def response | ||
output['response'] | ||
end | ||
|
||
private | ||
|
||
def queue | ||
InsightJob.perform_later(self) | ||
end | ||
end |
This file contains 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 @@ | ||
<div class="row"> | ||
<% if insights.any? %> | ||
<table class="table table-condensed table-hover span12"> | ||
<tr> | ||
<th>ID</th> | ||
<th>Model</th> | ||
<th>Template</th> | ||
<th>Created at</th> | ||
<th>Updated at</th> | ||
<th>Actions</th> | ||
</tr> | ||
|
||
<% insights.each do |insight| %> | ||
<tr class="<%= cycle('odd', 'even') %>"> | ||
<td class="id"><%= insight.to_param %></td> | ||
<td class="model"><%= insight.model %></td> | ||
<td class="temperature"><%= insight.temperature %></td> | ||
<td class="template"><%= truncate(insight.template, length: 150) %></td> | ||
<td class="created_at"><%= admin_date(insight.created_at) %></td> | ||
<td class="updated_at"><%= admin_date(insight.updated_at) %></td> | ||
<td><%= link_to "Show", admin_info_request_insight_path(info_request, insight), class: 'btn' %></td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
<% else %> | ||
<p class="span12">None yet.</p> | ||
<% end %> | ||
</div> | ||
|
||
<div class="row"> | ||
<p class="span12"> | ||
<%= link_to "New insight", new_admin_info_request_insight_path(info_request), :class => "btn btn-info" %> | ||
</p> | ||
</div> |
This file contains 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,45 @@ | ||
<% @title = 'New insight' %> | ||
|
||
<div class="row"> | ||
<div class="span12"> | ||
<div class="page-header"> | ||
<h1><%= @title %></h1> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<%= form_for [:admin, @info_request, @insight], html: { class: 'form form-horizontal' } do |f| %> | ||
<%= foi_error_messages_for :insight %> | ||
|
||
<div class="control-group"> | ||
<%= f.label :model, class: 'control-label' %> | ||
<div class="controls"> | ||
<%= f.text_field :model, class: 'span6' %> | ||
</div> | ||
</div> | ||
|
||
<div class="control-group"> | ||
<%= f.label :temperature, class: 'control-label' %> | ||
<div class="controls"> | ||
<%= f.range_field :temperature, min: 0, max: 1, step: 0.1, class: 'span6', autocomplete: 'off', oninput: 'insight_temperature_display.value = insight_temperature.value' %> | ||
<%= content_tag(:output, @insight.temperature, id: 'insight_temperature_display') %> | ||
</div> | ||
</div> | ||
|
||
<div class="control-group"> | ||
<%= f.label :template, class: 'control-label' %> | ||
<div class="controls"> | ||
<%= f.text_area :template, class: 'span6', rows: 10 %> | ||
|
||
<div class="help-block"> | ||
Add <strong>[initial_request]</strong> to substitute in the first 500 | ||
characters from the body of the initial outgoing message into the prompt | ||
sent to the model. | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="form-actions"> | ||
<%= submit_tag 'Create', class: 'btn btn-success' %> | ||
</div> | ||
<% end %> |
This file contains 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,28 @@ | ||
<table class="table table-striped table-condensed"> | ||
<tbody> | ||
<tr> | ||
<td> | ||
<b>ID</b> | ||
</td> | ||
<td> | ||
<%= @insight.id %> | ||
</td> | ||
</tr> | ||
<% @insight.for_admin_column do |name, value| %> | ||
<tr> | ||
<td> | ||
<b><%= name.humanize %></b> | ||
</td> | ||
<td> | ||
<% if name == 'prompt' || name == 'response' %> | ||
<pre><%= value&.strip %></pre> | ||
<% else %> | ||
<%= h admin_value(value) %> | ||
<% end %> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
|
||
<%= link_to "Destroy", admin_info_request_insight_path(@info_request, @insight), method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %> |
This file contains 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 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 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,13 @@ | ||
class CreateInsights < ActiveRecord::Migration[7.0] | ||
def change | ||
create_table :insights do |t| | ||
t.references :info_request, foreign_key: true | ||
t.string :model | ||
t.decimal :temperature, precision: 8, scale: 2 | ||
t.text :template | ||
t.jsonb :output | ||
|
||
t.timestamps | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.