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

Add a langsmith_pyo3 extra to the LangSmith SDK. #1263

Merged
merged 1 commit into from
Nov 27, 2024
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
59 changes: 53 additions & 6 deletions python/langsmith/client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Client for interacting with the LangSmith API.

Check notice on line 1 in python/langsmith/client.py

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

........... WARNING: the benchmark result may be unstable * the standard deviation (127 ms) is 18% of the mean (696 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. create_5_000_run_trees: Mean +- std dev: 696 ms +- 127 ms ........... WARNING: the benchmark result may be unstable * the standard deviation (171 ms) is 12% of the mean (1.40 sec) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. create_10_000_run_trees: Mean +- std dev: 1.40 sec +- 0.17 sec ........... WARNING: the benchmark result may be unstable * the standard deviation (139 ms) is 10% of the mean (1.36 sec) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. create_20_000_run_trees: Mean +- std dev: 1.36 sec +- 0.14 sec ........... dumps_class_nested_py_branch_and_leaf_200x400: Mean +- std dev: 698 us +- 10 us ........... dumps_class_nested_py_leaf_50x100: Mean +- std dev: 25.2 ms +- 0.7 ms ........... dumps_class_nested_py_leaf_100x200: Mean +- std dev: 104 ms +- 2 ms ........... dumps_dataclass_nested_50x100: Mean +- std dev: 25.5 ms +- 0.2 ms ........... WARNING: the benchmark result may be unstable * the standard deviation (16.8 ms) is 24% of the mean (71.0 ms) Try to rerun the benchmark with more runs, values and/or loops. Run 'python -m pyperf system tune' command to reduce the system jitter. Use pyperf stats, pyperf dump and pyperf hist to analyze results. Use --quiet option to hide these warnings. dumps_pydantic_nested_50x100: Mean +- std dev: 71.0 ms +- 16.8 ms ........... dumps_pydanticv1_nested_50x100: Mean +- std dev: 197 ms +- 3 ms

Check notice on line 1 in python/langsmith/client.py

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-----------------------------------------------+----------+------------------------+ | Benchmark | main | changes | +===============================================+==========+========================+ | dumps_pydanticv1_nested_50x100 | 216 ms | 197 ms: 1.10x faster | +-----------------------------------------------+----------+------------------------+ | create_5_000_run_trees | 708 ms | 696 ms: 1.02x faster | +-----------------------------------------------+----------+------------------------+ | dumps_class_nested_py_branch_and_leaf_200x400 | 704 us | 698 us: 1.01x faster | +-----------------------------------------------+----------+------------------------+ | dumps_class_nested_py_leaf_50x100 | 25.1 ms | 25.2 ms: 1.00x slower | +-----------------------------------------------+----------+------------------------+ | create_20_000_run_trees | 1.36 sec | 1.36 sec: 1.00x slower | +-----------------------------------------------+----------+------------------------+ | dumps_class_nested_py_leaf_100x200 | 104 ms | 104 ms: 1.01x slower | +-----------------------------------------------+----------+------------------------+ | dumps_dataclass_nested_50x100 | 25.4 ms | 25.5 ms: 1.01x slower | +-----------------------------------------------+----------+------------------------+ | create_10_000_run_trees | 1.35 sec | 1.40 sec: 1.04x slower | +-----------------------------------------------+----------+------------------------+ | dumps_pydantic_nested_50x100 | 64.0 ms | 71.0 ms: 1.11x slower | +-----------------------------------------------+----------+------------------------+ | Geometric mean | (ref) | 1.00x slower | +-----------------------------------------------+----------+------------------------+

Use the client to customize API keys / workspace ocnnections, SSl certs,
etc. for tracing.
Expand Down Expand Up @@ -369,6 +369,7 @@
"_write_api_urls",
"_settings",
"_manual_cleanup",
"_pyo3_client",
]

def __init__(
Expand Down Expand Up @@ -516,6 +517,42 @@
else ls_utils.get_env_var("HIDE_OUTPUTS") == "true"
)

# To trigger this code, set the `LANGSMITH_USE_PYO3_CLIENT` env var to any value.
self._pyo3_client = None
if ls_utils.get_env_var("USE_PYO3_CLIENT") is not None:
langsmith_pyo3 = None
try:
import langsmith_pyo3 # type: ignore[import-not-found, no-redef]
except ImportError as e:
logger.warning(
"Failed to import `langsmith_pyo3` when PyO3 client was requested, "
"falling back to Python impl: %s",
repr(e),
)

if langsmith_pyo3:
# TODO: tweak these constants as needed
queue_capacity = 1_000_000
batch_size = 100
batch_timeout_millis = 1000
worker_threads = 1

try:
self._pyo3_client = langsmith_pyo3.BlockingTracingClient(
self.api_url,
self.api_key,
queue_capacity,
batch_size,
batch_timeout_millis,
worker_threads,
)
except Exception as e:
logger.warning(
"Failed to instantiate `langsmith_pyo3.BlockingTracingClient` "
"when PyO3 client was requested, falling back to Python impl: %s",
repr(e),
)

self._settings: Union[ls_schemas.LangSmithSettings, None] = None

self._manual_cleanup = False
Expand Down Expand Up @@ -1226,16 +1263,26 @@
copy=False,
)
self._insert_runtime_env([run_create])

if (
self.tracing_queue is not None
# batch ingest requires trace_id and dotted_order to be set
and run_create.get("trace_id") is not None
run_create.get("trace_id") is not None
and run_create.get("dotted_order") is not None
):
serialized_op = serialize_run_dict("post", run_create)
self.tracing_queue.put(
TracingQueueItem(run_create["dotted_order"], serialized_op)
)
if self._pyo3_client is not None:
# `self._run_transform()` above turns the `id` key into a `UUID` object.
# We need to pass a string since `orjson` doesn't seem to serialize `UUID` objects.
run_create["id"] = str(run_create["id"])
self._pyo3_client.create_run(run_create)
elif self.tracing_queue is not None:
serialized_op = serialize_run_dict("post", run_create)
self.tracing_queue.put(
TracingQueueItem(run_create["dotted_order"], serialized_op)
)
else:
# Neither Rust nor Python batch ingestion is configured,
# fall back to the non-batch approach.
self._create_run(run_create)
else:
self._create_run(run_create)

Expand Down
50 changes: 48 additions & 2 deletions python/poetry.lock

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

4 changes: 4 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ orjson = { version = "^3.9.14", markers = "platform_python_implementation != 'Py
httpx = ">=0.23.0,<1"
requests-toolbelt = "^1.0.0"

# Enabled via `langsmith_pyo3` extra: `pip install langsmith[langsmith_pyo3]`.
langsmith-pyo3 = { version = "^0.1.0rc2", optional = true }

[tool.poetry.group.dev.dependencies]
pytest = "^7.3.1"
black = ">=23.3,<25.0"
Expand Down Expand Up @@ -71,6 +74,7 @@ pytest-socket = "^0.7.0"

[tool.poetry.extras]
vcr = ["vcrpy"]
langsmith_pyo3 = ["langsmith-pyo3"]

[build-system]
requires = ["poetry-core"]
Expand Down