Enable chat for artists
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
defmodule MusicLibrary.ArtistChat do
|
||||
@behaviour MusicLibrary.Chat
|
||||
|
||||
alias MusicLibrary.Artists.ArtistInfo
|
||||
|
||||
@impl true
|
||||
def stream_response(messages, {artist, artist_info}, callback) do
|
||||
instructions = build_instructions(artist, artist_info)
|
||||
|
||||
OpenAI.chat_stream(messages, on_chunk: callback, instructions: instructions)
|
||||
end
|
||||
|
||||
defp build_instructions(artist, artist_info) do
|
||||
context = build_context(artist, artist_info)
|
||||
|
||||
"""
|
||||
You are a knowledgeable music assistant. Answer questions about the artist \
|
||||
the user is currently viewing. Use the provided artist information as your \
|
||||
primary reference, and use web search to find additional up-to-date \
|
||||
information when helpful. Be concise and accurate. When unsure, say so.
|
||||
|
||||
Artist information:
|
||||
#{context}
|
||||
"""
|
||||
end
|
||||
|
||||
defp build_context(artist, artist_info) do
|
||||
country = ArtistInfo.country(artist_info)
|
||||
description = ArtistInfo.wikipedia_description(artist_info)
|
||||
summary = ArtistInfo.wikipedia_summary(artist_info)
|
||||
|
||||
parts =
|
||||
[
|
||||
{"Name", artist.name},
|
||||
{"Country", country.name},
|
||||
{"Description", description},
|
||||
{"Biography", summary}
|
||||
]
|
||||
|> Enum.reject(fn {_label, value} -> value in [nil, ""] end)
|
||||
|> Enum.map_join("\n", fn {label, value} -> "#{label}: #{value}" end)
|
||||
|
||||
parts
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,7 @@
|
||||
defmodule MusicLibrary.Chat do
|
||||
@callback stream_response(
|
||||
messages :: list(map()),
|
||||
context :: term(),
|
||||
callback :: (String.t() -> any())
|
||||
) :: :ok | {:error, term()}
|
||||
end
|
||||
@@ -1,7 +1,10 @@
|
||||
defmodule MusicLibrary.RecordChat do
|
||||
@behaviour MusicLibrary.Chat
|
||||
|
||||
alias MusicLibrary.Records.Record
|
||||
|
||||
def stream_response(messages, record, embedding_text, callback) do
|
||||
@impl true
|
||||
def stream_response(messages, {record, embedding_text}, callback) do
|
||||
instructions = build_instructions(record, embedding_text)
|
||||
|
||||
OpenAI.chat_stream(messages, on_chunk: callback, instructions: instructions)
|
||||
|
||||
+8
-12
@@ -1,9 +1,8 @@
|
||||
defmodule MusicLibraryWeb.Components.RecordChat do
|
||||
defmodule MusicLibraryWeb.Components.Chat do
|
||||
use MusicLibraryWeb, :live_component
|
||||
|
||||
require Logger
|
||||
|
||||
alias MusicLibrary.RecordChat
|
||||
alias MusicLibraryWeb.Markdown
|
||||
|
||||
def open(id), do: Fluxon.open_dialog(id)
|
||||
@@ -55,7 +54,7 @@ defmodule MusicLibraryWeb.Components.RecordChat do
|
||||
>
|
||||
<div class="flex items-center gap-2 pb-4 border-b border-zinc-200 dark:border-zinc-700">
|
||||
<h2 class="text-lg font-semibold text-zinc-900 dark:text-zinc-100">
|
||||
{gettext("Chat about %{title}", title: @record.title)}
|
||||
{gettext("Chat about %{title}", title: @title)}
|
||||
</h2>
|
||||
<.button
|
||||
:if={@messages != []}
|
||||
@@ -82,10 +81,7 @@ defmodule MusicLibraryWeb.Components.RecordChat do
|
||||
name="hero-chat-bubble-left-right"
|
||||
class="size-12 mb-4 text-zinc-300 dark:text-zinc-600"
|
||||
/>
|
||||
<p class="text-sm font-medium">{gettext("Ask anything about this album")}</p>
|
||||
<p class="text-xs mt-1">
|
||||
{gettext("e.g. \"What genre is this?\", \"Tell me about the artists\"")}
|
||||
</p>
|
||||
<p class="text-sm font-medium">{@empty_prompt}</p>
|
||||
</div>
|
||||
|
||||
<div
|
||||
@@ -144,7 +140,7 @@ defmodule MusicLibraryWeb.Components.RecordChat do
|
||||
<.input
|
||||
name="message"
|
||||
value=""
|
||||
placeholder={gettext("Ask about this album...")}
|
||||
placeholder={@placeholder}
|
||||
class="flex-1"
|
||||
disabled={@loading}
|
||||
autocomplete="off"
|
||||
@@ -215,11 +211,11 @@ defmodule MusicLibraryWeb.Components.RecordChat do
|
||||
user_message = %{role: "user", content: String.trim(text)}
|
||||
|
||||
messages = socket.assigns.messages ++ [user_message]
|
||||
record = socket.assigns.record
|
||||
embedding_text = socket.assigns.embedding_text
|
||||
chat_module = socket.assigns.chat_module
|
||||
chat_context = socket.assigns.chat_context
|
||||
|
||||
Task.Supervisor.start_child(MusicLibrary.TaskSupervisor, fn ->
|
||||
case RecordChat.stream_response(messages, record, embedding_text, fn chunk ->
|
||||
case chat_module.stream_response(messages, chat_context, fn chunk ->
|
||||
Phoenix.LiveView.send_update(parent_pid, __MODULE__,
|
||||
id: component_id,
|
||||
chunk: chunk
|
||||
@@ -232,7 +228,7 @@ defmodule MusicLibraryWeb.Components.RecordChat do
|
||||
)
|
||||
|
||||
{:error, reason} ->
|
||||
Logger.error("RecordChat streaming error: #{reason}")
|
||||
Logger.error("Chat streaming error: #{reason}")
|
||||
|
||||
Phoenix.LiveView.send_update(parent_pid, __MODULE__,
|
||||
id: component_id,
|
||||
@@ -21,6 +21,18 @@
|
||||
data-slot="icon"
|
||||
/>
|
||||
</.button>
|
||||
<.button
|
||||
variant="soft"
|
||||
phx-click={MusicLibraryWeb.Components.Chat.open("artist-chat-sheet")}
|
||||
>
|
||||
<span class="sr-only">{gettext("Chat about artist")}</span>
|
||||
<.icon
|
||||
name="hero-chat-bubble-left-right"
|
||||
class="h-5 w-5"
|
||||
aria-hidden="true"
|
||||
data-slot="icon"
|
||||
/>
|
||||
</.button>
|
||||
<.dropdown id={"actions-#{@artist.musicbrainz_id}"} placement="bottom-end">
|
||||
<:toggle>
|
||||
<.button variant="soft">
|
||||
@@ -332,6 +344,17 @@
|
||||
musicbrainz_id={@artist.musicbrainz_id}
|
||||
/>
|
||||
|
||||
<.live_component
|
||||
id="artist-chat"
|
||||
sheet_id="artist-chat-sheet"
|
||||
module={MusicLibraryWeb.Components.Chat}
|
||||
title={@artist.name}
|
||||
chat_module={MusicLibrary.ArtistChat}
|
||||
chat_context={{@artist, @artist_info}}
|
||||
placeholder={gettext("Ask about this artist...")}
|
||||
empty_prompt={gettext("Ask anything about this artist")}
|
||||
/>
|
||||
|
||||
<.structured_modal
|
||||
:if={@live_action == :edit}
|
||||
id="artist-info-modal"
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
</.button>
|
||||
<.button
|
||||
variant="soft"
|
||||
phx-click={MusicLibraryWeb.Components.RecordChat.open("record-chat-sheet")}
|
||||
phx-click={MusicLibraryWeb.Components.Chat.open("record-chat-sheet")}
|
||||
>
|
||||
<span class="sr-only">{gettext("Chat about album")}</span>
|
||||
<.icon
|
||||
@@ -342,9 +342,12 @@
|
||||
<.live_component
|
||||
id="record-chat"
|
||||
sheet_id="record-chat-sheet"
|
||||
module={MusicLibraryWeb.Components.RecordChat}
|
||||
record={@record}
|
||||
embedding_text={@embedding_text}
|
||||
module={MusicLibraryWeb.Components.Chat}
|
||||
title={@record.title}
|
||||
chat_module={MusicLibrary.RecordChat}
|
||||
chat_context={{@record, @embedding_text}}
|
||||
placeholder={gettext("Ask about this album...")}
|
||||
empty_prompt={gettext("Ask anything about this album")}
|
||||
/>
|
||||
|
||||
<.structured_modal
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
<.button_group>
|
||||
<.button
|
||||
variant="soft"
|
||||
phx-click={MusicLibraryWeb.Components.RecordChat.open("record-chat-sheet")}
|
||||
phx-click={MusicLibraryWeb.Components.Chat.open("record-chat-sheet")}
|
||||
>
|
||||
<span class="sr-only">{gettext("Chat about album")}</span>
|
||||
<.icon
|
||||
@@ -307,9 +307,12 @@
|
||||
<.live_component
|
||||
id="record-chat"
|
||||
sheet_id="record-chat-sheet"
|
||||
module={MusicLibraryWeb.Components.RecordChat}
|
||||
record={@record}
|
||||
embedding_text={@embedding_text}
|
||||
module={MusicLibraryWeb.Components.Chat}
|
||||
title={@record.title}
|
||||
chat_module={MusicLibrary.RecordChat}
|
||||
chat_context={{@record, @embedding_text}}
|
||||
placeholder={gettext("Ask about this album...")}
|
||||
empty_prompt={gettext("Ask anything about this album")}
|
||||
/>
|
||||
|
||||
<.structured_modal
|
||||
|
||||
Reference in New Issue
Block a user