patterns-ai-core / langchainrb

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

Anthropic throws an error when Database tool is used #826

Closed andreibondarev closed 1 month ago

andreibondarev commented 1 month ago

Describe the bug A clear and concise description of what the bug is.

To Reproduce

llm = Langchain::LLM::Anthropic.new(api_key: ENV["ANTHROPIC_API_KEY"])
llm.chat messages:[{role:"user", content:"hello"}], tools: Langchain::Tool::Database.function_schemas.to_anthropic_format

Expected behavior The method call is not supposed to throw an error.

Desktop (please complete the following information):

andreibondarev commented 1 month ago

Trace:


irb(main):006> llm.chat messages:[{role:"user", content:"hello"}], tools: Langchain::Tool::Database.function_schemas.to_anthropic_format

I, [2024-10-11T20:51:26.344119 #62514]  INFO -- request: POST https://api.anthropic.com/v1/messages
I, [2024-10-11T20:51:26.344183 #62514]  INFO -- request: x-api-key: "[MASKED]"
anthropic-version: "2023-06-01"
Content-Type: "application/json"
I, [2024-10-11T20:51:26.344214 #62514]  INFO -- request: {"messages":[{"role":"user","content":"hello"}],"tools":"#<Langchain::Tool::Database:0x0000000123294310>","model":"claude-3-5-sonnet-20240620","max_tokens":256,"temperature":0.0}
I, [2024-10-11T20:51:26.674608 #62514]  INFO -- error: /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/response/raise_error.rb:30:in `on_complete': the server responded with status 400 (Faraday::BadRequestError)
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/middleware.rb:57:in `block in call'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/response.rb:42:in `on_complete'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/middleware.rb:56:in `call'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/middleware.rb:56:in `call'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/response/logger.rb:23:in `call'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/rack_builder.rb:152:in `build_response'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/connection.rb:452:in `run_request'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/faraday-2.12.0/lib/faraday/connection.rb:280:in `post'
    from /Users/andrei/Code/anthropic/lib/anthropic/http.rb:21:in `json_post'
    from /Users/andrei/Code/anthropic/lib/anthropic/client.rb:67:in `messages'
    from /Users/andrei/Code/langchain/lib/langchain/llm/anthropic.rb:121:in `chat'
    from (irb):6:in `<top (required)>'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/workspace.rb:121:in `eval'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/workspace.rb:121:in `evaluate'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/context.rb:633:in `evaluate_expression'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb/context.rb:600:in `evaluate'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1049:in `block (2 levels) in eval_input'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1380:in `signal_status'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1041:in `block in eval_input'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1120:in `block in each_top_level_statement'
    from <internal:kernel>:187:in `loop'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1117:in `each_top_level_statement'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1040:in `eval_input'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1021:in `block in run'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1020:in `catch'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:1020:in `run'
    from /Users/andrei/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.13.1/lib/irb.rb:904:in `start'
    from bin/console:51:in `<main>'

I, [2024-10-11T20:51:26.675104 #62514]  INFO -- error: date: "Sat, 12 Oct 2024 00:51:26 GMT"
content-type: "application/json"
content-length: "105"
connection: "keep-alive"
x-should-retry: "false"
request-id: "req_015h76Rz3haqy5sEUE11N6YS"
x-cloud-trace-context: "75256c67288cb8462ee6a04232589349"
via: "1.1 google"
cf-cache-status: "DYNAMIC"
x-robots-tag: "none"
server: "cloudflare"
cf-ray: "8d1319fb6d18c993-IAD"
I, [2024-10-11T20:51:26.675955 #62514]  INFO -- error: {"type"=>"error",
 "error"=>
  {"type"=>"invalid_request_error",
   "message"=>"tools: Input should be a valid list"}}
andreibondarev commented 1 month ago

@dghirardo is looking into this.