Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Azure OpenAI Service streaming fix #161

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/chat_models/chat_open_ai.ex
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ defmodule LangChain.ChatModels.ChatOpenAI do
:skip
end

def do_process_response(_model, %{"choices" => []}), do: :skip

def do_process_response(model, %{"choices" => choices} = _data) when is_list(choices) do
# process each response individually. Return a list of all processed choices
for choice <- choices do
Expand Down
2 changes: 1 addition & 1 deletion lib/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ defmodule LangChain.Utils do
parsed_data =
parsed_data
|> Enum.map(transform_data_fn)
|> Enum.reject(&(&1 == :skip || &1 == []))
|> Enum.reject(&(&1 == :skip))

# execute the callback function for each MessageDelta and an optional
# TokenUsage
Expand Down
4 changes: 4 additions & 0 deletions test/chat_models/chat_open_ai_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,10 @@ defmodule LangChain.ChatModels.ChatOpenAITest do
%{model: model}
end

test "returns skip when given an empty choices list", %{model: model} do
assert :skip == ChatOpenAI.do_process_response(model, %{"choices" => []})
end

test "handles receiving a message", %{model: model} do
response = %{
"message" => %{"role" => "assistant", "content" => "Greetings!"},
Expand Down
26 changes: 0 additions & 26 deletions test/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -168,30 +168,4 @@ defmodule LangChain.UtilsTest do
assert reason == "ChatModel module \"Elixir.Missing.Module\" not found"
end
end

describe "handle_stream_fn/3" do
test "skips transformed messages with :skip" do
function =
Utils.handle_stream_fn(%{callbacks: []}, fn _ -> {[[]], ""} end, fn _ ->
:skip
end)

{:cont, {_model, %{body: body}}} = function.({:data, ""}, {1, %Req.Response{status: 200}})

assert body == []
end

test "skips transformed messages with empty list" do
empty_transform_message = []

function =
Utils.handle_stream_fn(%{callbacks: []}, fn _ -> {[[]], ""} end, fn _ ->
empty_transform_message
end)

{:cont, {_model, %{body: body}}} = function.({:data, ""}, {1, %Req.Response{status: 200}})

assert body == []
end
end
end
Loading