This PR refactors the schema we introduced in #7518 based on the feedback from production tests. Here is the change log - Decouple Inbox association to a new table inbox_response_sources -> this lets us share the same response source between multiple inboxes - Add a status field to responses. This ensures that, by default, responses are created in pending status. You can do quality assurance before making them active. In future, this status can be leveraged by the bot to auto-generate response questions from conversations which require a handoff - Add response_source association to responses and remove hard dependency from response_documents. This lets users write free-form question answers based on conversations, which doesn't necessarily need a response source.
83 lines
2.2 KiB
Ruby
83 lines
2.2 KiB
Ruby
class ResponseBuilderJob < ApplicationJob
|
|
queue_as :default
|
|
|
|
def perform(response_document)
|
|
reset_previous_responses(response_document)
|
|
data = prepare_data(response_document)
|
|
response = post_request(data)
|
|
create_responses(response, response_document)
|
|
end
|
|
|
|
private
|
|
|
|
def reset_previous_responses(response_document)
|
|
response_document.responses.destroy_all
|
|
end
|
|
|
|
def prepare_data(response_document)
|
|
{
|
|
model: 'gpt-3.5-turbo',
|
|
messages: [
|
|
{
|
|
role: 'system',
|
|
content: system_message_content
|
|
},
|
|
{
|
|
role: 'user',
|
|
content: response_document.content
|
|
}
|
|
]
|
|
}
|
|
end
|
|
|
|
def system_message_content
|
|
<<~SYSTEM_MESSAGE_CONTENT
|
|
You are a content writer looking to convert user content into short FAQs which can be added to your website's helper centre.
|
|
Format the webpage content provided in the message to FAQ format like the following example.#{' '}
|
|
Ensure that you only generate faqs from the information provider in the message.#{' '}
|
|
Ensure that output is always valid json.#{' '}
|
|
If no match is available, return an empty JSON.
|
|
```
|
|
[ { "question": "What is the pricing?",
|
|
"answer" : " There are different pricing tiers available."
|
|
}]
|
|
```
|
|
SYSTEM_MESSAGE_CONTENT
|
|
end
|
|
|
|
def post_request(data)
|
|
headers = prepare_headers
|
|
HTTParty.post(
|
|
'https://api.openai.com/v1/chat/completions',
|
|
headers: headers,
|
|
body: data.to_json
|
|
)
|
|
end
|
|
|
|
def prepare_headers
|
|
{
|
|
'Content-Type' => 'application/json',
|
|
'Authorization' => "Bearer #{ENV.fetch('OPENAI_API_KEY')}"
|
|
}
|
|
end
|
|
|
|
def create_responses(response, response_document)
|
|
response_body = JSON.parse(response.body)
|
|
content = response_body.dig('choices', 0, 'message', 'content')
|
|
|
|
return if content.nil?
|
|
|
|
faqs = JSON.parse(content.strip)
|
|
|
|
faqs.each do |faq|
|
|
response_document.responses.create!(
|
|
question: faq['question'],
|
|
answer: faq['answer'],
|
|
response_source: response_document.response_source
|
|
)
|
|
end
|
|
rescue JSON::ParserError => e
|
|
Rails.logger.error "Error in parsing GPT processed response document : #{e.message}"
|
|
end
|
|
end
|