jupyterlab / jupyter-ai

A generative AI extension for JupyterLab
https://jupyter-ai.readthedocs.io/
BSD 3-Clause "New" or "Revised" License
3.1k stars 306 forks source link

llm_chain.astream(..) fails with error in langchain_google_genai #914

Closed jhgoebbert closed 1 month ago

jhgoebbert commented 1 month ago

Description

When trying to use the Gemini model in Jupyter-AI any chat-message leads to the following error.

  File "/lib/python3.11/site-packages/jupyter_ai/chat_handlers/default.py", line 104, in process_message
    async for chunk in self.llm_chain.astream(

fails with error in

  File "/lib/python3.11/site-packages/langchain_core/runnables/config.py", line 566, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain_google_genai/llms.py", line 338, in _stream
    for stream_resp in _completion_with_retry(
TypeError: 'NoneType' object is not iterable
Full backtrace
```
Traceback (most recent call last):
  File "/lib/python3.11/site-packages/jupyter_ai/chat_handlers/base.py", line 181, in on_message
    await self.process_message(message)
  File "/lib/python3.11/site-packages/jupyter_ai/chat_handlers/default.py", line 104, in process_message
    async for chunk in self.llm_chain.astream(
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 5238, in astream
    async for item in self.bound.astream(
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 5238, in astream
    async for item in self.bound.astream(
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3276, in astream
    async for chunk in self.atransform(input_aiter(), config, **kwargs):
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3259, in atransform
    async for chunk in self._atransform_stream_with_config(
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2158, in _atransform_stream_with_config
    chunk: Output = await asyncio.create_task(  # type: ignore[call-arg]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3229, in _atransform
    async for output in final_pipeline:
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1330, in atransform
    async for output in self.astream(final, config, **kwargs):
  File "/lib/python3.11/site-packages/langchain_core/runnables/branch.py", line 431, in astream
    async for chunk in runnable.astream(
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 5238, in astream
    async for item in self.bound.astream(
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3276, in astream
    async for chunk in self.atransform(input_aiter(), config, **kwargs):
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3259, in atransform
    async for chunk in self._atransform_stream_with_config(
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 2158, in _atransform_stream_with_config
    chunk: Output = await asyncio.create_task(  # type: ignore[call-arg]
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 3229, in _atransform
    async for output in final_pipeline:
  File "/lib/python3.11/site-packages/langchain_core/runnables/base.py", line 1330, in atransform
    async for output in self.astream(final, config, **kwargs):
  File "/lib/python3.11/site-packages/langchain_core/language_models/llms.py", line 590, in astream
    raise e
  File "/lib/python3.11/site-packages/langchain_core/language_models/llms.py", line 573, in astream
    async for chunk in self._astream(
  File "/lib/python3.11/site-packages/langchain_core/language_models/llms.py", line 685, in _astream
    item = await run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain_core/runnables/config.py", line 575, in run_in_executor
    return await asyncio.get_running_loop().run_in_executor(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/p/software/juwels/stages/2024/software/Python/3.11.3-GCCcore-12.3.0/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/lib/python3.11/site-packages/langchain_core/runnables/config.py", line 566, in wrapper
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/p/usersoftware/swmanage/goebbert1/stage2024/jupyterlab-test/easybuild/juwels/software/jupyter-ai-gemini/2.19.0-GCCcore-12.3.0/lib/python3.11/site-packages/langchain_google_genai/llms.py", line 338, in _stream
    for stream_resp in _completion_with_retry(
TypeError: 'NoneType' object is not iterable
```

I am using

dlqqq commented 1 month ago

Thank you for reporting this issue on behalf of our users! The error indicates that the langchain_google_genai package is not able to connect to the API at all. Can you try this again after verifying your API credentials are still valid, and that Google's Gemini service is online? You can do both by trying to call the Gemini models through langchain_google_genai directly right before starting Jupyter AI.

jhgoebbert commented 1 month ago

Oh, I am sorry. It just means that I have no permissions to connect/use the model (for whatever reason)?

Is there any chance that Jupyter-AI can check this in future to avoid that users get confused by backtraces because of a wrong key / missing permissions?

jhgoebbert commented 1 month ago
curl \
>   -H 'Content-Type: application/json' \
>   -d '{"contents":[{"parts":[{"text":"Explain how AI works"}]}]}' \
>   -X POST 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-latest:generateContent?key=GOOGLE_API_KEY'
{
  "error": {
    "code": 400,
    "message": "Gemini API free tier is not available in your country. Please enable billing on your project in Google AI Studio.",
    "status": "FAILED_PRECONDITION"
  }
}

The reason was error code 400 == The Google API is not available in Germany.