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 12, 2024
2 parents 5747bb8 + 1974271 commit 0849161
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 12 deletions.
5 changes: 4 additions & 1 deletion app/controllers/admin/openai_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ class OpenaiRequestsController < Admin::BaseController
before_action :set_openai_request, only: [:show]

def index
@openai_requests = OpenaiRequest.page(page).per(per)
@openai_requests = OpenaiRequest
.order(updated_at: :desc)
.page(page)
.per(per)
end

def show
Expand Down
5 changes: 4 additions & 1 deletion app/jobs/openai_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ def perform openai_request_id
MatchingServices::Connect.new(instance: instance).perform do |on|
on.success do |response|
openai_request.update_columns(
error: nil,
response: response.to_json,
openai_assistant_id: response.metadata[:assistant_id],
openai_thread_id: response.metadata[:thread_id],
openai_run_id: response.metadata[:run_id],
Expand All @@ -31,7 +33,8 @@ def perform openai_request_id

on.failure do |error, response|
openai_request.update_columns(
response: response,
error: error,
response: response.to_json,
status: :error,
run_ends_at: Time.current,
updated_at: Time.current
Expand Down
4 changes: 3 additions & 1 deletion app/models/openai_request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ def formatted_response
end

def matching_response
@matching_response ||= MatchingServices::Response.new(response: response)
@matching_response ||= MatchingServices::Response.new(response: JSON.parse(response))
rescue
@matching_response ||= MatchingServices::Response.new(response: Hash.new)
end

attr_accessor :forced_matching
Expand Down
2 changes: 1 addition & 1 deletion app/serializers/openai/resource_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Openai
class ResourceSerializer < ActiveModel::Serializer
attributes :uuid_v2, :name, :is_video, :description
attributes :id, :name, :is_video, :description

def description
object.text_description_only
Expand Down
12 changes: 7 additions & 5 deletions app/services/matching_services/connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ def user_message
end

def get_formatted_prompt
instance_class = if instance.respond_to?(:action) && instance.action?
instance.contribution? ? 'contribution' : 'solicitation'
else
instance.class.name.camelize.downcase
action_type = opposite_action_type = instance.class.name.camelize.downcase

if instance.respond_to?(:action) && instance.action?
action_type = instance.contribution? ? 'contribution' : 'solicitation'
opposite_action_type = instance.contribution? ? 'solicitation' : 'contribution'
end

@configuration.prompt
.gsub("{{action_type}}", instance_class)
.gsub("{{action_type}}", action_type)
.gsub("{{opposite_action_type}}", opposite_action_type)
.gsub("{{name}}", instance.name)
.gsub("{{description}}", instance.description)
end
Expand Down
6 changes: 5 additions & 1 deletion app/services/matching_services/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ def parsed_response
JSON.parse(json)
end

def to_json
@response.to_json
end

def recommandations
return [] unless @parsed_response

Expand Down Expand Up @@ -63,7 +67,7 @@ def each_recommandation &block
recommandations.each_with_index do |recommandation, index|
next unless recommandation["id"]
next unless TYPES.include?(recommandation["type"])
next unless instance = recommandation["type"].classify.constantize.find_by_id_or_uuid(recommandation["id"])
next unless instance = recommandation["type"].classify.constantize.find_by_id(recommandation["id"])

yield(instance, recommandation["score"], recommandation["explanation"], index)
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/openai_requests/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@

<p>
<strong>Réponse:</strong>
<%= @openai_request.response %>
<pre wrap="true"><%= @openai_request.response %></pre>
</p>

<p>
<strong>Réponse formattée:</strong>
<%= @openai_request.formatted_response %>
<pre wrap="true"><%= @openai_request.formatted_response %></pre>
</p>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class AddResponseToOpenaiRequests < ActiveRecord::Migration[6.1]
def change
add_column :openai_requests, :response, :string
add_column :openai_requests, :error, :string
end
end
1 change: 1 addition & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,7 @@
t.datetime "updated_at", precision: 6, null: false
t.string "instance_class", default: "Entourage"
t.string "response"
t.string "error"
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 0849161

Please sign in to comment.