2023-08-30 02:15:03 -04:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
|
|
|
class TestPersona < DiscourseAi::AiBot::Personas::Persona
|
2024-01-04 08:44:07 -05:00
|
|
|
def tools
|
2023-08-30 02:15:03 -04:00
|
|
|
[
|
2024-01-04 08:44:07 -05:00
|
|
|
DiscourseAi::AiBot::Tools::ListTags,
|
|
|
|
DiscourseAi::AiBot::Tools::Search,
|
|
|
|
DiscourseAi::AiBot::Tools::Image,
|
2023-08-30 02:15:03 -04:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def system_prompt
|
|
|
|
<<~PROMPT
|
|
|
|
{site_url}
|
|
|
|
{site_title}
|
|
|
|
{site_description}
|
|
|
|
{participants}
|
|
|
|
{time}
|
|
|
|
PROMPT
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
RSpec.describe DiscourseAi::AiBot::Personas::Persona do
|
|
|
|
let :persona do
|
|
|
|
TestPersona.new
|
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
let :topic_with_users do
|
|
|
|
topic = Topic.new
|
|
|
|
topic.allowed_users = [User.new(username: "joe"), User.new(username: "jane")]
|
|
|
|
topic
|
|
|
|
end
|
2023-08-31 03:02:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
after do
|
|
|
|
# we are rolling back transactions so we can create poison cache
|
|
|
|
AiPersona.persona_cache.flush!
|
|
|
|
end
|
2023-11-09 19:39:49 -05:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
let(:context) do
|
|
|
|
{
|
|
|
|
site_url: Discourse.base_url,
|
|
|
|
site_title: "test site title",
|
|
|
|
site_description: "test site description",
|
|
|
|
time: Time.zone.now,
|
|
|
|
participants: topic_with_users.allowed_users.map(&:username).join(", "),
|
|
|
|
}
|
|
|
|
end
|
2023-09-14 02:46:56 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
fab!(:user)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
it "renders the system prompt" do
|
|
|
|
freeze_time
|
2023-08-31 03:02:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
rendered = persona.craft_prompt(context)
|
2024-01-12 12:36:44 -05:00
|
|
|
system_message = rendered.messages.first[:content]
|
2023-08-31 03:02:03 -04:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
expect(system_message).to include(Discourse.base_url)
|
|
|
|
expect(system_message).to include("test site title")
|
|
|
|
expect(system_message).to include("test site description")
|
|
|
|
expect(system_message).to include("joe, jane")
|
|
|
|
expect(system_message).to include(Time.zone.now.to_s)
|
2023-08-31 03:02:03 -04:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
tools = rendered.tools
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
expect(tools.find { |t| t[:name] == "search" }).to be_present
|
|
|
|
expect(tools.find { |t| t[:name] == "tags" }).to be_present
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
# needs to be configured so it is not available
|
|
|
|
expect(tools.find { |t| t[:name] == "image" }).to be_nil
|
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
it "can correctly parse arrays in tools" do
|
|
|
|
SiteSetting.ai_openai_api_key = "123"
|
|
|
|
|
|
|
|
# Dall E tool uses an array for params
|
|
|
|
xml = <<~XML
|
|
|
|
<function_calls>
|
|
|
|
<invoke>
|
|
|
|
<tool_name>dall_e</tool_name>
|
|
|
|
<tool_id>call_JtYQMful5QKqw97XFsHzPweB</tool_id>
|
|
|
|
<parameters>
|
|
|
|
<prompts>["cat oil painting", "big car"]</prompts>
|
|
|
|
</parameters>
|
|
|
|
</invoke>
|
|
|
|
</function_calls>
|
|
|
|
XML
|
|
|
|
dall_e = DiscourseAi::AiBot::Personas::DallE3.new.find_tool(xml)
|
|
|
|
expect(dall_e.parameters[:prompts]).to eq(["cat oil painting", "big car"])
|
|
|
|
end
|
2023-11-09 19:39:49 -05:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
describe "custom personas" do
|
|
|
|
it "is able to find custom personas" do
|
|
|
|
Group.refresh_automatic_groups!
|
|
|
|
|
|
|
|
# define an ai persona everyone can see
|
|
|
|
persona =
|
|
|
|
AiPersona.create!(
|
|
|
|
name: "zzzpun_bot",
|
|
|
|
description: "you write puns",
|
|
|
|
system_prompt: "you are pun bot",
|
|
|
|
commands: ["ImageCommand"],
|
|
|
|
allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
|
|
|
|
)
|
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
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
custom_persona = DiscourseAi::AiBot::Personas::Persona.all(user: user).last
|
|
|
|
expect(custom_persona.name).to eq("zzzpun_bot")
|
|
|
|
expect(custom_persona.description).to eq("you write puns")
|
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
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
instance = custom_persona.new
|
|
|
|
expect(instance.tools).to eq([DiscourseAi::AiBot::Tools::Image])
|
2024-01-12 12:36:44 -05:00
|
|
|
expect(instance.craft_prompt(context).messages.first[:content]).to eq("you are pun bot")
|
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
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
# should update
|
|
|
|
persona.update!(name: "zzzpun_bot2")
|
|
|
|
custom_persona = DiscourseAi::AiBot::Personas::Persona.all(user: user).last
|
|
|
|
expect(custom_persona.name).to eq("zzzpun_bot2")
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
# can be disabled
|
|
|
|
persona.update!(enabled: false)
|
|
|
|
last_persona = DiscourseAi::AiBot::Personas::Persona.all(user: user).last
|
|
|
|
expect(last_persona.name).not_to eq("zzzpun_bot2")
|
2023-09-03 22:05:27 -04:00
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
persona.update!(enabled: true)
|
|
|
|
# no groups have access
|
|
|
|
persona.update!(allowed_group_ids: [])
|
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
|
|
|
|
2024-01-04 22:39:32 -05:00
|
|
|
last_persona = DiscourseAi::AiBot::Personas::Persona.all(user: user).last
|
|
|
|
expect(last_persona.name).not_to eq("zzzpun_bot2")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "available personas" do
|
|
|
|
it "includes all personas by default" do
|
|
|
|
Group.refresh_automatic_groups!
|
|
|
|
|
|
|
|
# must be enabled to see it
|
|
|
|
SiteSetting.ai_stability_api_key = "abc"
|
|
|
|
SiteSetting.ai_google_custom_search_api_key = "abc"
|
|
|
|
SiteSetting.ai_google_custom_search_cx = "abc123"
|
|
|
|
|
|
|
|
# should be ordered by priority and then alpha
|
|
|
|
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to eq(
|
|
|
|
[
|
|
|
|
DiscourseAi::AiBot::Personas::General,
|
|
|
|
DiscourseAi::AiBot::Personas::Artist,
|
|
|
|
DiscourseAi::AiBot::Personas::Creative,
|
2024-02-18 22:52:12 -05:00
|
|
|
DiscourseAi::AiBot::Personas::DiscourseHelper,
|
2024-01-04 22:39:32 -05:00
|
|
|
DiscourseAi::AiBot::Personas::Researcher,
|
|
|
|
DiscourseAi::AiBot::Personas::SettingsExplorer,
|
|
|
|
DiscourseAi::AiBot::Personas::SqlHelper,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
# omits personas if key is missing
|
|
|
|
SiteSetting.ai_stability_api_key = ""
|
|
|
|
SiteSetting.ai_google_custom_search_api_key = ""
|
|
|
|
|
|
|
|
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly(
|
|
|
|
DiscourseAi::AiBot::Personas::General,
|
|
|
|
DiscourseAi::AiBot::Personas::SqlHelper,
|
|
|
|
DiscourseAi::AiBot::Personas::SettingsExplorer,
|
|
|
|
DiscourseAi::AiBot::Personas::Creative,
|
2024-02-18 22:52:12 -05:00
|
|
|
DiscourseAi::AiBot::Personas::DiscourseHelper,
|
2024-01-04 22:39:32 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
AiPersona.find(
|
|
|
|
DiscourseAi::AiBot::Personas::Persona.system_personas[
|
|
|
|
DiscourseAi::AiBot::Personas::General
|
|
|
|
],
|
|
|
|
).update!(enabled: false)
|
|
|
|
|
|
|
|
expect(DiscourseAi::AiBot::Personas::Persona.all(user: user)).to contain_exactly(
|
|
|
|
DiscourseAi::AiBot::Personas::SqlHelper,
|
|
|
|
DiscourseAi::AiBot::Personas::SettingsExplorer,
|
|
|
|
DiscourseAi::AiBot::Personas::Creative,
|
2024-02-18 22:52:12 -05:00
|
|
|
DiscourseAi::AiBot::Personas::DiscourseHelper,
|
2024-01-04 22:39:32 -05:00
|
|
|
)
|
2023-08-31 03:02:03 -04:00
|
|
|
end
|
2023-08-30 02:15:03 -04:00
|
|
|
end
|
|
|
|
end
|