From 24381263baada442c6367c9a78a9b8288b9daed6 Mon Sep 17 00:00:00 2001 From: Josh Brito <65372461+Jbrito6492@users.noreply.github.com> Date: Sat, 6 Jul 2024 19:52:46 -0700 Subject: [PATCH] Issue 697 - Show API error when calling `.complete` on Anthropic client (#698) --- lib/langchain/llm/anthropic.rb | 14 +++++++++++++- lib/langchain/llm/base.rb | 1 + spec/fixtures/llm/anthropic/error.json | 7 +++++++ spec/langchain/llm/anthropic_spec.rb | 19 +++++++++++++++++++ 4 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 spec/fixtures/llm/anthropic/error.json diff --git a/lib/langchain/llm/anthropic.rb b/lib/langchain/llm/anthropic.rb index 502f0899d..aa282d952 100644 --- a/lib/langchain/llm/anthropic.rb +++ b/lib/langchain/llm/anthropic.rb @@ -81,7 +81,10 @@ def complete( parameters[:metadata] = metadata if metadata parameters[:stream] = stream if stream - response = client.complete(parameters: parameters) + response = with_api_error_handling do + client.complete(parameters: parameters) + end + Langchain::LLM::AnthropicResponse.new(response) end @@ -114,6 +117,15 @@ def chat(params = {}) Langchain::LLM::AnthropicResponse.new(response) end + def with_api_error_handling + response = yield + return if response.empty? + + raise Langchain::LLM::ApiError.new "Anthropic API error: #{response.dig("error", "message")}" if response&.dig("error") + + response + end + private def set_extra_headers! diff --git a/lib/langchain/llm/base.rb b/lib/langchain/llm/base.rb index 2f58752d4..73410ec26 100644 --- a/lib/langchain/llm/base.rb +++ b/lib/langchain/llm/base.rb @@ -8,6 +8,7 @@ class ApiError < StandardError; end # Langchain.rb provides a common interface to interact with all supported LLMs: # # - {Langchain::LLM::AI21} + # - {Langchain::LLM::Anthropic} # - {Langchain::LLM::Azure} # - {Langchain::LLM::Cohere} # - {Langchain::LLM::GooglePalm} diff --git a/spec/fixtures/llm/anthropic/error.json b/spec/fixtures/llm/anthropic/error.json new file mode 100644 index 000000000..4051eea03 --- /dev/null +++ b/spec/fixtures/llm/anthropic/error.json @@ -0,0 +1,7 @@ +{ + "type": "error", + "error": { + "type": "invalid_request_error", + "message": "The request is invalid. Please check the request and try again." + } +} diff --git a/spec/langchain/llm/anthropic_spec.rb b/spec/langchain/llm/anthropic_spec.rb index 9cc140fbb..f7bbf2831 100644 --- a/spec/langchain/llm/anthropic_spec.rb +++ b/spec/langchain/llm/anthropic_spec.rb @@ -30,6 +30,25 @@ expect(subject.complete(prompt: completion).model).to eq("claude-2.1") end end + + context "with failed API call" do + let(:fixture) { File.read("spec/fixtures/llm/anthropic/error.json") } + + before do + allow(subject.client).to receive(:complete) + .with(parameters: { + model: described_class::DEFAULTS[:completion_model_name], + prompt: completion, + temperature: described_class::DEFAULTS[:temperature], + max_tokens_to_sample: described_class::DEFAULTS[:max_tokens_to_sample] + }) + .and_return(JSON.parse(fixture)) + end + + it "raises an error" do + expect { subject.complete(prompt: completion) }.to raise_error(Langchain::LLM::ApiError, "Anthropic API error: The request is invalid. Please check the request and try again.") + end + end end describe "#chat" do