patterns-ai-core / langchainrb

Build LLM-powered applications in Ruby
https://rubydoc.info/gems/langchainrb
MIT License
1.4k stars 194 forks source link

hard to debug failed Gemini API Calls #773

Closed palladius closed 1 month ago

palladius commented 1 month ago

Every time Gemini calls fail, I find it very hard to troubleshoot. The error I get is something like this:

/Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/llm/google_gemini.rb:80:in `chat': #<Net::HTTPBadRequest:0x00000001301d9e40> (StandardError)
        from /Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/assistants/assistant.rb:324:in `chat_with_llm'

and the only info I get is that its probably a 400.

I found the culprit to be this code:

      if wrapped_response.chat_completion || Array(wrapped_response.tool_calls).any?
        wrapped_response
      else
        # First with pry, then by just puts-ing it helps A LOT
        puts(response.body)
        raise StandardError.new(response)
      end

By changing it to raise StandardError.new(response.body) you get something like this:

/Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/llm/google_gemini.rb:79:in `chat': { (StandardError)
  "error": {
    "code": 400,
    "message": "API key expired. Please renew the API key.",
    "status": "INVALID_ARGUMENT",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "API_KEY_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "service": "generativelanguage.googleapis.com"
        }
      }
    ]
  }
}
        from /Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/assistants/assistant.rb:324:in `chat_with_llm'

I think it's prety safe and much more meaningful to just raise the response body payload, since Google gives meaningful messages like "API KEY expired" or "API not available in this country".

palladius commented 1 month ago

I proposed a patch both for Gemini and for Vertex. Even in vertex, changing that line helps.

Before the cure (ignore the file name, its solving a different issue from this):

/Users/ricc/git/ecommerce-ai-assistant-demo-gemini/lib/vertex_monkey_patch.rb:52:in `chat': #<Net::HTTPForbidden:0x000000013c914b18> (StandardError)

After the cure:


/Users/ricc/git/ecommerce-ai-assistant-demo-gemini/lib/vertex_monkey_patch.rb:52:in `chat': { (StandardError)
  "error": {
    "code": 403,
    "message": "Permission denied on resource project palladius-genai-fake-project.",
    "status": "PERMISSION_DENIED",
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help",
        "links": [
          {
            "description": "Google developers console",
            "url": "https://console.developers.google.com"
          }
        ]
      },
      {
        "@type": "type.googleapis.com/google.rpc.ErrorInfo",
        "reason": "CONSUMER_INVALID",
        "domain": "googleapis.com",
        "metadata": {
          "service": "aiplatform.googleapis.com",
          "consumer": "projects/palladius-genai-fake-project"
        }
      }
    ]
  }
}
palladius commented 1 month ago

another PR for Vertex: https://github.com/patterns-ai-core/langchainrb/pull/775

andreibondarev commented 1 month ago

Fixed.