patterns-ai-core / langchainrb

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

NotImplementedError: Langchain::LLM::GoogleGemini does not support generating embeddings (NotImplementedError) #752

Closed palladius closed 1 month ago

palladius commented 2 months ago

API call should be:

curl "https://generativelanguage.googleapis.com/v1beta/models/text-embedding-004:embedContent?key=$GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"model": "models/text-embedding-004",
     "content": {
     "parts":[{
     "text": "Hello world"}]}, }' 2> /dev/null 
andreibondarev commented 2 months ago

@palladius Hmm... what is the Ruby code you're running? See below:

irb(main):003> llm = Langchain::LLM::GoogleGemini.new(api_key: ENV["GOOGLE_GEMINI_API_KEY"])
irb(main):004> llm.embed text: "Hello world"
=>
#<Langchain::LLM::GoogleGeminiResponse:0x000000010a8d4108
 @model="text-embedding-004",
 @raw_response=
  {"embedding"=>
    {"values"=>
      [0.013168523,
       -0.008711934,
       -0.046782676,
       ...
palladius commented 2 months ago

I'm running ruby 3.2.1 I can confirm the same error with latest gem 0.15.4

irb(main):007> embedding = @llm.embed text: "Hello world"
/usr/local/google/home/ricc/.local/share/gem/ruby/3.2.0/gems/langchainrb-0.15.4/lib/langchain/llm/google_gemini.rb:96:in `embed': undefined method `URI' for #<Langchain::LLM::GoogleGemini:0x00007faed850c290 @api_key="REDACTED", @defaults={:chat_completion_model_name=>"gemini-1.5-pro-latest", :embeddings_model_name=>"text-embedding-004", :temperature=>0.0}, @chat_parameters=#<Langchain::LLM::UnifiedParameters:0x00007faed3a09108 @schema={:messages=>{}, :model=>{:default=>"gemini-1.5-pro-latest"}, :prompt=>{}, :system=>{}, :response_format=>{}, :stop=>{}, :stream=>{}, :max_tokens=>{}, :temperature=>{:default=>0.0}, :top_p=>{}, :top_k=>{}, :frequency_penalty=>{}, :presence_penalty=>{}, :repetition_penalty=>{}, :seed=>{}, :tools=>{:default=>[]}, :tool_choice=>{}, :logit_bias=>{}, :generation_config=>{:default=>nil}, :safety_settings=>{:default=>nil}, :contents=>{}, :system_instruction=>{}, :tool_config=>{}}, @aliases={}, @remapped={:messages=>:contents, :system=>:system_instruction, :tool_choice=>:tool_config}, @ignored=#<Set: {}>>> (NoMethodError)

      uri = URI("https://generativelanguage.googleapis.com/v1beta/models/#{model}:embedContent?key=#{api_key}")
            ^^^
        from (irb):7:in `<main>'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/irb-1.14.0/exe/irb:9:in `<top (required)>'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/bin/irb:25:in `load'
        from /usr/local/google/home/ricc/.rbenv/versions/3.2.1/bin/irb:25:in `<main>'
irb(main):008> 

Another small thing: whenever I render @llm I get the api_key in output which makes it very EASY to publish the key on github by mistake. Is there a way t mark an attribute as private/password and make api_key such? Or do I have to rewrite the inspect function?

palladius commented 2 months ago

trying again with 3.3.4:

irb(main):001> require 'langchain'
=> true
irb(main):002> RUBY_VERSION
=> "3.3.4"
irb(main):003> llm = Langchain::LLM::GoogleGemini.new(api_key: ENV["GEMINI_API_KEY"])
=> 
#<Langchain::LLM::GoogleGemini:0x00007fe718892b80
...
irb(main):004> llm.embed text: "Hello world"
/usr/local/google/home/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/langchainrb-0.15.4/lib/langchain/llm/google_gemini.rb:96:in `embed': undefined method `URI' for an instance of Langchain::LLM::GoogleGemini (NoMethodError)

      uri = URI("https://generativelanguage.googleapis.com/v1beta/models/#{model}:embedContent?key=#{api_key}")
            ^^^
        from (irb):4:in `<main>'
        from <internal:kernel>:187:in `loop'
        from /usr/local/google/home/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/irb-1.14.0/exe/irb:9:in `<top (required)>'
        from /usr/local/google/home/ricc/.rbenv/versions/3.3.4/bin/irb:25:in `load'
        from /usr/local/google/home/ricc/.rbenv/versions/3.3.4/bin/irb:25:in `<main>'

Same issue.

palladius commented 2 months ago

even cleaning up all my gems and bundler with latest ruby i get the same:

rm -rf /usr/local/google/home/ricc/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/
rm -rf /usr/local/google/home/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/
gem install bundler
gem install irb
bundle install
irb

irb(main):001> require 'langchain'
=> true
irb(main):002> RUBY_VERSION
=> "3.3.4"
irb(main):003> llm = Langchain::LLM::GoogleGemini.new(api_key: ENV["GEMINI_API_KEY"])
=>
#<Langchain::LLM::GoogleGemini:0x00007fa5d7f97458
...
irb(main):004> llm.embed text: "Hello world"
/usr/local/google/home/ricc/.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/langchainrb-0.15.4/lib/langchain/llm/google_gemini.rb:96:in `embed': undefined method `URI' for an instance of Langchain::LLM::GoogleGemini (NoMethodError)

      uri = URI("https://generativelanguage.googleapis.com/v1beta/models/#{model}:embedContent?key=#{api_key}")
            ^^^
        from (irb):4:in `<main>'
        from <internal:kernel>:187:in `loop'
        from /usr/local/google/home/ricc/.local/share/gem/ruby/3.3.0/gems/irb-1.14.0/exe/irb:9:in `<top (required)>'
        from /usr/local/google/home/ricc/.rbenv/versions/3.3.4/bin/irb:25:in `load'
        from /usr/local/google/home/ricc/.rbenv/versions/3.3.4/bin/irb:25:in `<main>'

I'll now try in a different computer.

andreibondarev commented 1 month ago

This bug was fixed with https://github.com/patterns-ai-core/langchainrb/pull/754.