diff --git a/lib/chat_models/chat_anthropic.ex b/lib/chat_models/chat_anthropic.ex index 4d800bc..dd7d6a4 100644 --- a/lib/chat_models/chat_anthropic.ex +++ b/lib/chat_models/chat_anthropic.ex @@ -367,7 +367,7 @@ defmodule LangChain.ChatModels.ChatAnthropic do end {:ok, %Req.Response{status: 529}} -> - {:error, LangChainError.exception(type: "overloaded", message: "Overloaded")} + {:error, LangChainError.exception(type: "overloaded_error", message: "Overloaded")} {:error, %Req.TransportError{reason: :timeout} = err} -> {:error, @@ -415,7 +415,9 @@ defmodule LangChain.ChatModels.ChatAnthropic do data - {:error, %LangChainError{} = error} -> + # The error tuple was successfully received from the API. Unwrap it and + # return it as an error. + {:ok, {:error, %LangChainError{} = error}} -> {:error, error} {:error, %Req.TransportError{reason: :timeout} = err} -> diff --git a/test/chains/llm_chain_test.exs b/test/chains/llm_chain_test.exs index 63b6269..e0b4092 100644 --- a/test/chains/llm_chain_test.exs +++ b/test/chains/llm_chain_test.exs @@ -6,6 +6,7 @@ defmodule LangChain.Chains.LLMChainTest do import LangChain.Fixtures alias LangChain.ChatModels.ChatOpenAI + alias LangChain.ChatModels.ChatAnthropic alias LangChain.Chains.LLMChain alias LangChain.PromptTemplate alias LangChain.Function @@ -17,6 +18,8 @@ defmodule LangChain.Chains.LLMChainTest do alias LangChain.LangChainError alias LangChain.MessageProcessors.JsonProcessor + @anthropic_test_model "claude-3-opus-20240229" + setup do {:ok, chat} = ChatOpenAI.new(%{temperature: 0}) @@ -956,6 +959,7 @@ defmodule LangChain.Chains.LLMChainTest do |> LLMChain.run() assert reason.type == nil + assert reason.message == "Invalid 'messages': empty array. Expected an array with minimum length 1, but got an empty array instead." end @@ -971,6 +975,7 @@ defmodule LangChain.Chains.LLMChainTest do |> LLMChain.run() assert reason.type == nil + assert reason.message == "Invalid 'messages': empty array. Expected an array with minimum length 1, but got an empty array instead." end @@ -1032,6 +1037,24 @@ defmodule LangChain.Chains.LLMChainTest do assert_received {:function_called, "fly_regions"} end + test "returns error when receives overloaded_error from Anthropic" do + # Made NOT LIVE here + expect(ChatAnthropic, :call, fn _model, _prompt, _tools -> + # IO.puts "ChatAnthropic.call OVERLOAD USED!!!!" + {:error, + LangChainError.exception(type: "overloaded_error", message: "Overloaded (from test)")} + end) + + model = ChatAnthropic.new!(%{stream: true, model: @anthropic_test_model}) + + assert {:error, _updated_chain, reason} = + LLMChain.new!(%{llm: model}) + |> LLMChain.run() + + assert reason.type == "overloaded_error" + assert reason.message == "Overloaded (from test)" + end + test "errors when messages have PromptTemplates" do messages = [ PromptTemplate.new!(%{ diff --git a/test/chat_models/chat_anthropic_test.exs b/test/chat_models/chat_anthropic_test.exs index 9022b17..4ca60ff 100644 --- a/test/chat_models/chat_anthropic_test.exs +++ b/test/chat_models/chat_anthropic_test.exs @@ -555,6 +555,22 @@ defmodule LangChain.ChatModels.ChatAnthropicTest do "Received error from API: The security token included in the request is invalid." end + test "returns error tuple when receiving overloaded_error" do + # Made NOT LIVE here + expect(Req, :post, fn _req_struct, _opts -> + # IO.puts "REQ OVERLOAD USED!!!!" + {:ok, + {:error, + LangChainError.exception(type: "overloaded_error", message: "Overloaded (from test)")}} + end) + + model = ChatAnthropic.new!(%{stream: true, model: @test_model}) + assert {:error, reason} = ChatAnthropic.call(model, "prompt", []) + + assert reason.type == "overloaded_error" + assert reason.message == "Overloaded (from test)" + end + for api <- @apis do Module.put_attribute(__MODULE__, :tag, {:"live_#{api}", true}) @tag live_call: true, live_api: api diff --git a/test/test_helper.exs b/test/test_helper.exs index 12ad4d2..d8a7987 100644 --- a/test/test_helper.exs +++ b/test/test_helper.exs @@ -13,6 +13,7 @@ Application.put_env( Mimic.copy(LangChain.Utils.BedrockStreamDecoder) Mimic.copy(LangChain.Utils.AwsEventstreamDecoder) +Mimic.copy(Req) Mimic.copy(LangChain.ChatModels.ChatOpenAI) Mimic.copy(LangChain.ChatModels.ChatAnthropic) Mimic.copy(LangChain.ChatModels.ChatMistralAI)