Skip to content

Commit

Permalink
Update Go module dependencies and enhance assistant message handling …
Browse files Browse the repository at this point in the history
…in Neo API

- Added the github.com/spf13/cast dependency to improve type conversion capabilities.
- Enhanced the requestMessages function to include prompts, allowing for more dynamic message handling.
- Updated the LoadBuiltIn function to ensure the "Built-in" tag is added if it doesn't already exist, improving assistant organization.
- Refactored sorting logic to utilize the cast package for better type safety.

These changes support improved functionality and maintainability in the assistant management process.
  • Loading branch information
trheyi committed Jan 3, 2025
1 parent 55c0580 commit 0d676b3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ require (
github.com/richardlehane/msoleps v1.0.4 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/spf13/cast v1.7.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tcnksm/go-gitconfig v0.1.2 // indirect
github.com/tidwall/btree v1.7.0 // indirect
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3x
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
Expand Down Expand Up @@ -219,6 +220,8 @@ github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncj
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cast v1.7.1 h1:cuNEagBQEHWN1FnbGEjCXL2szYEXqfJPbP2HNUaca9Y=
github.com/spf13/cast v1.7.1/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
19 changes: 19 additions & 0 deletions neo/assistant/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,25 @@ func (ast *Assistant) Chat(ctx context.Context, messages []map[string]interface{

func (ast *Assistant) requestMessages(ctx context.Context, messages []map[string]interface{}) ([]map[string]interface{}, error) {
newMessages := []map[string]interface{}{}

// With Prompts
if ast.Prompts != nil {
for _, prompt := range ast.Prompts {
message := map[string]interface{}{
"role": prompt.Role,
"content": prompt.Content,
}

name := ast.Name
if prompt.Name != "" {
name = prompt.Name
}

message["name"] = name
newMessages = append(newMessages, message)
}
}

length := len(messages)
for index, message := range messages {
role, ok := message["role"].(string)
Expand Down
23 changes: 20 additions & 3 deletions neo/assistant/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

jsoniter "github.com/json-iterator/go"
"github.com/spf13/cast"
"github.com/yaoapp/gou/fs"
"github.com/yaoapp/gou/rag/driver"
v8 "github.com/yaoapp/gou/runtime/v8"
Expand Down Expand Up @@ -66,11 +67,27 @@ func LoadBuiltIn() error {

assistant.Readonly = true
assistant.BuiltIn = true
assistant.Sort = sort
if assistant.Sort == 0 {
assistant.Sort = sort
}
if assistant.Tags == nil {
assistant.Tags = []string{"Built-in"}
}

// Check if the assistant has Built-in tag
hasBuiltIn := false
for _, tag := range assistant.Tags {
if tag == "Built-in" {
hasBuiltIn = true
break
}
}

// add Built-in tag if not exists
if !hasBuiltIn {
assistant.Tags = append(assistant.Tags, "Built-in")
}

// Save the assistant
err = assistant.Save()
if err != nil {
Expand Down Expand Up @@ -284,8 +301,8 @@ func loadMap(data map[string]interface{}) (*Assistant, error) {
}

// sort
if v, ok := data["sort"].(int); ok {
assistant.Sort = v
if v, has := data["sort"]; has {
assistant.Sort = cast.ToInt(v)
}

// path
Expand Down

0 comments on commit 0d676b3

Please sign in to comment.