forked from joakimwinum/chatbot.rb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
130 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
class MagellanRagQueryJob < SlackResponseJob | ||
queue_as :default | ||
|
||
VALID_MODELS = [ | ||
# OpenAI API | ||
"gpt-4o".freeze, | ||
] | ||
|
||
DEFAULT_MODEL = ENV.fetch("DEFAULT_MODEL", VALID_MODELS[0]) | ||
|
||
class InvalidOptionError < StandardError; end | ||
|
||
Options = Struct.new( | ||
:model, | ||
keyword_init: true | ||
) do | ||
def validate! | ||
validate_model! unless model.nil? | ||
end | ||
|
||
def validate_model! | ||
MagellanRagQueryJob::VALID_MODELS.each do |valid_model| | ||
case valid_model | ||
when Regexp | ||
return if valid_model.match?(model) | ||
else | ||
return if model == valid_model | ||
end | ||
end | ||
raise InvalidOptionError, "Invalid model is specified: #{model}" | ||
end | ||
end | ||
|
||
def model_for_message(message) | ||
message.conversation.model || DEFAULT_MODEL | ||
end | ||
|
||
def perform(params) | ||
if params["message_id"].blank? | ||
logger.warn "Empty message_id is given" | ||
return | ||
end | ||
|
||
message = Message.find(params["message_id"]) | ||
if message.blank? | ||
logger.warn "Unable to find Message with id=#{message_id}" | ||
return | ||
end | ||
|
||
if message.query.present? | ||
logger.warn "Message with id=#{message.id} already has its query and response" | ||
return | ||
end | ||
|
||
options = Options.new(**params.fetch("options", {})) | ||
|
||
begin | ||
start_response(message) | ||
process_query(message, options) | ||
ensure | ||
finish_response(message) | ||
end | ||
rescue Exception => error | ||
logger.error "ERROR: #{error.message}" | ||
raise unless Rails.env.production? | ||
end | ||
|
||
private def process_query(message, options) | ||
# TODO: MAGELLAN RAG | ||
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