35 lines
907 B
Ruby
35 lines
907 B
Ruby
|
|
class Captain::Tools::SearchDocumentationService < Captain::Tools::BaseTool
|
||
|
|
def self.name
|
||
|
|
'search_documentation'
|
||
|
|
end
|
||
|
|
description 'Search and retrieve documentation from knowledge base'
|
||
|
|
|
||
|
|
param :query, desc: 'Search Query', required: true
|
||
|
|
|
||
|
|
def execute(query:)
|
||
|
|
Rails.logger.info { "#{self.class.name}: #{query}" }
|
||
|
|
|
||
|
|
responses = assistant.responses.approved.search(query)
|
||
|
|
|
||
|
|
return 'No FAQs found for the given query' if responses.empty?
|
||
|
|
|
||
|
|
responses.map { |response| format_response(response) }.join
|
||
|
|
end
|
||
|
|
|
||
|
|
private
|
||
|
|
|
||
|
|
def format_response(response)
|
||
|
|
formatted_response = "
|
||
|
|
Question: #{response.question}
|
||
|
|
Answer: #{response.answer}
|
||
|
|
"
|
||
|
|
if response.documentable.present? && response.documentable.try(:external_link)
|
||
|
|
formatted_response += "
|
||
|
|
Source: #{response.documentable.external_link}
|
||
|
|
"
|
||
|
|
end
|
||
|
|
|
||
|
|
formatted_response
|
||
|
|
end
|
||
|
|
end
|