wrap_anthropic#

langsmith.wrappers._anthropic.wrap_anthropic(client: C, *, tracing_extra: TracingExtra | None = None) C[source]#

修补 Anthropic 客户端使其可追踪。

参数:
  • client (Union[Anthropic, AsyncAnthropic]) – 要修补的客户端。

  • tracing_extra (Optional[TracingExtra], 可选) – 额外的追踪信息。默认为 None。

返回:

修补后的客户端。

返回类型:

Union[Anthropic, AsyncAnthropic]

示例

import anthropic
from langsmith import wrappers

client = wrappers.wrap_anthropic(anthropic.Anthropic())

# Use Anthropic client same as you normally would:
system = "You are a helpful assistant."
messages = [
    {
        "role": "user",
        "content": "What physics breakthroughs do you predict will happen by 2300?",
    }
]
completion = client.messages.create(
    model="claude-3-5-sonnet-latest",
    messages=messages,
    max_tokens=1000,
    system=system,
)
print(completion.content)

# You can also use the streaming context manager:
with client.messages.stream(
    model="claude-3-5-sonnet-latest",
    messages=messages,
    max_tokens=1000,
    system=system,
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)
    message = stream.get_final_message()