Skip to content

Commit

Permalink
genai: chore for changing mypy settings
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu007 committed Sep 1, 2024
1 parent 18b866e commit 4f0087c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 28 deletions.
10 changes: 6 additions & 4 deletions libs/genai/langchain_google_genai/_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

import google.ai.generativelanguage as glm
import google.ai.generativelanguage_v1beta.types as gapic
import proto # type: ignore[import]
from google.generativeai.types.content_types import ToolDict # type: ignore[import]
import proto # type: ignore[import-untyped]
from google.generativeai.types.content_types import ( # type: ignore[import-untyped]
ToolDict,
)
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.tools import BaseTool
from langchain_core.tools import tool as callable_as_lc_tool
Expand Down Expand Up @@ -147,7 +149,7 @@ def convert_to_genai_function_declarations(
tools: Sequence[_ToolsType],
) -> gapic.Tool:
if not isinstance(tools, collections.abc.Sequence):
logger.warning(
logger.warning( # type: ignore[unreachable]
"convert_to_genai_function_declarations expects a Sequence "
"and not a single tool."
)
Expand Down Expand Up @@ -205,7 +207,7 @@ def _format_to_gapic_function_declaration(
function = cast(dict, tool)
function["parameters"] = {}
else:
if "parameters" in tool and tool["parameters"].get("properties"):
if "parameters" in tool and tool["parameters"].get("properties"): # type: ignore[index]
function = convert_to_openai_tool(cast(dict, tool))["function"]
else:
function = cast(dict, tool)
Expand Down
2 changes: 1 addition & 1 deletion libs/genai/langchain_google_genai/_genai_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from google.api_core import client_options as client_options_lib
from google.api_core import exceptions as gapi_exception
from google.api_core import gapic_v1
from google.auth import credentials, exceptions # type: ignore
from google.auth import credentials, exceptions
from google.protobuf import timestamp_pb2

_logger = logging.getLogger(__name__)
Expand Down
16 changes: 7 additions & 9 deletions libs/genai/langchain_google_genai/chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import google.api_core

# TODO: remove ignore once the google package is published with types
import proto # type: ignore[import]
import proto # type: ignore[import-untyped]
import requests
from google.ai.generativelanguage_v1beta.types import (
Blob,
Expand All @@ -46,8 +46,8 @@
ToolConfig,
VideoMetadata,
)
from google.generativeai.types import Tool as GoogleTool # type: ignore[import]
from google.generativeai.types.content_types import ( # type: ignore[import]
from google.generativeai.types import Tool as GoogleTool # type: ignore[import-untyped]
from google.generativeai.types.content_types import ( # type: ignore[import-untyped]
FunctionDeclarationType,
ToolDict,
)
Expand Down Expand Up @@ -214,7 +214,7 @@ async def _achat_with_retry(generation_method: Callable, **kwargs: Any) -> Any:
Any: The result from the chat generation method.
"""
retry_decorator = _create_retry_decorator()
from google.api_core.exceptions import InvalidArgument # type: ignore
from google.api_core.exceptions import InvalidArgument

@retry_decorator
async def _achat_with_retry(**kwargs: Any) -> Any:
Expand Down Expand Up @@ -283,7 +283,7 @@ def _url_to_pil(image_source: str) -> Image:
)
try:
if isinstance(image_source, IMAGE_TYPES):
return image_source # type: ignore[return-value]
return image_source
elif _is_url(image_source):
if image_source.startswith("gs://"):
return _load_image_from_gcs(image_source)
Expand Down Expand Up @@ -1232,9 +1232,7 @@ def bind_tools(
genai_tools = [tool_to_dict(convert_to_genai_function_declarations(tools))]
if tool_choice:
all_names = [
f["name"] # type: ignore[index]
for t in genai_tools
for f in t["function_declarations"]
f["name"] for t in genai_tools for f in t["function_declarations"]
]
tool_config = _tool_choice_to_tool_config(tool_choice, all_names)
return self.bind(tools=genai_tools, tool_config=tool_config, **kwargs)
Expand All @@ -1248,4 +1246,4 @@ def _get_tool_name(
tool: Union[ToolDict, GoogleTool],
) -> str:
genai_tool = tool_to_dict(convert_to_genai_function_declarations([tool]))
return [f["name"] for f in genai_tool["function_declarations"]][0] # type: ignore[index]
return [f["name"] for f in genai_tool["function_declarations"]][0]
2 changes: 1 addition & 1 deletion libs/genai/langchain_google_genai/google_vector_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ def as_aqa(self, **kwargs: Any) -> Runnable[str, AqaOutput]:
return (
RunnablePassthrough[str]()
| {
"prompt": RunnablePassthrough(),
"prompt": RunnablePassthrough(), # type: ignore
"passages": self.as_retriever(),
}
| RunnableLambda(_toAqaInput)
Expand Down
13 changes: 5 additions & 8 deletions libs/genai/langchain_google_genai/llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import Any, Callable, Dict, Iterator, List, Optional, Union

import google.api_core
import google.generativeai as genai # type: ignore[import]
import google.generativeai as genai # type: ignore[import-untyped]
from langchain_core.callbacks import (
AsyncCallbackManagerForLLMRun,
CallbackManagerForLLMRun,
Expand All @@ -15,10 +15,7 @@
from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr, root_validator
from langchain_core.utils import get_from_dict_or_env

from langchain_google_genai._enums import (
HarmBlockThreshold,
HarmCategory,
)
from langchain_google_genai._enums import HarmBlockThreshold, HarmCategory


class GoogleModelFamily(str, Enum):
Expand Down Expand Up @@ -167,9 +164,9 @@ class _BaseGoogleGenerativeAI(BaseModel):
)

safety_settings: Optional[Dict[HarmCategory, HarmBlockThreshold]] = None
"""The default safety settings to use for all generations.
For example:
"""The default safety settings to use for all generations.
For example:
from google.generativeai.types.safety_types import HarmBlockThreshold, HarmCategory
Expand Down
6 changes: 4 additions & 2 deletions libs/genai/tests/integration_tests/test_chat_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Generator, List, Optional, Type

import pytest
from google.generativeai.types import ( # type: ignore[import]
from google.generativeai.types import ( # type: ignore[import-untyped]
HarmBlockThreshold,
HarmCategory,
)
Expand All @@ -21,7 +21,9 @@
)
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.tools import tool
from langchain_standard_tests.integration_tests import ChatModelIntegrationTests
from langchain_standard_tests.integration_tests import (
ChatModelIntegrationTests,
)

from langchain_google_genai import ChatGoogleGenerativeAI
from langchain_google_genai.chat_models import ChatGoogleGenerativeAIError
Expand Down
2 changes: 1 addition & 1 deletion libs/genai/tests/integration_tests/test_llms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Generator

import pytest
from google.generativeai.types import ( # type: ignore[import]
from google.generativeai.types import ( # type: ignore[import-untyped]
HarmBlockThreshold,
HarmCategory,
)
Expand Down
4 changes: 2 additions & 2 deletions libs/genai/tests/unit_tests/test_function_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def sum_two_numbers(a: float, b: float) -> str:
"""
return str(a + b)

schema = convert_to_genai_function_declarations([sum_two_numbers]) # type: ignore
schema = convert_to_genai_function_declarations([sum_two_numbers])
function_declaration = schema.function_declarations[0]
assert function_declaration.name == "sum_two_numbers"
assert function_declaration.parameters
Expand All @@ -48,7 +48,7 @@ def do_something_optional(a: float, b: float = 0) -> str:
"""Some description"""
return str(a + b)

schema = convert_to_genai_function_declarations([do_something_optional]) # type: ignore
schema = convert_to_genai_function_declarations([do_something_optional])
function_declaration = schema.function_declarations[0]
assert function_declaration.name == "do_something_optional"
assert function_declaration.parameters
Expand Down

0 comments on commit 4f0087c

Please sign in to comment.