mirror of
https://github.com/lingble/chatwoot.git
synced 2025-10-29 02:02:27 +00:00
This commit introduces the ability to associate response sources to an inbox, allowing external webpages to be parsed by Chatwoot. The parsed data is converted into embeddings for use with GPT models when managing customer queries. The implementation relies on the `pgvector` extension for PostgreSQL. Database migrations related to this feature are handled separately by `Features::ResponseBotService`. A future update will integrate these migrations into the default rails migrations, once compatibility with Postgres extensions across all self-hosted installation options is confirmed. Additionally, a new GitHub action has been added to the CI pipeline to ensure the execution of specs related to this feature.
77 lines
2.0 KiB
Ruby
77 lines
2.0 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)
|
|
faqs = JSON.parse(response_body['choices'][0]['message']['content'].strip)
|
|
|
|
faqs.each do |faq|
|
|
response_document.responses.create!(
|
|
question: faq['question'],
|
|
answer: faq['answer'],
|
|
account_id: response_document.account_id
|
|
)
|
|
end
|
|
end
|
|
end
|