diff --git a/lib/music_library/record_chat.ex b/lib/music_library/record_chat.ex index 8385db0a..97a01e21 100644 --- a/lib/music_library/record_chat.ex +++ b/lib/music_library/record_chat.ex @@ -2,16 +2,12 @@ defmodule MusicLibrary.RecordChat do alias MusicLibrary.Records.Record def stream_response(messages, record, embedding_text, callback) do - system_prompt = build_system_prompt(record, embedding_text) + instructions = build_instructions(record, embedding_text) - full_messages = [ - %{role: "system", content: system_prompt} | messages - ] - - OpenAI.chat_stream(full_messages, on_chunk: callback) + OpenAI.chat_stream(messages, on_chunk: callback, instructions: instructions) end - defp build_system_prompt(%Record{} = record, embedding_text) do + defp build_instructions(%Record{} = record, embedding_text) do context = case embedding_text do nil -> basic_context(record) @@ -21,8 +17,9 @@ defmodule MusicLibrary.RecordChat do """ You are a knowledgeable music assistant. Answer questions about the album \ - the user is currently viewing. Use the provided information as your primary \ - reference. Be concise and accurate. When unsure, say so. + the user is currently viewing. Use the provided album 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. Album information: #{context} diff --git a/lib/open_ai.ex b/lib/open_ai.ex index 67cb48d7..89b91977 100644 --- a/lib/open_ai.ex +++ b/lib/open_ai.ex @@ -17,11 +17,12 @@ defmodule OpenAI do end def chat_stream(messages, opts \\ []) when is_list(messages) do - model = Keyword.get(opts, :model, "gpt-4o-mini") + model = Keyword.get(opts, :model, "gpt-4.1") temperature = Keyword.get(opts, :temperature, 0.7) + instructions = Keyword.get(opts, :instructions, "") on_chunk = Keyword.fetch!(opts, :on_chunk) - case API.chat_stream(messages, model, temperature, api_key(), on_chunk) do + case API.chat_stream(messages, instructions, model, temperature, api_key(), on_chunk) do :ok -> :ok {:error, _reason} = error -> error end diff --git a/lib/open_ai/api.ex b/lib/open_ai/api.ex index 5fba6237..bb474f50 100644 --- a/lib/open_ai/api.ex +++ b/lib/open_ai/api.ex @@ -48,7 +48,7 @@ defmodule OpenAI.API do ) end - def chat_stream(messages, model, temperature, api_key, cb) do + def chat_stream(messages, instructions, model, temperature, api_key, cb) do fun = fn request, finch_request, finch_name, finch_options -> fun = fn {:status, status}, response -> @@ -59,11 +59,11 @@ defmodule OpenAI.API do {:data, data}, response -> data - |> String.split("data: ") - |> Enum.each(fn str -> - str + |> String.split("\n") + |> Enum.each(fn line -> + line |> String.trim() - |> decode_chat_chunk(cb) + |> decode_responses_event(cb) end) response @@ -75,14 +75,16 @@ defmodule OpenAI.API do end end - case Req.post("https://api.openai.com/v1/chat/completions", - receive_timeout: 30_000, + case Req.post("https://api.openai.com/v1/responses", + receive_timeout: 60_000, connect_options: [ timeout: 5_000 ], json: %{ model: model, - messages: messages, + instructions: instructions, + input: messages, + tools: [%{type: "web_search_preview"}], stream: true, temperature: temperature }, @@ -122,13 +124,15 @@ defmodule OpenAI.API do defp decode_body("[DONE]", _), do: :ok defp decode_body(json, cb), do: cb.(JSON.decode!(json)) - defp decode_chat_chunk("", _cb), do: :ok - defp decode_chat_chunk("[DONE]", _cb), do: :ok + defp decode_responses_event("", _cb), do: :ok + defp decode_responses_event("event:" <> _rest, _cb), do: :ok - defp decode_chat_chunk(json, cb) do - case get_in(JSON.decode!(json), ["choices", Access.at(0), "delta", "content"]) do - nil -> :ok - content -> cb.(content) + defp decode_responses_event("data: " <> json, cb) do + case JSON.decode!(json) do + %{"type" => "response.output_text.delta", "delta" => delta} -> cb.(delta) + _other -> :ok end end + + defp decode_responses_event(_line, _cb), do: :ok end