Skip to content

Commit

Permalink
Change LANGCHAIN_REVISION_ID to revision_id; use git describe as ba…
Browse files Browse the repository at this point in the history
…ckup; don't overwrite metadata from env variables (#365)
  • Loading branch information
samnoyes authored Jan 18, 2024
1 parent 2ec5135 commit 72db54b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
4 changes: 3 additions & 1 deletion python/langsmith/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,9 @@ def create_run(
metadata: dict = run_extra.setdefault("metadata", {})
runtime_env = ls_env.get_runtime_and_metrics()
langchain_metadata = ls_env.get_langchain_env_var_metadata()
metadata.update(**langchain_metadata)
metadata.update(
{k: v for k, v in langchain_metadata.items() if k not in metadata}
)
run_extra["runtime"] = {**runtime_env, **runtime}
headers = {
**self._headers,
Expand Down
26 changes: 11 additions & 15 deletions python/langsmith/env/_git.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,12 @@
T = TypeVar("T")


def _exec_git(command: List[str]) -> Optional[str]:
def exec_git(command: List[str]) -> Optional[str]:
try:
return subprocess.check_output(
["git"] + command, encoding="utf-8", stderr=subprocess.DEVNULL
).strip()
except FileNotFoundError:
logger.warning("git is not installed, or cannot be found in PATH")
return None
except subprocess.CalledProcessError as e:
logger.debug(f"Error running git command: {e}")
except BaseException:
return None


Expand All @@ -41,18 +37,18 @@ class GitInfo(TypedDict, total=False):
def get_git_info(remote: str = "origin") -> Optional[GitInfo]:
"""Get information about the git repository."""

if not _exec_git(["rev-parse", "--is-inside-work-tree"]):
if not exec_git(["rev-parse", "--is-inside-work-tree"]):
return 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(
"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"]),
"dirty": exec_git(["status", "--porcelain"]) != "",
"author_name": exec_git(["log", "-1", "--format=%an"]),
"author_email": exec_git(["log", "-1", "--format=%ae"]),
}
25 changes: 23 additions & 2 deletions python/langsmith/env/_runtime_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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
Expand Down Expand Up @@ -164,11 +165,31 @@ def get_langchain_env_var_metadata() -> dict:
"LANGCHAIN_PROJECT",
"LANGCHAIN_SESSION",
}
return {
langchain_metadata = {
k: v
for k, v in os.environ.items()
if k.startswith("LANGCHAIN_") and k not in excluded and "key" not in k.lower()
if k.startswith("LANGCHAIN_")
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", "--dirty"])
except BaseException:
return None


@functools.lru_cache(maxsize=1)
Expand Down

0 comments on commit 72db54b

Please sign in to comment.