Refactor assistant structure and replace base assistant with local implementation

- Replaced the base assistant package with a local assistant implementation, enhancing modularity and maintainability.
- Updated the newAssistantByConnector function to utilize the local assistant, ensuring consistent error handling and improved functionality.
- Removed obsolete base assistant files, streamlining the codebase and reducing complexity.
- Added a new 'mentionable' field to the assistant table in the Xun implementation, allowing for better management of assistant visibility in mentions.
This commit is contained in:
Max 2024-12-20 15:48:28 +08:00
parent af52dd35a1
commit 76e7156dac
5 changed files with 19 additions and 31 deletions

View file

@ -1,4 +1,4 @@
package base
package local
import (
"context"
@ -6,7 +6,7 @@ import (
)
// Chat the chat
func (ast *Base) Chat(ctx context.Context, messages []map[string]interface{}, option map[string]interface{}, cb func(data []byte) int) error {
func (ast *Local) Chat(ctx context.Context, messages []map[string]interface{}, option map[string]interface{}, cb func(data []byte) int) error {
if ast.openai == nil {
return fmt.Errorf("api is not initialized")

View file

@ -1,4 +1,4 @@
package base
package local
import (
"context"
@ -31,7 +31,7 @@ var AllowedFileTypes = map[string]string{
var MaxSize int64 = 20 * 1024 * 1024
// Upload the file
func (ast *Base) Upload(ctx context.Context, file *multipart.FileHeader, reader io.Reader, option map[string]interface{}) (*assistant.File, error) {
func (ast *Local) Upload(ctx context.Context, file *multipart.FileHeader, reader io.Reader, option map[string]interface{}) (*assistant.File, error) {
// check file size
if file.Size > MaxSize {
@ -69,13 +69,13 @@ func (ast *Base) Upload(ctx context.Context, file *multipart.FileHeader, reader
}, nil
}
func (ast *Base) id(temp string, ext string) (string, error) {
func (ast *Local) id(temp string, ext string) (string, error) {
date := time.Now().Format("20060102")
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(temp)))[:8]
return fmt.Sprintf("/__assistants/%s/%s/%s%s", ast.ID, date, hash, ext), nil
}
func (ast *Base) allowed(contentType string) bool {
func (ast *Local) allowed(contentType string) bool {
if _, ok := AllowedFileTypes[contentType]; ok {
return true
}
@ -87,7 +87,7 @@ func (ast *Base) allowed(contentType string) bool {
}
// Download downloads a file
func (ast *Base) Download(ctx context.Context, fileID string) (*assistant.FileResponse, error) {
func (ast *Local) Download(ctx context.Context, fileID string) (*assistant.FileResponse, error) {
// Get the data filesystem
data, err := fs.Get("data")

View file

@ -1,4 +1,4 @@
package base
package local
import (
"context"
@ -8,16 +8,16 @@ import (
"github.com/yaoapp/yao/openai"
)
// Base the base assistant
type Base struct {
// Local the local assistant
type Local struct {
ID string `json:"assistant_id"`
Prompts []assistant.Prompt `json:"prompts,omitempty"`
Connector connector.Connector `json:"-" yaml:"-"`
openai *openai.OpenAI
}
// New create a new base assistant
func New(connector connector.Connector, prompts []assistant.Prompt, id string) (*Base, error) {
// New create a new local assistant
func New(connector connector.Connector, prompts []assistant.Prompt, id string) (*Local, error) {
setting := connector.Setting()
api, err := openai.NewOpenAI(setting)
@ -25,10 +25,10 @@ func New(connector connector.Connector, prompts []assistant.Prompt, id string) (
return nil, err
}
return &Base{Connector: connector, ID: id, Prompts: prompts, openai: api}, nil
return &Local{Connector: connector, ID: id, Prompts: prompts, openai: api}, nil
}
// List list all assistants
func (ast *Base) List(ctx context.Context, param assistant.QueryParam) ([]assistant.Assistant, error) {
func (ast *Local) List(ctx context.Context, param assistant.QueryParam) ([]assistant.Assistant, error) {
return nil, nil
}

View file

@ -23,19 +23,6 @@ type Xun struct {
setting Setting
}
type row struct {
Role string `json:"role"` // Message role
Name string `json:"name"` // User name
Content string `json:"content"` // Message content
Sid string `json:"sid"` // Session ID
Cid string `json:"cid"` // Chat ID from chat history
UID string `json:"uid"` // User ID
Context map[string]interface{} `json:"context"` // Message context
CreatedAt time.Time `json:"created_at"` // Created time
UpdatedAt *time.Time `json:"updated_at"` // Updated time
ExpiredAt interface{} `json:"expired_at"` // Expired time
}
// Public interface methods and constructor remain exported:
// - NewXun
// - UpdateChatTitle
@ -228,6 +215,7 @@ func (conv *Xun) initAssistantTable() error {
table.JSON("option").Null()
table.JSON("prompts").Null()
table.JSON("flows").Null()
table.Boolean("mentionable").SetDefault(true).Index() // Whether this assistant can appear in @ mention list
table.TimestampTz("created_at").SetDefaultRaw("NOW()").Index()
table.TimestampTz("updated_at").Null().Index()
})
@ -244,7 +232,7 @@ func (conv *Xun) initAssistantTable() error {
return err
}
fields := []string{"id", "assistant_id", "type", "name", "avatar", "connector", "description", "option", "prompts", "flows", "created_at", "updated_at"}
fields := []string{"id", "assistant_id", "type", "name", "avatar", "connector", "description", "option", "prompts", "flows", "mentionable", "created_at", "updated_at"}
for _, field := range fields {
if !tab.HasColumn(field) {
return fmt.Errorf("%s is required", field)

View file

@ -11,7 +11,7 @@ import (
"github.com/yaoapp/gou/connector"
"github.com/yaoapp/kun/log"
"github.com/yaoapp/yao/neo/assistant"
"github.com/yaoapp/yao/neo/assistant/base"
"github.com/yaoapp/yao/neo/assistant/local"
"github.com/yaoapp/yao/neo/assistant/openai"
"github.com/yaoapp/yao/neo/conversation"
"github.com/yaoapp/yao/neo/message"
@ -417,9 +417,9 @@ func (neo *DSL) newAssistantByConnector(id string) (assistant.API, error) {
}
// Base on the assistant list hook
api, err := base.New(conn, neo.Prompts, id)
api, err := local.New(conn, neo.Prompts, id)
if err != nil {
return nil, fmt.Errorf("Create base assistant error: %s", err.Error())
return nil, fmt.Errorf("Create local assistant error: %s", err.Error())
}
return api, nil
}