Use gpt 4.1 with search
This commit is contained in:
@@ -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}
|
||||
|
||||
+3
-2
@@ -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
|
||||
|
||||
+18
-14
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user