initial commit
This commit is contained in:
30
venv/Lib/site-packages/langsmith/env/__init__.py
vendored
Normal file
30
venv/Lib/site-packages/langsmith/env/__init__.py
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
"""Utilities to get information about the runtime environment."""
|
||||
from langsmith.env._git import get_git_info
|
||||
from langsmith.env._runtime_env import (
|
||||
get_docker_compose_command,
|
||||
get_docker_compose_version,
|
||||
get_docker_environment,
|
||||
get_docker_version,
|
||||
get_langchain_env_var_metadata,
|
||||
get_langchain_env_vars,
|
||||
get_langchain_environment,
|
||||
get_release_shas,
|
||||
get_runtime_and_metrics,
|
||||
get_runtime_environment,
|
||||
get_system_metrics,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"get_docker_compose_command",
|
||||
"get_docker_compose_version",
|
||||
"get_docker_environment",
|
||||
"get_docker_version",
|
||||
"get_langchain_env_var_metadata",
|
||||
"get_langchain_env_vars",
|
||||
"get_langchain_environment",
|
||||
"get_release_shas",
|
||||
"get_runtime_and_metrics",
|
||||
"get_runtime_environment",
|
||||
"get_system_metrics",
|
||||
"get_git_info",
|
||||
]
|
||||
BIN
venv/Lib/site-packages/langsmith/env/__pycache__/__init__.cpython-311.pyc
vendored
Normal file
BIN
venv/Lib/site-packages/langsmith/env/__pycache__/__init__.cpython-311.pyc
vendored
Normal file
Binary file not shown.
BIN
venv/Lib/site-packages/langsmith/env/__pycache__/_git.cpython-311.pyc
vendored
Normal file
BIN
venv/Lib/site-packages/langsmith/env/__pycache__/_git.cpython-311.pyc
vendored
Normal file
Binary file not shown.
BIN
venv/Lib/site-packages/langsmith/env/__pycache__/_runtime_env.cpython-311.pyc
vendored
Normal file
BIN
venv/Lib/site-packages/langsmith/env/__pycache__/_runtime_env.cpython-311.pyc
vendored
Normal file
Binary file not shown.
64
venv/Lib/site-packages/langsmith/env/_git.py
vendored
Normal file
64
venv/Lib/site-packages/langsmith/env/_git.py
vendored
Normal file
@@ -0,0 +1,64 @@
|
||||
"""Fetch information about any current git repo."""
|
||||
|
||||
import functools
|
||||
import logging
|
||||
import subprocess
|
||||
from typing import List, Optional, TypeVar
|
||||
|
||||
from typing_extensions import TypedDict
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
def exec_git(command: List[str]) -> Optional[str]:
|
||||
try:
|
||||
return subprocess.check_output(
|
||||
["git"] + command, encoding="utf-8", stderr=subprocess.DEVNULL
|
||||
).strip()
|
||||
except BaseException:
|
||||
return None
|
||||
|
||||
|
||||
class GitInfo(TypedDict, total=False):
|
||||
repo_name: Optional[str]
|
||||
remote_url: Optional[str]
|
||||
commit: Optional[str]
|
||||
branch: Optional[str]
|
||||
author_name: Optional[str]
|
||||
author_email: Optional[str]
|
||||
commit_time: Optional[str]
|
||||
dirty: Optional[bool]
|
||||
tags: Optional[str]
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_git_info(remote: str = "origin") -> GitInfo:
|
||||
"""Get information about the git repository."""
|
||||
if not exec_git(["rev-parse", "--is-inside-work-tree"]):
|
||||
return GitInfo(
|
||||
remote_url=None,
|
||||
commit=None,
|
||||
branch=None,
|
||||
author_name=None,
|
||||
author_email=None,
|
||||
commit_time=None,
|
||||
dirty=None,
|
||||
tags=None,
|
||||
repo_name=None,
|
||||
)
|
||||
|
||||
return {
|
||||
"remote_url": exec_git(["remote", "get-url", remote]),
|
||||
"commit": exec_git(["rev-parse", "HEAD"]),
|
||||
"commit_time": exec_git(["log", "-1", "--format=%ct"]),
|
||||
"branch": exec_git(["rev-parse", "--abbrev-ref", "HEAD"]),
|
||||
"tags": exec_git(
|
||||
["describe", "--tags", "--exact-match", "--always", "--dirty"]
|
||||
),
|
||||
"dirty": exec_git(["status", "--porcelain"]) != "",
|
||||
"author_name": exec_git(["log", "-1", "--format=%an"]),
|
||||
"author_email": exec_git(["log", "-1", "--format=%ae"]),
|
||||
"repo_name": (exec_git(["rev-parse", "--show-toplevel"]) or "").split("/")[-1],
|
||||
}
|
||||
236
venv/Lib/site-packages/langsmith/env/_runtime_env.py
vendored
Normal file
236
venv/Lib/site-packages/langsmith/env/_runtime_env.py
vendored
Normal file
@@ -0,0 +1,236 @@
|
||||
"""Environment information."""
|
||||
|
||||
import functools
|
||||
import logging
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
from typing import Dict, List, Optional, Union
|
||||
|
||||
from langsmith.utils import get_docker_compose_command
|
||||
from langsmith.env._git import exec_git
|
||||
|
||||
try:
|
||||
# psutil is an optional dependency
|
||||
import psutil
|
||||
|
||||
_PSUTIL_AVAILABLE = True
|
||||
except ImportError:
|
||||
_PSUTIL_AVAILABLE = False
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_runtime_and_metrics() -> dict:
|
||||
"""Get the runtime information as well as metrics."""
|
||||
return {**get_runtime_environment(), **get_system_metrics()}
|
||||
|
||||
|
||||
def get_system_metrics() -> Dict[str, Union[float, dict]]:
|
||||
"""Get CPU and other performance metrics."""
|
||||
global _PSUTIL_AVAILABLE
|
||||
if not _PSUTIL_AVAILABLE:
|
||||
return {}
|
||||
try:
|
||||
process = psutil.Process(os.getpid())
|
||||
metrics: Dict[str, Union[float, dict]] = {}
|
||||
|
||||
with process.oneshot():
|
||||
mem_info = process.memory_info()
|
||||
metrics["thread_count"] = float(process.num_threads())
|
||||
metrics["mem"] = {
|
||||
"rss": float(mem_info.rss),
|
||||
}
|
||||
ctx_switches = process.num_ctx_switches()
|
||||
cpu_times = process.cpu_times()
|
||||
metrics["cpu"] = {
|
||||
"time": {
|
||||
"sys": cpu_times.system,
|
||||
"user": cpu_times.user,
|
||||
},
|
||||
"ctx_switches": {
|
||||
"voluntary": float(ctx_switches.voluntary),
|
||||
"involuntary": float(ctx_switches.involuntary),
|
||||
},
|
||||
"percent": process.cpu_percent(),
|
||||
}
|
||||
return metrics
|
||||
except Exception as e:
|
||||
# If psutil is installed but not compatible with the build,
|
||||
# we'll just cease further attempts to use it.
|
||||
_PSUTIL_AVAILABLE = False
|
||||
logger.debug("Failed to get system metrics: %s", e)
|
||||
return {}
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_runtime_environment() -> dict:
|
||||
"""Get information about the environment."""
|
||||
# Lazy import to avoid circular imports
|
||||
from langsmith import __version__
|
||||
|
||||
shas = get_release_shas()
|
||||
return {
|
||||
"sdk": "langsmith-py",
|
||||
"sdk_version": __version__,
|
||||
"library": "langsmith",
|
||||
"platform": platform.platform(),
|
||||
"runtime": "python",
|
||||
"py_implementation": platform.python_implementation(),
|
||||
"runtime_version": platform.python_version(),
|
||||
"langchain_version": get_langchain_environment(),
|
||||
"langchain_core_version": get_langchain_core_version(),
|
||||
**shas,
|
||||
}
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_langchain_environment() -> Optional[str]:
|
||||
try:
|
||||
import langchain # type: ignore
|
||||
|
||||
return langchain.__version__
|
||||
except: # noqa
|
||||
return None
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_langchain_core_version() -> Optional[str]:
|
||||
try:
|
||||
import langchain_core # type: ignore
|
||||
|
||||
return langchain_core.__version__
|
||||
except ImportError:
|
||||
return None
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_docker_version() -> Optional[str]:
|
||||
import subprocess
|
||||
|
||||
try:
|
||||
docker_version = (
|
||||
subprocess.check_output(["docker", "--version"]).decode("utf-8").strip()
|
||||
)
|
||||
except FileNotFoundError:
|
||||
docker_version = "unknown"
|
||||
except: # noqa
|
||||
return None
|
||||
return docker_version
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_docker_compose_version() -> Optional[str]:
|
||||
try:
|
||||
docker_compose_version = (
|
||||
subprocess.check_output(["docker-compose", "--version"])
|
||||
.decode("utf-8")
|
||||
.strip()
|
||||
)
|
||||
except FileNotFoundError:
|
||||
docker_compose_version = "unknown"
|
||||
except: # noqa
|
||||
return None
|
||||
return docker_compose_version
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def _get_compose_command() -> Optional[List[str]]:
|
||||
try:
|
||||
compose_command = get_docker_compose_command()
|
||||
except ValueError as e:
|
||||
compose_command = [f"NOT INSTALLED: {e}"]
|
||||
except: # noqa
|
||||
return None
|
||||
return compose_command
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_docker_environment() -> dict:
|
||||
"""Get information about the environment."""
|
||||
compose_command = _get_compose_command()
|
||||
return {
|
||||
"docker_version": get_docker_version(),
|
||||
"docker_compose_command": (
|
||||
" ".join(compose_command) if compose_command is not None else None
|
||||
),
|
||||
"docker_compose_version": get_docker_compose_version(),
|
||||
}
|
||||
|
||||
|
||||
def get_langchain_env_vars() -> dict:
|
||||
"""Retrieve the langchain environment variables."""
|
||||
env_vars = {k: v for k, v in os.environ.items() if k.startswith("LANGCHAIN_")}
|
||||
for key in list(env_vars):
|
||||
if "key" in key.lower():
|
||||
v = env_vars[key]
|
||||
env_vars[key] = v[:2] + "*" * (len(v) - 4) + v[-2:]
|
||||
return env_vars
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_langchain_env_var_metadata() -> dict:
|
||||
"""Retrieve the langchain environment variables."""
|
||||
excluded = {
|
||||
"LANGCHAIN_API_KEY",
|
||||
"LANGCHAIN_ENDPOINT",
|
||||
"LANGCHAIN_TRACING_V2",
|
||||
"LANGCHAIN_PROJECT",
|
||||
"LANGCHAIN_SESSION",
|
||||
"LANGSMITH_RUNS_ENDPOINTS",
|
||||
}
|
||||
langchain_metadata = {
|
||||
k: v
|
||||
for k, v in os.environ.items()
|
||||
if (k.startswith("LANGCHAIN_") or k.startswith("LANGSMITH_"))
|
||||
and k not in excluded
|
||||
and "key" not in k.lower()
|
||||
and "secret" not in k.lower()
|
||||
and "token" not in k.lower()
|
||||
}
|
||||
env_revision_id = langchain_metadata.pop("LANGCHAIN_REVISION_ID", None)
|
||||
if env_revision_id:
|
||||
langchain_metadata["revision_id"] = env_revision_id
|
||||
elif default_revision_id := _get_default_revision_id():
|
||||
langchain_metadata["revision_id"] = default_revision_id
|
||||
|
||||
return langchain_metadata
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def _get_default_revision_id() -> Optional[str]:
|
||||
"""Get the default revision ID based on `git describe`."""
|
||||
try:
|
||||
return exec_git(["describe", "--tags", "--always", "--dirty"])
|
||||
except BaseException:
|
||||
return None
|
||||
|
||||
|
||||
@functools.lru_cache(maxsize=1)
|
||||
def get_release_shas() -> Dict[str, str]:
|
||||
common_release_envs = [
|
||||
"VERCEL_GIT_COMMIT_SHA",
|
||||
"NEXT_PUBLIC_VERCEL_GIT_COMMIT_SHA",
|
||||
"COMMIT_REF",
|
||||
"RENDER_GIT_COMMIT",
|
||||
"CI_COMMIT_SHA",
|
||||
"CIRCLE_SHA1",
|
||||
"CF_PAGES_COMMIT_SHA",
|
||||
"REACT_APP_GIT_SHA",
|
||||
"SOURCE_VERSION",
|
||||
"GITHUB_SHA",
|
||||
"TRAVIS_COMMIT",
|
||||
"GIT_COMMIT",
|
||||
"BUILD_VCS_NUMBER",
|
||||
"bamboo_planRepository_revision",
|
||||
"Build.SourceVersion",
|
||||
"BITBUCKET_COMMIT",
|
||||
"DRONE_COMMIT_SHA",
|
||||
"SEMAPHORE_GIT_SHA",
|
||||
"BUILDKITE_COMMIT",
|
||||
]
|
||||
shas = {}
|
||||
for env in common_release_envs:
|
||||
env_var = os.environ.get(env)
|
||||
if env_var is not None:
|
||||
shas[env] = env_var
|
||||
return shas
|
||||
Reference in New Issue
Block a user