From 97434a64c5a1c067e12272d2a3f3dca729616b78 Mon Sep 17 00:00:00 2001 From: Zander Chase <130414180+vowelparrot@users.noreply.github.com> Date: Mon, 15 May 2023 15:38:49 +0000 Subject: [PATCH] Add Environment Info to Run (#4691) Store the environment info within the `extra` fields of the Run --- langchain/callbacks/tracers/schemas.py | 9 +++++++++ langchain/env.py | 16 ++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 langchain/env.py diff --git a/langchain/callbacks/tracers/schemas.py b/langchain/callbacks/tracers/schemas.py index 221cd14c..5e036667 100644 --- a/langchain/callbacks/tracers/schemas.py +++ b/langchain/callbacks/tracers/schemas.py @@ -8,6 +8,7 @@ from uuid import UUID from pydantic import BaseModel, Field, root_validator +from langchain.env import get_runtime_environment from langchain.schema import LLMResult @@ -136,6 +137,14 @@ class RunCreate(RunBase): name: str session_id: UUID + @root_validator(pre=True) + def add_runtime_env(cls, values: Dict[str, Any]) -> Dict[str, Any]: + """Add env info to the run.""" + extra = values.get("extra", {}) + extra["runtime"] = get_runtime_environment() + values["extra"] = extra + return values + ChainRun.update_forward_refs() ToolRun.update_forward_refs() diff --git a/langchain/env.py b/langchain/env.py new file mode 100644 index 00000000..e898de0d --- /dev/null +++ b/langchain/env.py @@ -0,0 +1,16 @@ +import platform +from functools import lru_cache + + +@lru_cache(maxsize=1) +def get_runtime_environment() -> dict: + """Get information about the environment.""" + # Lazy import to avoid circular imports + from langchain import __version__ + + return { + "library_version": __version__, + "platform": platform.platform(), + "runtime": "python", + "runtime_version": platform.python_version(), + }