Skip to content

Commit

Permalink
refactor(chatMessages.go): remove unused imports and reorder imports …
Browse files Browse the repository at this point in the history
…for better organization and readability

feat(chatMessages.go): set AI_API_TYPE and AI_MODEL_NAME configuration values based on the last message's meta data to improve AI model selection
refactor(chatMessages.go): change LastMessage function parameter type from pointer to value for better consistency and readability
  • Loading branch information
MohammadBnei committed Mar 1, 2024
1 parent ee44973 commit 660fe6a
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions service/chatMessages.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"sort"
"time"

"github.com/MohammadBnei/go-ai-cli/config"
"github.com/MohammadBnei/go-ai-cli/tool"
"github.com/bwmarrin/snowflake"
"github.com/jinzhu/copier"
"github.com/pkoukk/tiktoken-go"
Expand All @@ -18,6 +16,9 @@ import (
"github.com/tmc/langchaingo/llms"
"github.com/tmc/langchaingo/schema"
"gopkg.in/yaml.v3"

"github.com/MohammadBnei/go-ai-cli/config"
"github.com/MohammadBnei/go-ai-cli/tool"
)

type ROLES string
Expand Down Expand Up @@ -114,6 +115,12 @@ func (c *ChatMessages) LoadFromFile(filename string) (err error) {

c.SetMessagesOrder()

lastMessage := c.LastMessage(RoleAssistant)
if lastMessage != nil && lastMessage.Meta.ApiType != "" && lastMessage.Meta.Model != "" {
viper.Set(config.AI_API_TYPE, lastMessage.Meta.ApiType)
viper.Set(config.AI_MODEL_NAME, lastMessage.Meta.Model)
}

return nil
}

Expand Down Expand Up @@ -283,11 +290,11 @@ func (c *ChatMessages) ClearMessages() {
c.TotalTokens = 0
}

func (c *ChatMessages) LastMessage(role *ROLES) *ChatMessage {
func (c *ChatMessages) LastMessage(role ROLES) *ChatMessage {
messages := c.Messages
if role != nil {
if role != "" {
messages = lo.Filter(c.Messages, func(item ChatMessage, _ int) bool {
return item.Role == *role
return item.Role == role
})
}
if len(messages) == 0 {
Expand Down

0 comments on commit 660fe6a

Please sign in to comment.