Sam a3c827efcc
FEATURE: allow personas to supply top_p and temperature params (#459)
* FEATURE: allow personas to supply top_p and temperature params

Code assistance generally are more focused at a lower temperature
This amends it so SQL Helper runs at 0.2 temperature vs the more
common default across LLMs of 1.0.

Reduced temperature leads to more focused, concise and predictable
answers for the SQL Helper

* fix tests

* This is not perfect, but far better than what we do today

Instead of fishing for

1. Draft sequence
2. Draft body

We skip (2), this means the composer "only" needs 1 http request to
open, we also want to eliminate (1) but it is a bit of a trickier
core change, may figure out how to pull it off (defer it to first draft save)

Value of bot drafts < value of opening bot conversations really fast
2024-02-03 07:09:34 +11:00

72 lines
2.1 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 tools
[Tools::DbSchema]
end
def temperature
0.2
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