From b06380d9fa805c7a1bffbe054f12ad0cc0dc0526 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 27 Oct 2023 16:21:09 +1100 Subject: [PATCH] FIX: avoid semicolons at the end of queries for SQL Helper (#268) This makes it easier to cut and paste snippets it is producing Also fine tune the prompt in an attempt to hone gpt 3.5 which is very finicky --- lib/modules/ai_bot/commands/db_schema_command.rb | 2 +- lib/modules/ai_bot/personas/sql_helper.rb | 15 ++++++++++++--- .../ai_bot/commands/db_schema_command_spec.rb | 2 +- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/modules/ai_bot/commands/db_schema_command.rb b/lib/modules/ai_bot/commands/db_schema_command.rb index 4f01c765..96831ae8 100644 --- a/lib/modules/ai_bot/commands/db_schema_command.rb +++ b/lib/modules/ai_bot/commands/db_schema_command.rb @@ -48,7 +48,7 @@ module DiscourseAi::AiBot::Commands schema_info = table_info.map { |table_name, columns| "#{table_name}(#{columns.join(",")})" }.join("\n") - { tables: @tables, schema_info: schema_info } + { schema_info: schema_info, tables: tables } end end end diff --git a/lib/modules/ai_bot/personas/sql_helper.rb b/lib/modules/ai_bot/personas/sql_helper.rb index 3900afa2..190dcbea 100644 --- a/lib/modules/ai_bot/personas/sql_helper.rb +++ b/lib/modules/ai_bot/personas/sql_helper.rb @@ -38,9 +38,18 @@ module DiscourseAi def system_prompt <<~PROMPT You are a PostgreSQL expert. - You understand and generate Discourse Markdown but specialize in creating queries. - You live in a Discourse Forum Message. - The schema in your training set MAY be out of date. + - You understand and generate Discourse Markdown but specialize in creating queries. + - You live in a Discourse Forum Message. + - The schema in your training set MAY be out of date. + - When generating SQL NEVER end SQL samples with a semicolon (;). + - When generating SQL always use ```sql markdown code blocks. + - Always format SQL in a highly readable format. + + Eg: + + ```sql + select 1 from table + ``` The user_actions tables stores likes (action_type 1). the topics table stores private/personal messages it uses archetype private_message for them. diff --git a/spec/lib/modules/ai_bot/commands/db_schema_command_spec.rb b/spec/lib/modules/ai_bot/commands/db_schema_command_spec.rb index 15da0041..e63277ad 100644 --- a/spec/lib/modules/ai_bot/commands/db_schema_command_spec.rb +++ b/spec/lib/modules/ai_bot/commands/db_schema_command_spec.rb @@ -10,7 +10,7 @@ RSpec.describe DiscourseAi::AiBot::Commands::DbSchemaCommand do expect(result[:schema_info]).to include("posts") expect(result[:schema_info]).to include("topics") - expect(result[:tables]).to eq(%w[posts topics]) + expect(result[:tables]).to eq("posts,topics") end end end