@@ -23,6 +23,7 @@ local notify = require('CopilotChat.notify')
23
23
local utils = require (' CopilotChat.utils' )
24
24
local file_cache = {}
25
25
local url_cache = {}
26
+ local embedding_cache = {}
26
27
27
28
local M = {}
28
29
@@ -661,15 +662,21 @@ function M.filter_embeddings(prompt, model, headless, embeddings)
661
662
notify .publish (notify .STATUS , ' Ranking embeddings' )
662
663
663
664
-- Build query from history and prompt
664
- local query = ' '
665
+ local query = prompt
665
666
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
671
679
end
672
- query = query .. ' \n ' .. prompt
673
680
674
681
-- Rank embeddings by symbols
675
682
embeddings = data_ranked_by_symbols (query , embeddings , MIN_SYMBOL_SIMILARITY )
@@ -678,26 +685,46 @@ function M.filter_embeddings(prompt, model, headless, embeddings)
678
685
log .debug (string.format (' %s: %s - %s' , i , item .score , item .filename ))
679
686
end
680
687
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 , {
683
704
content = query ,
684
705
filename = ' query' ,
685
706
filetype = ' raw' ,
686
707
})
687
708
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
690
717
691
718
-- Rate embeddings by relatedness to the query
692
- local embedded_query = table.remove (embeddings , # embeddings )
719
+ local embedded_query = table.remove (results , # results )
693
720
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
697
724
log .debug (string.format (' %s: %s - %s' , i , item .score , item .filename ))
698
725
end
699
726
700
- return embeddings
727
+ return results
701
728
end
702
729
703
730
return M
0 commit comments