diff --git a/python/langsmith/__init__.py b/python/langsmith/__init__.py index 59a887444..c8becf5b7 100644 --- a/python/langsmith/__init__.py +++ b/python/langsmith/__init__.py @@ -1,17 +1,47 @@ """LangSmith Client.""" -from importlib import metadata +from typing import TYPE_CHECKING, Any, List -try: - __version__ = metadata.version(__package__) -except metadata.PackageNotFoundError: - # Case where package metadata is not available. - __version__ = "" +if TYPE_CHECKING: + from langsmith.client import Client + from langsmith.run_helpers import trace, traceable + + +def __getattr__(name: str) -> Any: + if name == "__version__": + try: + from importlib import metadata + + return metadata.version(__package__) + except metadata.PackageNotFoundError: + return "" + elif name == "Client": + from langsmith.client import Client + + return Client + elif name == "RunTree": + from langsmith.run_trees import RunTree + + return RunTree + elif name == "EvaluationResult": + from langsmith.evaluation.evaluator import EvaluationResult + + return EvaluationResult + elif name == "RunEvaluator": + from langsmith.evaluation.evaluator import RunEvaluator + + return RunEvaluator + elif name == "trace": + from langsmith.run_helpers import trace + + return trace + elif name == "traceable": + from langsmith.run_helpers import traceable + + return traceable + + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") -from langsmith.client import Client -from langsmith.evaluation.evaluator import EvaluationResult, RunEvaluator -from langsmith.run_helpers import trace, traceable -from langsmith.run_trees import RunTree __all__ = [ "Client", @@ -22,3 +52,7 @@ "traceable", "trace", ] + + +def __dir__() -> List[str]: + return __all__ diff --git a/python/langsmith/evaluation/__init__.py b/python/langsmith/evaluation/__init__.py index 1dd2ecbed..18d0ccbed 100644 --- a/python/langsmith/evaluation/__init__.py +++ b/python/langsmith/evaluation/__init__.py @@ -1,14 +1,55 @@ """Evaluation Helpers.""" -from langsmith.evaluation._runner import evaluate, evaluate_existing -from langsmith.evaluation.evaluator import ( - EvaluationResult, - EvaluationResults, - RunEvaluator, - run_evaluator, -) -from langsmith.evaluation.integrations._langchain import LangChainStringEvaluator -from langsmith.evaluation.string_evaluator import StringEvaluator +from typing import TYPE_CHECKING, Any, List + +if TYPE_CHECKING: + from typing import List + + from langsmith.evaluation.evaluator import ( + EvaluationResult, + EvaluationResults, + RunEvaluator, + ) + from langsmith.evaluation.integrations._langchain import LangChainStringEvaluator + + +def __getattr__(name: str) -> Any: + if name == "evaluate": + from langsmith.evaluation._runner import evaluate + + return evaluate + elif name == "evaluate_existing": + from langsmith.evaluation._runner import evaluate_existing + + return evaluate_existing + elif name == "EvaluationResult": + from langsmith.evaluation.evaluator import EvaluationResult + + return EvaluationResult + elif name == "EvaluationResults": + from langsmith.evaluation.evaluator import EvaluationResults + + return EvaluationResults + elif name == "RunEvaluator": + from langsmith.evaluation.evaluator import RunEvaluator + + return RunEvaluator + elif name == "run_evaluator": + from langsmith.evaluation.evaluator import run_evaluator + + return run_evaluator + elif name == "StringEvaluator": + from langsmith.evaluation.string_evaluator import StringEvaluator + + return StringEvaluator + elif name == "LangChainStringEvaluator": + from langsmith.evaluation.integrations._langchain import ( + LangChainStringEvaluator, + ) + + return LangChainStringEvaluator + raise AttributeError(f"module {__name__} has no attribute {name}") + __all__ = [ "run_evaluator", @@ -20,3 +61,7 @@ "evaluate_existing", "LangChainStringEvaluator", ] + + +def __dir__() -> List[str]: + return __all__