Skip to content

Commit

Permalink
Merge pull request #812 from trheyi/main
Browse files Browse the repository at this point in the history
Refactor Neo (60%)
  • Loading branch information
trheyi authored Dec 30, 2024
2 parents ef6c39b + 882277d commit d55bfef
Show file tree
Hide file tree
Showing 15 changed files with 2,472 additions and 377 deletions.
573 changes: 531 additions & 42 deletions neo/api.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions neo/assistant/base/chat.go → neo/assistant/local/chat.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package base
package local

import (
"context"
"fmt"
)

// 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")
Expand Down
10 changes: 5 additions & 5 deletions neo/assistant/base/file.go → neo/assistant/local/file.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package base
package local

import (
"context"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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")
Expand Down
14 changes: 7 additions & 7 deletions neo/assistant/base/base.go → neo/assistant/local/local.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package base
package local

import (
"context"
Expand All @@ -8,27 +8,27 @@ 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)
if err != nil {
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
}
17 changes: 10 additions & 7 deletions neo/assistant/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ type QueryParam struct {

// Assistant the assistant
type Assistant struct {
ID string `json:"assistant_id"` // Assistant ID
Name string `json:"name,omitempty"` // Assistant Name
Connector string `json:"connector"` // AI Connector
Description string `json:"description,omitempty"` // Assistant Description
Option map[string]interface{} `json:"option,omitempty"` // AI Option
Prompts []Prompt `json:"prompts,omitempty"` // AI Prompts
API API `json:"-" yaml:"-"` // Assistant API
ID string `json:"assistant_id"` // Assistant ID
Type string `json:"type,omitempty"` // Assistant Type, default is assistant
Name string `json:"name,omitempty"` // Assistant Name
Avatar string `json:"avatar,omitempty"` // Assistant Avatar
Connector string `json:"connector"` // AI Connector
Description string `json:"description,omitempty"` // Assistant Description
Option map[string]interface{} `json:"option,omitempty"` // AI Option
Prompts []Prompt `json:"prompts,omitempty"` // AI Prompts
Flows []map[string]interface{} `json:"flows,omitempty"` // Assistant Flows
API API `json:"-" yaml:"-"` // Assistant API
}

// File the file
Expand Down
61 changes: 30 additions & 31 deletions neo/conversation/mongo.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
package conversation

// Mongo conversation
// Mongo represents a MongoDB-based conversation storage
type Mongo struct{}

// NewMongo create a new conversation
// NewMongo creates a new MongoDB conversation storage
func NewMongo() *Mongo {
return &Mongo{}
}

// UpdateChatTitle update the chat title
func (conv *Mongo) UpdateChatTitle(sid string, cid string, title string) error {
return nil
// GetChats retrieves a list of chats
func (m *Mongo) GetChats(sid string, filter ChatFilter) (*ChatGroupResponse, error) {
return &ChatGroupResponse{}, nil
}

// GetChats get the chat list
func (conv *Mongo) GetChats(sid string, filter ChatFilter) (*ChatGroupResponse, error) {
return &ChatGroupResponse{
Groups: []ChatGroup{},
Page: filter.Page,
PageSize: filter.PageSize,
Total: 0,
LastPage: 1,
}, nil
// GetChat retrieves a single chat's information
func (m *Mongo) GetChat(sid string, cid string) (*ChatInfo, error) {
return &ChatInfo{}, nil
}

// GetHistory get the history
func (conv *Mongo) GetHistory(sid string, cid string) ([]map[string]interface{}, error) {
// GetHistory retrieves chat history
func (m *Mongo) GetHistory(sid string, cid string) ([]map[string]interface{}, error) {
return []map[string]interface{}{}, nil
}

// SaveHistory save the history
func (conv *Mongo) SaveHistory(sid string, messages []map[string]interface{}, cid string) error {
// SaveHistory saves chat history
func (m *Mongo) SaveHistory(sid string, messages []map[string]interface{}, cid string, context map[string]interface{}) error {
return nil
}

// DeleteChat deletes a single chat
func (m *Mongo) DeleteChat(sid string, cid string) error {
return nil
}

// GetRequest get the request
func (conv *Mongo) GetRequest(sid string, rid string) ([]map[string]interface{}, error) {
return nil, nil
// DeleteAllChats deletes all chats
func (m *Mongo) DeleteAllChats(sid string) error {
return nil
}

// SaveRequest save the request
func (conv *Mongo) SaveRequest(sid string, rid string, cid string, messages []map[string]interface{}) error {
// UpdateChatTitle updates chat title
func (m *Mongo) UpdateChatTitle(sid string, cid string, title string) error {
return nil
}

// GetChat get the chat info and its history
func (conv *Mongo) GetChat(sid string, cid string) (*ChatInfo, error) {
return nil, nil
// SaveAssistant saves assistant information
func (m *Mongo) SaveAssistant(assistant map[string]interface{}) (interface{}, error) {
return assistant["assistant_id"], nil
}

// DeleteChat deletes a specific chat and its history
func (conv *Mongo) DeleteChat(sid string, cid string) error {
// DeleteAssistant deletes an assistant
func (m *Mongo) DeleteAssistant(assistantID string) error {
return nil
}

// DeleteAllChats deletes all chats and their histories for a user
func (conv *Mongo) DeleteAllChats(sid string) error {
return nil
// GetAssistants retrieves a list of assistants
func (m *Mongo) GetAssistants(filter AssistantFilter) (*AssistantResponse, error) {
return &AssistantResponse{}, nil
}
61 changes: 30 additions & 31 deletions neo/conversation/redis.go
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
package conversation

// Redis conversation
// Redis represents a Redis-based conversation storage
type Redis struct{}

// NewRedis create a new conversation
// NewRedis creates a new Redis conversation storage
func NewRedis() *Redis {
return &Redis{}
}

// UpdateChatTitle update the chat title
func (conv *Redis) UpdateChatTitle(sid string, cid string, title string) error {
return nil
// GetChats retrieves a list of chats
func (r *Redis) GetChats(sid string, filter ChatFilter) (*ChatGroupResponse, error) {
return &ChatGroupResponse{}, nil
}

// GetChats get the chat list
func (conv *Redis) GetChats(sid string, filter ChatFilter) (*ChatGroupResponse, error) {
return &ChatGroupResponse{
Groups: []ChatGroup{},
Page: filter.Page,
PageSize: filter.PageSize,
Total: 0,
LastPage: 1,
}, nil
// GetChat retrieves a single chat's information
func (r *Redis) GetChat(sid string, cid string) (*ChatInfo, error) {
return &ChatInfo{}, nil
}

// GetHistory get the history
func (conv *Redis) GetHistory(sid string, cid string) ([]map[string]interface{}, error) {
// GetHistory retrieves chat history
func (r *Redis) GetHistory(sid string, cid string) ([]map[string]interface{}, error) {
return []map[string]interface{}{}, nil
}

// SaveHistory save the history
func (conv *Redis) SaveHistory(sid string, messages []map[string]interface{}, cid string) error {
// SaveHistory saves chat history
func (r *Redis) SaveHistory(sid string, messages []map[string]interface{}, cid string, context map[string]interface{}) error {
return nil
}

// DeleteChat deletes a single chat
func (r *Redis) DeleteChat(sid string, cid string) error {
return nil
}

// GetRequest get the request
func (conv *Redis) GetRequest(sid string, rid string) ([]map[string]interface{}, error) {
return nil, nil
// DeleteAllChats deletes all chats
func (r *Redis) DeleteAllChats(sid string) error {
return nil
}

// SaveRequest save the request
func (conv *Redis) SaveRequest(sid string, rid string, cid string, messages []map[string]interface{}) error {
// UpdateChatTitle updates chat title
func (r *Redis) UpdateChatTitle(sid string, cid string, title string) error {
return nil
}

// GetChat get the chat info and its history
func (conv *Redis) GetChat(sid string, cid string) (*ChatInfo, error) {
return nil, nil
// SaveAssistant saves assistant information
func (r *Redis) SaveAssistant(assistant map[string]interface{}) (interface{}, error) {
return assistant["assistant_id"], nil
}

// DeleteChat deletes a specific chat and its history
func (conv *Redis) DeleteChat(sid string, cid string) error {
// DeleteAssistant deletes an assistant
func (r *Redis) DeleteAssistant(assistantID string) error {
return nil
}

// DeleteAllChats deletes all chats and their histories for a user
func (conv *Redis) DeleteAllChats(sid string) error {
return nil
// GetAssistants retrieves a list of assistants
func (r *Redis) GetAssistants(filter AssistantFilter) (*AssistantResponse, error) {
return &AssistantResponse{}, nil
}
Loading

0 comments on commit d55bfef

Please sign in to comment.