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

Fix streaming error when using Azure OpenAI Service #158

Merged

Conversation

stevehodgkiss
Copy link
Contributor

Trying out Azure OpenAI service and ran into an issue with streaming chat completions (gpt-4o version 2024-05-13).

A case clause error occurs in LLMChain.do_run (link). The issue is a message is being transformed and returning [].

16:20:35.537 [error] {{:case_clause, {:ok, [[], [%LangChain.MessageDelta{content: "", status: :incomplete, index: 0, role: :assistant, tool_calls: nil}], [%LangChain.MessageDelta{

The message from Azure OpenAI that results in a [] return value from do_process_return is (from here):

%{
  "choices" => [],
  "created" => 0,
  "id" => "",
  "model" => "",
  "object" => "",
  "prompt_filter_results" => [
    %{
      "content_filter_results" => %{
        "hate" => %{"filtered" => false, "severity" => "safe"},
        "self_harm" => %{"filtered" => false, "severity" => "safe"},
        "sexual" => %{"filtered" => false, "severity" => "safe"},
        "violence" => %{"filtered" => false, "severity" => "safe"}
      },
      "prompt_index" => 0
    }
  ]
}

I went with adding this to Utils.handle_stream_fn since it would prevent this error for other transform return values that potentially could return [], but an alternative fix would be to add the following to ChatOpenAI before L664:

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

@stevehodgkiss stevehodgkiss changed the title Fix streaming issue with Azure OpenAI Service Fix streaming error when using Azure OpenAI Service Jul 18, 2024
Copy link
Owner

@brainlid brainlid left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

❤️💛💙💜

@brainlid brainlid merged commit 28ba8ce into brainlid:main Jul 21, 2024
1 check passed
@brainlid
Copy link
Owner

Instead of matching on the :skip or [], could a pattern match in the ChatOpenAI module match on the empty choices and return a :skip?

@stevehodgkiss
Copy link
Contributor Author

Yep, that's an option. That's probably better now that I think about it a bit more, because having this in Utils won't prevent empty choices from slipping through as [] in the non-streaming case (although I didn't see this message/error in non-streaming). The reason I went with Utils is so that it would prevent this invalid state and subsequent error for any chat model, but again this would apply to the streaming case 🤔

Shall I open a PR to move this fix to ChatOpenAI?

@brainlid
Copy link
Owner

Shall I open a PR to move this fix to ChatOpenAI?

Yes please. Sorry, I thought more about the changed after I approved and merged it. Thanks!

stevehodgkiss added a commit to stevehodgkiss/langchain that referenced this pull request Jul 22, 2024
brainlid pushed a commit that referenced this pull request Jul 22, 2024
* Revert "Fix streaming issue with Azure OpenAI Service (#158)"

This reverts commit 28ba8ce.

* Skip messages with no choices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants