- Converted initialization methods from receiver methods to package-level functions - Simplified method calls by directly using the global Neo instance - Removed redundant method receiver references in initialization logic - Improved code readability and reduced unnecessary method complexity This change streamlines the Neo initialization process and makes the code more straightforward and maintainable.
194 lines
3.6 KiB
Go
194 lines
3.6 KiB
Go
package neo
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
|
|
"github.com/fatih/color"
|
|
"github.com/yaoapp/gou/application"
|
|
"github.com/yaoapp/gou/connector"
|
|
"github.com/yaoapp/kun/log"
|
|
"github.com/yaoapp/yao/config"
|
|
"github.com/yaoapp/yao/neo/assistant"
|
|
"github.com/yaoapp/yao/neo/rag"
|
|
"github.com/yaoapp/yao/neo/store"
|
|
"github.com/yaoapp/yao/neo/vision"
|
|
"github.com/yaoapp/yao/neo/vision/driver"
|
|
)
|
|
|
|
// Neo the neo AI assistant
|
|
var Neo *DSL
|
|
|
|
// Load load AIGC
|
|
func Load(cfg config.Config) error {
|
|
|
|
setting := DSL{
|
|
ID: "neo",
|
|
Prompts: []assistant.Prompt{},
|
|
Option: map[string]interface{}{},
|
|
Allows: []string{},
|
|
StoreSetting: store.Setting{
|
|
Prefix: "yao_neo_",
|
|
Connector: "default",
|
|
},
|
|
}
|
|
|
|
bytes, err := application.App.Read(filepath.Join("neo", "neo.yml"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
err = application.Parse("neo.yml", bytes, &setting)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if setting.StoreSetting.MaxSize == 0 {
|
|
setting.StoreSetting.MaxSize = 100
|
|
}
|
|
|
|
Neo = &setting
|
|
|
|
// Store Setting
|
|
err = initStore()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Initialize RAG
|
|
initRAG()
|
|
|
|
// Initialize Vision
|
|
initVision()
|
|
|
|
// Initialize Assistant
|
|
err = initAssistant()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// initRAG initialize the RAG instance
|
|
func initRAG() {
|
|
if Neo.RAGSetting.Engine.Driver == "" {
|
|
return
|
|
}
|
|
instance, err := rag.New(Neo.RAGSetting)
|
|
if err != nil {
|
|
color.Red("[Neo] Failed to initialize RAG: %v", err)
|
|
log.Error("[Neo] Failed to initialize RAG: %v", err)
|
|
return
|
|
}
|
|
|
|
Neo.RAG = instance
|
|
}
|
|
|
|
// initStore initialize the store
|
|
func initStore() error {
|
|
|
|
var err error
|
|
if Neo.StoreSetting.Connector == "default" || Neo.StoreSetting.Connector == "" {
|
|
Neo.Store, err = store.NewXun(Neo.StoreSetting)
|
|
return err
|
|
}
|
|
|
|
// other connector
|
|
conn, err := connector.Select(Neo.StoreSetting.Connector)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if conn.Is(connector.DATABASE) {
|
|
Neo.Store, err = store.NewXun(Neo.StoreSetting)
|
|
return err
|
|
|
|
} else if conn.Is(connector.REDIS) {
|
|
Neo.Store = store.NewRedis()
|
|
return nil
|
|
|
|
} else if conn.Is(connector.MONGO) {
|
|
Neo.Store = store.NewMongo()
|
|
return nil
|
|
}
|
|
|
|
return fmt.Errorf("%s store connector %s not support", Neo.ID, Neo.StoreSetting.Connector)
|
|
}
|
|
|
|
// initVision initialize the Vision instance
|
|
func initVision() {
|
|
if Neo.VisionSetting.Storage.Driver == "" {
|
|
return
|
|
}
|
|
|
|
cfg := &driver.Config{
|
|
Storage: Neo.VisionSetting.Storage,
|
|
Model: Neo.VisionSetting.Model,
|
|
}
|
|
|
|
instance, err := vision.New(cfg)
|
|
if err != nil {
|
|
color.Red("[Neo] Failed to initialize Vision: %v", err)
|
|
log.Error("[Neo] Failed to initialize Vision: %v", err)
|
|
return
|
|
}
|
|
|
|
Neo.Vision = instance
|
|
}
|
|
|
|
// initAssistant initialize the assistant
|
|
func initAssistant() error {
|
|
|
|
// Set Storage
|
|
assistant.SetStorage(Neo.Store)
|
|
|
|
// Assistant RAG
|
|
if Neo.RAG != nil {
|
|
assistant.SetRAG(
|
|
Neo.RAG.Engine(),
|
|
Neo.RAG.FileUpload(),
|
|
Neo.RAG.Vectorizer(),
|
|
assistant.RAGSetting{
|
|
IndexPrefix: Neo.RAGSetting.IndexPrefix,
|
|
},
|
|
)
|
|
}
|
|
|
|
// Assistant Vision
|
|
if Neo.Vision != nil {
|
|
assistant.SetVision(Neo.Vision)
|
|
}
|
|
|
|
// Default Connector
|
|
assistant.SetConnector(Neo.Connector)
|
|
|
|
// Load Built-in Assistants
|
|
err := assistant.LoadBuiltIn()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Default Assistant
|
|
defaultAssistant, err := defaultAssistant()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
Neo.Assistant = defaultAssistant
|
|
return nil
|
|
}
|
|
|
|
// defaultAssistant get the default assistant
|
|
func defaultAssistant() (*assistant.Assistant, error) {
|
|
if Neo.Use != "" {
|
|
return assistant.Get(Neo.Use)
|
|
}
|
|
|
|
name := Neo.Name
|
|
if name == "" {
|
|
name = "Neo"
|
|
}
|
|
|
|
return assistant.GetByConnector(Neo.Connector, name)
|
|
}
|