Skip to content

Commit

Permalink
move functions package (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Aug 12, 2023
1 parent 46ea21d commit c638939
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 174 deletions.
68 changes: 68 additions & 0 deletions ai/functions/function.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package functions

import (
"context"
"fmt"
"strings"

"github.com/JunNishimura/Chatify/ai/model"
"github.com/JunNishimura/Chatify/util"
"github.com/JunNishimura/spotify/v2"
)

func SetGenres(genresInfo *util.Info[[]string], value []string) {
util.SetInfo(genresInfo, value)
}

func SetDanceability(danceabilityInfo *util.Info[float64], value float64) {
util.SetInfo(danceabilityInfo, value)
}

func SetValence(valence *util.Info[float64], value float64) {
util.SetInfo(valence, value)
}

func SetPopularity(popularity *util.Info[int], value int) {
util.SetInfo(popularity, value)
}

const (
RecommendCount = 5
)

func Recommend(ctx context.Context, client *spotify.Client, musicOrientation *model.MusicOrientation) (string, error) {
// genres length needs to be less than 5
genres := musicOrientation.Genres.Value
if len(genres) > 5 {
genres = genres[:5]
}

seeds := spotify.Seeds{
Genres: genres,
}

trackAttrib := spotify.NewTrackAttributes().
TargetDanceability(musicOrientation.Danceability.Value).
TargetValence(musicOrientation.Valence.Value).
TargetPopularity(musicOrientation.Popularity.Value)

recommendations, err := client.GetRecommendations(ctx, seeds, trackAttrib, spotify.Limit(RecommendCount))
if err != nil {
return "", fmt.Errorf("fail to get recommendations: %v", err)
}

var output string
for i, track := range recommendations.Tracks {
output += fmt.Sprintf("[%d]\n", i+1)
output += fmt.Sprintf("album: %s\n", track.Album.Name)
var artists []string
for _, artist := range track.Artists {
artists = append(artists, artist.Name)
}
output += fmt.Sprintf("artists: %s\n", strings.Join(artists, ","))
output += fmt.Sprintf("url: %s\n", track.ExternalURLs["spotify"])
output += "\n"
}

return output, nil
}
143 changes: 143 additions & 0 deletions ai/functions/template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package functions

import (
"strings"

"github.com/MakeNowJust/heredoc/v2"
"github.com/sashabaranov/go-openai"
)

// function name
const (
RecommendFunctionName = "recommend"
SetGenresFunctionName = "setGenres"
SetDanceabilityFunctionName = "setDanceability"
SetValenceFunctionName = "setValence"
SetPopularityFunctionName = "setPopularity"

ObjectType = "object"
StringType = "string"
NumberType = "number"
)

type Parameters struct {
Type string `json:"type"`
Properties any `json:"properties"`
Required []string `json:"required"`
}

type SetProperties struct {
QualitativeValue Property `json:"qualitative_value,omitempty"`
QuantitativeValue Property `json:"quantitative_value,omitempty"`
}

type Property struct {
Type string `json:"type,omitempty"`
Description string `json:"description,omitempty"`
}

func GetFunctionDefinitions(genres []string) []openai.FunctionDefinition {
return []openai.FunctionDefinition{
{
Name: SetGenresFunctionName,
Description: "Save the genre of the music the user wants to listent to.",
Parameters: Parameters{
Type: ObjectType,
Properties: SetProperties{
QualitativeValue: Property{
Type: StringType,
Description: heredoc.Docf(`
Information on music genres.
Multiple elements can be entered. e.g. j-pop,k-pop,chill.
The genres that can be taken as arguments are as follows: %s"`,
strings.Join(genres, ","),
),
},
},
Required: []string{"qualitative_value"},
},
},
{
Name: SetDanceabilityFunctionName,
Description: heredoc.Doc(`
Save the danceability value the user wants to listen to.
Danceability describes how suitable a track is for dancing based on a combination of musical elements including tempo, rhythm stability, beat strength, and overall regularity.`,
),
Parameters: Parameters{
Type: ObjectType,
Properties: SetProperties{
QualitativeValue: Property{
Type: StringType,
Description: "A qualitative expression of the music danceability the user wants.",
},
QuantitativeValue: Property{
Type: NumberType,
Description: heredoc.Doc(`
A quantitative expression of the music danceability the user wants.
A value ranges from 0.0 to 1.0.
A value of 0.0 is least danceable and 1.0 is most danceable.`,
),
},
},
Required: []string{"qualitative_value", "quatitative_value"},
},
},
{
Name: SetValenceFunctionName,
Description: heredoc.Doc(`
Save the valence value the user wants to listen to.
Valence describes how much the musical positiveness conveyed by a track.`,
),
Parameters: Parameters{
Type: ObjectType,
Properties: SetProperties{
QualitativeValue: Property{
Type: StringType,
Description: "A qualitative expression of the music valence the user wants.",
},
QuantitativeValue: Property{
Type: NumberType,
Description: heredoc.Doc(`
A quantitative expression of the music danceability the user wants.
A value ranges from 0.0 to 1.0.
Tracks with high valence sound more positive, while tracks with low valence sound more negative.`,
),
},
},
Required: []string{"qualitative_value", "quatitative_value"},
},
},
{
Name: SetPopularityFunctionName,
Description: heredoc.Doc(`
Save the popularity value the user wants to listen to.
Popularity describes how much the track is popular`,
),
Parameters: Parameters{
Type: ObjectType,
Properties: SetProperties{
QualitativeValue: Property{
Type: StringType,
Description: "A qualitative expression of the music popularity the user wants.",
},
QuantitativeValue: Property{
Type: NumberType,
Description: heredoc.Doc(`
A quantitative expression of the music danceability the user wants.
A value ranges from 0 to 100.
Tracks with high popularity is more popular.`,
),
},
},
Required: []string{"qualitative_value", "quatitative_value"},
},
},
}
}

func GetRecommendFunctionDefinition() openai.FunctionDefinition {
return openai.FunctionDefinition{
Name: RecommendFunctionName,
Description: "Recommend the music tracks based on information about what kind of music the user wants to listen",
}
}
19 changes: 0 additions & 19 deletions functions/definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"strings"

"github.com/JunNishimura/Chatify/object"
"github.com/JunNishimura/spotify/v2"
)

Expand Down Expand Up @@ -49,21 +48,3 @@ func Recommend(ctx context.Context, client *spotify.Client, genres string, dance

return output, nil
}

func SetGenres(musicOrientationInfo *object.MusicOrientationInfo, genres string) {
cleanGenres := strings.TrimSpace(genres)
splitGenres := strings.Split(cleanGenres, ",")
musicOrientationInfo.SetGenres(splitGenres)
}

func SetDanceability(musicOrientationInfo *object.MusicOrientationInfo, danceability float64) {
musicOrientationInfo.SetDanceability(danceability)
}

func SetValence(musicOrientaionInfo *object.MusicOrientationInfo, valence float64) {
musicOrientaionInfo.SetValence(valence)
}

func SetPopularity(musicOrientaionInfo *object.MusicOrientationInfo, popularity int) {
musicOrientaionInfo.SetPopularity(popularity)
}
155 changes: 0 additions & 155 deletions functions/template.go

This file was deleted.

0 comments on commit c638939

Please sign in to comment.