-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
46ea21d
commit c638939
Showing
4 changed files
with
211 additions
and
174 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.