Handle OpenAI api streaming error messages
This commit is contained in:
+68
-30
@@ -33,39 +33,63 @@ defmodule OpenAI.API do
|
|||||||
any())) ::
|
any())) ::
|
||||||
:ok | {:error, String.t()}
|
:ok | {:error, String.t()}
|
||||||
def chat_stream(messages, instructions, model, temperature, config, cb) do
|
def chat_stream(messages, instructions, model, temperature, config, cb) do
|
||||||
case config
|
config
|
||||||
|> new_request()
|
|> new_request()
|
||||||
|> Req.merge(
|
|> Req.merge(
|
||||||
url: "/v1/responses",
|
url: "/v1/responses",
|
||||||
receive_timeout: 60_000,
|
receive_timeout: 60_000,
|
||||||
connect_options: [timeout: 5_000],
|
connect_options: [timeout: 5_000],
|
||||||
json: %{
|
json: %{
|
||||||
model: model,
|
model: model,
|
||||||
instructions: instructions,
|
instructions: instructions,
|
||||||
input: messages,
|
input: messages,
|
||||||
tools: [%{type: "web_search_preview"}],
|
tools: [%{type: "web_search_preview"}],
|
||||||
stream: true,
|
stream: true,
|
||||||
temperature: temperature
|
temperature: temperature
|
||||||
},
|
},
|
||||||
into: fn {:data, data}, {req, resp} ->
|
into: fn {:data, data}, {req, resp} ->
|
||||||
buffer = Req.Request.get_private(req, :sse_buffer, "")
|
buffer = Req.Request.get_private(req, :sse_buffer, "")
|
||||||
{events, buffer} = ServerSentEvents.parse(buffer <> data)
|
{events, buffer} = ServerSentEvents.parse(buffer <> data)
|
||||||
req = Req.Request.put_private(req, :sse_buffer, buffer)
|
req = Req.Request.put_private(req, :sse_buffer, buffer)
|
||||||
|
|
||||||
for %{data: json} <- events do
|
decode_events(events, cb, req, resp)
|
||||||
decode_responses_event(json, cb)
|
end
|
||||||
end
|
)
|
||||||
|
|> do_chat_stream()
|
||||||
|
end
|
||||||
|
|
||||||
{:cont, {req, resp}}
|
defp decode_events([], _cb, req, resp), do: {:cont, {req, resp}}
|
||||||
end
|
|
||||||
)
|
defp decode_events([%{data: json} | rest], cb, req, resp) do
|
||||||
|> Req.post() do
|
case decode_responses_event(json, cb) do
|
||||||
{:ok, %{status: status}} when status in 200..299 -> :ok
|
{:error, message} ->
|
||||||
{:ok, %{body: body}} -> {:error, "OpenAI API error: #{inspect(body)}"}
|
Logger.error(message)
|
||||||
{:error, exception} -> {:error, "Connection error: #{Exception.message(exception)}"}
|
{:halt, {req, Req.Response.put_private(resp, :error, message)}}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
decode_events(rest, cb, req, resp)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp do_chat_stream(req) do
|
||||||
|
case Req.post(req) do
|
||||||
|
{:ok, %{status: status} = resp} when status in 200..299 ->
|
||||||
|
if message = Req.Response.get_private(resp, :error) do
|
||||||
|
{:error, message}
|
||||||
|
else
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
{:ok, %{body: body}} ->
|
||||||
|
{:error, "OpenAI API error: #{inspect(body)}"}
|
||||||
|
|
||||||
|
{:error, exception} ->
|
||||||
|
{:error, "Connection error: #{Exception.message(exception)}"}
|
||||||
|
end
|
||||||
|
catch
|
||||||
|
{:stream_error, message} -> {:error, message}
|
||||||
|
end
|
||||||
|
|
||||||
@spec get_embeddings(String.t(), OpenAI.Config.t()) :: {:ok, [float()]} | {:error, term()}
|
@spec get_embeddings(String.t(), OpenAI.Config.t()) :: {:ok, [float()]} | {:error, term()}
|
||||||
def get_embeddings(text, config) do
|
def get_embeddings(text, config) do
|
||||||
resp =
|
resp =
|
||||||
@@ -101,8 +125,22 @@ defmodule OpenAI.API do
|
|||||||
|
|
||||||
defp decode_responses_event(json, cb) do
|
defp decode_responses_event(json, cb) do
|
||||||
case JSON.decode!(json) do
|
case JSON.decode!(json) do
|
||||||
%{"type" => "response.output_text.delta", "delta" => delta} -> cb.(delta)
|
%{"type" => "response.output_text.delta", "delta" => delta} ->
|
||||||
_other -> :ok
|
cb.(delta)
|
||||||
|
|
||||||
|
%{"type" => "error", "error" => %{"message" => message}} ->
|
||||||
|
{:error, message}
|
||||||
|
|
||||||
|
%{"type" => "response.failed", "response" => %{"error" => %{"message" => message}}} ->
|
||||||
|
{:error, message}
|
||||||
|
|
||||||
|
%{"type" => "response." <> _} ->
|
||||||
|
:ok
|
||||||
|
|
||||||
|
other ->
|
||||||
|
Logger.warning(fn ->
|
||||||
|
"Unexpected response: #{inspect(other)}"
|
||||||
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user