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.
52 lines
1.3 KiB
Ruby
52 lines
1.3 KiB
Ruby
require 'openai'
|
|
|
|
class Captain::Copilot::ChatService < Llm::BaseOpenAiService
|
|
include Captain::ChatHelper
|
|
|
|
def initialize(assistant, config)
|
|
super()
|
|
|
|
@assistant = assistant
|
|
@conversation_history = config[:conversation_history]
|
|
@previous_messages = config[:previous_messages] || []
|
|
@language = config[:language] || 'english'
|
|
|
|
register_tools
|
|
@messages = [system_message, conversation_history_context] + @previous_messages
|
|
@response = ''
|
|
end
|
|
|
|
def generate_response(input)
|
|
@messages << { role: 'user', content: input } if input.present?
|
|
response = request_chat_completion
|
|
Rails.logger.info("[CAPTAIN][CopilotChatService] Incrementing response usage for #{@assistant.account.id}")
|
|
@assistant.account.increment_response_usage
|
|
|
|
response
|
|
end
|
|
|
|
private
|
|
|
|
def register_tools
|
|
@tool_registry = Captain::ToolRegistryService.new(@assistant)
|
|
@tool_registry.register_tool(Captain::Tools::SearchDocumentationService)
|
|
end
|
|
|
|
def system_message
|
|
{
|
|
role: 'system',
|
|
content: Captain::Llm::SystemPromptsService.copilot_response_generator(@assistant.config['product_name'], @language)
|
|
}
|
|
end
|
|
|
|
def conversation_history_context
|
|
{
|
|
role: 'system',
|
|
content: "
|
|
Message History with the user is below:
|
|
#{@conversation_history}
|
|
"
|
|
}
|
|
end
|
|
end
|