Skip to content

Commit

Permalink
update model (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Aug 28, 2023
1 parent b85a35d commit 35c351c
Showing 1 changed file with 87 additions and 3 deletions.
90 changes: 87 additions & 3 deletions ui/cmd/hey/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import (
"context"
"strings"

"github.com/JunNishimura/Chatify/ai/functions"
"github.com/JunNishimura/Chatify/ai/model"
"github.com/JunNishimura/Chatify/ai/prompt"
"github.com/JunNishimura/Chatify/auth"
"github.com/JunNishimura/Chatify/config"
"github.com/JunNishimura/Chatify/ui/style"
"github.com/JunNishimura/Chatify/utils"
Expand Down Expand Up @@ -84,22 +87,103 @@ type Model struct {
chatCompMessages []openai.ChatCompletionMessage
conversation []*Message
functions []openai.FunctionDefinition
availableGenres []string
recommendItems []list.Item
err error
}

func NewModel() *Model {
func NewModel() (*Model, error) {
ctx := context.Background()

window := utils.NewWindow()

cfg, err := loadConfig()
if err != nil {
return nil, err
}

openAIAPIkey := cfg.GetClientValue(config.OpenAIAPIKey)
openAIAPIclient := openai.NewClient(openAIAPIkey)

spotifyClient, err := getSpotifyClient(ctx, cfg)
if err != nil {
return nil, err
}

availableGenres, err := spotifyClient.GetAvailableGenreSeeds(ctx)
if err != nil {
return nil, err
}

user, err := getUser(ctx, spotifyClient)
if err != nil {
return nil, err
}

return &Model{
ctx: context.Background(),
ctx: ctx,
window: window,
textInput: newTextInput(window.Width),
list: newListModel([]list.Item{}, 0, 0),
cfg: cfg,
user: user,
spotifyClient: spotifyClient,
openaiClient: openAIAPIclient,
questionIndex: 0,
functionCall: "auto",
chatCompMessages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: prompt.Base,
},
},
functions: functions.GetFunctionDefinitions(availableGenres),
}, nil
}

func loadConfig() (*config.Config, error) {
cfg, err := config.New()
if err != nil {
return nil, err
}

if err := cfg.Load(); err != nil {
return nil, err
}

return cfg, nil
}

func getSpotifyClient(ctx context.Context, cfg *config.Config) (*spotify.Client, error) {
a := auth.NewAuth(cfg)

token := cfg.GetToken()

newToken, err := a.RefreshToken(ctx, token)
if err != nil {
return nil, err
}

// update an access token if it has expired
if token.AccessToken != newToken.AccessToken {
if err := cfg.SetToken(token); err != nil {
return nil, err
}
}

client := spotify.New(a.Client(ctx, newToken))

return client, nil
}

func getUser(ctx context.Context, client *spotify.Client) (*model.User, error) {
curUser, err := client.CurrentUser(ctx)
if err != nil {
return nil, err
}

user := model.NewUser(curUser.DisplayName)

return user, nil
}

const listTitle = "Chatify's recommendation"
Expand Down

0 comments on commit 35c351c

Please sign in to comment.