feat: simplufy execute_ruby_llm_request chain

This commit is contained in:
Shivam Mishra
2025-12-17 18:15:57 +05:30
parent 9c76cde654
commit 9589050ea8
+13 -31
View File
@@ -41,39 +41,25 @@ class Captain::BaseEditorService
def execute_ruby_llm_request(model:, messages:)
Llm::Config.with_api_key(api_key, api_base: api_base) do |context|
chat = context.chat(model: model)
setup_chat_with_messages(chat, messages)
system_msg = messages.find { |m| m[:role] == 'system' }
chat.with_instructions(system_msg[:content]) if system_msg
conversation_messages = messages.reject { |m| m[:role] == 'system' }
return { error: 'No conversation messages provided', error_code: 400, request_messages: messages } if conversation_messages.empty?
add_messages_if_needed(chat, conversation_messages)
response = chat.ask(conversation_messages.last[:content])
build_ruby_llm_response(response, messages)
end
rescue StandardError => e
ChatwootExceptionTracker.new(e, account: account).capture_exception
build_error_response_from_exception(e, messages)
{ error: e.message, request_messages: messages }
end
def setup_chat_with_messages(chat, messages)
apply_system_instructions(chat, messages)
response = send_conversation_messages(chat, messages)
return { error: 'No conversation messages provided', error_code: 400, request_messages: messages } if response.nil?
def add_messages_if_needed(chat, conversation_messages)
return if conversation_messages.length == 1
build_ruby_llm_response(response, messages)
end
def apply_system_instructions(chat, messages)
system_msg = messages.find { |m| m[:role] == 'system' }
chat.with_instructions(system_msg[:content]) if system_msg
end
def send_conversation_messages(chat, messages)
conversation_messages = messages.reject { |m| m[:role] == 'system' }
return nil if conversation_messages.empty?
return chat.ask(conversation_messages.first[:content]) if conversation_messages.length == 1
add_conversation_history(chat, conversation_messages[0...-1])
chat.ask(conversation_messages.last[:content])
end
def add_conversation_history(chat, messages)
messages.each do |msg|
conversation_messages[0...-1].each do |msg|
chat.add_message(role: msg[:role].to_sym, content: msg[:content])
end
end
@@ -102,10 +88,6 @@ class Captain::BaseEditorService
}
end
def build_error_response_from_exception(error, messages)
{ error: error.message, request_messages: messages }
end
def conversation_messages(start_from: 0)
messages = []
character_count = start_from