Resumable intermediates #2

Merged
tangent merged 17 commits from resumable-intermediates into main 2026-08-23 23:59:39 -06:00
2 changed files with 61 additions and 23 deletions
Showing only changes of commit 0a2380e60b - Show all commits
+19 -7
View File
@@ -1,9 +1,9 @@
local timing = {} local timing = {
entries = {}
}
function timing.timer() local function to_minutes(t) -- floored to tenths
return setmetatable({ return math.floor( t / 60 * 10 ) / 10
entries = {},
}, timing)
end end
function timing:start() function timing:start()
@@ -15,8 +15,20 @@ function timing:stop()
local entry = self.entries[#self.entries] local entry = self.entries[#self.entries]
entry.stop_time = os.time() entry.stop_time = os.time()
-- delta in minutes, floored to tenths return to_minutes(entry.stop_time - entry.start_time) -- return delta
return math.floor( (entry.stop_time - entry.start_time) / 60 * 10 ) / 10 end
function timing:print_report()
local minimum, maximum, sum = math.huge, -math.huge, 0
for _, data in pairs(self.entries) do
local delta = data.stop_time - data.start_time
if delta > maximum then maximum = delta end
if delta < minimum then minimum = delta end
sum = sum + delta
end
print("Total: " .. to_minutes(sum) .. " minutes.")
print("Averge: " .. to_minutes(sum / #self.entries) .. " minutes. Fastest: "
.. to_minutes(minimum) .. " minutes. Slowest: " .. to_minutes(maximum) .. " minutes.")
end end
return timing return timing
+42 -16
View File
@@ -2,7 +2,8 @@
package.path = (arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)")) .. "lib" .. package.config:sub(1, 1) .. "?.lua;" .. package.path package.path = (arg[0]:match("@?(.*/)") or arg[0]:match("@?(.*\\)")) .. "lib" .. package.config:sub(1, 1) .. "?.lua;" .. package.path
local utility = require "utility" local utility = require "utility"
-- TODO import timing library local prompts = require "prompts"
local timing = require "timing"
local config = utility.get_config("read-only") local config = utility.get_config("read-only")
if not config.llm_covers then config.llm_covers = {} end if not config.llm_covers then config.llm_covers = {} end
@@ -10,7 +11,17 @@ if not config.llm_covers.calibre_location then config.llm_covers.calibre_locatio
if not config.llm_covers.default_model then config.llm_covers.default_model = "gemma4:12b-mlx" end if not config.llm_covers.default_model then config.llm_covers.default_model = "gemma4:12b-mlx" end
if not config.llm_covers.maximum_bytes then config.llm_covers.maximum_bytes = 40000 end if not config.llm_covers.maximum_bytes then config.llm_covers.maximum_bytes = 40000 end
-- TODO import prompts, use new names from fork local read_all = function(file_name)
return utility.open(file_name, "r", function(file)
return file:read("*all")
end)
end
local write_all = function(file_name, text)
return utility.open(file_name, "w", function(file)
file:write(text)
file:write("\n")
end)
end
local strip_reasoning = function(text, reasoning_lines) local strip_reasoning = function(text, reasoning_lines)
if not reasoning_lines then reasoning_lines = {} end if not reasoning_lines then reasoning_lines = {} end
@@ -31,18 +42,27 @@ local strip_reasoning = function(text, reasoning_lines)
end end
end end
-- TODO import and rewrite send_prompt to use new timing library local send_prompt = function(text, model)
if type(text) == "table" then text = table.concat(text, "\n") end
local read_all = function(file_name) local tmp_file_name = utility.tmp_file_name()
return utility.open(file_name, "r", function(file) utility.open(tmp_file_name, "w", function(file)
return file:read("*all")
end)
end
local write_all = function(file_name, text)
return utility.open(file_name, "w", function(file)
file:write(text) file:write(text)
file:write("\n")
end) end)
timing:start()
-- word wrap breaks the raw output badly, so I need to implement my own for terminal output somehow
local output = utility.capture_safe("cat " .. tmp_file_name:enquote() .. " | ollama run " .. (model or default_model) .. " --nowordwrap")
os.execute("ollama stop " .. (model or default_model)) -- NOTE this makes things slower, but more stable
local delta = timing:stop()
print("Took " .. delta .. " minutes.")
os.execute("rm " .. tmp_file_name)
if not output then error("ollama failed to generate output") end
output = output:sub(1, -2) -- strip extra newline from utility.capture_safe
output = strip_reasoning(output)
return output
end end
local make_directories = function() local make_directories = function()
@@ -166,7 +186,7 @@ local condense_summary = function(name, data, sections)
print("Loaded condensed summary segment. " .. #sections .. " sections remaining.") print("Loaded condensed summary segment. " .. #sections .. " sections remaining.")
else else
print("Condensing summary. " .. #sections .. " sections remaining.") print("Condensing summary. " .. #sections .. " sections remaining.")
local text = send_prompt(squish_partials_prompt .. table.concat(sections_slice, "\n\n")) local text = send_prompt(prompts.combine_summaries .. table.concat(sections_slice, "\n\n"))
condensed_sections[#condensed_sections + 1] = text condensed_sections[#condensed_sections + 1] = text
print(#text .. " characters added to final context.") print(#text .. " characters added to final context.")
write_all(intermediate_condensation_file, text) write_all(intermediate_condensation_file, text)
@@ -199,7 +219,7 @@ local generate_summary = function(name, data)
else else
print("Processing section " .. (i + 1) .. " of " .. math.floor(data.extracted_file_size/config.llm_covers.maximum_bytes + 1)) print("Processing section " .. (i + 1) .. " of " .. math.floor(data.extracted_file_size/config.llm_covers.maximum_bytes + 1))
local section = text:sub(i * config.llm_covers.maximum_bytes, (i + 1) * config.llm_covers.maximum_bytes - 1) local section = text:sub(i * config.llm_covers.maximum_bytes, (i + 1) * config.llm_covers.maximum_bytes - 1)
section = send_prompt(partial_prompt .. section) section = send_prompt(prompts.section_summarization .. section)
sections[#sections + 1] = section sections[#sections + 1] = section
print(#section .. " characters added to intermediate context.") print(#section .. " characters added to intermediate context.")
write_all(intermediate_section_file, section) write_all(intermediate_section_file, section)
@@ -238,17 +258,23 @@ local generate_cover_briefs = function(books)
end end
print("Generating cover brief.") print("Generating cover brief.")
text = send_prompt(cover_brief_prompt .. text) text = send_prompt(prompts.cover_brief .. text)
local cover_brief_file = "cover_briefs/" .. name .. ".txt" local cover_brief_file = "cover_briefs/" .. name .. ".txt"
write_all(cover_brief_file, text) write_all(cover_brief_file, text)
write_all("for_gemini/" .. name .. ".txt", "Title: " .. name .. gemini_prompt .. text) write_all("for_gemini/" .. name .. ".txt", "Title: " .. name .. prompts.for_gemini .. text)
data.cover_brief_file = cover_brief_file data.cover_brief_file = cover_brief_file
end end
end end
make_directories() make_directories()
generate_cover_briefs(get_books())
local books = get_books()
convert_ebooks(books)
generate_cover_briefs(books)
print("")
timing:print_report()
-- ebook_file, extracted_file, extracted_file_size, -- ebook_file, extracted_file, extracted_file_size,
-- intermediate_file, condensed_intermediate_file, cover_brief_file -- intermediate_file, condensed_intermediate_file, cover_brief_file