langchain-ai / langchain-google

MIT License
117 stars 147 forks source link

[vertexai]: Use one gcs client in Imageutils #609

Open david-gang opened 1 day ago

david-gang commented 1 day ago

Fixes #594

Till now very time the load_bytes was called a new gcs client was created. Now just one client is created. First i wanted to create the client in the constructor but I saw in the code that not always a client is created and in some environments it could have failed. So i want to be on the safe side and don't change the current behavior.

I don't know which tests i can add as i don't change any visible behavior. So tests should pass without problems.

david-gang commented 1 day ago

when i run the test locally it succeeds. No idea what to do now.

make integration_test                                                      
poetry run pytest --release tests/integration_tests/
==================================================================================== test session starts =====================================================================================
platform darwin -- Python 3.12.4, pytest-7.4.4, pluggy-1.5.0
configfile: pyproject.toml
plugins: syrupy-4.7.2, asyncio-0.21.2, mock-3.14.0, anyio-4.6.2.post1
asyncio: mode=Mode.AUTO
collected 207 items                                                                                                                                                                          

tests/integration_tests/test_callbacks.py ....x                                                                                                                                        [  2%]
tests/integration_tests/test_chains.py ...                                                                                                                                             [  3%]
tests/integration_tests/test_chat_models.py ..x...........s.Xs....s.......sssss...x.....                                                                                               [ 25%]
tests/integration_tests/test_compile.py .                                                                                                                                              [ 25%]
tests/integration_tests/test_embeddings.py .............                                                                                                                               [ 31%]
tests/integration_tests/test_evaluation.py ssss                                                                                                                                        [ 33%]
tests/integration_tests/test_gemma.py ssss                                                                                                                                             [ 35%]
tests/integration_tests/test_image_utils.py s                                                                                                                                          [ 36%]
tests/integration_tests/test_llms.py ...xx.....                                                                                                                                        [ 41%]
tests/integration_tests/test_llms_safety.py ss                                                                                                                                         [ 42%]
tests/integration_tests/test_maas.py ssssssssssssssssssssssssssss                                                                                                                      [ 55%]
tests/integration_tests/test_medlm.py ssssss                                                                                                                                           [ 58%]
tests/integration_tests/test_model_garden.py sssssssssss                                                                                                                               [ 63%]
tests/integration_tests/test_standard.py ..........................x...........................                                                                                        [ 89%]
tests/integration_tests/test_vectorstores.py sssssssssssssssss                                                                                                                         [ 98%]
tests/integration_tests/test_vision_models.py ....                                                                                                                                     [100%]

====================================================================================== warnings summary ======================================================================================
tests/integration_tests/test_chat_models.py:46
  open-source/langchain-google/libs/vertexai/tests/integration_tests/test_chat_models.py:46: LangChainBetaWarning: Introduced in 0.2.24. API subject to change.
    rate_limiter = InMemoryRateLimiter(requests_per_second=1.0)

tests/integration_tests/test_callbacks.py: 5 warnings
tests/integration_tests/test_llms.py: 18 warnings
 Library/Caches/pypoetry/virtualenvs/langchain-google-vertexai-xL9sAccH-py3.12/lib/python3.12/site-packages/proto/message.py:389: DeprecationWarning: The argument `including_default_value_fields` has been removed from
                  Protobuf 5.x. Please use `always_print_fields_with_no_presence` instead.

    warnings.warn(

tests/integration_tests/test_callbacks.py::test_chat_call[gemini-1.0-pro-001]
  open-source/langchain-google/libs/vertexai/tests/integration_tests/test_callbacks.py:39: LangChainDeprecationWarning: The method `BaseChatModel.__call__` was deprecated in langchain-core 0.1.7 and will be removed in 1.0. Use :meth:`~invoke` instead.
    _ = llm([message])

tests/integration_tests/test_chat_models.py::test_chat_vertexai_gemini_function_calling_with_multiple_parts
  open-source/langchain-google/libs/vertexai/tests/integration_tests/test_chat_models.py:737: LangChainDeprecationWarning: The method `BaseTool.__call__` was deprecated in langchain-core 0.1.47 and will be removed in 1.0. Use :meth:`~invoke` instead.
    tool_response = search("sparrow")

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
==================================================================================== slowest 5 durations =====================================================================================
76.10s call     tests/integration_tests/test_chat_models.py::test_multimodal_media_inline_base64_template
41.19s call     tests/integration_tests/test_vision_models.py::test_vertex_ai_image_generation_and_edition
35.74s call     tests/integration_tests/test_standard.py::TestGeminiAIStandard::test_structured_few_shot_examples
34.98s call     tests/integration_tests/test_chat_models.py::test_candidates
34.53s call     tests/integration_tests/test_llms.py::test_vertex_generate_multiple_candidates
======================================================= 119 passed, 81 skipped, 6 xfailed, 1 xpassed, 26 warnings in 694.70s (0:11:34) =======================================================
david-gang commented 1 day ago

Just for info. The test fails on lint in python 3.9 due to other issues:

Run make lint_package
./scripts/lint_imports.sh
poetry run ruff check .
poetry run ruff format langchain_google_vertexai --diff
30 files already formatted
poetry run ruff check --select I langchain_google_vertexai
mkdir .mypy_cache; poetry run mypy langchain_google_vertexai --cache-dir .mypy_cache
mkdir: cannot create directory ‘.mypy_cache’: File exists
langchain_google_vertexai/_anthropic_utils.py:34: error: Unused "type: ignore"
comment  [unused-ignore]
        from anthropic.types import RawMessageStreamEvent  # type: ignore
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
langchain_google_vertexai/model_garden.py:151: error: Unused "type: ignore"
comment  [unused-ignore]
            from anthropic import (  # type: ignore
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [Makefile:43: lint_package] Error 1

This needs to be fixed in the main branch and is not connected to my code.

lkuligin commented 20 hours ago

@david-gang thanks a lot for your contribtion! please, feel free to ignore the failing integrations tests (it's a known issue, I need to fix it), but I'd kindly ask you to fix the linter (even if it's not directly related to your changes). We can't merge with linter failing.

david-gang commented 19 hours ago

@lkuligin fixed the mypy errors

Before the latest commit

make lint
./scripts/lint_imports.sh
poetry run ruff check .
poetry run ruff format . --diff
65 files already formatted
poetry run ruff check --select I .
mkdir .mypy_cache; poetry run mypy . --cache-dir .mypy_cache
mkdir: .mypy_cache: File exists
langchain_google_vertexai/_anthropic_utils.py:34: error: Unused "type: ignore" comment  [unused-ignore]
        from anthropic.types import RawMessageStreamEvent  # type: ignore
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
langchain_google_vertexai/model_garden.py:151: error: Unused "type: ignore" comment  [unused-ignore]
            from anthropic import (  # type: ignore
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
make: *** [lint] Error 1

Now

vertexai % make lint
./scripts/lint_imports.sh
poetry run ruff check .
poetry run ruff format . --diff
65 files already formatted
poetry run ruff check --select I .
mkdir .mypy_cache; poetry run mypy . --cache-dir .mypy_cache
mkdir: .mypy_cache: File exists