2023-08-30 02:15:03 -04:00
|
|
|
#frozen_string_literal: true
|
|
|
|
|
|
|
|
module DiscourseAi
|
|
|
|
module AiBot
|
|
|
|
module Personas
|
|
|
|
class SqlHelper < Persona
|
|
|
|
def self.schema
|
|
|
|
return @schema if defined?(@schema)
|
|
|
|
|
|
|
|
tables = Hash.new
|
2024-03-08 16:46:40 -05:00
|
|
|
priority_tables = %w[
|
|
|
|
posts
|
|
|
|
topics
|
|
|
|
notifications
|
|
|
|
users
|
|
|
|
user_actions
|
|
|
|
user_emails
|
|
|
|
categories
|
|
|
|
groups
|
|
|
|
]
|
2023-08-30 02:15:03 -04:00
|
|
|
|
|
|
|
DB.query(<<~SQL).each { |row| (tables[row.table_name] ||= []) << row.column_name }
|
|
|
|
select table_name, column_name from information_schema.columns
|
|
|
|
where table_schema = 'public'
|
|
|
|
order by table_name
|
|
|
|
SQL
|
|
|
|
|
2024-03-08 16:46:40 -05:00
|
|
|
priority =
|
|
|
|
+(priority_tables.map { |name| "#{name}(#{tables[name].join(",")})" }.join("\n"))
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-03-08 16:46:40 -05:00
|
|
|
other_tables = +""
|
2023-08-30 02:15:03 -04:00
|
|
|
tables.each do |table_name, _|
|
|
|
|
next if priority_tables.include?(table_name)
|
2024-03-08 16:46:40 -05:00
|
|
|
other_tables << "#{table_name} "
|
2023-08-30 02:15:03 -04:00
|
|
|
end
|
|
|
|
|
2024-03-08 16:46:40 -05:00
|
|
|
@schema = { priority_tables: priority, other_tables: other_tables }
|
2023-08-30 02:15:03 -04:00
|
|
|
end
|
|
|
|
|
2024-01-04 08:44:07 -05:00
|
|
|
def tools
|
|
|
|
[Tools::DbSchema]
|
2023-08-30 02:15:03 -04:00
|
|
|
end
|
|
|
|
|
2024-02-02 15:09:34 -05:00
|
|
|
def temperature
|
|
|
|
0.2
|
|
|
|
end
|
|
|
|
|
2023-08-30 02:15:03 -04:00
|
|
|
def system_prompt
|
|
|
|
<<~PROMPT
|
|
|
|
You are a PostgreSQL expert.
|
2024-03-08 16:46:40 -05:00
|
|
|
- Avoid returning any text to the user prior to a tool call.
|
2023-10-27 01:21:09 -04:00
|
|
|
- You understand and generate Discourse Markdown but specialize in creating queries.
|
|
|
|
- You live in a Discourse Forum Message.
|
2024-03-08 16:46:40 -05:00
|
|
|
- Format SQL for maximum readability. Use line breaks, indentation, and spaces around operators. Add comments if needed to explain complex logic.
|
|
|
|
- Never warn or inform end user you are going to look up schema.
|
|
|
|
- Always try to get ALL the schema you need in the least tool calls.
|
|
|
|
- Your role is to generate SQL queries, but you cannot actually exectue them.
|
|
|
|
- When generating SQL always use ```sql Markdown code blocks.
|
2023-10-27 01:21:09 -04:00
|
|
|
- When generating SQL NEVER end SQL samples with a semicolon (;).
|
|
|
|
|
2024-06-14 01:28:40 -04:00
|
|
|
- You also understand the special formatting rules for Data Explorer in Discourse.
|
|
|
|
- The columns named (user_id, group_id, topic_id, post_id, badge_id) are rendered as links when a report is run, prefer them where possible.
|
|
|
|
- You can define custom params to create flexible queries, example:
|
|
|
|
-- [params]
|
|
|
|
-- int :num = 1
|
|
|
|
-- text :name
|
|
|
|
|
|
|
|
SELECT :num, :name
|
|
|
|
- You support the types (integer, text, boolean, date)
|
|
|
|
|
|
|
|
|
|
|
|
- When generating SQL use markdown formatting for code blocks, example:
|
2023-10-27 01:21:09 -04:00
|
|
|
|
|
|
|
```sql
|
|
|
|
select 1 from table
|
|
|
|
```
|
2023-08-30 02:15:03 -04:00
|
|
|
|
|
|
|
The user_actions tables stores likes (action_type 1).
|
2024-03-08 16:46:40 -05:00
|
|
|
The topics table stores private/personal messages it uses archetype private_message for them.
|
2023-08-30 02:15:03 -04:00
|
|
|
notification_level can be: {muted: 0, regular: 1, tracking: 2, watching: 3, watching_first_post: 4}.
|
|
|
|
bookmarkable_type can be: Post,Topic,ChatMessage and more
|
|
|
|
|
|
|
|
Current time is: {time}
|
2024-03-08 16:46:40 -05:00
|
|
|
Participants here are: {participants}
|
2023-08-30 02:15:03 -04:00
|
|
|
|
2024-03-08 16:46:40 -05:00
|
|
|
Here is a partial list of tables in the database (you can retrieve schema from these tables as needed)
|
|
|
|
|
|
|
|
```
|
|
|
|
#{self.class.schema[:other_tables]}
|
|
|
|
```
|
|
|
|
|
|
|
|
You may look up schema for the tables listed above.
|
|
|
|
|
|
|
|
Here is full information on priority tables:
|
|
|
|
|
|
|
|
```
|
|
|
|
#{self.class.schema[:priority_tables]}
|
|
|
|
```
|
|
|
|
|
|
|
|
NEVER look up schema for the tables listed above, as their full schema is already provided.
|
2023-08-30 02:15:03 -04:00
|
|
|
|
|
|
|
PROMPT
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|