Migration Guide: https://chwt.app/v4/migration This PR imports all the work related to Captain into the EE codebase. Captain represents the AI-based features in Chatwoot and includes the following key components: - Assistant: An assistant has a persona, the product it would be trained on. At the moment, the data at which it is trained is from websites. Future integrations on Notion documents, PDF etc. This PR enables connecting an assistant to an inbox. The assistant would run the conversation every time before transferring it to an agent. - Copilot for Agents: When an agent is supporting a customer, we will be able to offer additional help to lookup some data or fetch information from integrations etc via copilot. - Conversation FAQ generator: When a conversation is resolved, the Captain integration would identify questions which were not in the knowledge base. - CRM memory: Learns from the conversations and identifies important information about the contact. --------- Co-authored-by: Vishnu Narayanan <vishnu@chatwoot.com> Co-authored-by: Sojan <sojan@pepalo.com> Co-authored-by: iamsivin <iamsivin@gmail.com> Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
102 lines
2.4 KiB
Ruby
102 lines
2.4 KiB
Ruby
require 'openai'
|
|
|
|
class Captain::Llm::AssistantChatService < Captain::Llm::BaseOpenAiService
|
|
def initialize(assistant: nil)
|
|
super()
|
|
|
|
@assistant = assistant
|
|
@messages = [system_message]
|
|
@response = ''
|
|
end
|
|
|
|
def generate_response(input, previous_messages = [], role = 'user')
|
|
@messages += previous_messages
|
|
@messages << { role: role, content: input } if input.present?
|
|
request_chat_completion
|
|
end
|
|
|
|
private
|
|
|
|
def system_message
|
|
{
|
|
role: 'system',
|
|
content: Captain::Llm::SystemPromptsService.assistant_response_generator(@assistant.config['product_name'])
|
|
}
|
|
end
|
|
|
|
def search_documentation_tool
|
|
{
|
|
type: 'function',
|
|
function: {
|
|
name: 'search_documentation',
|
|
description: "Use this function to get documentation on functionalities you don't know about.",
|
|
parameters: {
|
|
type: 'object',
|
|
properties: {
|
|
search_query: {
|
|
type: 'string',
|
|
description: 'The search query to look up in the documentation.'
|
|
}
|
|
},
|
|
required: ['search_query']
|
|
}
|
|
}
|
|
}
|
|
end
|
|
|
|
def request_chat_completion
|
|
response = @client.chat(
|
|
parameters: {
|
|
model: DEFAULT_MODEL,
|
|
messages: @messages,
|
|
tools: [search_documentation_tool],
|
|
response_format: { type: 'json_object' }
|
|
}
|
|
)
|
|
|
|
handle_response(response)
|
|
@response
|
|
end
|
|
|
|
def handle_response(response)
|
|
message = response.dig('choices', 0, 'message')
|
|
|
|
if message['tool_calls']
|
|
process_tool_calls(message['tool_calls'])
|
|
else
|
|
@response = JSON.parse(message['content'].strip)
|
|
end
|
|
end
|
|
|
|
def process_tool_calls(tool_calls)
|
|
process_tool_call(tool_calls.first)
|
|
end
|
|
|
|
def process_tool_call(tool_call)
|
|
return unless tool_call['function']['name'] == 'search_documentation'
|
|
|
|
query = JSON.parse(tool_call['function']['arguments'])['search_query']
|
|
sections = fetch_documentation(query)
|
|
append_tool_response(sections)
|
|
request_chat_completion
|
|
end
|
|
|
|
def fetch_documentation(query)
|
|
@assistant
|
|
.responses
|
|
.search(query)
|
|
.map { |response| format_response(response) }.join
|
|
end
|
|
|
|
def format_response(response)
|
|
"\n\nQuestion: #{response[:question]}\nAnswer: #{response[:answer]}"
|
|
end
|
|
|
|
def append_tool_response(sections)
|
|
@messages << {
|
|
role: 'assistant',
|
|
content: "Found the following FAQs in the documentation:\n #{sections}"
|
|
}
|
|
end
|
|
end
|