2023-05-05 14:28:31 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
2024-03-07 14:37:23 -05:00
|
|
|
USER_AGENT = "Discourse AI Bot 1.0 (https://www.discourse.org)"
|
|
|
|
|
2023-05-05 14:28:31 -04:00
|
|
|
class EntryPoint
|
2024-03-12 01:51:41 -04:00
|
|
|
Bot = Struct.new(:id, :name, :llm)
|
|
|
|
|
2024-03-12 20:24:22 -04:00
|
|
|
def self.all_bot_ids
|
2024-10-15 16:20:31 -04:00
|
|
|
AiPersona
|
|
|
|
.persona_users
|
|
|
|
.map { |persona| persona[:user_id] }
|
|
|
|
.concat(LlmModel.where(enabled_chat_bot: true).pluck(:user_id))
|
2024-03-12 20:24:22 -04:00
|
|
|
end
|
|
|
|
|
2024-06-18 13:32:14 -04:00
|
|
|
def self.find_participant_in(participant_ids)
|
|
|
|
model = LlmModel.includes(:user).where(user_id: participant_ids).last
|
|
|
|
return if model.nil?
|
|
|
|
|
|
|
|
bot_user = model.user
|
|
|
|
|
|
|
|
Bot.new(bot_user.id, bot_user.username_lower, model.name)
|
2024-03-12 01:51:41 -04:00
|
|
|
end
|
|
|
|
|
2024-06-18 13:32:14 -04:00
|
|
|
def self.find_user_from_model(model_name)
|
|
|
|
# Hack(Roman): Added this because Command R Plus had a different in the bot settings.
|
|
|
|
# Will eventually ammend it with a data migration.
|
|
|
|
name = model_name
|
|
|
|
name = "command-r-plus" if name == "cohere-command-r-plus"
|
|
|
|
|
|
|
|
LlmModel.joins(:user).where(name: name).last&.user
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.enabled_user_ids_and_models_map
|
|
|
|
DB.query_hash(<<~SQL)
|
|
|
|
SELECT users.username AS username, users.id AS id, llms.name AS model_name, llms.display_name AS display_name
|
|
|
|
FROM llm_models llms
|
|
|
|
INNER JOIN users ON llms.user_id = users.id
|
|
|
|
WHERE llms.enabled_chat_bot
|
|
|
|
SQL
|
2023-05-16 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
2024-03-12 01:51:41 -04:00
|
|
|
# Most errors are simply "not_allowed"
|
2024-06-18 13:32:14 -04:00
|
|
|
# we do not want to reveal information about this system
|
2024-03-12 01:51:41 -04:00
|
|
|
# the 2 exceptions are "other_people_in_pm" and "other_content_in_pm"
|
|
|
|
# in both cases you have access to the PM so we are not revealing anything
|
|
|
|
def self.ai_share_error(topic, guardian)
|
|
|
|
return nil if guardian.can_share_ai_bot_conversation?(topic)
|
|
|
|
|
|
|
|
return :not_allowed if !guardian.can_see?(topic)
|
|
|
|
|
|
|
|
# other people in PM
|
|
|
|
if topic.topic_allowed_users.where("user_id > 0 and user_id <> ?", guardian.user.id).exists?
|
|
|
|
return :other_people_in_pm
|
|
|
|
end
|
|
|
|
|
|
|
|
# other content in PM
|
|
|
|
if topic.posts.where("user_id > 0 and user_id <> ?", guardian.user.id).exists?
|
|
|
|
return :other_content_in_pm
|
|
|
|
end
|
|
|
|
|
|
|
|
:not_allowed
|
|
|
|
end
|
|
|
|
|
2023-05-05 14:28:31 -04:00
|
|
|
def inject_into(plugin)
|
2024-05-05 19:49:02 -04:00
|
|
|
plugin.register_modifier(:chat_allowed_bot_user_ids) do |user_ids, guardian|
|
|
|
|
if guardian.user
|
2024-10-15 19:33:24 -04:00
|
|
|
allowed_chat =
|
|
|
|
AiPersona.allowed_modalities(
|
|
|
|
user: guardian.user,
|
|
|
|
allow_chat_direct_messages: true,
|
|
|
|
allow_chat_channel_mentions: true,
|
|
|
|
)
|
2024-05-06 00:42:55 -04:00
|
|
|
allowed_bot_ids = allowed_chat.map { |info| info[:user_id] }
|
2024-05-05 19:49:02 -04:00
|
|
|
user_ids.concat(allowed_bot_ids)
|
|
|
|
end
|
|
|
|
user_ids
|
|
|
|
end
|
|
|
|
|
2023-10-23 02:00:58 -04:00
|
|
|
plugin.on(:site_setting_changed) do |name, _old_value, _new_value|
|
2024-06-18 13:32:14 -04:00
|
|
|
if name == :ai_bot_enabled || name == :discourse_ai_enabled
|
2023-10-23 02:00:58 -04:00
|
|
|
DiscourseAi::AiBot::SiteSettingsExtension.enable_or_disable_ai_bots
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-12 01:51:41 -04:00
|
|
|
Oneboxer.register_local_handler(
|
|
|
|
"discourse_ai/ai_bot/shared_ai_conversations",
|
|
|
|
) do |url, route|
|
|
|
|
if route[:action] == "show" && share_key = route[:share_key]
|
|
|
|
if conversation = SharedAiConversation.find_by(share_key: share_key)
|
|
|
|
conversation.onebox
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
plugin.on(:reduce_excerpt) do |doc, options|
|
|
|
|
doc.css("details").remove if options && options[:strip_details]
|
|
|
|
end
|
|
|
|
|
2023-05-05 14:28:31 -04:00
|
|
|
plugin.register_seedfu_fixtures(
|
|
|
|
Rails.root.join("plugins", "discourse-ai", "db", "fixtures", "ai_bot"),
|
|
|
|
)
|
|
|
|
|
2023-08-30 02:15:03 -04:00
|
|
|
plugin.add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:ai_enabled_personas,
|
|
|
|
include_condition: -> do
|
|
|
|
SiteSetting.ai_bot_enabled && scope.authenticated? &&
|
|
|
|
scope.user.in_any_groups?(SiteSetting.ai_bot_allowed_groups_map)
|
|
|
|
end,
|
|
|
|
) do
|
2024-01-04 08:44:07 -05:00
|
|
|
DiscourseAi::AiBot::Personas::Persona
|
2023-11-09 19:39:49 -05:00
|
|
|
.all(user: scope.user)
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
.map do |persona|
|
2024-10-15 16:20:31 -04:00
|
|
|
{
|
|
|
|
id: persona.id,
|
|
|
|
name: persona.name,
|
|
|
|
description: persona.description,
|
|
|
|
force_default_llm: persona.force_default_llm,
|
|
|
|
username: persona.username,
|
|
|
|
}
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
end
|
|
|
|
|
2024-04-15 09:22:06 -04:00
|
|
|
plugin.add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:can_debug_ai_bot_conversations,
|
|
|
|
include_condition: -> do
|
|
|
|
SiteSetting.ai_bot_enabled && scope.authenticated? &&
|
|
|
|
SiteSetting.ai_bot_debugging_allowed_groups.present? &&
|
|
|
|
scope.user.in_any_groups?(SiteSetting.ai_bot_debugging_allowed_groups_map)
|
|
|
|
end,
|
|
|
|
) { true }
|
|
|
|
|
2023-08-16 16:29:58 -04:00
|
|
|
plugin.add_to_serializer(
|
|
|
|
:current_user,
|
|
|
|
:ai_enabled_chat_bots,
|
|
|
|
include_condition: -> do
|
|
|
|
SiteSetting.ai_bot_enabled && scope.authenticated? &&
|
|
|
|
scope.user.in_any_groups?(SiteSetting.ai_bot_allowed_groups_map)
|
|
|
|
end,
|
|
|
|
) do
|
2024-06-18 13:32:14 -04:00
|
|
|
bots_map = ::DiscourseAi::AiBot::EntryPoint.enabled_user_ids_and_models_map
|
2023-08-16 16:29:58 -04:00
|
|
|
|
2024-04-15 09:22:06 -04:00
|
|
|
persona_users = AiPersona.persona_users(user: scope.user)
|
|
|
|
if persona_users.present?
|
2024-10-24 15:24:53 -04:00
|
|
|
persona_users.filter! { |persona_user| persona_user[:username].present? }
|
|
|
|
|
2024-06-18 13:32:14 -04:00
|
|
|
bots_map.concat(
|
2024-04-15 09:22:06 -04:00
|
|
|
persona_users.map do |persona_user|
|
|
|
|
{
|
|
|
|
"id" => persona_user[:user_id],
|
|
|
|
"username" => persona_user[:username],
|
2024-10-15 16:20:31 -04:00
|
|
|
"force_default_llm" => persona_user[:force_default_llm],
|
2024-06-18 13:32:14 -04:00
|
|
|
"is_persona" => true,
|
2024-04-15 09:22:06 -04:00
|
|
|
}
|
2024-02-29 15:53:42 -05:00
|
|
|
end,
|
|
|
|
)
|
|
|
|
end
|
2024-06-18 13:32:14 -04:00
|
|
|
|
|
|
|
bots_map
|
2023-08-16 16:29:58 -04:00
|
|
|
end
|
|
|
|
|
2024-03-12 01:51:41 -04:00
|
|
|
plugin.add_to_serializer(:current_user, :can_share_ai_bot_conversations) do
|
|
|
|
scope.user.in_any_groups?(SiteSetting.ai_bot_public_sharing_allowed_groups_map)
|
|
|
|
end
|
|
|
|
|
2023-05-16 13:38:21 -04:00
|
|
|
plugin.register_svg_icon("robot")
|
2024-11-25 08:20:17 -05:00
|
|
|
plugin.register_svg_icon("info")
|
2023-05-16 13:38:21 -04:00
|
|
|
|
2023-08-30 02:15:03 -04:00
|
|
|
plugin.add_to_serializer(
|
|
|
|
:topic_view,
|
|
|
|
:ai_persona_name,
|
|
|
|
include_condition: -> { SiteSetting.ai_bot_enabled && object.topic.private_message? },
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
) do
|
|
|
|
id = topic.custom_fields["ai_persona_id"]
|
2024-01-04 08:44:07 -05:00
|
|
|
name =
|
|
|
|
DiscourseAi::AiBot::Personas::Persona.find_by(user: scope.user, id: id.to_i)&.name if id
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
name || topic.custom_fields["ai_persona"]
|
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-02-15 00:37:59 -05:00
|
|
|
plugin.on(:post_created) { |post| DiscourseAi::AiBot::Playground.schedule_reply(post) }
|
2023-10-11 18:14:19 -04:00
|
|
|
|
2024-05-05 19:49:02 -04:00
|
|
|
plugin.on(:chat_message_created) do |chat_message, channel, user, context|
|
|
|
|
DiscourseAi::AiBot::Playground.schedule_chat_reply(chat_message, channel, user, context)
|
|
|
|
end
|
|
|
|
|
2023-10-11 18:14:19 -04:00
|
|
|
if plugin.respond_to?(:register_editable_topic_custom_field)
|
FEATURE: UI to update ai personas on admin page (#290)
Introduces a UI to manage customizable personas (admin only feature)
Part of the change was some extensive internal refactoring:
- AIBot now has a persona set in the constructor, once set it never changes
- Command now takes in bot as a constructor param, so it has the correct persona and is not generating AIBot objects on the fly
- Added a .prettierignore file, due to the way ALE is configured in nvim it is a pre-req for prettier to work
- Adds a bunch of validations on the AIPersona model, system personas (artist/creative etc...) are all seeded. We now ensure
- name uniqueness, and only allow certain properties to be touched for system personas.
- (JS note) the client side design takes advantage of nested routes, the parent route for personas gets all the personas via this.store.findAll("ai-persona") then child routes simply reach into this model to find a particular persona.
- (JS note) data is sideloaded into the ai-persona model the meta property supplied from the controller, resultSetMeta
- This removes ai_bot_enabled_personas and ai_bot_enabled_chat_commands, both should be controlled from the UI on a per persona basis
- Fixes a long standing bug in token accounting ... we were doing to_json.length instead of to_json.to_s.length
- Amended it so {commands} are always inserted at the end unconditionally, no need to add it to the template of the system message as it just confuses things
- Adds a concept of required_commands to stock personas, these are commands that must be configured for this stock persona to show up.
- Refactored tests so we stop requiring inference_stubs, it was very confusing to need it, added to plugin.rb for now which at least is clearer
- Migrates the persona selector to gjs
---------
Co-authored-by: Joffrey JAFFEUX <j.jaffeux@gmail.com>
Co-authored-by: Martin Brennan <martin@discourse.org>
2023-11-21 00:56:43 -05:00
|
|
|
plugin.register_editable_topic_custom_field(:ai_persona_id)
|
2023-10-11 18:14:19 -04:00
|
|
|
end
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
|
2024-10-29 19:28:20 -04:00
|
|
|
plugin.add_api_key_scope(
|
|
|
|
:discourse_ai,
|
|
|
|
{ stream_completion: { actions: %w[discourse_ai/admin/ai_personas#stream_reply] } },
|
|
|
|
)
|
|
|
|
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
plugin.on(:site_setting_changed) do |name, old_value, new_value|
|
2024-04-12 09:32:46 -04:00
|
|
|
if name == :ai_embeddings_model && SiteSetting.ai_embeddings_enabled? &&
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
new_value != old_value
|
2024-04-12 09:32:46 -04:00
|
|
|
RagDocumentFragment.delete_all
|
|
|
|
UploadReference
|
|
|
|
.where(target: AiPersona.all)
|
|
|
|
.each do |ref|
|
|
|
|
Jobs.enqueue(
|
|
|
|
:digest_rag_upload,
|
|
|
|
ai_persona_id: ref.target_id,
|
|
|
|
upload_id: ref.upload_id,
|
|
|
|
)
|
FEATURE: AI Bot RAG support. (#537)
This PR lets you associate uploads to an AI persona, which we'll split and generate embeddings from. When building the system prompt to get a bot reply, we'll do a similarity search followed by a re-ranking (if available). This will let us find the most relevant fragments from the body of knowledge you associated with the persona, resulting in better, more informed responses.
For now, we'll only allow plain-text files, but this will change in the future.
Commits:
* FEATURE: RAG embeddings for the AI Bot
This first commit introduces a UI where admins can upload text files, which we'll store, split into fragments,
and generate embeddings of. In a next commit, we'll use those to give the bot additional information during
conversations.
* Basic asymmetric similarity search to provide guidance in system prompt
* Fix tests and lint
* Apply reranker to fragments
* Uploads filter, css adjustments and file validations
* Add placeholder for rag fragments
* Update annotations
2024-04-01 12:43:34 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2023-05-05 14:28:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|