@@ -23,6 +23,7 @@ local notify = require('CopilotChat.notify')
2323local utils = require (' CopilotChat.utils' )
2424local file_cache = {}
2525local url_cache = {}
26+ local embedding_cache = {}
2627
2728local M = {}
2829
@@ -661,15 +662,21 @@ function M.filter_embeddings(prompt, model, headless, embeddings)
661662 notify .publish (notify .STATUS , ' Ranking embeddings' )
662663
663664 -- Build query from history and prompt
664- local query = ' '
665+ local query = prompt
665666 if not headless then
666- for _ , message in ipairs (client .history ) do
667- if message .role == ' user' then
668- query = query .. ' \n ' .. message .content
669- end
670- end
667+ query = table.concat (
668+ vim
669+ .iter (client .history )
670+ :filter (function (m )
671+ return m .role == ' user'
672+ end )
673+ :map (function (m )
674+ return vim .trim (m .content )
675+ end )
676+ :totable (),
677+ ' \n '
678+ ) .. ' \n ' .. prompt
671679 end
672- query = query .. ' \n ' .. prompt
673680
674681 -- Rank embeddings by symbols
675682 embeddings = data_ranked_by_symbols (query , embeddings , MIN_SYMBOL_SIMILARITY )
@@ -678,26 +685,46 @@ function M.filter_embeddings(prompt, model, headless, embeddings)
678685 log .debug (string.format (' %s: %s - %s' , i , item .score , item .filename ))
679686 end
680687
681- -- Embed the query
682- table.insert (embeddings , {
688+ -- Prepare embeddings for processing
689+ local to_process = {}
690+ local results = {}
691+ for _ , input in ipairs (embeddings ) do
692+ input .filename = input .filename or ' unknown'
693+ input .filetype = input .filetype or ' text'
694+ if input .content then
695+ local cache_key = input .filename .. utils .quick_hash (input .content )
696+ if embedding_cache [cache_key ] then
697+ table.insert (results , embedding_cache [cache_key ])
698+ else
699+ table.insert (to_process , input )
700+ end
701+ end
702+ end
703+ table.insert (to_process , {
683704 content = query ,
684705 filename = ' query' ,
685706 filetype = ' raw' ,
686707 })
687708
688- -- Get embeddings from all items
689- embeddings = client :embed (embeddings , model )
709+ -- Embed the data and process the results
710+ for _ , input in ipairs (client :embed (to_process , model )) do
711+ if input .filetype ~= ' raw' then
712+ local cache_key = input .filename .. utils .quick_hash (input .content )
713+ embedding_cache [cache_key ] = input
714+ end
715+ table.insert (results , input )
716+ end
690717
691718 -- Rate embeddings by relatedness to the query
692- local embedded_query = table.remove (embeddings , # embeddings )
719+ local embedded_query = table.remove (results , # results )
693720 log .debug (' Embedded query:' , embedded_query .content )
694- embeddings = data_ranked_by_relatedness (embedded_query , embeddings , MIN_SEMANTIC_SIMILARITY )
695- log .debug (' Ranked embeddings:' , # embeddings )
696- for i , item in ipairs (embeddings ) do
721+ results = data_ranked_by_relatedness (embedded_query , results , MIN_SEMANTIC_SIMILARITY )
722+ log .debug (' Ranked embeddings:' , # results )
723+ for i , item in ipairs (results ) do
697724 log .debug (string.format (' %s: %s - %s' , i , item .score , item .filename ))
698725 end
699726
700- return embeddings
727+ return results
701728end
702729
703730return M
0 commit comments