Class: PromptCanary::Storage::SQLite
- Inherits:
-
Object
- Object
- PromptCanary::Storage::SQLite
- Defined in:
- lib/prompt_canary/storage/sqlite.rb
Instance Method Summary collapse
-
#initialize(path: "prompt_canary.db") ⇒ SQLite
constructor
A new instance of SQLite.
- #read_recent(prompt:, version:, limit:) ⇒ Object
- #write(record) ⇒ Object
Constructor Details
#initialize(path: "prompt_canary.db") ⇒ SQLite
Returns a new instance of SQLite.
9 10 11 12 13 |
# File 'lib/prompt_canary/storage/sqlite.rb', line 9 def initialize(path: "prompt_canary.db") @db = ::SQLite3::Database.new(path) @db.results_as_hash = true create_table end |
Instance Method Details
#read_recent(prompt:, version:, limit:) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/prompt_canary/storage/sqlite.rb', line 29 def read_recent(prompt:, version:, limit:) rows = @db.execute( "SELECT * FROM calls WHERE prompt = ? AND version = ? ORDER BY recorded_at DESC LIMIT ?", [prompt, version, limit] ) rows.reverse.map do |row| { prompt: row["prompt"], version: row["version"], latency_ms: row["latency_ms"], tokens: row["tokens"] ? JSON.parse(row["tokens"], symbolize_names: true) : nil, error: row["error"] ? StandardError.new(row["error"]) : nil, recorded_at: Time.parse(row["recorded_at"]) } end end |
#write(record) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/prompt_canary/storage/sqlite.rb', line 15 def write(record) @db.execute( "INSERT INTO calls (prompt, version, latency_ms, tokens, error, recorded_at) VALUES (?, ?, ?, ?, ?, ?)", [ record[:prompt], record[:version], record[:latency_ms], JSON.dump(record[:tokens]), record[:error]&., record[:recorded_at].iso8601 ] ) end |