Skip to content

Commit

Permalink
Merge branch 'EN-7354-openai-matching-clean' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-entourage committed Dec 11, 2024
2 parents bb261ac + 5b39def commit 5747bb8
Show file tree
Hide file tree
Showing 12 changed files with 176 additions and 6 deletions.
1 change: 0 additions & 1 deletion Procfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ web_backoffice: bundle exec puma -C config/puma_backoffice.rb

worker_1: bundle exec sidekiq -c ${SIDEKIQ_CONCURRENCY:-2} -q sms -q salesforce -q translation
worker_2: bundle exec sidekiq -c ${SIDEKIQ_CONCURRENCY:-10} -q mailers -q default -q openai_requests -q broadcast -q denorm

release: ACTIVERECORD_STATEMENT_TIMEOUT=90s bundle exec rake db:migrate
2 changes: 1 addition & 1 deletion app/controllers/admin/actions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
@params = params.permit([:area, :search]).to_h
@area = params[:area].presence&.to_sym || :all

@actions = Action.includes([:user, matchings: :match])
@actions = Action.includes([:user, :openai_request, matchings: :match])
@actions = @actions.search_by(params[:search]) if params[:search].present?
@actions = @actions.with_moderation_area(@area.to_s) if @area && @area != :all
@actions = @actions.page(page).per(per)
Expand Down
28 changes: 28 additions & 0 deletions app/controllers/admin/openai_requests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Admin
class OpenaiRequestsController < Admin::BaseController
layout 'admin_large'

before_action :set_openai_request, only: [:show]

def index
@openai_requests = OpenaiRequest.page(page).per(per)
end

def show
end

private

def set_openai_request
@openai_request = OpenaiRequest.find(params[:id])
end

def page
params[:page] || 1
end

def per
params[:per] || 25
end
end
end
5 changes: 3 additions & 2 deletions app/jobs/openai_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def perform openai_request_id
openai_thread_id: response.metadata[:thread_id],
openai_run_id: response.metadata[:run_id],
openai_message_id: response.metadata[:message_id],
status: nil,
status: :success,
run_ends_at: Time.current,
updated_at: Time.current
)
Expand All @@ -29,8 +29,9 @@ def perform openai_request_id
instance.save(validate: false)
end

on.failure do |error|
on.failure do |error, response|
openai_request.update_columns(
response: response,
status: :error,
run_ends_at: Time.current,
updated_at: Time.current
Expand Down
28 changes: 28 additions & 0 deletions app/models/openai_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ def instance
instance_class.constantize.find(instance_id)
end

def instance_baseclass
instance_class.constantize.base_class.find(instance_id)
end

def error?
status.to_s == "error"
end

def assistant_link
"https://platform.openai.com/playground/assistants?assistant=#{openai_assistant_id}"
end

def thread_link
"https://platform.openai.com/threads/#{openai_thread_id}"
end

def response_valid?
matching_response.valid?
end

def formatted_response
matching_response.parsed_response
end

def matching_response
@matching_response ||= MatchingServices::Response.new(response: response)
end

attr_accessor :forced_matching

def run
Expand Down
4 changes: 2 additions & 2 deletions app/services/matching_services/connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def perform

response = Response.new(response: find_run_message(thread['id'], run['id']))

return callback.on_failure.try(:call, "Response not valid") unless response.valid?
return callback.on_failure.try(:call, "Response not valid", response) unless response.valid?

callback.on_success.try(:call, response)
rescue => e
callback.on_failure.try(:call, e.message)
callback.on_failure.try(:call, e.message, nil)
end

private
Expand Down
6 changes: 6 additions & 0 deletions app/views/admin/actions/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<th>Date de création</th>
<th>Statut</th>
<th>Matchings</th>
<th>Matchings status</th>
</thead>
<% @actions.each_with_index do |action, i| %>
<% parite = i.even? ? 'pair' : 'impair' %>
Expand All @@ -52,6 +53,11 @@
</p>
<% end %>
</td>
<td>
<% if action.openai_request %>
<%= link_to action.openai_request.status, admin_openai_request_path(action.openai_request) %>
<% end %>
</td>
</tr>
<% end %>
</table>
Expand Down
29 changes: 29 additions & 0 deletions app/views/admin/openai_requests/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div class="container">
<div style="margin: auto">
<h1>openai_requests</h1>

<div class="row">
<% unless @openai_requests.none? %>
<table class="table custom-table">
<thead>
<th>Nom</th>
<th>Créé le</th>
</thead>
<% @openai_requests.each_with_index do |openai_request, i| %>
<% parite = i.even? ? 'pair' : 'impair' %>

<tr class="above-row row-<%= parite %>">
<td><%= link_to openai_request.instance.name, admin_openai_request_path(openai_request) %></td>
<td><%= l openai_request.created_at %></td>
</tr>
<% end %>
</table>
<% end %>
</div>

<%= page_entries_info @openai_requests, entry_name: 'openai_requests' %>
<div class="container">
<%= paginate(@openai_requests) %>
</div>
</div>
</div>
72 changes: 72 additions & 0 deletions app/views/admin/openai_requests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<div class="container">
<div class="row">
<div class="col-xs-offset-2 col-xs-8">
<ol class="breadcrumb">
<li><%= link_to "OpenAI requests", admin_openai_requests_path %></li>
<li><%= @openai_request.instance.name %></li>
</ol>

<div class="panel panel-default">
<div class="panel-heading">
<%= link_to @openai_request.instance.name, [:admin, @openai_request.instance_baseclass] %>
</div>

<div class="panel-body">
<p>
<% if @openai_request.error? %>
<span class="label label-danger">error</span>
<% else %>
<span class="label label-info">success</span>
<% end %>
</p>

<p>
<strong>Assistant OpenAI</strong>
<%= link_to @openai_request.openai_assistant_id, @openai_request.assistant_link %>
</p>

<p>
<strong>Thread OpenAI</strong>
<%= link_to @openai_request.openai_thread_id, @openai_request.thread_link %>
</p>

<p>
<strong>Instance type</strong>
<%= @openai_request.instance_type %>
</p>

<p>
<strong>Instance id</strong>
<%= @openai_request.instance_id %>
</p>

<p>
<strong>Lancé le:</strong>
<%= l @openai_request.run_starts_at if @openai_request.run_starts_at %>
</p>

<p>
<strong>Fini le:</strong>
<%= l @openai_request.run_ends_at if @openai_request.run_ends_at %>
</p>

<p>
<strong>Validité réponse:</strong>
<%= @openai_request.response_valid? %>
</p>

<p>
<strong>Réponse:</strong>
<%= @openai_request.response %>
</p>

<p>
<strong>Réponse formattée:</strong>
<%= @openai_request.formatted_response %>
</p>
</div>
</div>

</div>
</div>
</div>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@
end

resources :openai_assistants, only: [:index, :edit, :update]
resources :openai_requests, only: [:index, :show]

resources :recommandations do
member do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20241211144500_add_response_to_openai_requests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddResponseToOpenaiRequests < ActiveRecord::Migration[6.1]
def change
add_column :openai_requests, :response, :string
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,7 @@
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "instance_class", default: "Entourage"
t.string "response"
t.index ["instance_type", "instance_id"], name: "index_openai_requests_on_instance_type_and_instance_id", unique: true
end

Expand Down

0 comments on commit 5747bb8

Please sign in to comment.