From 1a14c394c16264ce4658c89686293075822975e9 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Mon, 6 Jan 2025 12:29:37 -0800 Subject: [PATCH] x --- libs/core/Makefile | 26 ++ libs/core/it.py | 1 + .../core/langchain_core/callbacks/__init__.py | 209 +++++++++-- libs/core/langchain_core/tools/__init__.py | 181 +++++++--- libs/core/langchain_core/utils/utils.py | 9 +- libs/core/memray-flamegraph-memory.html | 339 ++++++++++++++++++ 6 files changed, 686 insertions(+), 79 deletions(-) create mode 100644 libs/core/it.py create mode 100644 libs/core/memray-flamegraph-memory.html diff --git a/libs/core/Makefile b/libs/core/Makefile index 169e49a4979..8a991e0a3ac 100644 --- a/libs/core/Makefile +++ b/libs/core/Makefile @@ -73,3 +73,29 @@ help: @echo 'tests - run unit tests' @echo 'test TEST_FILE= - run all tests in file' @echo 'test_watch - run unit tests in watch mode' + +########## +# CUSTOM # +########## + +bench-imports: + time poetry run python -c "from typing import Any" + time poetry run python -c "from typing import Any" + time poetry run python -c "from langchain_core import __version__" + time poetry run python -c "from langchain_core.tools import tool" + time poetry run python -c "from langchain_core.messages import AIMessage" + time poetry run python -c "from langchain_core.documents import Document" + time poetry run python -c "from langchain_core._api import deprecated" + time poetry run python -c "from langchain_core._api import beta" + time poetry run python -c "from langchain_core.callbacks import BaseCallbackHandler" + time poetry run python -c "from langchain_core.document_loaders import BaseLoader" + time poetry run python -c "from langchain_core.embeddings import Embeddings" + time poetry run python -c "from langchain_core.language_models import BaseChatModel" + time poetry run python -c "from langchain_core.language_models import BaseLLM" + time poetry run python -c "from langchain_core.load import loads" + time poetry run python -c "from langchain_core.output_parsers import BaseOutputParser" + time poetry run python -c "from langchain_core.prompts import ChatPromptTemplate" + time poetry run python -c "from langchain_core.runnables import RunnableSerializable" + time poetry run python -c "from langchain_core.tools import BaseTool" + time poetry run python -c "from langchain_core.tracers import BaseTracer" + time poetry run python -c "from langchain_core.vectorstores import VectorStore" diff --git a/libs/core/it.py b/libs/core/it.py new file mode 100644 index 00000000000..6fe22aa7707 --- /dev/null +++ b/libs/core/it.py @@ -0,0 +1 @@ +from langchain_core.tools import tool diff --git a/libs/core/langchain_core/callbacks/__init__.py b/libs/core/langchain_core/callbacks/__init__.py index a55f41ce344..0a35c1fcdf0 100644 --- a/libs/core/langchain_core/callbacks/__init__.py +++ b/libs/core/langchain_core/callbacks/__init__.py @@ -6,43 +6,180 @@ BaseCallbackHandler --> CallbackHandler # Example: AimCallbackHandler """ +from typing import TYPE_CHECKING, Any -from langchain_core.callbacks.base import ( - AsyncCallbackHandler, - BaseCallbackHandler, - BaseCallbackManager, - CallbackManagerMixin, - Callbacks, - ChainManagerMixin, - LLMManagerMixin, - RetrieverManagerMixin, - RunManagerMixin, - ToolManagerMixin, -) -from langchain_core.callbacks.file import FileCallbackHandler -from langchain_core.callbacks.manager import ( - AsyncCallbackManager, - AsyncCallbackManagerForChainGroup, - AsyncCallbackManagerForChainRun, - AsyncCallbackManagerForLLMRun, - AsyncCallbackManagerForRetrieverRun, - AsyncCallbackManagerForToolRun, - AsyncParentRunManager, - AsyncRunManager, - BaseRunManager, - CallbackManager, - CallbackManagerForChainGroup, - CallbackManagerForChainRun, - CallbackManagerForLLMRun, - CallbackManagerForRetrieverRun, - CallbackManagerForToolRun, - ParentRunManager, - RunManager, - adispatch_custom_event, - dispatch_custom_event, -) -from langchain_core.callbacks.stdout import StdOutCallbackHandler -from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler +if TYPE_CHECKING: + + from langchain_core.callbacks.base import ( + AsyncCallbackHandler, + BaseCallbackHandler, + BaseCallbackManager, + CallbackManagerMixin, + Callbacks, + ChainManagerMixin, + LLMManagerMixin, + RetrieverManagerMixin, + RunManagerMixin, + ToolManagerMixin, + ) + from langchain_core.callbacks.file import FileCallbackHandler + from langchain_core.callbacks.manager import ( + AsyncCallbackManager, + AsyncCallbackManagerForChainGroup, + AsyncCallbackManagerForChainRun, + AsyncCallbackManagerForLLMRun, + AsyncCallbackManagerForRetrieverRun, + AsyncCallbackManagerForToolRun, + AsyncParentRunManager, + AsyncRunManager, + BaseRunManager, + CallbackManager, + CallbackManagerForChainGroup, + CallbackManagerForChainRun, + CallbackManagerForLLMRun, + CallbackManagerForRetrieverRun, + CallbackManagerForToolRun, + ParentRunManager, + RunManager, + adispatch_custom_event, + dispatch_custom_event, + ) + from langchain_core.callbacks.stdout import StdOutCallbackHandler + from langchain_core.callbacks.streaming_stdout import StreamingStdOutCallbackHandler + +def __getattr__(name: str) -> Any: + if name == "dispatch_custom_event": + from langchain_core.callbacks.manager import dispatch_custom_event + + return dispatch_custom_event + if name == "adispatch_custom_event": + from langchain_core.callbacks.manager import adispatch_custom_event + + return adispatch_custom_event + if name == "RetrieverManagerMixin": + from langchain_core.callbacks.base import RetrieverManagerMixin + + return RetrieverManagerMixin + if name == "LLMManagerMixin": + from langchain_core.callbacks.base import LLMManagerMixin + + return LLMManagerMixin + if name == "ChainManagerMixin": + from langchain_core.callbacks.base import ChainManagerMixin + + return ChainManagerMixin + if name == "ToolManagerMixin": + from langchain_core.callbacks.base import ToolManagerMixin + + return ToolManagerMixin + if name == "Callbacks": + from langchain_core.callbacks.base import Callbacks + + return Callbacks + if name == "CallbackManagerMixin": + from langchain_core.callbacks.base import CallbackManagerMixin + + return CallbackManagerMixin + if name == "RunManagerMixin": + from langchain_core.callbacks.base import RunManagerMixin + + return RunManagerMixin + if name == "BaseCallbackHandler": + from langchain_core.callbacks.base import BaseCallbackHandler + + return BaseCallbackHandler + if name == "AsyncCallbackHandler": + from langchain_core.callbacks.base import AsyncCallbackHandler + + return AsyncCallbackHandler + if name == "BaseCallbackManager": + from langchain_core.callbacks.manager import BaseCallbackManager + + return BaseCallbackManager + if name == "BaseRunManager": + from langchain_core.callbacks.manager import BaseRunManager + + return BaseRunManager + if name == "RunManager": + from langchain_core.callbacks.manager import RunManager + + return RunManager + if name == "ParentRunManager": + from langchain_core.callbacks.manager import ParentRunManager + + return ParentRunManager + if name == "AsyncRunManager": + from langchain_core.callbacks.manager import AsyncRunManager + + return AsyncRunManager + if name == "AsyncParentRunManager": + from langchain_core.callbacks.manager import AsyncParentRunManager + + return AsyncParentRunManager + if name == "CallbackManagerForLLMRun": + from langchain_core.callbacks.manager import CallbackManagerForLLMRun + + return CallbackManagerForLLMRun + if name == "AsyncCallbackManagerForLLMRun": + from langchain_core.callbacks.manager import AsyncCallbackManagerForLLMRun + + return AsyncCallbackManagerForLLMRun + if name == "CallbackManagerForChainRun": + from langchain_core.callbacks.manager import CallbackManagerForChainRun + + return CallbackManagerForChainRun + if name == "AsyncCallbackManagerForChainRun": + from langchain_core.callbacks.manager import AsyncCallbackManagerForChainRun + + return AsyncCallbackManagerForChainRun + if name == "CallbackManagerForToolRun": + from langchain_core.callbacks.manager import CallbackManagerForToolRun + + return CallbackManagerForToolRun + if name == "AsyncCallbackManagerForToolRun": + from langchain_core.callbacks.manager import AsyncCallbackManagerForToolRun + + return AsyncCallbackManagerForToolRun + if name == "CallbackManagerForRetrieverRun": + from langchain_core.callbacks.manager import CallbackManagerForRetrieverRun + + return CallbackManagerForRetrieverRun + if name == "AsyncCallbackManagerForRetrieverRun": + from langchain_core.callbacks.manager import AsyncCallbackManagerForRetrieverRun + + return AsyncCallbackManagerForRetrieverRun + if name == "CallbackManager": + from langchain_core.callbacks.manager import CallbackManager + + return CallbackManager + if name == "CallbackManagerForChainGroup": + from langchain_core.callbacks.manager import CallbackManagerForChainGroup + + return CallbackManagerForChainGroup + if name == "AsyncCallbackManager": + from langchain_core.callbacks.manager import AsyncCallbackManager + + return AsyncCallbackManager + if name == "AsyncCallbackManagerForChainGroup": + from langchain_core.callbacks.manager import AsyncCallbackManagerForChainGroup + + return AsyncCallbackManagerForChainGroup + if name == "StdOutCallbackHandler": + from langchain_core.callbacks.stdout import StdOutCallbackHandler + + return StdOutCallbackHandler + if name == "StreamingStdOutCallbackHandler": + from langchain_core.callbacks.streaming_stdout import ( + StreamingStdOutCallbackHandler, + ) + + return StreamingStdOutCallbackHandler + if name == "FileCallbackHandler": + from langchain_core.callbacks.file import FileCallbackHandler + + return FileCallbackHandler + msg = f"module {__name__} has no attribute {name}" + raise AttributeError(msg) __all__ = [ "dispatch_custom_event", diff --git a/libs/core/langchain_core/tools/__init__.py b/libs/core/langchain_core/tools/__init__.py index 969e62e828a..f3e644d603a 100644 --- a/libs/core/langchain_core/tools/__init__.py +++ b/libs/core/langchain_core/tools/__init__.py @@ -19,43 +19,144 @@ tool for the job. from __future__ import annotations -from langchain_core.tools.base import ( - FILTERED_ARGS as FILTERED_ARGS, -) -from langchain_core.tools.base import ( - BaseTool as BaseTool, -) -from langchain_core.tools.base import ( - BaseToolkit as BaseToolkit, -) -from langchain_core.tools.base import ( - InjectedToolArg as InjectedToolArg, -) -from langchain_core.tools.base import InjectedToolCallId as InjectedToolCallId -from langchain_core.tools.base import SchemaAnnotationError as SchemaAnnotationError -from langchain_core.tools.base import ( - ToolException as ToolException, -) -from langchain_core.tools.base import ( - _get_runnable_config_param as _get_runnable_config_param, -) -from langchain_core.tools.base import ( - create_schema_from_function as create_schema_from_function, -) -from langchain_core.tools.convert import ( - convert_runnable_to_tool as convert_runnable_to_tool, -) -from langchain_core.tools.convert import tool as tool -from langchain_core.tools.render import ToolsRenderer as ToolsRenderer -from langchain_core.tools.render import ( - render_text_description as render_text_description, -) -from langchain_core.tools.render import ( - render_text_description_and_args as render_text_description_and_args, -) -from langchain_core.tools.retriever import RetrieverInput as RetrieverInput -from langchain_core.tools.retriever import ( - create_retriever_tool as create_retriever_tool, -) -from langchain_core.tools.simple import Tool as Tool -from langchain_core.tools.structured import StructuredTool as StructuredTool +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from typing import Any + + from langchain_core.tools.base import ( + FILTERED_ARGS as FILTERED_ARGS, + ) + from langchain_core.tools.base import ( + BaseTool as BaseTool, + ) + from langchain_core.tools.base import ( + BaseToolkit as BaseToolkit, + ) + from langchain_core.tools.base import ( + InjectedToolArg as InjectedToolArg, + ) + from langchain_core.tools.base import InjectedToolCallId as InjectedToolCallId + from langchain_core.tools.base import SchemaAnnotationError as SchemaAnnotationError + from langchain_core.tools.base import ( + ToolException as ToolException, + ) + from langchain_core.tools.base import ( + _get_runnable_config_param as _get_runnable_config_param, + ) + from langchain_core.tools.base import ( + create_schema_from_function as create_schema_from_function, + ) + from langchain_core.tools.convert import ( + convert_runnable_to_tool as convert_runnable_to_tool, + ) + from langchain_core.tools.convert import tool as tool + from langchain_core.tools.render import ToolsRenderer as ToolsRenderer + from langchain_core.tools.render import ( + render_text_description as render_text_description, + ) + from langchain_core.tools.render import ( + render_text_description_and_args as render_text_description_and_args, + ) + from langchain_core.tools.retriever import RetrieverInput as RetrieverInput + from langchain_core.tools.retriever import ( + create_retriever_tool as create_retriever_tool, + ) + from langchain_core.tools.simple import Tool as Tool + from langchain_core.tools.structured import StructuredTool as StructuredTool + +def __getattr__(name: str) -> Any: + if name == "FILTERED_ARGS": + from langchain_core.tools.base import FILTERED_ARGS + + return FILTERED_ARGS + if name == "BaseTool": + from langchain_core.tools.base import BaseTool + + return BaseTool + if name == "BaseToolkit": + from langchain_core.tools.base import BaseToolkit + + return BaseToolkit + if name == "InjectedToolArg": + from langchain_core.tools.base import InjectedToolArg + + return InjectedToolArg + if name == "InjectedToolCallId": + from langchain_core.tools.base import InjectedToolCallId + + return InjectedToolCallId + if name == "SchemaAnnotationError": + from langchain_core.tools.base import SchemaAnnotationError + + return SchemaAnnotationError + if name == "ToolException": + from langchain_core.tools.base import ToolException + + return ToolException + if name == "_get_runnable_config_param": + from langchain_core.tools.base import _get_runnable_config_param + + return _get_runnable_config_param + if name == "create_schema_from_function": + from langchain_core.tools.base import create_schema_from_function + + return create_schema_from_function + if name == "convert_runnable_to_tool": + from langchain_core.tools.convert import convert_runnable_to_tool + + return convert_runnable_to_tool + if name == "tool": + from langchain_core.tools.convert import tool + + return tool + if name == "ToolsRenderer": + from langchain_core.tools.render import ToolsRenderer + + return ToolsRenderer + if name == "render_text_description": + from langchain_core.tools.render import render_text_description + + return render_text_description + if name == "render_text_description_and_args": + from langchain_core.tools.render import render_text_description_and_args + + return render_text_description_and_args + if name == "RetrieverInput": + from langchain_core.tools.retriever import RetrieverInput + + return RetrieverInput + if name == "create_retriever_tool": + from langchain_core.tools.retriever import create_retriever_tool + + return create_retriever_tool + if name == "Tool": + from langchain_core.tools.simple import Tool + + return Tool + if name == "StructuredTool": + from langchain_core.tools.structured import StructuredTool + + return StructuredTool + raise AttributeError(f"module {__name__} has no attribute {name}") + +__all__ = [ + "FILTERED_ARGS", + "BaseTool", + "BaseToolkit", + "InjectedToolArg", + "InjectedToolCallId", + "SchemaAnnotationError", + "ToolException", + "_get_runnable_config_param", + "create_schema_from_function", + "convert_runnable_to_tool", + "tool", + "ToolsRenderer", + "render_text_description", + "render_text_description_and_args", + "RetrieverInput", + "create_retriever_tool", + "Tool", + "StructuredTool", +] diff --git a/libs/core/langchain_core/utils/utils.py b/libs/core/langchain_core/utils/utils.py index c66ed52228c..038e4051e1a 100644 --- a/libs/core/langchain_core/utils/utils.py +++ b/libs/core/langchain_core/utils/utils.py @@ -8,11 +8,13 @@ import os import warnings from collections.abc import Sequence from importlib.metadata import version -from typing import Any, Callable, Optional, Union, overload +from typing import TYPE_CHECKING, Any, Callable, Optional, Union, overload from packaging.version import parse from pydantic import SecretStr -from requests import HTTPError, Response + +if TYPE_CHECKING: + from requests import Response from langchain_core.utils.pydantic import ( is_pydantic_v1_subclass, @@ -57,7 +59,7 @@ def xor_args(*arg_groups: tuple[str, ...]) -> Callable: return decorator -def raise_for_status_with_text(response: Response) -> None: +def raise_for_status_with_text(response: "Response") -> None: """Raise an error with the response text. Args: @@ -66,6 +68,7 @@ def raise_for_status_with_text(response: Response) -> None: Raises: ValueError: If the response has an error status code. """ + from requests import HTTPError try: response.raise_for_status() except HTTPError as e: diff --git a/libs/core/memray-flamegraph-memory.html b/libs/core/memray-flamegraph-memory.html new file mode 100644 index 00000000000..c6f73b43f33 --- /dev/null +++ b/libs/core/memray-flamegraph-memory.html @@ -0,0 +1,339 @@ + + + + + + + + + memray - flamegraph report + + + + + + + + + + + + +
+
+
+ + + +
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +