Shrink collection summary to fit OpenAI token limit
Deduplicate records by musicbrainz_id (merging formats) and cap genres to 3 per record, reducing the summary from ~32K to ~24K estimated tokens.
This commit is contained in:
@@ -8,9 +8,10 @@ defmodule MusicLibrary.Chats.CollectionChat do
|
||||
alias MusicLibrary.Chats.Prompt
|
||||
|
||||
@impl true
|
||||
@spec stream_response([map()], String.t(), (String.t() -> any())) :: :ok | {:error, term()}
|
||||
def stream_response(messages, collection_summary, callback) do
|
||||
instructions = build_instructions(collection_summary)
|
||||
@spec stream_response([map()], {String.t(), non_neg_integer()}, (String.t() -> any())) ::
|
||||
:ok | {:error, term()}
|
||||
def stream_response(messages, {summary, record_count}, callback) do
|
||||
instructions = build_instructions(summary, record_count)
|
||||
|
||||
OpenAI.chat_stream(messages,
|
||||
on_chunk: callback,
|
||||
@@ -19,9 +20,7 @@ defmodule MusicLibrary.Chats.CollectionChat do
|
||||
)
|
||||
end
|
||||
|
||||
defp build_instructions(collection_summary) do
|
||||
record_count = count_records(collection_summary)
|
||||
|
||||
defp build_instructions(collection_summary, record_count) do
|
||||
Prompt.build("""
|
||||
Answer questions about the user's music collection. \
|
||||
Use the provided collection catalog as your primary reference. \
|
||||
@@ -31,12 +30,4 @@ defmodule MusicLibrary.Chats.CollectionChat do
|
||||
#{collection_summary}\
|
||||
""")
|
||||
end
|
||||
|
||||
defp count_records(""), do: 0
|
||||
|
||||
defp count_records(summary) do
|
||||
summary
|
||||
|> String.split("\n")
|
||||
|> length()
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user