patterns-ai-core / langchainrb

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

[BUG] PaLM model token length validation exception #238

Closed alchaplinsky closed 1 year ago

alchaplinsky commented 1 year ago

Problem with PaLM model token length validation while sending a message through Conversation.

to reproduce:

 llm = Langchain::LLM::GooglePalm.new api_key: ENV['GOOGLE_PALM_API_KEY']
 convo = Langchain::Conversation.new llm: llm
 convo.message 'Hello'

Result:

langchainrb/lib/langchain/utils/token_length/base_validator.rb:17:in `+': nil can't be coerced into Integer (TypeError)
from /Users/alex/OSS/langchainrb/lib/langchain/utils/token_length/base_validator.rb:17:in `sum'
from /Users/alex/OSS/langchainrb/lib/langchain/utils/token_length/base_validator.rb:17:in `validate_max_tokens!'
from /Users/alex/OSS/langchainrb/lib/langchain/llm/google_palm.rb:92:in `chat'
from /Users/alex/OSS/langchainrb/lib/langchain/conversation.rb:83:in `llm_response'
from /Users/alex/OSS/langchainrb/lib/langchain/conversation.rb:57:in `message'
from (irb):4:in `<main>'
from bin/console:50:in `<main>'
andreibondarev commented 1 year ago

@alchaplinsky Works for me. Is your GOOGLE_PALM_API_KEY set?

alchaplinsky commented 1 year ago

@andreibondarev Sure. Did you try on the most recent version or the main branch? It fails for me in main.

andreibondarev commented 1 year ago

@alchaplinsky On main

andreibondarev commented 1 year ago
 -> langchain [main*]: bin/console
irb(main):001:0> llm = Langchain::LLM::GooglePalm.new api_key: ENV['GOOGLE_PALM_API_KEY']
=> #<Langchain::LLM::GooglePalm:0x00007fa2b5182e78 @client=#<GooglePalmApi::Client:0x00007fa2b5178b58 @api_key="AIzaSyBRzMe0CRTkgIYctVPTy9eepY2f1z8FDtk">, @defaults={:temperature=>0.0, :dimension=>768, :completion_model_name=>"text-bison-...
irb(main):002:0>
irb(main):003:0> convo = Langchain::Conversation.new llm: llm
=> #<Langchain::Conversation:0x00007fa2e530d218 @llm=#<Langchain::LLM::GooglePalm:0x00007fa2b5182e78 @client=#<GooglePalmApi::Client:0x00007fa2b5178b58 @api_key="AIzaSyBRzMe0CRTkgIYctVPTy9eepY2f1z8FDtk">, @defaults={:temperature=>0.0, :di...
irb(main):004:0> convo.message 'Hello'

=> "Hello! How can I help you today?"
alchaplinsky commented 1 year ago

This was happening due to API error because PaLM2 API can not be called from outside of the US atm.