Skip to content

Commit

Permalink
getattr for reduced cold start times (#570)
Browse files Browse the repository at this point in the history
Co-authored-by: William Fu-Hinthorn <[email protected]>
  • Loading branch information
efriis and hinthornw authored Apr 3, 2024
1 parent 847eb45 commit cf63abd
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 19 deletions.
54 changes: 44 additions & 10 deletions python/langsmith/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -22,3 +52,7 @@
"traceable",
"trace",
]


def __dir__() -> List[str]:
return __all__
63 changes: 54 additions & 9 deletions python/langsmith/evaluation/__init__.py
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -20,3 +61,7 @@
"evaluate_existing",
"LangChainStringEvaluator",
]


def __dir__() -> List[str]:
return __all__

0 comments on commit cf63abd

Please sign in to comment.