Fix: flow control

This commit is contained in:
Grail Finder
2025-01-09 19:58:08 +03:00
parent 363bbae2c7
commit f40d8afe08
2 changed files with 37 additions and 24 deletions

2
bot.go
View File

@@ -147,7 +147,7 @@ func chatRagUse(qText string) (string, error) {
} }
// get raw text // get raw text
resps := []string{} resps := []string{}
logger.Info("sqlvec resp", "vecs", respVecs) logger.Info("sqlvec resp", "vecs len", len(respVecs))
for _, rv := range respVecs { for _, rv := range respVecs {
resps = append(resps, rv.RawText) resps = append(resps, rv.RawText)
} }

View File

@@ -13,6 +13,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"sync"
"github.com/neurosnap/sentences/english" "github.com/neurosnap/sentences/english"
) )
@@ -56,7 +57,7 @@ func (r *RAG) LoadRAG(fpath string) error {
var ( var (
// TODO: to config // TODO: to config
workers = 5 workers = 5
batchSize = 200 batchSize = 100
maxChSize = 1000 maxChSize = 1000
// //
// psize = 3 // psize = 3
@@ -68,7 +69,11 @@ func (r *RAG) LoadRAG(fpath string) error {
vectorCh = make(chan []models.VectorRow, maxChSize) vectorCh = make(chan []models.VectorRow, maxChSize)
errCh = make(chan error, 1) errCh = make(chan error, 1)
doneCh = make(chan bool, 1) doneCh = make(chan bool, 1)
lock = new(sync.Mutex)
) )
defer close(doneCh)
defer close(errCh)
defer close(batchCh)
// group sentences // group sentences
paragraphs := []string{} paragraphs := []string{}
par := strings.Builder{} par := strings.Builder{}
@@ -100,18 +105,19 @@ func (r *RAG) LoadRAG(fpath string) error {
left, right = right, right+batchSize left, right = right, right+batchSize
ctn++ ctn++
} }
r.logger.Info("finished batching", "batches#", len(batchCh)) r.logger.Info("finished batching", "batches#", len(batchCh), "paragraphs", len(paragraphs), "sentences", len(sents))
for w := 0; w < workers; w++ { for w := 0; w < workers; w++ {
go r.batchToVectorHFAsync(len(paragraphs), batchCh, vectorCh, errCh, doneCh, path.Base(fpath)) go r.batchToVectorHFAsync(lock, w, batchCh, vectorCh, errCh, doneCh, path.Base(fpath))
} }
// wait for emb to be done
<-doneCh
// write to db // write to db
return r.writeVectors(vectorCh, doneCh) return r.writeVectors(vectorCh)
} }
func (r *RAG) writeVectors(vectorCh <-chan []models.VectorRow, doneCh <-chan bool) error { func (r *RAG) writeVectors(vectorCh chan []models.VectorRow) error {
for { for {
select { for batch := range vectorCh {
case batch := <-vectorCh:
for _, vector := range batch { for _, vector := range batch {
if err := r.store.WriteVector(&vector); err != nil { if err := r.store.WriteVector(&vector); err != nil {
r.logger.Error("failed to write vector", "error", err, "slug", vector.Slug) r.logger.Error("failed to write vector", "error", err, "slug", vector.Slug)
@@ -119,36 +125,43 @@ func (r *RAG) writeVectors(vectorCh <-chan []models.VectorRow, doneCh <-chan boo
// return err // return err
} }
} }
r.logger.Info("wrote batch to db", "size", len(batch)) r.logger.Info("wrote batch to db", "size", len(batch), "vector_chan_len", len(vectorCh))
case <-doneCh: if len(vectorCh) == 0 {
r.logger.Info("rag finished") r.logger.Info("finished writing vectors")
return nil defer close(vectorCh)
return nil
}
} }
} }
} }
func (r *RAG) batchToVectorHFAsync(limit int, inputCh <-chan map[int][]string, func (r *RAG) batchToVectorHFAsync(lock *sync.Mutex, id int, inputCh <-chan map[int][]string,
vectorCh chan<- []models.VectorRow, errCh chan error, doneCh chan bool, filename string) { vectorCh chan<- []models.VectorRow, errCh chan error, doneCh chan bool, filename string) {
r.logger.Info("to vector batches", "batches#", len(inputCh))
for { for {
lock.Lock()
if len(inputCh) == 0 {
if len(doneCh) == 0 {
doneCh <- true
}
lock.Unlock()
return
}
select { select {
case linesMap := <-inputCh: case linesMap := <-inputCh:
for leftI, v := range linesMap { for leftI, v := range linesMap {
r.fecthEmbHF(v, errCh, vectorCh, fmt.Sprintf("%s_%d", filename, leftI), filename) r.fecthEmbHF(v, errCh, vectorCh, fmt.Sprintf("%s_%d", filename, leftI), filename)
if leftI+200 >= limit { // last batch // if leftI+200 >= limit { // last batch
doneCh <- true // // doneCh <- true
return // return
} // }
} }
case <-doneCh: lock.Unlock()
r.logger.Info("got done")
close(errCh)
close(doneCh)
return
case err := <-errCh: case err := <-errCh:
r.logger.Error("got an error", "error", err) r.logger.Error("got an error", "error", err)
lock.Unlock()
return return
} }
r.logger.Info("to vector batches", "batches#", len(inputCh), "worker#", id)
} }
} }
@@ -202,7 +215,7 @@ func (r *RAG) fecthEmbHF(lines []string, errCh chan error, vectorCh chan<- []mod
vector := models.VectorRow{ vector := models.VectorRow{
Embeddings: e, Embeddings: e,
RawText: lines[i], RawText: lines[i],
Slug: slug, Slug: fmt.Sprintf("%s_%d", slug, i),
FileName: filename, FileName: filename,
} }
vectors[i] = vector vectors[i] = vector