Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Run object from SDK #6067

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion langchain/callbacks/tracers/langchain.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
from langchainplus_sdk import LangChainPlusClient

from langchain.callbacks.tracers.base import BaseTracer
from langchain.callbacks.tracers.schemas import Run, RunTypeEnum, TracerSession
from langchain.callbacks.tracers.schemas import (
Run,
RunTypeEnum,
TracerSession,
)
from langchain.env import get_runtime_environment
from langchain.schema import BaseMessage, messages_to_dict

Expand Down
70 changes: 19 additions & 51 deletions langchain/callbacks/tracers/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from __future__ import annotations

import datetime
from enum import Enum
from typing import Any, Dict, List, Optional
from uuid import UUID

from langchainplus_sdk.schemas import RunBase as BaseRunV2
from langchainplus_sdk.schemas import RunTypeEnum
from pydantic import BaseModel, Field, root_validator

from langchain.env import get_runtime_environment
from langchain.schema import LLMResult


Expand Down Expand Up @@ -88,36 +88,11 @@ class ToolRun(BaseRun):
# Begin V2 API Schemas


class RunTypeEnum(str, Enum):
"""Enum for run types."""
class Run(BaseRunV2):
"""Run schema for the V2 API in the Tracer."""

tool = "tool"
chain = "chain"
llm = "llm"


class RunBase(BaseModel):
"""Base Run schema."""

id: Optional[UUID]
start_time: datetime.datetime = Field(default_factory=datetime.datetime.utcnow)
end_time: datetime.datetime = Field(default_factory=datetime.datetime.utcnow)
extra: Optional[Dict[str, Any]] = None
error: Optional[str]
execution_order: int
child_execution_order: Optional[int]
serialized: dict
inputs: dict
outputs: Optional[dict]
reference_example_id: Optional[UUID]
run_type: RunTypeEnum
parent_run_id: Optional[UUID]


class Run(RunBase):
"""Run schema when loading from the DB."""

name: str
child_execution_order: int
child_runs: List[Run] = Field(default_factory=list)

@root_validator(pre=True)
Expand All @@ -131,26 +106,19 @@ def assign_name(cls, values: dict) -> dict:
return values


class RunCreate(RunBase):
name: str
session_name: Optional[str] = None

@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


class RunUpdate(BaseModel):
end_time: Optional[datetime.datetime]
error: Optional[str]
outputs: Optional[dict]
parent_run_id: Optional[UUID]
reference_example_id: Optional[UUID]


ChainRun.update_forward_refs()
ToolRun.update_forward_refs()

__all__ = [
"BaseRun",
"ChainRun",
"LLMRun",
"Run",
"RunTypeEnum",
"ToolRun",
"TracerSession",
"TracerSessionBase",
"TracerSessionV1",
"TracerSessionV1Base",
"TracerSessionV1Create",
]
18 changes: 9 additions & 9 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ singlestoredb = {version = "^0.6.1", optional = true}
pyspark = {version = "^3.4.0", optional = true}
tigrisdb = {version = "^1.0.0b6", optional = true}
nebula3-python = {version = "^3.4.0", optional = true}
langchainplus-sdk = ">=0.0.7"
langchainplus-sdk = ">=0.0.9"
awadb = {version = "^0.3.2", optional = true}
azure-search-documents = {version = "11.4.0a20230509004", source = "azure-sdk-dev", optional = true}

Expand Down
4 changes: 3 additions & 1 deletion tests/unit_tests/callbacks/test_callback_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,10 @@ def test_callback_manager_inheritance() -> None:
assert child_manager2.inheritable_handlers == [handler1]


def test_callback_manager_configure() -> None:
def test_callback_manager_configure(monkeypatch: pytest.MonkeyPatch) -> None:
"""Test callback manager configuration."""
monkeypatch.setenv("LANGCHAIN_TRACING_V2", "false")
monkeypatch.setenv("LANGCHAIN_TRACING", "false")
handler1, handler2, handler3, handler4 = (
FakeCallbackHandler(),
FakeCallbackHandler(),
Expand Down
27 changes: 27 additions & 0 deletions tests/unit_tests/callbacks/test_schemas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import langchain.callbacks.tracers.schemas as schemas
from langchain.callbacks.tracers.schemas import __all__ as schemas_all


def test_public_api() -> None:
"""Test for changes in the public API."""
expected_all = [
"BaseRun",
"ChainRun",
"LLMRun",
"Run",
"RunTypeEnum",
"ToolRun",
"TracerSession",
"TracerSessionBase",
"TracerSessionV1",
"TracerSessionV1Base",
"TracerSessionV1Create",
]

assert sorted(schemas_all) == expected_all

# Assert that the object is actually present in the schema module
for module_name in expected_all:
assert (
hasattr(schemas, module_name) and getattr(schemas, module_name) is not None
)