Refactor Neo initialization methods to use global Neo instance
- 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.
This commit is contained in:
parent
d8b070338f
commit
86d4ea71cc
2 changed files with 30 additions and 30 deletions
56
neo/load.go
56
neo/load.go
|
|
@ -50,19 +50,19 @@ func Load(cfg config.Config) error {
|
||||||
Neo = &setting
|
Neo = &setting
|
||||||
|
|
||||||
// Store Setting
|
// Store Setting
|
||||||
err = Neo.initStore()
|
err = initStore()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Initialize RAG
|
// Initialize RAG
|
||||||
Neo.initRAG()
|
initRAG()
|
||||||
|
|
||||||
// Initialize Vision
|
// Initialize Vision
|
||||||
Neo.initVision()
|
initVision()
|
||||||
|
|
||||||
// Initialize Assistant
|
// Initialize Assistant
|
||||||
err = Neo.initAssistant()
|
err = initAssistant()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -71,60 +71,60 @@ func Load(cfg config.Config) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// initRAG initialize the RAG instance
|
// initRAG initialize the RAG instance
|
||||||
func (neo *DSL) initRAG() {
|
func initRAG() {
|
||||||
if neo.RAGSetting.Engine.Driver == "" {
|
if Neo.RAGSetting.Engine.Driver == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
instance, err := rag.New(neo.RAGSetting)
|
instance, err := rag.New(Neo.RAGSetting)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
color.Red("[Neo] Failed to initialize RAG: %v", err)
|
color.Red("[Neo] Failed to initialize RAG: %v", err)
|
||||||
log.Error("[Neo] Failed to initialize RAG: %v", err)
|
log.Error("[Neo] Failed to initialize RAG: %v", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
neo.RAG = instance
|
Neo.RAG = instance
|
||||||
}
|
}
|
||||||
|
|
||||||
// initStore initialize the store
|
// initStore initialize the store
|
||||||
func (neo *DSL) initStore() error {
|
func initStore() error {
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
if neo.StoreSetting.Connector == "default" || neo.StoreSetting.Connector == "" {
|
if Neo.StoreSetting.Connector == "default" || Neo.StoreSetting.Connector == "" {
|
||||||
neo.Store, err = store.NewXun(neo.StoreSetting)
|
Neo.Store, err = store.NewXun(Neo.StoreSetting)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// other connector
|
// other connector
|
||||||
conn, err := connector.Select(neo.StoreSetting.Connector)
|
conn, err := connector.Select(Neo.StoreSetting.Connector)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if conn.Is(connector.DATABASE) {
|
if conn.Is(connector.DATABASE) {
|
||||||
neo.Store, err = store.NewXun(neo.StoreSetting)
|
Neo.Store, err = store.NewXun(Neo.StoreSetting)
|
||||||
return err
|
return err
|
||||||
|
|
||||||
} else if conn.Is(connector.REDIS) {
|
} else if conn.Is(connector.REDIS) {
|
||||||
neo.Store = store.NewRedis()
|
Neo.Store = store.NewRedis()
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
} else if conn.Is(connector.MONGO) {
|
} else if conn.Is(connector.MONGO) {
|
||||||
neo.Store = store.NewMongo()
|
Neo.Store = store.NewMongo()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("%s store connector %s not support", neo.ID, neo.StoreSetting.Connector)
|
return fmt.Errorf("%s store connector %s not support", Neo.ID, Neo.StoreSetting.Connector)
|
||||||
}
|
}
|
||||||
|
|
||||||
// initVision initialize the Vision instance
|
// initVision initialize the Vision instance
|
||||||
func (neo *DSL) initVision() {
|
func initVision() {
|
||||||
if neo.VisionSetting.Storage.Driver == "" {
|
if Neo.VisionSetting.Storage.Driver == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := &driver.Config{
|
cfg := &driver.Config{
|
||||||
Storage: neo.VisionSetting.Storage,
|
Storage: Neo.VisionSetting.Storage,
|
||||||
Model: neo.VisionSetting.Model,
|
Model: Neo.VisionSetting.Model,
|
||||||
}
|
}
|
||||||
|
|
||||||
instance, err := vision.New(cfg)
|
instance, err := vision.New(cfg)
|
||||||
|
|
@ -134,11 +134,11 @@ func (neo *DSL) initVision() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
neo.Vision = instance
|
Neo.Vision = instance
|
||||||
}
|
}
|
||||||
|
|
||||||
// initAssistant initialize the assistant
|
// initAssistant initialize the assistant
|
||||||
func (neo *DSL) initAssistant() error {
|
func initAssistant() error {
|
||||||
|
|
||||||
// Set Storage
|
// Set Storage
|
||||||
assistant.SetStorage(Neo.Store)
|
assistant.SetStorage(Neo.Store)
|
||||||
|
|
@ -170,7 +170,7 @@ func (neo *DSL) initAssistant() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default Assistant
|
// Default Assistant
|
||||||
defaultAssistant, err := Neo.defaultAssistant()
|
defaultAssistant, err := defaultAssistant()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
@ -180,15 +180,15 @@ func (neo *DSL) initAssistant() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// defaultAssistant get the default assistant
|
// defaultAssistant get the default assistant
|
||||||
func (neo *DSL) defaultAssistant() (*assistant.Assistant, error) {
|
func defaultAssistant() (*assistant.Assistant, error) {
|
||||||
if neo.Use != "" {
|
if Neo.Use != "" {
|
||||||
return assistant.Get(neo.Use)
|
return assistant.Get(Neo.Use)
|
||||||
}
|
}
|
||||||
|
|
||||||
name := neo.Name
|
name := Neo.Name
|
||||||
if name == "" {
|
if name == "" {
|
||||||
name = "Neo"
|
name = "Neo"
|
||||||
}
|
}
|
||||||
|
|
||||||
return assistant.GetByConnector(neo.Connector, name)
|
return assistant.GetByConnector(Neo.Connector, name)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ import (
|
||||||
// Answer reply the message
|
// Answer reply the message
|
||||||
func (neo *DSL) Answer(ctx chatctx.Context, question string, c *gin.Context) error {
|
func (neo *DSL) Answer(ctx chatctx.Context, question string, c *gin.Context) error {
|
||||||
var err error
|
var err error
|
||||||
var ast assistant.API = neo.Assistant
|
var ast assistant.API = Neo.Assistant
|
||||||
if ctx.AssistantID != "" {
|
if ctx.AssistantID != "" {
|
||||||
ast, err = neo.Select(ctx.AssistantID)
|
ast, err = neo.Select(ctx.AssistantID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -28,7 +28,7 @@ func (neo *DSL) Answer(ctx chatctx.Context, question string, c *gin.Context) err
|
||||||
// Select select an assistant
|
// Select select an assistant
|
||||||
func (neo *DSL) Select(id string) (assistant.API, error) {
|
func (neo *DSL) Select(id string) (assistant.API, error) {
|
||||||
if id == "" {
|
if id == "" {
|
||||||
return neo.Assistant, nil
|
return Neo.Assistant, nil
|
||||||
}
|
}
|
||||||
return assistant.Get(id)
|
return assistant.Get(id)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue