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