Files
chatwoot/enterprise/lib/chat_gpt.rb
Sojan Jose 480f34803b feat: Response Bot using GPT and Webpage Sources (#7518)
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.
2023-07-21 18:11:51 +03:00

42 lines
1.8 KiB
Ruby

class ChatGpt
def self.base_uri
'https://api.openai.com'
end
def initialize(context_sections = '')
@model = 'gpt-4'
system_message = { 'role': 'system',
'content': 'You are a very enthusiastic customer support representative who loves ' \
'to help people! Given the following Context sections from the ' \
'documentation, continue the conversation with only that information, ' \
"outputed in markdown format along with context_ids in format 'response \n {context_ids: [values] }' " \
"\n If you are unsure and the answer is not explicitly written in the documentation, " \
"say 'Sorry, I don't know how to help with that. Do you want to chat with a human agent?' " \
"If they ask to Chat with human agent return text 'conversation_handoff'." \
"Context sections: \n" \
"\n\n #{context_sections}}" }
@messages = [
system_message
]
end
def generate_response(input, previous_messages = [])
previous_messages.each do |message|
@messages << message
end
@messages << { 'role': 'user', 'content': input } if input.present?
headers = { 'Content-Type' => 'application/json',
'Authorization' => "Bearer #{ENV.fetch('OPENAI_API_KEY')}" }
body = {
model: @model,
messages: @messages
}.to_json
response = HTTParty.post("#{self.class.base_uri}/v1/chat/completions", headers: headers, body: body)
response_body = JSON.parse(response.body)
response_body['choices'][0]['message']['content'].strip
end
end