2023-05-05 15:28:31 -03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
2024-03-08 06:37:23 +11:00
|
|
|
USER_AGENT = "Discourse AI Bot 1.0 (https://www.discourse.org)"
|
|
|
|
|
2023-05-05 15:28:31 -03:00
|
|
|
class EntryPoint
|
2023-08-24 07:20:24 +10:00
|
|
|
REQUIRE_TITLE_UPDATE = "discourse-ai-title-update"
|
|
|
|
|
2023-05-11 10:03:03 -03:00
|
|
|
GPT4_ID = -110
|
|
|
|
GPT3_5_TURBO_ID = -111
|
2023-07-27 11:24:44 +10:00
|
|
|
CLAUDE_V2_ID = -112
|
2023-12-11 14:59:57 +11:00
|
|
|
GPT4_TURBO_ID = -113
|
2024-01-04 12:22:43 -03:00
|
|
|
MIXTRAL_ID = -114
|
2024-01-04 18:15:34 -03:00
|
|
|
GEMINI_ID = -115
|
2024-01-11 15:56:40 +11:00
|
|
|
FAKE_ID = -116 # only used for dev and test
|
2024-03-06 06:04:37 +11:00
|
|
|
CLAUDE_3_OPUS_ID = -117
|
|
|
|
CLAUDE_3_SONNET_ID = -118
|
2024-01-11 15:56:40 +11:00
|
|
|
|
2023-10-23 17:00:58 +11:00
|
|
|
BOTS = [
|
|
|
|
[GPT4_ID, "gpt4_bot", "gpt-4"],
|
|
|
|
[GPT3_5_TURBO_ID, "gpt3.5_bot", "gpt-3.5-turbo"],
|
|
|
|
[CLAUDE_V2_ID, "claude_bot", "claude-2"],
|
2023-12-11 14:59:57 +11:00
|
|
|
[GPT4_TURBO_ID, "gpt4t_bot", "gpt-4-turbo"],
|
2024-01-04 12:22:43 -03:00
|
|
|
[MIXTRAL_ID, "mixtral_bot", "mixtral-8x7B-Instruct-V0.1"],
|
2024-01-04 18:15:34 -03:00
|
|
|
[GEMINI_ID, "gemini_bot", "gemini-pro"],
|
2024-01-11 15:56:40 +11:00
|
|
|
[FAKE_ID, "fake_bot", "fake"],
|
2024-03-06 06:04:37 +11:00
|
|
|
[CLAUDE_3_OPUS_ID, "claude_3_opus_bot", "claude-3-opus"],
|
|
|
|
[CLAUDE_3_SONNET_ID, "claude_3_sonnet_bot", "claude-3-sonnet"],
|
2023-10-23 17:00:58 +11:00
|
|
|
]
|
2023-05-05 15:28:31 -03:00
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
BOT_USER_IDS = BOTS.map(&:first)
|
|
|
|
|
2024-03-12 16:51:41 +11:00
|
|
|
Bot = Struct.new(:id, :name, :llm)
|
|
|
|
|
2024-03-13 11:24:22 +11:00
|
|
|
def self.all_bot_ids
|
|
|
|
BOT_USER_IDS.concat(AiPersona.mentionables.map { |mentionable| mentionable[:user_id] })
|
|
|
|
end
|
|
|
|
|
2024-03-12 16:51:41 +11:00
|
|
|
def self.find_bot_by_id(id)
|
|
|
|
found = DiscourseAi::AiBot::EntryPoint::BOTS.find { |bot| bot[0] == id }
|
|
|
|
return if !found
|
|
|
|
Bot.new(found[0], found[1], found[2])
|
|
|
|
end
|
|
|
|
|
2023-05-16 14:38:21 -03:00
|
|
|
def self.map_bot_model_to_user_id(model_name)
|
|
|
|
case model_name
|
2023-12-11 14:59:57 +11:00
|
|
|
in "gpt-4-turbo"
|
|
|
|
GPT4_TURBO_ID
|
2023-05-16 14:38:21 -03:00
|
|
|
in "gpt-3.5-turbo"
|
|
|
|
GPT3_5_TURBO_ID
|
|
|
|
in "gpt-4"
|
|
|
|
GPT4_ID
|
2023-07-27 11:24:44 +10:00
|
|
|
in "claude-2"
|
|
|
|
CLAUDE_V2_ID
|
2024-01-04 12:22:43 -03:00
|
|
|
in "mixtral-8x7B-Instruct-V0.1"
|
|
|
|
MIXTRAL_ID
|
2024-01-04 18:15:34 -03:00
|
|
|
in "gemini-pro"
|
|
|
|
GEMINI_ID
|
2024-01-11 15:56:40 +11:00
|
|
|
in "fake"
|
|
|
|
FAKE_ID
|
2024-03-06 06:04:37 +11:00
|
|
|
in "claude-3-opus"
|
|
|
|
CLAUDE_3_OPUS_ID
|
|
|
|
in "claude-3-sonnet"
|
|
|
|
CLAUDE_3_SONNET_ID
|
2023-05-16 14:38:21 -03:00
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-12 16:51:41 +11:00
|
|
|
# Most errors are simply "not_allowed"
|
|
|
|
# we do not want to reveal information about this sytem
|
|
|
|
# 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 15:28:31 -03:00
|
|
|
def inject_into(plugin)
|
2023-10-23 17:00:58 +11:00
|
|
|
plugin.on(:site_setting_changed) do |name, _old_value, _new_value|
|
2024-01-30 03:24:30 +11:00
|
|
|
if name == :ai_bot_enabled_chat_bots || name == :ai_bot_enabled ||
|
|
|
|
name == :discourse_ai_enabled
|
2023-10-23 17:00:58 +11:00
|
|
|
DiscourseAi::AiBot::SiteSettingsExtension.enable_or_disable_ai_bots
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-12 16:51:41 +11: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 15:28:31 -03:00
|
|
|
plugin.register_seedfu_fixtures(
|
|
|
|
Rails.root.join("plugins", "discourse-ai", "db", "fixtures", "ai_bot"),
|
|
|
|
)
|
|
|
|
|
2023-08-30 16:15:03 +10: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 10:44:07 -03:00
|
|
|
DiscourseAi::AiBot::Personas::Persona
|
2023-11-10 11:39:49 +11: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 16:56:43 +11:00
|
|
|
.map do |persona|
|
|
|
|
{ id: persona.id, name: persona.name, description: persona.description }
|
|
|
|
end
|
2023-08-30 16:15:03 +10:00
|
|
|
end
|
|
|
|
|
2023-08-17 06:29:58 +10: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
|
|
|
|
model_map = {}
|
|
|
|
SiteSetting
|
|
|
|
.ai_bot_enabled_chat_bots
|
|
|
|
.split("|")
|
|
|
|
.each do |bot_name|
|
|
|
|
model_map[
|
|
|
|
::DiscourseAi::AiBot::EntryPoint.map_bot_model_to_user_id(bot_name)
|
|
|
|
] = bot_name
|
|
|
|
end
|
|
|
|
|
|
|
|
# not 100% ideal, cause it is one extra query, but we need it
|
|
|
|
bots = DB.query_hash(<<~SQL, user_ids: model_map.keys)
|
|
|
|
SELECT username, id FROM users WHERE id IN (:user_ids)
|
|
|
|
SQL
|
|
|
|
|
|
|
|
bots.each { |hash| hash["model_name"] = model_map[hash["id"]] }
|
2024-03-01 07:53:42 +11:00
|
|
|
mentionables = AiPersona.mentionables(user: scope.user)
|
|
|
|
if mentionables.present?
|
|
|
|
bots.concat(
|
|
|
|
mentionables.map do |mentionable|
|
|
|
|
{ "id" => mentionable[:user_id], "username" => mentionable[:username] }
|
|
|
|
end,
|
|
|
|
)
|
|
|
|
end
|
2023-08-17 06:29:58 +10:00
|
|
|
bots
|
|
|
|
end
|
|
|
|
|
2024-02-19 13:26:24 +11:00
|
|
|
plugin.add_to_serializer(:current_user, :can_use_assistant) do
|
|
|
|
scope.user.in_any_groups?(SiteSetting.ai_helper_allowed_groups_map)
|
|
|
|
end
|
|
|
|
|
|
|
|
plugin.add_to_serializer(:current_user, :can_use_assistant_in_post) do
|
|
|
|
scope.user.in_any_groups?(SiteSetting.post_ai_helper_allowed_groups_map)
|
|
|
|
end
|
|
|
|
|
|
|
|
plugin.add_to_serializer(:current_user, :can_use_custom_prompts) do
|
|
|
|
scope.user.in_any_groups?(SiteSetting.ai_helper_custom_prompts_allowed_groups_map)
|
|
|
|
end
|
|
|
|
|
2024-03-12 16:51:41 +11: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 14:38:21 -03:00
|
|
|
plugin.register_svg_icon("robot")
|
|
|
|
|
2023-08-30 16:15:03 +10: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 16:56:43 +11:00
|
|
|
) do
|
|
|
|
id = topic.custom_fields["ai_persona_id"]
|
2024-01-04 10:44:07 -03: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 16:56:43 +11:00
|
|
|
name || topic.custom_fields["ai_persona"]
|
|
|
|
end
|
2023-08-30 16:15:03 +10:00
|
|
|
|
2024-02-15 16:37:59 +11:00
|
|
|
plugin.on(:post_created) { |post| DiscourseAi::AiBot::Playground.schedule_reply(post) }
|
2023-10-11 19:14:19 -03: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 16:56:43 +11:00
|
|
|
plugin.register_editable_topic_custom_field(:ai_persona_id)
|
2023-10-11 19:14:19 -03:00
|
|
|
end
|
2023-05-05 15:28:31 -03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|