Avoid copying runs (#26689)

Also, re-unify run trees. Use a single shared client.
This commit is contained in:
William FH
2024-09-20 10:57:41 -07:00
committed by GitHub
parent 90031b1b3e
commit 19ce95d3c9
14 changed files with 433 additions and 417 deletions

View File

@@ -8,15 +8,7 @@ from concurrent.futures import Executor, Future, ThreadPoolExecutor
from contextlib import contextmanager
from contextvars import ContextVar, copy_context
from functools import partial
from typing import (
TYPE_CHECKING,
Any,
Callable,
Optional,
TypeVar,
Union,
cast,
)
from typing import TYPE_CHECKING, Any, Callable, Optional, TypeVar, Union, cast
from typing_extensions import ParamSpec, TypedDict
@@ -131,17 +123,29 @@ def _set_config_context(config: RunnableConfig) -> None:
Args:
config (RunnableConfig): The config to set.
"""
from langsmith import (
RunTree, # type: ignore
run_helpers, # type: ignore
)
from langchain_core.tracers.langchain import LangChainTracer
var_child_runnable_config.set(config)
if hasattr(RunTree, "from_runnable_config"):
# import _set_tracing_context, get_tracing_context
rt = RunTree.from_runnable_config(dict(config))
tc = run_helpers.get_tracing_context()
run_helpers._set_tracing_context({**tc, "parent": rt})
if (
(callbacks := config.get("callbacks"))
and (
parent_run_id := getattr(callbacks, "parent_run_id", None)
) # Is callback manager
and (
tracer := next(
(
handler
for handler in getattr(callbacks, "handlers", [])
if isinstance(handler, LangChainTracer)
),
None,
)
)
):
if run := tracer.run_map.get(str(parent_run_id)):
from langsmith.run_helpers import _set_tracing_context
_set_tracing_context({"parent": run})
def ensure_config(config: Optional[RunnableConfig] = None) -> RunnableConfig: