49 lines
1.5 KiB
Ruby
49 lines
1.5 KiB
Ruby
|
|
class Captain::Llm::AssistantChatService < Llm::BaseAiService
|
||
|
|
include Captain::ChatHelper
|
||
|
|
|
||
|
|
def initialize(assistant: nil, conversation_id: nil)
|
||
|
|
super()
|
||
|
|
|
||
|
|
@assistant = assistant
|
||
|
|
@conversation_id = conversation_id
|
||
|
|
|
||
|
|
@messages = [system_message]
|
||
|
|
@response = ''
|
||
|
|
@tools = build_tools
|
||
|
|
end
|
||
|
|
|
||
|
|
# additional_message: A single message (String) from the user that should be appended to the chat.
|
||
|
|
# It can be an empty String or nil when you only want to supply historical messages.
|
||
|
|
# message_history: An Array of already formatted messages that provide the previous context.
|
||
|
|
# role: The role for the additional_message (defaults to `user`).
|
||
|
|
#
|
||
|
|
# NOTE: Parameters are provided as keyword arguments to improve clarity and avoid relying on
|
||
|
|
# positional ordering.
|
||
|
|
def generate_response(additional_message: nil, message_history: [], role: 'user')
|
||
|
|
@messages += message_history
|
||
|
|
@messages << { role: role, content: additional_message } if additional_message.present?
|
||
|
|
request_chat_completion
|
||
|
|
end
|
||
|
|
|
||
|
|
private
|
||
|
|
|
||
|
|
def build_tools
|
||
|
|
[Captain::Tools::SearchDocumentationService.new(@assistant, user: nil)]
|
||
|
|
end
|
||
|
|
|
||
|
|
def system_message
|
||
|
|
{
|
||
|
|
role: 'system',
|
||
|
|
content: Captain::Llm::SystemPromptsService.assistant_response_generator(@assistant.name, @assistant.config['product_name'], @assistant.config)
|
||
|
|
}
|
||
|
|
end
|
||
|
|
|
||
|
|
def persist_message(message, message_type = 'assistant')
|
||
|
|
# No need to implement
|
||
|
|
end
|
||
|
|
|
||
|
|
def feature_name
|
||
|
|
'assistant'
|
||
|
|
end
|
||
|
|
end
|