cyberchitta / openai_ex

Community maintained Elixir library for OpenAI API
https://hexdocs.pm/openai_ex
Apache License 2.0
140 stars 19 forks source link

no function clause matching in OpenaiEx.Http.Finch.to_error/2 #100

Closed goodniceweb closed 4 months ago

goodniceweb commented 4 months ago

Describe the bug

Thank you for the great effort working on the library. It works great for my use-case. Though, I found a little bug when API experience problems and when I use Finch as a http client.

What happens

From time to time I see the error happens, that the connection is closed, especially when API experience latency and increased error rates

Incident Report for OpenAI: Increased latency and error rates

[error] Task #PID<0.3179.0> started from #PID<0.3127.0> terminating
** (FunctionClauseError) no function clause matching in OpenaiEx.Http.Finch.to_error/2
    (openai_ex 0.8.0) lib/openai_ex/http_finch.ex:81: OpenaiEx.Http.Finch.to_error(:closed,
      %Finch.Request{
        scheme: :https,
        host: "api.openai.com",
        port: 443,
        method: "POST",
        path: "/v1/chat/completions",
        headers: [{"Authorization", "Bearer sk-2B3RaAepzx4wQQGZgLgtE3BlbgFJQeCEfIgqka9qAQRyBp5S"}, {"Content-Type", "application/json"}], body: ["{\"", [[] | "messages"], "\":", [91, ["{\"", [[] | "role"], "\":", [34, [[] | "system"], 34], ",\"", [[] | "content"], "\":", [34, [[] | "You are a helpful assistant"]

Source of the issue

I think the error is related to the fact that when there is an error while doing a request with Finch

https://github.com/restlessronin/openai_ex/blob/52e1dd9a8bfff4c34fe24e7bbe25255f19e97f6c/lib/openai_ex/http_finch.ex#L36

The only error we expect is the timeout error:

https://github.com/restlessronin/openai_ex/blob/52e1dd9a8bfff4c34fe24e7bbe25255f19e97f6c/lib/openai_ex/http_finch.ex#L81

There are no other function clauses.

Suggested solution

  1. I'd create a separate function clause for the error when a connection is closed
    def to_error(:closed, request), do: {:error, Error.connection_closed(request)}
  2. I'd also add a default function clause, so other developers don't need to monkey patch the module, trying to handle any other possible error Finch might add at any point in the future.
    def to_error(reason, request), do: {:error, Error.other_error(reason, request)}

If you agree, I can work on a PR for this.

To Reproduce

  1. Wait for the OpenAI API to work poorly
  2. Run a few requests toward the chat completions endpoint
  3. Sometimes it happens

Code snippets

No response

OS

macOS

Elixir version

1.15.2-otp-26

Library version

v0.8.0

restlessronin commented 4 months ago

@goodniceweb That sounds about right. Please submit a PR and I will merge it.

restlessronin commented 4 months ago

Hi @goodniceweb, could you please let me know if you are going to submit a PR? Otherwise I will add the fixes myself in a day or so.

goodniceweb commented 4 months ago

Hi @restlessronin I'm working on the PR. Thanks for letting me know you're ok with the proposed solution

restlessronin commented 4 months ago

@goodniceweb Thank you for the PR!!

🎉🎉🎉🎉

restlessronin commented 4 months ago

@goodniceweb published at hex as v0.8.2