From 8bb6b9841db322db8c5e8357c2c379482be15441 Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Wed, 13 Mar 2024 07:54:24 -0400 Subject: [PATCH] docs(readme): mention vertex API (#388) --- README.md | 39 ++++++++++++++++++++++++++++++++++----- examples/vertex.py | 8 ++++---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 3996f08c..911ae0a3 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,16 @@ Streaming with `client.messages.stream(...)` exposes [various helpers for your c Alternatively, you can use `client.messages.create(..., stream=True)` which only returns an async iterable of the events in the stream and thus uses less memory (it does not build up a final message object for you). +## Token counting + +You can see the exact usage for a given request through the `usage` response property, e.g. + +```py +message = client.messages.create(...) +message.usage +# Usage(input_tokens=25, output_tokens=13) +``` + ## AWS Bedrock This library also provides support for the [Anthropic Bedrock API](https://aws.amazon.com/bedrock/claude/) if you install this library with the `bedrock` extra, e.g. `pip install -U anthropic[bedrock]`. @@ -189,16 +199,35 @@ print(message) For a more fully fledged example see [`examples/bedrock.py`](https://github.com/anthropics/anthropic-sdk-python/blob/main/examples/bedrock.py). -## Token counting +## Google Vertex -You can see the exact usage for a given request through the `usage` response property, e.g. +> [!IMPORTANT] +> This API is in private preview. + +This library also provides support for the [Anthropic Vertex API](https://cloud.google.com/vertex-ai?hl=en) if you install this library with the `vertex` extra, e.g. `pip install -U anthropic[vertex]`. + +You can then import and instantiate a separate `AnthropicVertex`/`AsyncAnthropicVertexAsync` class, which has the same API as the base `Anthropic`/`AsyncAnthropic` class. ```py -message = client.messages.create(...) -message.usage -# Usage(input_tokens=25, output_tokens=13) +from anthropic import AnthropicVertex + +client = AnthropicVertex() + +message = client.messages.create( + model="claude-3-sonnet@20240229", + max_tokens=100, + messages=[ + { + "role": "user", + "content": "Hello!", + } + ], +) +print(message) ``` +For a more complete example see [`examples/vertex.py`](https://github.com/anthropics/anthropic-sdk-python/blob/main/examples/vertex.py). + ## Using types Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typing.html#typing.TypedDict). Responses are [Pydantic models](https://docs.pydantic.dev), which provide helper methods for things like: diff --git a/examples/vertex.py b/examples/vertex.py index ef504011..e1547eb0 100644 --- a/examples/vertex.py +++ b/examples/vertex.py @@ -9,12 +9,12 @@ def sync_client() -> None: client = AnthropicVertex() message = client.messages.create( - model="claude-instant-1p2", + model="claude-3-sonnet@20240229", max_tokens=100, messages=[ { "role": "user", - "content": "Say hello there!", + "content": "Hello!", } ], ) @@ -27,12 +27,12 @@ async def async_client() -> None: client = AsyncAnthropicVertex() message = await client.messages.create( - model="claude-instant-1p2", + model="claude-3-sonnet@20240229", max_tokens=1024, messages=[ { "role": "user", - "content": "Say hello there!", + "content": "Hello!", } ], )