This PR introduces the concept of a tool registry. The implementation is straightforward: you can define a tool by creating a class with a function name. The function name gets registered in the registry and can be referenced during LLM calls. When the LLM invokes a tool using the registered name, the registry locates and executes the appropriate tool. If the LLM calls an unregistered tool, the registry returns an error indicating that the tool is not defined.
50 lines
1.1 KiB
Ruby
50 lines
1.1 KiB
Ruby
class Captain::Tools::SearchDocumentationService < Captain::Tools::BaseService
|
|
def name
|
|
'search_documentation'
|
|
end
|
|
|
|
def description
|
|
'Search and retrieve documentation from knowledge base'
|
|
end
|
|
|
|
def parameters
|
|
{
|
|
type: 'object',
|
|
properties: {
|
|
search_query: {
|
|
type: 'string',
|
|
description: 'The search query to look up in the documentation.'
|
|
}
|
|
},
|
|
required: ['search_query']
|
|
}
|
|
end
|
|
|
|
def execute(arguments)
|
|
query = arguments['search_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
|