aboutsummaryrefslogtreecommitdiff
path: root/code/ai_recommendations.py
diff options
context:
space:
mode:
authorParker <contact@pkrm.dev>2024-04-12 00:29:24 -0500
committerParker <contact@pkrm.dev>2024-04-12 00:29:24 -0500
commit66bf2a6cc21590a938a03913f36586744c871f57 (patch)
tree54a37f2e5c2f48ea6178fed8b88cb6d7bf356047 /code/ai_recommendations.py
parent2ebd5421b7f37234dc400ed4f6cf2e6b0c62e0ed (diff)
Create autoplay feature
Use ChatGPT to get song recommendations based on current song queue.
Diffstat (limited to 'code/ai_recommendations.py')
-rw-r--r--code/ai_recommendations.py74
1 files changed, 74 insertions, 0 deletions
diff --git a/code/ai_recommendations.py b/code/ai_recommendations.py
new file mode 100644
index 0000000..3aa2da5
--- /dev/null
+++ b/code/ai_recommendations.py
@@ -0,0 +1,74 @@
+from lavalink import LoadType
+
+from global_variables import CLIENT
+
+
+async def add_song_recommendations(bot_user, player, number, inputs, retries: int = 1):
+ input_string = ""
+ for song, artist in inputs.items():
+ input_string += f"{song} - {artist}, "
+ # Remove the final ", "
+ input_string = input_string[:-2]
+
+ completion = (
+ CLIENT.chat.completions.create(
+ messages=[
+ {
+ "role": "user",
+ "content": f"""I need songs that are similar in nature to ones that I list.
+ Send {number} songs formatted as:
+
+ SONG NAME - ARTIST NAME
+ SONG NAME - ARTIST NAME
+ ...
+
+ Do not provide anything except for the exactly what I need, no
+ list numbers, no quotations, only what I have shown.
+
+ The songs you should base the list off of are: {input_string}
+
+ NOTE: If you believe that there are not many songs that are similar to the ones I list, then please just respond with the message "SONG FIND ERROR"
+ """,
+ }
+ ],
+ model="gpt-3.5-turbo",
+ )
+ .choices[0]
+ .message.content.strip()
+ .strip('"')
+ )
+
+ # Sometimes, we get false failures, so we check for a failure, and it we haven't tried
+ # at least 3 times, then continue retrying, otherwise, we actually can't get any songs
+ if completion == "SONG FIND ERROR":
+ if retries <= 3:
+ await add_song_recommendations(
+ bot_user, player, number, inputs, retries + 1
+ )
+ else:
+ return False
+
+ else:
+ for entry in completion.split("\n"):
+ song, artist = entry.split(" - ")
+
+ ytsearch = f"ytsearch:{song} {artist} audio"
+ results = await player.node.get_tracks(ytsearch)
+
+ if not results.tracks or results.load_type in (
+ LoadType.EMPTY,
+ LoadType.ERROR,
+ ):
+ dzsearch = f"dzsearch:{song} {artist}"
+ results = await player.node.get_tracks(dzsearch)
+
+ if not results.tracks or results.load_type in (
+ LoadType.EMPTY,
+ LoadType.ERROR,
+ ):
+ continue
+
+ track = results.tracks[0]
+ player.add(requester=bot_user, track=track)
+
+ return True