should be ready to go
This commit is contained in:
@@ -23,7 +23,7 @@ sources. Example:
|
|||||||
|
|
||||||
```json
|
```json
|
||||||
{
|
{
|
||||||
"source name (unused by the script)":{
|
"source name":{
|
||||||
"filters":{
|
"filters":{
|
||||||
"blacklist":".git",
|
"blacklist":".git",
|
||||||
"extension_whitelist":"md"
|
"extension_whitelist":"md"
|
||||||
@@ -36,12 +36,11 @@ sources. Example:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The filters are based on `utility.tree`'s filter options. `initialize_command`
|
The filters are based on `utility.tree`'s filter options (optional).
|
||||||
is only run the first time (optional), while `refresh_command` is run each time
|
`initialize_command` is only run the first time (optional),
|
||||||
(optional). Commands will be run in the specified `path`. `strip_frontmatter` is
|
while `refresh_command` is run each time (optional).
|
||||||
for removing YAML frontmatter (common in Markdown files) if truthy.
|
Commands will be run in the specified `path`.
|
||||||
|
`strip_frontmatter` will remove YAML frontmatter (common in Markdown files).
|
||||||
Filters are optional.
|
|
||||||
|
|
||||||
### `synopsis_generator.lua`
|
### `synopsis_generator.lua`
|
||||||
Chooses a random file within `notebook`, and generates a novel synopsis from it.
|
Chooses a random file within `notebook`, and generates a novel synopsis from it.
|
||||||
|
|||||||
+15
-1
@@ -18,7 +18,7 @@ if package.config:sub(1, 1) == "\\" then
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
utility = {
|
utility = {
|
||||||
OS = "UNIX-like",
|
OS = "Linux",
|
||||||
path_separator = "/",
|
path_separator = "/",
|
||||||
temp_directory = "/tmp/",
|
temp_directory = "/tmp/",
|
||||||
commands = {
|
commands = {
|
||||||
@@ -299,6 +299,18 @@ utility.file_size = function(file_path)
|
|||||||
return utility.open(file_path, "rb", function(file) return file:seek("end") end)
|
return utility.open(file_path, "rb", function(file) return file:seek("end") end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
utility.sha512sum = function(file_path)
|
||||||
|
local sha512sum
|
||||||
|
if utility.OS == "Linux" then
|
||||||
|
sha512sum = os.capture_safe("shasum -p -t -a 512 " .. file_path:enquote()) -- TODO check this
|
||||||
|
elseif utility.OS == "macOS" then
|
||||||
|
sha512sum = os.capture_safe("shasum -U -a 512 " .. file_path:enquote())
|
||||||
|
elseif utility.OS == "Windows" then
|
||||||
|
error("utility.sha512sum() not implemented for Windows.")
|
||||||
|
end
|
||||||
|
return sha512sum:sub(1, 128)
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
utility.escape_quotes_and_escapes = function(input)
|
utility.escape_quotes_and_escapes = function(input)
|
||||||
@@ -570,4 +582,6 @@ end
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if utility.capture_safe("uname"):find("Darwin") == 1 then utility.OS = "macOS" end
|
||||||
|
|
||||||
return utility
|
return utility
|
||||||
|
|||||||
+70
-23
@@ -21,8 +21,21 @@ if not config.models then
|
|||||||
utility.save_config()
|
utility.save_config()
|
||||||
end
|
end
|
||||||
|
|
||||||
local refresh_file_list = function(data_source)
|
local embeddings
|
||||||
timing.mark("Assembling file list.")
|
local embeddings_file_path = "PRIVATE_DATA/memory/+embeddings.json"
|
||||||
|
if not utility.path_exists(embeddings_file_path) then
|
||||||
|
os.execute("mkdir -p PRIVATE_DATA/memory")
|
||||||
|
utility.save_data({
|
||||||
|
files = {},
|
||||||
|
vectors = {},
|
||||||
|
}, embeddings_file_path)
|
||||||
|
end
|
||||||
|
embeddings = utility.load_data(embeddings_file_path)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
local refresh_file_list = function(source_name, data_source)
|
||||||
|
timing.mark("Assembling file list for " .. source_name .. ".")
|
||||||
|
|
||||||
local full_path = "PRIVATE_DATA" .. utility.path_separator .. data_source.path
|
local full_path = "PRIVATE_DATA" .. utility.path_separator .. data_source.path
|
||||||
if data_source.initialize_command and (not config.models.initialized_sources[data_source.path]) then
|
if data_source.initialize_command and (not config.models.initialized_sources[data_source.path]) then
|
||||||
@@ -47,7 +60,7 @@ local refresh_file_list = function(data_source)
|
|||||||
file_list[#file_list + 1] = file_name
|
file_list[#file_list + 1] = file_name
|
||||||
end)
|
end)
|
||||||
|
|
||||||
timing.mark("Finished assembling file list.")
|
timing.mark("Finished assembling file list for " .. source_name .. ".")
|
||||||
return file_list
|
return file_list
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -97,36 +110,70 @@ local process_file = function(data_source, file_name)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local embeddings = {}
|
local new_embeddings = {}
|
||||||
for i = 1, #chunks do
|
for i = 1, #chunks do
|
||||||
embeddings[i] = generate_embeddings(chunks[i])
|
new_embeddings[i] = generate_embeddings(chunks[i]) or {}
|
||||||
end
|
end
|
||||||
|
|
||||||
if embeddings[1] == nil then
|
if #new_embeddings[1] == 0 then
|
||||||
-- TODO average all other embeddings to this one
|
-- average all embeddings to make the core file embedding
|
||||||
|
local count = #new_embeddings[2]
|
||||||
|
for vector_index = 1, count do
|
||||||
|
local total = 0
|
||||||
|
for chunk = 2, #new_embeddings do
|
||||||
|
total = total + new_embeddings[chunk][vector_index]
|
||||||
|
end
|
||||||
|
new_embeddings[1][vector_index] = total / count
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
return chunks, embeddings
|
return chunks, new_embeddings
|
||||||
end
|
end
|
||||||
|
|
||||||
-- TODO somewhere near the top of this function chain needs to be the ability to recognize a file as unchanged and skip re-generating embeddings
|
|
||||||
-- this means running the same shasum command I will put below on that source file before trying to generate an embedding
|
|
||||||
-- something something merging new changes into the old data structure means we need to load that data structure first so we only replace modified chunks
|
|
||||||
local refresh_sources = function()
|
local refresh_sources = function()
|
||||||
|
local tmp_file_path = "PRIVATE_DATA/.tmp.2b65c19b-0883-49ca-8247-b1fe7760f922"
|
||||||
|
os.execute("mkdir -p PRIVATE_DATA/memory")
|
||||||
|
|
||||||
local sources = utility.load_data("PRIVATE_DATA/sources.json")
|
local sources = utility.load_data("PRIVATE_DATA/sources.json")
|
||||||
for _, data_source in pairs(sources) do
|
for source_name, data_source in pairs(sources) do
|
||||||
local file_list = refresh_file_list(data_source)
|
local file_list = refresh_file_list(source_name, data_source)
|
||||||
timing.mark("Generating embeddings.")
|
|
||||||
|
timing.mark("Generating embeddings for " .. source_name .. ".")
|
||||||
for _, file_name in ipairs(file_list) do
|
for _, file_name in ipairs(file_list) do
|
||||||
local chunks, embeddings = process_file(file_name)
|
local function loop()
|
||||||
-- TODO decide how these will be stored
|
local sha512sum = utility.sha512sum(tmp_file_path)
|
||||||
local sum_file_path = "PRIVATE_DATA/.tmp.2b65c19b-0883-49ca-8247-b1fe7760f922"
|
if embeddings.vectors[sha512sum] then return end
|
||||||
utility.write_file(sum_file_path, text)
|
|
||||||
-- -p ensures compatibility across OSes, -t ensures it is read as text (some OSes default differently), -a 512 ensures it is a 512-bit SHA2 sum
|
local file_chunks, file_embeddings = process_file(data_source, file_name)
|
||||||
local sha512sum = utility.capture_safe("shasum -p -t -a 512 " .. sum_file_path)
|
|
||||||
-- for chunks, save to a specific local tmp file to shasum, then mv that file based on the sum
|
local file_sums = {}
|
||||||
-- for embeddings, store sum = embedding ? (look at how generate_embeddings.lua worked)
|
for i = 1, #file_chunks do
|
||||||
|
local function loop()
|
||||||
|
local text = file_chunks[i]
|
||||||
|
local current_embedding = file_embeddings[i]
|
||||||
|
|
||||||
|
utility.write_file(tmp_file_path, text)
|
||||||
|
|
||||||
|
sha512sum = utility.sha512sum(tmp_file_path)
|
||||||
|
file_sums[#file_sums + 1] = sha512sum
|
||||||
|
if embeddings.vectors[sha512sum] then return end
|
||||||
|
|
||||||
|
os.execute(utility.commands.move .. tmp_file_path:enquote() .. " " .. "PRIVATE_DATA/memory/")
|
||||||
|
embeddings.vectors[sha512sum] = current_embedding
|
||||||
end
|
end
|
||||||
timing.mark("Finished generating embeddings.")
|
loop()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
embeddings.files[file_name] = file_sums
|
||||||
|
end
|
||||||
|
loop()
|
||||||
|
end
|
||||||
|
|
||||||
|
timing.mark("Finished generating embeddings for " .. source_name .. ".")
|
||||||
|
end
|
||||||
|
|
||||||
|
utility.save_data(embeddings)
|
||||||
|
os.execute("rm " .. tmp_file_path:enquote())
|
||||||
end
|
end
|
||||||
|
|
||||||
|
refresh_sources()
|
||||||
|
|||||||
@@ -6,13 +6,7 @@ local json = utility.require("dkjson")
|
|||||||
|
|
||||||
local file_name = arg[1]
|
local file_name = arg[1]
|
||||||
|
|
||||||
print("---")
|
print(utility.OS)
|
||||||
print(utility.capture_safe("shasum -p"))
|
|
||||||
print("---")
|
|
||||||
|
|
||||||
print("---")
|
|
||||||
print(utility.capture_safe("shasum -U -a 512 test.lua"))
|
|
||||||
print("---")
|
|
||||||
|
|
||||||
os.exit(0)
|
os.exit(0)
|
||||||
|
|
||||||
@@ -23,9 +17,6 @@ if prompt:sub(-1) ~= "\n" then
|
|||||||
prompt = prompt .. "\n\n"
|
prompt = prompt .. "\n\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
local file_contents = utility.open(file_name, "r", function(file)
|
local file_contents = utility.read_file(file_name)
|
||||||
return file:read("*all")
|
|
||||||
end)
|
|
||||||
prompt = prompt .. file_contents
|
prompt = prompt .. file_contents
|
||||||
|
-- print(utility.llm_prompt(prompt))
|
||||||
-- output = output:sub(1, -2) -- strip extra newline from utility.capture_safe
|
|
||||||
|
|||||||
Reference in New Issue
Block a user