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 wrap_anthropic #1464

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .github/actions/python-integration-tests/action.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: "Python Integration Tests"

Check notice on line 1 in .github/actions/python-integration-tests/action.yml

View workflow job for this annotation

GitHub Actions / benchmark

Benchmark results

........... create_5_000_run_trees: Mean +- std dev: 664 ms +- 44 ms ........... create_10_000_run_trees: Mean +- std dev: 1.35 sec +- 0.10 sec ........... create_20_000_run_trees: Mean +- std dev: 2.56 sec +- 0.17 sec ........... dumps_class_nested_py_branch_and_leaf_200x400: Mean +- std dev: 706 us +- 16 us ........... dumps_class_nested_py_leaf_50x100: Mean +- std dev: 24.9 ms +- 0.2 ms ........... dumps_class_nested_py_leaf_100x200: Mean +- std dev: 104 ms +- 2 ms ........... dumps_dataclass_nested_50x100: Mean +- std dev: 25.2 ms +- 0.2 ms ........... WARNING: the benchmark result may be unstable * the standard deviation (15.5 ms) is 22% of the mean (70.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: 70.0 ms +- 15.5 ms ........... dumps_pydanticv1_nested_50x100: Mean +- std dev: 197 ms +- 5 ms

Check notice on line 1 in .github/actions/python-integration-tests/action.yml

View workflow job for this annotation

GitHub Actions / benchmark

Comparison against main

+-----------------------------------------------+----------+------------------------+ | Benchmark | main | changes | +===============================================+==========+========================+ | dumps_pydanticv1_nested_50x100 | 217 ms | 197 ms: 1.10x faster | +-----------------------------------------------+----------+------------------------+ | create_20_000_run_trees | 2.67 sec | 2.56 sec: 1.05x faster | +-----------------------------------------------+----------+------------------------+ | create_5_000_run_trees | 677 ms | 664 ms: 1.02x faster | +-----------------------------------------------+----------+------------------------+ | dumps_dataclass_nested_50x100 | 25.3 ms | 25.2 ms: 1.00x faster | +-----------------------------------------------+----------+------------------------+ | dumps_class_nested_py_branch_and_leaf_200x400 | 709 us | 706 us: 1.00x faster | +-----------------------------------------------+----------+------------------------+ | dumps_class_nested_py_leaf_50x100 | 24.9 ms | 24.9 ms: 1.00x slower | +-----------------------------------------------+----------+------------------------+ | create_10_000_run_trees | 1.34 sec | 1.35 sec: 1.00x slower | +-----------------------------------------------+----------+------------------------+ | dumps_class_nested_py_leaf_100x200 | 103 ms | 104 ms: 1.01x slower | +-----------------------------------------------+----------+------------------------+ | dumps_pydantic_nested_50x100 | 66.0 ms | 70.0 ms: 1.06x slower | +-----------------------------------------------+----------+------------------------+ | Geometric mean | (ref) | 1.01x faster | +-----------------------------------------------+----------+------------------------+
description: "Run integration tests"
inputs:
python-version:
Expand Down Expand Up @@ -46,6 +46,7 @@
LANGSMITH_ENDPOINT: https://beta.api.smith.langchain.com
LANGSMITH_API_KEY: ${{ inputs.langchain-api-key-beta }}
OPENAI_API_KEY: ${{ inputs.openai-api-key }}
ANTHROPIC_API_KEY: ${{ inputs.anthropic-api-key }}
LANGSMITH_TEST_CACHE: tests/cassettes
run: make integration_tests_fast
shell: bash
Expand Down
1 change: 1 addition & 0 deletions python/langsmith/run_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,7 @@ def generator_wrapper(
)
raise e
_on_run_end(run_container, outputs=_get_function_result(results, reduce_fn))

return function_return

# "Stream" functions (used in methods like OpenAI/Anthropic's SDKs)
Expand Down
3 changes: 2 additions & 1 deletion python/langsmith/wrappers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""This module provides convenient tracing wrappers for popular libraries."""

from langsmith.wrappers._anthropic import wrap_anthropic
from langsmith.wrappers._openai import wrap_openai

__all__ = ["wrap_openai"]
__all__ = ["wrap_anthropic", "wrap_openai"]
202 changes: 202 additions & 0 deletions python/langsmith/wrappers/_anthropic.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
from __future__ import annotations

import functools
import logging
from typing import (
TYPE_CHECKING,
Any,
Callable,
List,
Mapping,
Optional,
Type,
TypeVar,
Union,
)

from typing_extensions import TypedDict

from langsmith import client as ls_client
from langsmith import run_helpers

if TYPE_CHECKING:
from anthropic import Anthropic, AsyncAnthropic
from anthropic.types import (
Completion,
Message,
MessageStreamEvent,
)

C = TypeVar("C", bound=Union["Anthropic", "AsyncAnthropic", Any])
logger = logging.getLogger(__name__)


@functools.lru_cache
def _get_not_given() -> Optional[Type]:
try:
from anthropic._types import NotGiven

return NotGiven
except ImportError:
return None


def _strip_not_given(d: dict) -> dict:
try:
not_given = _get_not_given()
if not_given is None:
return d
return {k: v for k, v in d.items() if not isinstance(v, not_given)}
except Exception as e:
logger.error(f"Error stripping NotGiven: {e}")
return d


def _accumulate_event(
*, event: MessageStreamEvent, current_snapshot: Message | None
) -> Message | None:
try:
from anthropic.types import ContentBlock
except ImportError:
logger.debug("Error importing ContentBlock")
return current_snapshot

if current_snapshot is None:
if event.type == "message_start":
return event.message

raise RuntimeError(
f'Unexpected event order, got {event.type} before "message_start"'
)

if event.type == "content_block_start":
# TODO: check index <-- from anthropic SDK :)
current_snapshot.content.append(
ContentBlock.construct(**event.content_block.model_dump()), # type: ignore[attr-defined]
)
elif event.type == "content_block_delta":
content = current_snapshot.content[event.index]
if content.type == "text" and event.delta.type == "text_delta":
content.text += event.delta.text
elif event.type == "message_delta":
current_snapshot.stop_reason = event.delta.stop_reason
current_snapshot.stop_sequence = event.delta.stop_sequence
current_snapshot.usage.output_tokens = event.usage.output_tokens

return current_snapshot


def _reduce_chat(all_chunks: List) -> dict:
full_message = None
for chunk in all_chunks:
try:
full_message = _accumulate_event(event=chunk, current_snapshot=full_message)
except RuntimeError as e:
logger.debug(f"Error accumulating event in Anthropic Wrapper: {e}")
return {"output": all_chunks}
if full_message is None:
return {"output": all_chunks}
return full_message.model_dump()


def _reduce_completions(all_chunks: List[Completion]) -> dict:
all_content = []
for chunk in all_chunks:
content = chunk.completion
if content is not None:
all_content.append(content)
content = "".join(all_content)
if all_chunks:
d = all_chunks[-1].model_dump()
d["choices"] = [{"text": content}]
else:
d = {"choices": [{"text": content}]}

return d


def _get_wrapper(
original_create: Callable,
name: str,
reduce_fn: Optional[Callable] = None,
tracing_extra: Optional[TracingExtra] = None,
force_stream: bool = False,
) -> Callable:
textra = tracing_extra or {}

@functools.wraps(original_create)
def create(*args, **kwargs):
stream = kwargs.get("stream")
decorator = run_helpers.traceable(
name=name,
run_type="llm",
reduce_fn=reduce_fn if force_stream or stream else None,
process_inputs=_strip_not_given,
**textra,
)

return decorator(original_create)(*args, **kwargs)

@functools.wraps(original_create)
async def acreate(*args, **kwargs):
stream = kwargs.get("stream")
decorator = run_helpers.traceable(
name=name,
run_type="llm",
reduce_fn=reduce_fn if force_stream or stream else None,
process_inputs=_strip_not_given,
**textra,
)
return await decorator(original_create)(*args, **kwargs)

return acreate if run_helpers.is_async(original_create) else create


class TracingExtra(TypedDict, total=False):
metadata: Optional[Mapping[str, Any]]
tags: Optional[List[str]]
client: Optional[ls_client.Client]


def wrap_anthropic(client: C, *, tracing_extra: Optional[TracingExtra] = None) -> C:
"""Patch the Anthropic client to make it traceable.

Args:
client (Union[Anthropic, AsyncAnthropic]): The client to patch.
tracing_extra (Optional[TracingExtra], optional): Extra tracing information.
Defaults to None.

Returns:
Union[Anthropic, AsyncAnthropic]: The patched client.

"""
client.messages.create = _get_wrapper( # type: ignore[method-assign]
client.messages.create,
"ChatAnthropic",
tracing_extra=tracing_extra,
)
client.messages.stream = _get_wrapper( # type: ignore[method-assign]
client.messages.stream,
"ChatAnthropic",
_reduce_chat,
force_stream=True,
tracing_extra=tracing_extra,
)
client.completions.create = _get_wrapper( # type: ignore[method-assign]
client.completions.create,
"Anthropic",
_reduce_completions,
tracing_extra=tracing_extra,
)

if (
hasattr(client, "beta")
and hasattr(client.beta, "messages")
and hasattr(client.beta.messages, "create")
):
client.beta.messages.create = _get_wrapper( # type: ignore[method-assign]
client.beta.messages.create, # type: ignore
"Anthropic",
tracing_extra=tracing_extra,
)
return client
Loading