mirror of
https://github.com/discourse/discourse-ai.git
synced 2025-03-09 02:40:50 +00:00
Previous to this change we relied on explicit loading for a files in Discourse AI. This had a few downsides: - Busywork whenever you add a file (an extra require relative) - We were not keeping to conventions internally ... some places were OpenAI others are OpenAi - Autoloader did not work which lead to lots of full application broken reloads when developing. This moves all of DiscourseAI into a Zeitwerk compatible structure. It also leaves some minimal amount of manual loading (automation - which is loading into an existing namespace that may or may not be there) To avoid needing /lib/discourse_ai/... we mount a namespace thus we are able to keep /lib pointed at ::DiscourseAi Various files were renamed to get around zeitwerk rules and minimize usage of custom inflections Though we can get custom inflections to work it is not worth it, will require a Discourse core patch which means we create a hard dependency.
72 lines
2.2 KiB
Ruby
72 lines
2.2 KiB
Ruby
#frozen_string_literal: true
|
|
|
|
module DiscourseAi
|
|
module AiBot
|
|
module Personas
|
|
class SqlHelper < Persona
|
|
def self.schema
|
|
return @schema if defined?(@schema)
|
|
|
|
tables = Hash.new
|
|
priority_tables = %w[posts topics notifications users user_actions user_emails]
|
|
|
|
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
|
|
|
|
schema = +(priority_tables.map { |name| "#{name}(#{tables[name].join(",")})" }.join("\n"))
|
|
|
|
schema << "\nOther tables (schema redacted, available on request): "
|
|
tables.each do |table_name, _|
|
|
next if priority_tables.include?(table_name)
|
|
schema << "#{table_name} "
|
|
end
|
|
|
|
@schema = schema
|
|
end
|
|
|
|
def commands
|
|
all_available_commands
|
|
end
|
|
|
|
def all_available_commands
|
|
[DiscourseAi::AiBot::Commands::DbSchemaCommand]
|
|
end
|
|
|
|
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.
|
|
- 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.
|
|
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}
|
|
|
|
|
|
The current schema for the current DB is:
|
|
{{
|
|
#{self.class.schema}
|
|
}}
|
|
PROMPT
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|