Use InMemoryDB in place of a RAGDB to store things as files
Signed-off-by: mudler <mudler@localai.io>
This commit is contained in:
@@ -55,34 +55,34 @@ func main() {
|
|||||||
stateDir := cwd + "/pool"
|
stateDir := cwd + "/pool"
|
||||||
os.MkdirAll(stateDir, 0755)
|
os.MkdirAll(stateDir, 0755)
|
||||||
|
|
||||||
var dbStore RAGDB
|
var ragDB RAGDB
|
||||||
lai := llm.NewClient(apiKey, apiURL+"/v1", timeout)
|
lai := llm.NewClient(apiKey, apiURL+"/v1", timeout)
|
||||||
|
|
||||||
switch vectorStore {
|
switch vectorStore {
|
||||||
case "localai":
|
case "localai":
|
||||||
laiStore := rag.NewStoreClient(apiURL, apiKey)
|
laiStore := rag.NewStoreClient(apiURL, apiKey)
|
||||||
dbStore = rag.NewLocalAIRAGDB(laiStore, lai)
|
ragDB = rag.NewLocalAIRAGDB(laiStore, lai)
|
||||||
default:
|
default:
|
||||||
var err error
|
var err error
|
||||||
dbStore, err = rag.NewChromemDB("local-agent-framework", stateDir, lai, embeddingModel)
|
ragDB, err = rag.NewChromemDB("local-agent-framework", stateDir, lai, embeddingModel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pool, err := NewAgentPool(testModel, apiURL, stateDir, dbStore)
|
db, err := NewInMemoryDB(stateDir, ragDB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := NewInMemoryDB(stateDir, dbStore)
|
pool, err := NewAgentPool(testModel, apiURL, stateDir, db)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(db.Database) > 0 && kbdisableIndexing != "true" {
|
if len(db.Database) > 0 && kbdisableIndexing != "true" {
|
||||||
xlog.Info("Loading knowledgebase from disk, to skip run with KBDISABLEINDEX=true")
|
xlog.Info("Loading knowledgebase from disk, to skip run with KBDISABLEINDEX=true")
|
||||||
if err := db.SaveToStore(); err != nil {
|
if err := db.SaveAllToStore(); err != nil {
|
||||||
xlog.Info("Error storing in the KB", err)
|
xlog.Info("Error storing in the KB", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,10 +20,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type InMemoryDatabase struct {
|
type InMemoryDatabase struct {
|
||||||
|
RAGDB
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
Database []string
|
Database []string
|
||||||
path string
|
path string
|
||||||
rag RAGDB
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadDB(path string) ([]string, error) {
|
func loadDB(path string) ([]string, error) {
|
||||||
@@ -48,7 +48,7 @@ func NewInMemoryDB(knowledgebase string, store RAGDB) (*InMemoryDatabase, error)
|
|||||||
return &InMemoryDatabase{
|
return &InMemoryDatabase{
|
||||||
Database: []string{},
|
Database: []string{},
|
||||||
path: poolfile,
|
path: poolfile,
|
||||||
rag: store,
|
RAGDB: store,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,21 +57,21 @@ func NewInMemoryDB(knowledgebase string, store RAGDB) (*InMemoryDatabase, error)
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &InMemoryDatabase{
|
return &InMemoryDatabase{
|
||||||
|
RAGDB: store,
|
||||||
Database: poolData,
|
Database: poolData,
|
||||||
path: poolfile,
|
path: poolfile,
|
||||||
rag: store,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *InMemoryDatabase) SaveToStore() error {
|
func (db *InMemoryDatabase) SaveAllToStore() error {
|
||||||
for _, d := range db.Database {
|
for _, d := range db.Database {
|
||||||
if d == "" {
|
if d == "" {
|
||||||
// skip empty chunks
|
// skip empty chunks
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
err := db.rag.Store(d)
|
err := db.Store(d)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error storing in the KB: %w", err)
|
return fmt.Errorf("error storing in the KB: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,17 +82,17 @@ func (db *InMemoryDatabase) Reset() error {
|
|||||||
db.Lock()
|
db.Lock()
|
||||||
db.Database = []string{}
|
db.Database = []string{}
|
||||||
db.Unlock()
|
db.Unlock()
|
||||||
if err := db.rag.Reset(); err != nil {
|
if err := db.RAGDB.Reset(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return db.SaveDB()
|
return db.SaveDB()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *InMemoryDatabase) AddEntry(entry string) error {
|
func (db *InMemoryDatabase) Store(entry string) error {
|
||||||
db.Lock()
|
db.Lock()
|
||||||
defer db.Unlock()
|
defer db.Unlock()
|
||||||
db.Database = append(db.Database, entry)
|
db.Database = append(db.Database, entry)
|
||||||
return nil
|
return db.RAGDB.Store(entry)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (db *InMemoryDatabase) SaveDB() error {
|
func (db *InMemoryDatabase) SaveDB() error {
|
||||||
@@ -150,15 +150,11 @@ func StringsToKB(db *InMemoryDatabase, chunkSize int, content ...string) {
|
|||||||
xlog.Info("chunks: ", len(chunks))
|
xlog.Info("chunks: ", len(chunks))
|
||||||
for _, chunk := range chunks {
|
for _, chunk := range chunks {
|
||||||
xlog.Info("Chunk size: ", len(chunk))
|
xlog.Info("Chunk size: ", len(chunk))
|
||||||
db.AddEntry(chunk)
|
db.Store(chunk)
|
||||||
}
|
}
|
||||||
|
|
||||||
db.SaveDB()
|
db.SaveDB()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := db.SaveToStore(); err != nil {
|
|
||||||
xlog.Info("Error storing in the KB", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// splitParagraphIntoChunks takes a paragraph and a maxChunkSize as input,
|
// splitParagraphIntoChunks takes a paragraph and a maxChunkSize as input,
|
||||||
|
|||||||
Reference in New Issue
Block a user