Module: SqlChatbot::Prompts::Answer
- Defined in:
- lib/sql_chatbot/prompts/answer.rb
Constant Summary collapse
- SYSTEM_PROMPTS =
{ "data" => <<~P.freeze, "data_with_code" => <<~P.freeze, "code" => <<~P.freeze, "navigation" => <<~P.freeze, "guidance" => <<~P.freeze, "greeting" => <<~P.freeze, "unsafe" => <<~P.freeze, }.freeze
Class Method Summary collapse
- .build_messages(question:, type:, history: [], sql_result: nil, sql_query: nil, code_snippets: nil, page_context: nil, navigation_links: nil, route_list: nil, enum_context: nil) ⇒ Object
- .format_code_snippets(snippets) ⇒ Object
- .format_sql_result(rows) ⇒ Object
- .format_value(val) ⇒ Object
Class Method Details
.build_messages(question:, type:, history: [], sql_result: nil, sql_query: nil, code_snippets: nil, page_context: nil, navigation_links: nil, route_list: nil, enum_context: nil) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/sql_chatbot/prompts/answer.rb', line 59 def self.(question:, type:, history: [], sql_result: nil, sql_query: nil, code_snippets: nil, page_context: nil, navigation_links: nil, route_list: nil, enum_context: nil) system_prompt = SYSTEM_PROMPTS[type] || SYSTEM_PROMPTS["data"] # Inject custom_context so the LLM can translate status codes, IDs, etc. if (type == "data" || type == "data_with_code") && defined?(SqlChatbot) && SqlChatbot.respond_to?(:config) custom = SqlChatbot.config&.custom_context if custom && !custom.strip.empty? system_prompt = system_prompt + "\n\nDOMAIN CONTEXT (use this to translate codes/IDs to human-readable labels):\n#{custom}" end end # Inject auto-detected enum mappings so the LLM can translate integer codes to labels if enum_context && !enum_context.strip.empty? && (type == "data" || type == "data_with_code") system_prompt = system_prompt + "\n\nENUM MAPPINGS (use these to translate integer status/type codes to human-readable labels):\n#{enum_context}" end user_content = "" if history && !history.empty? recent = history.last(4) history_text = recent.map { |m| "#{m[:role]}: #{m[:content]}" }.join("\n") user_content += "Conversation history:\n#{history_text}\n\n" end user_content += "Question: #{question}" if sql_result && (type == "data" || type == "data_with_code") user_content += "\n\nSQL Query:\n#{sql_query || 'N/A'}" user_content += "\n\nQuery Results:\n#{format_sql_result(sql_result)}" end if code_snippets && !code_snippets.empty? user_content += "\n\nRelevant Code:\n#{format_code_snippets(code_snippets)}" end if page_context && (type == "navigation" || type == "guidance") user_content += "\n\nCurrent page context:\n#{page_context}" end if && !.empty? && (type == "navigation" || type == "guidance") user_content += "\n\nAvailable navigation links:\n#{.join("\n")}" end if route_list && route_list != "No application routes detected." && (type == "navigation" || type == "guidance") user_content += "\n\n#{route_list}" end [ { role: "system", content: system_prompt }, { role: "user", content: user_content }, ] end |
.format_code_snippets(snippets) ⇒ Object
150 151 152 153 154 |
# File 'lib/sql_chatbot/prompts/answer.rb', line 150 def self.format_code_snippets(snippets) return "" if snippets.nil? || snippets.empty? snippets.map { |s| "File: #{s[:file_path]}\n```\n#{s[:content]}\n```" }.join("\n\n") end |
.format_sql_result(rows) ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sql_chatbot/prompts/answer.rb', line 111 def self.format_sql_result(rows) return "[ZERO RESULTS] No matching records exist." if rows.nil? || rows.empty? columns = rows.first.keys header = columns.join(" | ") separator = columns.map { "---" }.join(" | ") body = rows.map { |row| columns.map { |col| format_value(row[col]) }.join(" | ") }.join("\n") "#{header}\n#{separator}\n#{body}" end |
.format_value(val) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/sql_chatbot/prompts/answer.rb', line 122 def self.format_value(val) return "" if val.nil? case val when Time, DateTime val.strftime("%B %-d, %Y at %-I:%M %p") when Date val.strftime("%B %-d, %Y") else str = val.to_s if str.match?(/\A\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}/) begin Time.parse(str).strftime("%B %-d, %Y at %-I:%M %p") rescue str end elsif str.match?(/\A\d{4}-\d{2}-\d{2}\z/) begin Date.parse(str).strftime("%B %-d, %Y") rescue str end else str end end end |