File tree 1 file changed +44
-0
lines changed
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
1
+ import { defineTask } from 'nitropack/runtime'
2
+ import type { StoredEmbeddings } from '~~/server/utils/embeddings'
3
+
4
+ export default defineTask ( {
5
+ meta : {
6
+ name : 'populate-vectorize' ,
7
+ description : 'Populate Vectorize with issue embeddings' ,
8
+ } ,
9
+ async run ( ) {
10
+ const kv = hubKV ( )
11
+ const vectorize = typeof hubVectorize !== 'undefined' ? hubVectorize ( 'issues' ) : null
12
+ if ( ! vectorize ) {
13
+ return { error : 'Vectorize not available' }
14
+ }
15
+ const keys = await kv . getKeys ( 'issue:' ) . then ( r => r . reverse ( ) )
16
+ console . info ( 'Loaded' , keys . length , 'issues' )
17
+ const total = keys . length
18
+ let count = 1
19
+ try {
20
+ do {
21
+ const batch = await kv . getItems < StoredEmbeddings > ( keys . splice ( 0 , 100 ) )
22
+ for ( const issue of batch ) {
23
+ try {
24
+ await vectorize . insert ( [ {
25
+ id : issue . key ,
26
+ values : issue . value . embeddings ,
27
+ metadata : issue . value . metadata ,
28
+ } ] )
29
+ console . log ( `${ count ++ } /${ total } ` )
30
+ }
31
+ catch {
32
+ keys . push ( issue . key )
33
+ console . log ( 'retrying' , issue . key )
34
+ }
35
+ }
36
+ } while ( keys . length > 0 )
37
+ }
38
+ catch {
39
+ return { keys }
40
+ }
41
+
42
+ return { count }
43
+ } ,
44
+ } )
You can’t perform that action at this time.
0 commit comments