Skip to content

Commit

Permalink
attempt at Anthropic fix
Browse files Browse the repository at this point in the history
- related to issue #193
  • Loading branch information
brainlid committed Nov 23, 2024
1 parent 0d33d55 commit a35e5c9
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/chat_models/chat_anthropic.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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} ->
Expand Down
23 changes: 23 additions & 0 deletions test/chains/llm_chain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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})

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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!(%{
Expand Down
16 changes: 16 additions & 0 deletions test/chat_models/chat_anthropic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a35e5c9

Please sign in to comment.