Skip to content

Commit

Permalink
EN-7354 add openai_request.module_type to allow to work with differen…
Browse files Browse the repository at this point in the history
…t types of assistants
  • Loading branch information
nicolas-entourage committed Dec 17, 2024
1 parent 198790e commit 47c98cd
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
2 changes: 1 addition & 1 deletion app/jobs/openai_request_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def perform openai_request_id

return unless instance = openai_request.instance

OpenaiServices::MatchingPerformer.new(instance: instance).perform do |on|
OpenaiServices::MatchingPerformer.new(openai_request: openai_request).perform do |on|
on.success do |response|
openai_request.update_columns(
error: nil,
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/matchable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def on_save
def ensure_openai_request_exists!
return if @instance.openai_request

@instance.build_openai_request.save!
@instance.build_openai_request(module_type: :matching).save!
end
end
end
15 changes: 5 additions & 10 deletions app/services/openai_services/basic_performer.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
module OpenaiServices
class BasicPerformer
attr_reader :configuration, :client, :callback, :assistant_id, :instance
attr_reader :configuration, :client, :callback, :assistant_id, :openai_request, :instance

class BasicPerformerCallback < Callback
end

def initialize instance:
def initialize openai_request:
@openai_request = openai_request
@instance = @openai_request.instance
@callback = BasicPerformerCallback.new

@configuration = get_configuration
@configuration = OpenaiAssistant.find_by_module_type(@openai_request.module_type)

@client = OpenAI::Client.new(access_token: @configuration.api_key)
@assistant_id = @configuration.assistant_id

@instance = instance
end

def perform
Expand Down Expand Up @@ -71,11 +71,6 @@ def find_run_message thread_id, run_id

private

# OpenaiAssistant.find_by_version(?)
def get_configuration
raise NotImplementedError, "this method get_configuration has to be defined in your class"
end

# format: { role: string, content: { type: "text", text: string }}
def user_message
raise NotImplementedError, "this method user_message has to be defined in your class"
Expand Down
14 changes: 4 additions & 10 deletions app/services/openai_services/matching_performer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,6 @@ class MatchingPerformer < BasicPerformer
class MatcherCallback < Callback
end

def initialize instance:
super(instance: instance)

@user = instance.user
end

def get_configuration
OpenaiAssistant.find_by_module_type(:matching)
end

def user_message
{
role: "user",
Expand All @@ -31,6 +21,10 @@ def get_response_class

private

def user
@user ||= instance.user
end

def get_formatted_prompt
action_type = opposite_action_type = instance.class.name.camelize.downcase

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddModuleTypeToOpenaiRequests < ActiveRecord::Migration[6.1]
def change
add_column :openai_requests, :module_type, :string, default: :matching
end
end
4 changes: 4 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@
t.integer "days_for_outings", default: 30
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "module_type", default: "matching"
t.integer "max_prompt_tokens", default: 1048576
t.integer "max_completion_tokens", default: 1024
end

create_table "openai_requests", force: :cascade do |t|
Expand All @@ -734,6 +737,7 @@
t.string "instance_class", default: "Entourage"
t.string "response"
t.string "error"
t.string "module_type", default: "matching"
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 47c98cd

Please sign in to comment.