Enha (RAG): raw text as primary key in vector db

This commit is contained in:
Grail Finder
2025-01-07 11:11:44 +03:00
parent b822b3a161
commit 7bbedd93cf
9 changed files with 21 additions and 23 deletions

View File

@@ -33,6 +33,7 @@
- lets say we have two (or more) agents with the same name across multiple chats. These agents go and ask db for topics they memorised. Now they can access topics that aren't meant for them. (so memory should have an option: shareable; that indicates if that memory can be shared across chats); - lets say we have two (or more) agents with the same name across multiple chats. These agents go and ask db for topics they memorised. Now they can access topics that aren't meant for them. (so memory should have an option: shareable; that indicates if that memory can be shared across chats);
- delete chat option; - delete chat option;
- server mode: no tui but api calls with the func calling, rag, other middleware; - server mode: no tui but api calls with the func calling, rag, other middleware;
- boolean flag to use/not use tools. I see it as a msg from a tool to an llm "Hey, it might be good idea to use me!";
### FIX: ### FIX:
- bot responding (or hanging) blocks everything; + - bot responding (or hanging) blocks everything; +

View File

@@ -12,6 +12,7 @@ type Config struct {
LogFile string `toml:"LogFile"` LogFile string `toml:"LogFile"`
UserRole string `toml:"UserRole"` UserRole string `toml:"UserRole"`
ToolRole string `toml:"ToolRole"` ToolRole string `toml:"ToolRole"`
ToolUse bool `toml:"ToolUse"`
AssistantRole string `toml:"AssistantRole"` AssistantRole string `toml:"AssistantRole"`
AssistantIcon string `toml:"AssistantIcon"` AssistantIcon string `toml:"AssistantIcon"`
UserIcon string `toml:"UserIcon"` UserIcon string `toml:"UserIcon"`

View File

@@ -10,7 +10,7 @@ var (
botRespMode = false botRespMode = false
editMode = false editMode = false
selectedIndex = int(-1) selectedIndex = int(-1)
indexLine = "F12 to show keys help; bot resp mode: %v; char: %s; chat: %s; RAGEnabled: %v; EmbedURL: %s" indexLine = "F12 to show keys help; bot resp mode: %v; char: %s; chat: %s; RAGEnabled: %v; toolUseAdviced: %v"
focusSwitcher = map[tview.Primitive]tview.Primitive{} focusSwitcher = map[tview.Primitive]tview.Primitive{}
) )

View File

@@ -39,7 +39,6 @@ type Memory struct {
// vector models // vector models
type VectorRow struct { type VectorRow struct {
ID uint32 `db:"id" json:"id"`
Embeddings []float32 `db:"embeddings" json:"embeddings"` Embeddings []float32 `db:"embeddings" json:"embeddings"`
Slug string `db:"slug" json:"slug"` Slug string `db:"slug" json:"slug"`
RawText string `db:"raw_text" json:"raw_text"` RawText string `db:"raw_text" json:"raw_text"`

View File

@@ -88,7 +88,9 @@ func (r *RAG) writeVectors(vectorCh <-chan []models.VectorRow, doneCh <-chan boo
case batch := <-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 {
return err r.logger.Error("failed to write vector", "error", err, "slug", vector.Slug)
continue // a duplicate is not critical
// return err
} }
} }
r.logger.Info("wrote batch to db", "size", len(batch)) r.logger.Info("wrote batch to db", "size", len(batch))
@@ -226,14 +228,14 @@ func (r *RAG) LineToVector(line string) ([]float32, error) {
return emb[0], nil return emb[0], nil
} }
func (r *RAG) saveLine(topic, line string, emb *models.EmbeddingResp) error { // func (r *RAG) saveLine(topic, line string, emb *models.EmbeddingResp) error {
row := &models.VectorRow{ // row := &models.VectorRow{
Embeddings: emb.Embedding, // Embeddings: emb.Embedding,
Slug: topic, // Slug: topic,
RawText: line, // RawText: line,
} // }
return r.store.WriteVector(row) // return r.store.WriteVector(row)
} // }
func (r *RAG) SearchEmb(emb *models.EmbeddingResp) ([]models.VectorRow, error) { func (r *RAG) SearchEmb(emb *models.EmbeddingResp) ([]models.VectorRow, error) {
return r.store.SearchClosest(emb.Embedding) return r.store.SearchClosest(emb.Embedding)

View File

@@ -1,13 +1,11 @@
CREATE VIRTUAL TABLE IF NOT EXISTS embeddings USING vec0( CREATE VIRTUAL TABLE IF NOT EXISTS embeddings USING vec0(
id INTEGER PRIMARY KEY AUTOINCREMENT,
embedding FLOAT[5120], embedding FLOAT[5120],
slug TEXT NOT NULL, slug TEXT NOT NULL,
raw_text TEXT NOT NULL raw_text TEXT PRIMARY KEY,
); );
CREATE VIRTUAL TABLE IF NOT EXISTS embeddings_384 USING vec0( CREATE VIRTUAL TABLE IF NOT EXISTS embeddings_384 USING vec0(
id INTEGER PRIMARY KEY AUTOINCREMENT,
embedding FLOAT[384], embedding FLOAT[384],
slug TEXT NOT NULL, slug TEXT NOT NULL,
raw_text TEXT NOT NULL raw_text TEXT PRIMARY KEY
); );

View File

@@ -68,7 +68,6 @@ func (p ProviderSQL) WriteVector(row *models.VectorRow) error {
} }
err = stmt.Exec() err = stmt.Exec()
if err != nil { if err != nil {
p.logger.Error("failed exec a stmt", "error", err)
return err return err
} }
return nil return nil
@@ -85,7 +84,6 @@ func (p ProviderSQL) SearchClosest(q []float32) ([]models.VectorRow, error) {
} }
stmt, _, err := p.s3Conn.Prepare( stmt, _, err := p.s3Conn.Prepare(
fmt.Sprintf(`SELECT fmt.Sprintf(`SELECT
id,
distance, distance,
embedding, embedding,
slug, slug,
@@ -109,12 +107,11 @@ func (p ProviderSQL) SearchClosest(q []float32) ([]models.VectorRow, error) {
resp := []models.VectorRow{} resp := []models.VectorRow{}
for stmt.Step() { for stmt.Step() {
res := models.VectorRow{} res := models.VectorRow{}
res.ID = uint32(stmt.ColumnInt64(0)) res.Distance = float32(stmt.ColumnFloat(0))
res.Distance = float32(stmt.ColumnFloat(1)) emb := stmt.ColumnRawText(1)
emb := stmt.ColumnRawText(2)
res.Embeddings = decodeUnsafe(emb) res.Embeddings = decodeUnsafe(emb)
res.Slug = stmt.ColumnText(3) res.Slug = stmt.ColumnText(2)
res.RawText = stmt.ColumnText(4) res.RawText = stmt.ColumnText(3)
resp = append(resp, res) resp = append(resp, res)
} }
if err := stmt.Err(); err != nil { if err := stmt.Err(); err != nil {

2
tui.go
View File

@@ -230,7 +230,7 @@ func colorText() {
} }
func updateStatusLine() { func updateStatusLine() {
position.SetText(fmt.Sprintf(indexLine, botRespMode, cfg.AssistantRole, activeChatName, cfg.RAGEnabled, cfg.EmbedURL)) position.SetText(fmt.Sprintf(indexLine, botRespMode, cfg.AssistantRole, activeChatName, cfg.RAGEnabled, cfg.ToolUse))
} }
func initSysCards() ([]string, error) { func initSysCards() ([]string, error) {

BIN
vec0.so Executable file

Binary file not shown.