FEATURE: add ai_bot_enabled_chat commands and tune search (#94)

* FEATURE: add ai_bot_enabled_chat commands and tune search

This allows admins to disable/enable GPT command integrations.

Also hones search results which were looping cause the result did not denote
the failure properly (it lost context)

* include more context for google command
include more context for time command

* type
This commit is contained in:
Sam
2023-06-21 17:10:30 +10:00
committed by GitHub
parent d1ab79e82f
commit a028309cbd
11 changed files with 83 additions and 17 deletions
+4 -2
View File
@@ -133,7 +133,7 @@ module DiscourseAi
[chain_next_response, post]
end
def format_results(rows, column_names = nil)
def format_results(rows, column_names = nil, args: nil)
rows = rows.map { |row| yield row } if block_given?
if !column_names
@@ -154,7 +154,9 @@ module DiscourseAi
# this is not the most efficient format
# however this is needed cause GPT 3.5 / 4 was steered using JSON
{ column_names: column_names, rows: rows }
result = { column_names: column_names, rows: rows }
result[:args] = args if args
result
end
protected
@@ -59,7 +59,7 @@ module DiscourseAi::AiBot::Commands
@last_num_results = parsed.dig("searchInformation", "totalResults").to_i
format_results(results) do |result|
format_results(results, args: json_data) do |result|
{
title: result["title"],
link: result["link"],
@@ -15,7 +15,7 @@ module DiscourseAi::AiBot::Commands
[
Parameter.new(
name: "search_query",
description: "Search query to run against the discourse instance",
description: "Search query (correct bad spelling, remove connector words!)",
type: "string",
),
Parameter.new(
@@ -89,8 +89,8 @@ module DiscourseAi::AiBot::Commands
}
end
def process(search_string)
parsed = JSON.parse(search_string)
def process(search_args)
parsed = JSON.parse(search_args)
limit = nil
@@ -127,9 +127,9 @@ module DiscourseAi::AiBot::Commands
@last_num_results = posts.length
if posts.blank?
[]
{ args: search_args, rows: [], instruction: "nothing was found, expand your search" }
else
format_results(posts) do |post|
format_results(posts, args: search_args) do |post|
{
title: post.topic.title,
url: Discourse.base_path + post.url,
+3 -3
View File
@@ -8,14 +8,14 @@ module DiscourseAi::AiBot::Commands
end
def desc
"!time RUBY_COMPATIBLE_TIMEZONE - will generate the time in a timezone"
"Will generate the time in a timezone"
end
def parameters
[
Parameter.new(
name: "timezone",
description: "Ruby compatible timezone",
description: "ALWAYS supply a Ruby compatible timezone",
type: "string",
required: true,
),
@@ -45,7 +45,7 @@ module DiscourseAi::AiBot::Commands
@last_timezone = timezone
@last_time = time.to_s
time.to_s
{ args: args, time: time.to_s }
end
end
end
+9 -3
View File
@@ -46,11 +46,12 @@ module DiscourseAi
temperature: temperature,
top_p: top_p,
max_tokens: max_tokens,
functions: available_functions,
) { |key, old_value, new_value| new_value.nil? ? old_value : new_value }
model = model_for(low_cost: prefer_low_cost)
params[:functions] = available_functions if available_functions.present?
DiscourseAi::Inference::OpenAiCompletions.perform!(prompt, model, **params, &blk)
end
@@ -87,12 +88,14 @@ module DiscourseAi
end
def available_commands
# note: Summarize command is not ready yet, leave it out for now
@cmds ||=
return @cmds if @cmds
all_commands =
[
Commands::CategoriesCommand,
Commands::TimeCommand,
Commands::SearchCommand,
Commands::SummarizeCommand,
].tap do |cmds|
cmds << Commands::TagsCommand if SiteSetting.tagging_enabled
cmds << Commands::ImageCommand if SiteSetting.ai_stability_api_key.present?
@@ -101,6 +104,9 @@ module DiscourseAi
cmds << Commands::GoogleCommand
end
end
allowed_commands = SiteSetting.ai_bot_enabled_chat_commands.split("|")
@cmds = all_commands.filter { |klass| allowed_commands.include?(klass.name) }
end
def model_for(low_cost: false)