2023-05-11 09:03:03 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
|
|
|
class Bot
|
2024-02-28 00:46:32 -05:00
|
|
|
attr_reader :model
|
|
|
|
|
2023-05-11 09:03:03 -04:00
|
|
|
BOT_NOT_FOUND = Class.new(StandardError)
|
2023-09-14 02:46:56 -04:00
|
|
|
MAX_COMPLETIONS = 5
|
2024-03-01 15:53:21 -05:00
|
|
|
MAX_TOOLS = 5
|
2023-05-11 09:03:03 -04:00
|
|
|
|
2024-02-28 00:46:32 -05:00
|
|
|
def self.as(bot_user, persona: DiscourseAi::AiBot::Personas::General.new, model: nil)
|
|
|
|
new(bot_user, persona, model)
|
2023-05-11 09:03:03 -04:00
|
|
|
end
|
|
|
|
|
2024-02-28 00:46:32 -05:00
|
|
|
def initialize(bot_user, persona, model = nil)
|
2023-05-11 09:03:03 -04:00
|
|
|
@bot_user = bot_user
|
2024-01-04 08:44:07 -05:00
|
|
|
@persona = persona
|
2024-02-28 00:46:32 -05:00
|
|
|
@model = model || self.class.guess_model(bot_user) || @persona.class.default_llm
|
2023-05-11 09:03:03 -04:00
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
attr_reader :bot_user
|
2024-02-15 00:37:59 -05:00
|
|
|
attr_accessor :persona
|
2023-05-23 09:08:17 -04:00
|
|
|
|
2024-11-28 14:26:48 -05:00
|
|
|
def get_updated_title(conversation_context, post, user)
|
2024-01-12 12:36:44 -05:00
|
|
|
system_insts = <<~TEXT.strip
|
2024-09-03 01:52:20 -04:00
|
|
|
You are titlebot. Given a conversation, you will suggest a title.
|
|
|
|
|
|
|
|
- You will never respond with anything but the suggested title.
|
|
|
|
- You will always match the conversation language in your title suggestion.
|
|
|
|
- Title will capture the essence of the conversation.
|
|
|
|
TEXT
|
|
|
|
|
|
|
|
# conversation context may contain tool calls, and confusing user names
|
|
|
|
# clean it up
|
|
|
|
conversation = +""
|
|
|
|
conversation_context.each do |context|
|
|
|
|
if context[:type] == :user
|
|
|
|
conversation << "User said:\n#{context[:content]}\n\n"
|
|
|
|
elsif context[:type] == :model
|
|
|
|
conversation << "Model said:\n#{context[:content]}\n\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
instruction = <<~TEXT.strip
|
|
|
|
Given the following conversation:
|
|
|
|
|
|
|
|
{{{
|
|
|
|
#{conversation}
|
|
|
|
}}}
|
|
|
|
|
|
|
|
Reply only with a title that is 7 words or less.
|
2024-01-04 08:44:07 -05:00
|
|
|
TEXT
|
2023-05-20 03:45:54 -04:00
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
title_prompt =
|
2024-03-01 15:53:21 -05:00
|
|
|
DiscourseAi::Completions::Prompt.new(
|
|
|
|
system_insts,
|
2024-09-03 01:52:20 -04:00
|
|
|
messages: [type: :user, content: instruction],
|
2024-03-01 15:53:21 -05:00
|
|
|
topic_id: post.topic_id,
|
|
|
|
)
|
2024-01-12 12:36:44 -05:00
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
DiscourseAi::Completions::Llm
|
|
|
|
.proxy(model)
|
2024-11-28 14:26:48 -05:00
|
|
|
.generate(title_prompt, user: user, feature_name: "bot_title")
|
2024-01-04 08:44:07 -05:00
|
|
|
.strip
|
|
|
|
.split("\n")
|
|
|
|
.last
|
|
|
|
end
|
|
|
|
|
2024-10-04 19:46:57 -04:00
|
|
|
def force_tool_if_needed(prompt, context)
|
|
|
|
context[:chosen_tools] ||= []
|
|
|
|
forced_tools = persona.force_tool_use.map { |tool| tool.name }
|
|
|
|
force_tool = forced_tools.find { |name| !context[:chosen_tools].include?(name) }
|
|
|
|
|
2024-10-10 16:23:42 -04:00
|
|
|
if force_tool && persona.forced_tool_count > 0
|
|
|
|
user_turns = prompt.messages.select { |m| m[:type] == :user }.length
|
|
|
|
force_tool = false if user_turns > persona.forced_tool_count
|
|
|
|
end
|
|
|
|
|
2024-10-04 19:46:57 -04:00
|
|
|
if force_tool
|
|
|
|
context[:chosen_tools] << force_tool
|
|
|
|
prompt.tool_choice = force_tool
|
|
|
|
else
|
|
|
|
prompt.tool_choice = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
def reply(context, &update_blk)
|
FEATURE: Add Question Consolidator for robust Upload support in Personas (#596)
This commit introduces a new feature for AI Personas called the "Question Consolidator LLM". The purpose of the Question Consolidator is to consolidate a user's latest question into a self-contained, context-rich question before querying the vector database for relevant fragments. This helps improve the quality and relevance of the retrieved fragments.
Previous to this change we used the last 10 interactions, this is not ideal cause the RAG would "lock on" to an answer.
EG:
- User: how many cars are there in europe
- Model: detailed answer about cars in europe including the term car and vehicle many times
- User: Nice, what about trains are there in the US
In the above example "trains" and "US" becomes very low signal given there are pages and pages talking about cars and europe. This mean retrieval is sub optimal.
Instead, we pass the history to the "question consolidator", it would simply consolidate the question to "How many trains are there in the United States", which would make it fare easier for the vector db to find relevant content.
The llm used for question consolidator can often be less powerful than the model you are talking to, we recommend using lighter weight and fast models cause the task is very simple. This is configurable from the persona ui.
This PR also removes support for {uploads} placeholder, this is too complicated to get right and we want freedom to shift RAG implementation.
Key changes:
1. Added a new `question_consolidator_llm` column to the `ai_personas` table to store the LLM model used for question consolidation.
2. Implemented the `QuestionConsolidator` module which handles the logic for consolidating the user's latest question. It extracts the relevant user and model messages from the conversation history, truncates them if needed to fit within the token limit, and generates a consolidated question prompt.
3. Updated the `Persona` class to use the Question Consolidator LLM (if configured) when crafting the RAG fragments prompt. It passes the conversation context to the consolidator to generate a self-contained question.
4. Added UI elements in the AI Persona editor to allow selecting the Question Consolidator LLM. Also made some UI tweaks to conditionally show/hide certain options based on persona configuration.
5. Wrote unit tests for the QuestionConsolidator module and updated existing persona tests to cover the new functionality.
This feature enables AI Personas to better understand the context and intent behind a user's question by consolidating the conversation history into a single, focused question. This can lead to more relevant and accurate responses from the AI assistant.
2024-04-29 23:49:21 -04:00
|
|
|
llm = DiscourseAi::Completions::Llm.proxy(model)
|
|
|
|
prompt = persona.craft_prompt(context, llm: llm)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
|
|
|
total_completions = 0
|
|
|
|
ongoing_chain = true
|
|
|
|
raw_context = []
|
|
|
|
|
2024-02-02 15:09:34 -05:00
|
|
|
user = context[:user]
|
|
|
|
|
|
|
|
llm_kwargs = { user: user }
|
|
|
|
llm_kwargs[:temperature] = persona.temperature if persona.temperature
|
|
|
|
llm_kwargs[:top_p] = persona.top_p if persona.top_p
|
|
|
|
|
2024-06-03 18:59:15 -04:00
|
|
|
needs_newlines = false
|
2024-11-11 16:14:30 -05:00
|
|
|
tools_ran = 0
|
2024-06-03 18:59:15 -04:00
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
while total_completions <= MAX_COMPLETIONS && ongoing_chain
|
|
|
|
tool_found = false
|
2024-10-04 19:46:57 -04:00
|
|
|
force_tool_if_needed(prompt, context)
|
2024-01-04 08:44:07 -05:00
|
|
|
|
2024-11-18 17:22:39 -05:00
|
|
|
tool_halted = false
|
|
|
|
|
|
|
|
allow_partial_tool_calls = persona.allow_partial_tool_calls?
|
|
|
|
existing_tools = Set.new
|
|
|
|
|
2024-01-05 13:21:14 -05:00
|
|
|
result =
|
2024-11-18 17:22:39 -05:00
|
|
|
llm.generate(
|
|
|
|
prompt,
|
|
|
|
feature_name: "bot",
|
|
|
|
partial_tool_calls: allow_partial_tool_calls,
|
|
|
|
**llm_kwargs,
|
|
|
|
) do |partial, cancel|
|
|
|
|
tool =
|
|
|
|
persona.find_tool(
|
|
|
|
partial,
|
|
|
|
bot_user: user,
|
|
|
|
llm: llm,
|
|
|
|
context: context,
|
|
|
|
existing_tools: existing_tools,
|
|
|
|
)
|
2024-11-11 16:14:30 -05:00
|
|
|
tool = nil if tools_ran >= MAX_TOOLS
|
2024-03-01 15:53:21 -05:00
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
if tool.present?
|
2024-11-18 17:22:39 -05:00
|
|
|
existing_tools << tool
|
|
|
|
tool_call = partial
|
|
|
|
if tool_call.partial?
|
|
|
|
if tool.class.allow_partial_tool_calls?
|
|
|
|
tool.partial_invoke
|
|
|
|
update_blk.call("", cancel, tool.custom_raw, :partial_tool)
|
|
|
|
end
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2024-01-05 13:21:14 -05:00
|
|
|
tool_found = true
|
2024-06-03 18:59:15 -04:00
|
|
|
# a bit hacky, but extra newlines do no harm
|
|
|
|
if needs_newlines
|
2024-10-29 19:28:20 -04:00
|
|
|
update_blk.call("\n\n", cancel)
|
2024-06-03 18:59:15 -04:00
|
|
|
needs_newlines = false
|
|
|
|
end
|
|
|
|
|
2024-11-11 16:14:30 -05:00
|
|
|
process_tool(tool, raw_context, llm, cancel, update_blk, prompt, context)
|
|
|
|
tools_ran += 1
|
|
|
|
ongoing_chain &&= tool.chain_next_response?
|
2024-11-18 17:22:39 -05:00
|
|
|
|
|
|
|
tool_halted = true if !tool.chain_next_response?
|
2024-01-04 08:44:07 -05:00
|
|
|
else
|
2024-11-18 17:22:39 -05:00
|
|
|
next if tool_halted
|
2024-06-03 18:59:15 -04:00
|
|
|
needs_newlines = true
|
2024-11-11 16:14:30 -05:00
|
|
|
if partial.is_a?(DiscourseAi::Completions::ToolCall)
|
|
|
|
Rails.logger.warn("DiscourseAi: Tool not found: #{partial.name}")
|
|
|
|
else
|
|
|
|
update_blk.call(partial, cancel)
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
2023-05-20 03:45:54 -04:00
|
|
|
end
|
2023-06-05 17:09:33 -04:00
|
|
|
|
2024-01-05 13:21:14 -05:00
|
|
|
if !tool_found
|
|
|
|
ongoing_chain = false
|
|
|
|
raw_context << [result, bot_user.username]
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
total_completions += 1
|
2023-08-21 18:36:41 -04:00
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
# do not allow tools when we are at the end of a chain (total_completions == MAX_COMPLETIONS)
|
2024-01-12 12:36:44 -05:00
|
|
|
prompt.tools = [] if total_completions == MAX_COMPLETIONS
|
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
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
raw_context
|
2023-05-16 13:38:21 -04:00
|
|
|
end
|
|
|
|
|
2024-01-05 13:21:14 -05:00
|
|
|
private
|
|
|
|
|
2024-05-05 19:49:02 -04:00
|
|
|
def process_tool(tool, raw_context, llm, cancel, update_blk, prompt, context)
|
2024-03-01 15:53:21 -05:00
|
|
|
tool_call_id = tool.tool_call_id
|
2024-05-05 19:49:02 -04:00
|
|
|
invocation_result_json = invoke_tool(tool, llm, cancel, context, &update_blk).to_json
|
2024-03-01 15:53:21 -05:00
|
|
|
|
|
|
|
tool_call_message = {
|
|
|
|
type: :tool_call,
|
|
|
|
id: tool_call_id,
|
2024-03-08 16:46:40 -05:00
|
|
|
content: { arguments: tool.parameters }.to_json,
|
|
|
|
name: tool.name,
|
2024-03-01 15:53:21 -05:00
|
|
|
}
|
|
|
|
|
2024-03-08 16:46:40 -05:00
|
|
|
tool_message = {
|
|
|
|
type: :tool,
|
|
|
|
id: tool_call_id,
|
|
|
|
content: invocation_result_json,
|
|
|
|
name: tool.name,
|
|
|
|
}
|
2024-03-01 15:53:21 -05:00
|
|
|
|
|
|
|
if tool.standalone?
|
|
|
|
standalone_context =
|
|
|
|
context.dup.merge(
|
|
|
|
conversation_context: [
|
|
|
|
context[:conversation_context].last,
|
|
|
|
tool_call_message,
|
|
|
|
tool_message,
|
|
|
|
],
|
|
|
|
)
|
|
|
|
prompt = persona.craft_prompt(standalone_context)
|
|
|
|
else
|
|
|
|
prompt.push(**tool_call_message)
|
|
|
|
prompt.push(**tool_message)
|
|
|
|
end
|
|
|
|
|
2024-03-08 16:46:40 -05:00
|
|
|
raw_context << [tool_call_message[:content], tool_call_id, "tool_call", tool.name]
|
|
|
|
raw_context << [invocation_result_json, tool_call_id, "tool", tool.name]
|
2024-03-01 15:53:21 -05:00
|
|
|
end
|
|
|
|
|
2024-05-05 19:49:02 -04:00
|
|
|
def invoke_tool(tool, llm, cancel, context, &update_blk)
|
2024-11-18 17:22:39 -05:00
|
|
|
show_placeholder = !context[:skip_tool_details] && !tool.class.allow_partial_tool_calls?
|
2024-11-13 14:58:24 -05:00
|
|
|
|
|
|
|
update_blk.call("", cancel, build_placeholder(tool.summary, "")) if show_placeholder
|
2023-05-20 03:45:54 -04:00
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
result =
|
2024-05-07 07:55:46 -04:00
|
|
|
tool.invoke do |progress|
|
2024-11-13 14:58:24 -05:00
|
|
|
if show_placeholder
|
|
|
|
placeholder = build_placeholder(tool.summary, progress)
|
|
|
|
update_blk.call("", cancel, placeholder)
|
|
|
|
end
|
2024-01-04 08:44:07 -05:00
|
|
|
end
|
2023-08-28 20:43:58 -04:00
|
|
|
|
2024-11-13 14:58:24 -05:00
|
|
|
if show_placeholder
|
|
|
|
tool_details = build_placeholder(tool.summary, tool.details, custom_raw: tool.custom_raw)
|
2024-10-29 19:28:20 -04:00
|
|
|
update_blk.call(tool_details, cancel, nil, :tool_details)
|
2024-11-13 14:58:24 -05:00
|
|
|
elsif tool.custom_raw.present?
|
|
|
|
update_blk.call(tool.custom_raw, cancel, nil, :custom_raw)
|
2024-06-12 03:58:28 -04:00
|
|
|
end
|
2023-05-21 22:09:14 -04:00
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
result
|
2023-05-23 09:08:17 -04:00
|
|
|
end
|
2023-05-11 09:03:03 -04:00
|
|
|
|
2024-02-28 00:46:32 -05:00
|
|
|
def self.guess_model(bot_user)
|
2024-06-18 13:32:14 -04:00
|
|
|
associated_llm = LlmModel.find_by(user_id: bot_user.id)
|
2024-05-13 11:46:42 -04:00
|
|
|
|
2024-06-18 13:32:14 -04:00
|
|
|
return if associated_llm.nil? # Might be a persona user. Handled by constructor.
|
2024-05-13 11:46:42 -04:00
|
|
|
|
2024-06-18 13:32:14 -04:00
|
|
|
"custom:#{associated_llm.id}"
|
2023-06-19 18:45:31 -04:00
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
def build_placeholder(summary, details, custom_raw: nil)
|
|
|
|
placeholder = +(<<~HTML)
|
|
|
|
<details>
|
|
|
|
<summary>#{summary}</summary>
|
|
|
|
<p>#{details}</p>
|
|
|
|
</details>
|
|
|
|
HTML
|
2023-05-20 03:45:54 -04:00
|
|
|
|
2024-01-09 07:20:28 -05:00
|
|
|
if custom_raw
|
|
|
|
placeholder << "\n"
|
|
|
|
placeholder << custom_raw
|
|
|
|
else
|
|
|
|
# we need this for cursor placeholder to work
|
|
|
|
# doing this in CSS is very hard
|
|
|
|
# if changing test with a custom tool such as search
|
|
|
|
placeholder << "<span></span>\n\n"
|
|
|
|
end
|
2023-05-11 09:03:03 -04:00
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
placeholder
|
2023-05-11 09:03:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|