patterns-ai-core / langchainrb

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

llm/google_vertex_ai.rb:92:in `chat': undefined method `upcase' for an instance of Hash (NoMethodError) #776

Open palladius opened 2 weeks ago

palladius commented 2 weeks ago

Describe the bug

If I call Vertex AI LLM chat() functionality, I consistently get this error:

I, [2024-09-15T09:56:56.085633 #93423]  INFO -- : [Langchain.rb] [Langchain::Assistant]: Sending a call to Langchain::LLM::GoogleVertexAI
/Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/llm/google_vertex_ai.rb:92:in `chat': undefined method `upcase' for an instance of Hash (NoMethodError)

      params[:tool_choice] = {function_calling_config: {mode: params[:tool_choice].upcase}} if params[:tool_choice]
                                                                                  ^^^^^^^
        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'
        from /Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/assistants/assistant.rb:256:in `handle_user_or_tool_message'
        from /Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/assistants/assistant.rb:223:in `process_latest_message'
        from /Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/assistants/assistant.rb:205:in `handle_state'
        from /Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/assistants/assistant.rb:108:in `run'
        from /Users/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/bundler/gems/langchainrb-6e83c6c0a7b3/lib/langchain/assistants/assistant.rb:127:in `add_message_and_run'
        from /Users/ricc/git/ecommerce-ai-assistant-demo-gemini/lib/assistant.rb:7:in `msg'
        from test-e2e-vertexai.rb:101:in `<main>'

I'm not a Tool expert, but if i disable that line it works fine. I'm going to propose a fix, and maybe Andrei has a better one.

palladius commented 2 weeks ago

Oh sorry, silly me, it's already patched :)

https://github.com/patterns-ai-core/langchainrb/blob/main/lib/langchain/llm/google_vertex_ai.rb#L92

Andrei told me that that line was needed for some reason, so it's still good to have this bug open.

Is commenting out this functionality doing some damage on the Tool part?

      # params[:tool_choice] = {function_calling_config: {mode: params[:tool_choice].upcase}} if params[:tool_choice]

Feel free to resolve if the answer is no.