diff options
author | Grail Finder <wohilas@gmail.com> | 2025-01-07 11:11:44 +0300 |
---|---|---|
committer | Grail Finder <wohilas@gmail.com> | 2025-01-07 11:11:44 +0300 |
commit | 7bbedd93cf078fc7496a6779cf9eda6e588e64c0 (patch) | |
tree | 954a8c767362fab8da11fe1fad4643f2a44b62c9 | |
parent | b822b3a1613ef7f1c9ed8fa5aaddfaffbfc513a4 (diff) |
Enha (RAG): raw text as primary key in vector db
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | config/config.go | 1 | ||||
-rw-r--r-- | main.go | 2 | ||||
-rw-r--r-- | models/db.go | 1 | ||||
-rw-r--r-- | rag/main.go | 20 | ||||
-rw-r--r-- | storage/migrations/002_add_vector.up.sql | 6 | ||||
-rw-r--r-- | storage/vector.go | 11 | ||||
-rw-r--r-- | tui.go | 2 | ||||
-rwxr-xr-x | vec0.so | bin | 0 -> 146680 bytes |
9 files changed, 21 insertions, 23 deletions
@@ -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); - delete chat option; - 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: - bot responding (or hanging) blocks everything; + diff --git a/config/config.go b/config/config.go index 3c79564..eb5f3f1 100644 --- a/config/config.go +++ b/config/config.go @@ -12,6 +12,7 @@ type Config struct { LogFile string `toml:"LogFile"` UserRole string `toml:"UserRole"` ToolRole string `toml:"ToolRole"` + ToolUse bool `toml:"ToolUse"` AssistantRole string `toml:"AssistantRole"` AssistantIcon string `toml:"AssistantIcon"` UserIcon string `toml:"UserIcon"` @@ -10,7 +10,7 @@ var ( botRespMode = false editMode = false 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{} ) diff --git a/models/db.go b/models/db.go index db4ae75..ce1309a 100644 --- a/models/db.go +++ b/models/db.go @@ -39,7 +39,6 @@ type Memory struct { // vector models type VectorRow struct { - ID uint32 `db:"id" json:"id"` Embeddings []float32 `db:"embeddings" json:"embeddings"` Slug string `db:"slug" json:"slug"` RawText string `db:"raw_text" json:"raw_text"` diff --git a/rag/main.go b/rag/main.go index a7084bf..da919b4 100644 --- a/rag/main.go +++ b/rag/main.go @@ -88,7 +88,9 @@ func (r *RAG) writeVectors(vectorCh <-chan []models.VectorRow, doneCh <-chan boo case batch := <-vectorCh: for _, vector := range batch { 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)) @@ -226,14 +228,14 @@ func (r *RAG) LineToVector(line string) ([]float32, error) { return emb[0], nil } -func (r *RAG) saveLine(topic, line string, emb *models.EmbeddingResp) error { - row := &models.VectorRow{ - Embeddings: emb.Embedding, - Slug: topic, - RawText: line, - } - return r.store.WriteVector(row) -} +// func (r *RAG) saveLine(topic, line string, emb *models.EmbeddingResp) error { +// row := &models.VectorRow{ +// Embeddings: emb.Embedding, +// Slug: topic, +// RawText: line, +// } +// return r.store.WriteVector(row) +// } func (r *RAG) SearchEmb(emb *models.EmbeddingResp) ([]models.VectorRow, error) { return r.store.SearchClosest(emb.Embedding) diff --git a/storage/migrations/002_add_vector.up.sql b/storage/migrations/002_add_vector.up.sql index f64aecb..6c8fb52 100644 --- a/storage/migrations/002_add_vector.up.sql +++ b/storage/migrations/002_add_vector.up.sql @@ -1,13 +1,11 @@ CREATE VIRTUAL TABLE IF NOT EXISTS embeddings USING vec0( - id INTEGER PRIMARY KEY AUTOINCREMENT, embedding FLOAT[5120], slug TEXT NOT NULL, - raw_text TEXT NOT NULL + raw_text TEXT PRIMARY KEY, ); CREATE VIRTUAL TABLE IF NOT EXISTS embeddings_384 USING vec0( - id INTEGER PRIMARY KEY AUTOINCREMENT, embedding FLOAT[384], slug TEXT NOT NULL, - raw_text TEXT NOT NULL + raw_text TEXT PRIMARY KEY ); diff --git a/storage/vector.go b/storage/vector.go index 1579686..fe479d8 100644 --- a/storage/vector.go +++ b/storage/vector.go @@ -68,7 +68,6 @@ func (p ProviderSQL) WriteVector(row *models.VectorRow) error { } err = stmt.Exec() if err != nil { - p.logger.Error("failed exec a stmt", "error", err) return err } return nil @@ -85,7 +84,6 @@ func (p ProviderSQL) SearchClosest(q []float32) ([]models.VectorRow, error) { } stmt, _, err := p.s3Conn.Prepare( fmt.Sprintf(`SELECT - id, distance, embedding, slug, @@ -109,12 +107,11 @@ func (p ProviderSQL) SearchClosest(q []float32) ([]models.VectorRow, error) { resp := []models.VectorRow{} for stmt.Step() { res := models.VectorRow{} - res.ID = uint32(stmt.ColumnInt64(0)) - res.Distance = float32(stmt.ColumnFloat(1)) - emb := stmt.ColumnRawText(2) + res.Distance = float32(stmt.ColumnFloat(0)) + emb := stmt.ColumnRawText(1) res.Embeddings = decodeUnsafe(emb) - res.Slug = stmt.ColumnText(3) - res.RawText = stmt.ColumnText(4) + res.Slug = stmt.ColumnText(2) + res.RawText = stmt.ColumnText(3) resp = append(resp, res) } if err := stmt.Err(); err != nil { @@ -230,7 +230,7 @@ func colorText() { } 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) { Binary files differ |