From 5e35ffeec0a38055bba2f3998aa3e7c85790627a Mon Sep 17 00:00:00 2001 From: Stainless Bot <107565488+stainless-bot@users.noreply.github.com> Date: Thu, 4 Apr 2024 13:03:36 -0400 Subject: [PATCH] feat(api): tool use beta (#438) https://docs.anthropic.com/claude/docs/tool-use --- .stats.yml | 2 +- api.md | 24 + examples/tools.py | 53 + src/anthropic/_client.py | 8 + src/anthropic/resources/__init__.py | 14 + src/anthropic/resources/beta/__init__.py | 33 + src/anthropic/resources/beta/beta.py | 81 + .../resources/beta/tools/__init__.py | 33 + .../resources/beta/tools/messages.py | 1747 +++++++++++++++++ src/anthropic/resources/beta/tools/tools.py | 80 + src/anthropic/types/beta/__init__.py | 3 + src/anthropic/types/beta/tools/__init__.py | 12 + .../types/beta/tools/message_create_params.py | 277 +++ src/anthropic/types/beta/tools/tool_param.py | 34 + .../beta/tools/tool_result_block_param.py | 20 + .../types/beta/tools/tool_use_block.py | 17 + .../types/beta/tools/tool_use_block_param.py | 17 + .../beta/tools/tools_beta_content_block.py | 10 + .../types/beta/tools/tools_beta_message.py | 104 + .../beta/tools/tools_beta_message_param.py | 27 + src/anthropic/types/message.py | 3 - tests/api_resources/beta/__init__.py | 1 + tests/api_resources/beta/tools/__init__.py | 1 + .../api_resources/beta/tools/test_messages.py | 530 +++++ 24 files changed, 3127 insertions(+), 4 deletions(-) create mode 100644 examples/tools.py create mode 100644 src/anthropic/resources/beta/__init__.py create mode 100644 src/anthropic/resources/beta/beta.py create mode 100644 src/anthropic/resources/beta/tools/__init__.py create mode 100644 src/anthropic/resources/beta/tools/messages.py create mode 100644 src/anthropic/resources/beta/tools/tools.py create mode 100644 src/anthropic/types/beta/__init__.py create mode 100644 src/anthropic/types/beta/tools/__init__.py create mode 100644 src/anthropic/types/beta/tools/message_create_params.py create mode 100644 src/anthropic/types/beta/tools/tool_param.py create mode 100644 src/anthropic/types/beta/tools/tool_result_block_param.py create mode 100644 src/anthropic/types/beta/tools/tool_use_block.py create mode 100644 src/anthropic/types/beta/tools/tool_use_block_param.py create mode 100644 src/anthropic/types/beta/tools/tools_beta_content_block.py create mode 100644 src/anthropic/types/beta/tools/tools_beta_message.py create mode 100644 src/anthropic/types/beta/tools/tools_beta_message_param.py create mode 100644 tests/api_resources/beta/__init__.py create mode 100644 tests/api_resources/beta/tools/__init__.py create mode 100644 tests/api_resources/beta/tools/test_messages.py diff --git a/.stats.yml b/.stats.yml index fcbfe481..a2e9ecf6 100644 --- a/.stats.yml +++ b/.stats.yml @@ -1 +1 @@ -configured_endpoints: 2 +configured_endpoints: 3 diff --git a/api.md b/api.md index 49d42c16..0c84ef6f 100644 --- a/api.md +++ b/api.md @@ -27,3 +27,27 @@ Methods: - client.messages.create(\*\*params) -> Message - client.messages.stream(\*args) -> MessageStreamManager[MessageStream] | MessageStreamManager[MessageStreamT] + +# Beta + +## Tools + +### Messages + +Types: + +```python +from anthropic.types.beta.tools import ( + Tool, + ToolResultBlockParam, + ToolUseBlock, + ToolUseBlockParam, + ToolsBetaContentBlock, + ToolsBetaMessage, + ToolsBetaMessageParam, +) +``` + +Methods: + +- client.beta.tools.messages.create(\*\*params) -> ToolsBetaMessage diff --git a/examples/tools.py b/examples/tools.py new file mode 100644 index 00000000..da1f8059 --- /dev/null +++ b/examples/tools.py @@ -0,0 +1,53 @@ +from __future__ import annotations + +from anthropic import Anthropic +from anthropic.types.beta.tools import ToolParam, ToolsBetaMessageParam + +client = Anthropic() + +user_message: ToolsBetaMessageParam = { + "role": "user", + "content": "What is the weather in SF?", +} +tools: list[ToolParam] = [ + { + "name": "get_weather", + "description": "Get the weather for a specific location", + "input_schema": { + "type": "object", + "properties": {"location": {"type": "string"}}, + }, + } +] + +message = client.beta.tools.messages.create( + model="claude-3-opus-20240229", + max_tokens=1024, + messages=[user_message], + tools=tools, +) +print(f"Initial response: {message.model_dump_json(indent=2)}") + +assert message.stop_reason == "tool_use" + +tool = next(c for c in message.content if c.type == "tool_use") +response = client.beta.tools.messages.create( + model="claude-3-opus-20240229", + max_tokens=1024, + messages=[ + user_message, + {"role": message.role, "content": message.content}, + { + "role": "user", + "content": [ + { + "type": "tool_result", + "tool_use_id": tool.id, + "content": [{"type": "text", "text": "The weather is 73f"}], + } + ], + }, + ], + tools=tools, +) +print(f"\nFinal response: {response.model_dump_json(indent=2)}") diff --git a/src/anthropic/_client.py b/src/anthropic/_client.py index a3ad45fa..d847f018 100644 --- a/src/anthropic/_client.py +++ b/src/anthropic/_client.py @@ -58,6 +58,7 @@ class Anthropic(SyncAPIClient): completions: resources.Completions messages: resources.Messages + beta: resources.Beta with_raw_response: AnthropicWithRawResponse with_streaming_response: AnthropicWithStreamedResponse @@ -134,6 +135,7 @@ def __init__( self.completions = resources.Completions(self) self.messages = resources.Messages(self) + self.beta = resources.Beta(self) self.with_raw_response = AnthropicWithRawResponse(self) self.with_streaming_response = AnthropicWithStreamedResponse(self) @@ -318,6 +320,7 @@ def _make_status_error( class AsyncAnthropic(AsyncAPIClient): completions: resources.AsyncCompletions messages: resources.AsyncMessages + beta: resources.AsyncBeta with_raw_response: AsyncAnthropicWithRawResponse with_streaming_response: AsyncAnthropicWithStreamedResponse @@ -394,6 +397,7 @@ def __init__( self.completions = resources.AsyncCompletions(self) self.messages = resources.AsyncMessages(self) + self.beta = resources.AsyncBeta(self) self.with_raw_response = AsyncAnthropicWithRawResponse(self) self.with_streaming_response = AsyncAnthropicWithStreamedResponse(self) @@ -579,24 +583,28 @@ class AnthropicWithRawResponse: def __init__(self, client: Anthropic) -> None: self.completions = resources.CompletionsWithRawResponse(client.completions) self.messages = resources.MessagesWithRawResponse(client.messages) + self.beta = resources.BetaWithRawResponse(client.beta) class AsyncAnthropicWithRawResponse: def __init__(self, client: AsyncAnthropic) -> None: self.completions = resources.AsyncCompletionsWithRawResponse(client.completions) self.messages = resources.AsyncMessagesWithRawResponse(client.messages) + self.beta = resources.AsyncBetaWithRawResponse(client.beta) class AnthropicWithStreamedResponse: def __init__(self, client: Anthropic) -> None: self.completions = resources.CompletionsWithStreamingResponse(client.completions) self.messages = resources.MessagesWithStreamingResponse(client.messages) + self.beta = resources.BetaWithStreamingResponse(client.beta) class AsyncAnthropicWithStreamedResponse: def __init__(self, client: AsyncAnthropic) -> None: self.completions = resources.AsyncCompletionsWithStreamingResponse(client.completions) self.messages = resources.AsyncMessagesWithStreamingResponse(client.messages) + self.beta = resources.AsyncBetaWithStreamingResponse(client.beta) Client = Anthropic diff --git a/src/anthropic/resources/__init__.py b/src/anthropic/resources/__init__.py index cc6cc5be..318d5cdd 100644 --- a/src/anthropic/resources/__init__.py +++ b/src/anthropic/resources/__init__.py @@ -1,5 +1,13 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. +from .beta import ( + Beta, + AsyncBeta, + BetaWithRawResponse, + AsyncBetaWithRawResponse, + BetaWithStreamingResponse, + AsyncBetaWithStreamingResponse, +) from .messages import ( Messages, AsyncMessages, @@ -30,4 +38,10 @@ "AsyncMessagesWithRawResponse", "MessagesWithStreamingResponse", "AsyncMessagesWithStreamingResponse", + "Beta", + "AsyncBeta", + "BetaWithRawResponse", + "AsyncBetaWithRawResponse", + "BetaWithStreamingResponse", + "AsyncBetaWithStreamingResponse", ] diff --git a/src/anthropic/resources/beta/__init__.py b/src/anthropic/resources/beta/__init__.py new file mode 100644 index 00000000..77971b13 --- /dev/null +++ b/src/anthropic/resources/beta/__init__.py @@ -0,0 +1,33 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from .beta import ( + Beta, + AsyncBeta, + BetaWithRawResponse, + AsyncBetaWithRawResponse, + BetaWithStreamingResponse, + AsyncBetaWithStreamingResponse, +) +from .tools import ( + Tools, + AsyncTools, + ToolsWithRawResponse, + AsyncToolsWithRawResponse, + ToolsWithStreamingResponse, + AsyncToolsWithStreamingResponse, +) + +__all__ = [ + "Tools", + "AsyncTools", + "ToolsWithRawResponse", + "AsyncToolsWithRawResponse", + "ToolsWithStreamingResponse", + "AsyncToolsWithStreamingResponse", + "Beta", + "AsyncBeta", + "BetaWithRawResponse", + "AsyncBetaWithRawResponse", + "BetaWithStreamingResponse", + "AsyncBetaWithStreamingResponse", +] diff --git a/src/anthropic/resources/beta/beta.py b/src/anthropic/resources/beta/beta.py new file mode 100644 index 00000000..3ffe29e2 --- /dev/null +++ b/src/anthropic/resources/beta/beta.py @@ -0,0 +1,81 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from .tools import ( + Tools, + AsyncTools, + ToolsWithRawResponse, + AsyncToolsWithRawResponse, + ToolsWithStreamingResponse, + AsyncToolsWithStreamingResponse, +) +from ..._compat import cached_property +from ..._resource import SyncAPIResource, AsyncAPIResource +from .tools.tools import Tools, AsyncTools + +__all__ = ["Beta", "AsyncBeta"] + + +class Beta(SyncAPIResource): + @cached_property + def tools(self) -> Tools: + return Tools(self._client) + + @cached_property + def with_raw_response(self) -> BetaWithRawResponse: + return BetaWithRawResponse(self) + + @cached_property + def with_streaming_response(self) -> BetaWithStreamingResponse: + return BetaWithStreamingResponse(self) + + +class AsyncBeta(AsyncAPIResource): + @cached_property + def tools(self) -> AsyncTools: + return AsyncTools(self._client) + + @cached_property + def with_raw_response(self) -> AsyncBetaWithRawResponse: + return AsyncBetaWithRawResponse(self) + + @cached_property + def with_streaming_response(self) -> AsyncBetaWithStreamingResponse: + return AsyncBetaWithStreamingResponse(self) + + +class BetaWithRawResponse: + def __init__(self, beta: Beta) -> None: + self._beta = beta + + @cached_property + def tools(self) -> ToolsWithRawResponse: + return ToolsWithRawResponse(self._beta.tools) + + +class AsyncBetaWithRawResponse: + def __init__(self, beta: AsyncBeta) -> None: + self._beta = beta + + @cached_property + def tools(self) -> AsyncToolsWithRawResponse: + return AsyncToolsWithRawResponse(self._beta.tools) + + +class BetaWithStreamingResponse: + def __init__(self, beta: Beta) -> None: + self._beta = beta + + @cached_property + def tools(self) -> ToolsWithStreamingResponse: + return ToolsWithStreamingResponse(self._beta.tools) + + +class AsyncBetaWithStreamingResponse: + def __init__(self, beta: AsyncBeta) -> None: + self._beta = beta + + @cached_property + def tools(self) -> AsyncToolsWithStreamingResponse: + return AsyncToolsWithStreamingResponse(self._beta.tools) diff --git a/src/anthropic/resources/beta/tools/__init__.py b/src/anthropic/resources/beta/tools/__init__.py new file mode 100644 index 00000000..bd98aeea --- /dev/null +++ b/src/anthropic/resources/beta/tools/__init__.py @@ -0,0 +1,33 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from .tools import ( + Tools, + AsyncTools, + ToolsWithRawResponse, + AsyncToolsWithRawResponse, + ToolsWithStreamingResponse, + AsyncToolsWithStreamingResponse, +) +from .messages import ( + Messages, + AsyncMessages, + MessagesWithRawResponse, + AsyncMessagesWithRawResponse, + MessagesWithStreamingResponse, + AsyncMessagesWithStreamingResponse, +) + +__all__ = [ + "Messages", + "AsyncMessages", + "MessagesWithRawResponse", + "AsyncMessagesWithRawResponse", + "MessagesWithStreamingResponse", + "AsyncMessagesWithStreamingResponse", + "Tools", + "AsyncTools", + "ToolsWithRawResponse", + "AsyncToolsWithRawResponse", + "ToolsWithStreamingResponse", + "AsyncToolsWithStreamingResponse", +] diff --git a/src/anthropic/resources/beta/tools/messages.py b/src/anthropic/resources/beta/tools/messages.py new file mode 100644 index 00000000..ff79a972 --- /dev/null +++ b/src/anthropic/resources/beta/tools/messages.py @@ -0,0 +1,1747 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing import List, Iterable, overload +from typing_extensions import Literal + +import httpx + +from .... import _legacy_response +from ....types import MessageStreamEvent +from ...._types import NOT_GIVEN, Body, Query, Headers, NotGiven +from ...._utils import ( + required_args, + maybe_transform, + async_maybe_transform, +) +from ...._compat import cached_property +from ...._resource import SyncAPIResource, AsyncAPIResource +from ...._response import to_streamed_response_wrapper, async_to_streamed_response_wrapper +from ...._streaming import Stream, AsyncStream +from ...._base_client import ( + make_request_options, +) +from ....types.beta.tools import ToolParam, ToolsBetaMessage, ToolsBetaMessageParam, message_create_params + +__all__ = ["Messages", "AsyncMessages"] + + +class Messages(SyncAPIResource): + @cached_property + def with_raw_response(self) -> MessagesWithRawResponse: + return MessagesWithRawResponse(self) + + @cached_property + def with_streaming_response(self) -> MessagesWithStreamingResponse: + return MessagesWithStreamingResponse(self) + + @overload + def create( + self, + *, + max_tokens: int, + messages: Iterable[ToolsBetaMessageParam], + model: str, + metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN, + stop_sequences: List[str] | NotGiven = NOT_GIVEN, + stream: Literal[False] | NotGiven = NOT_GIVEN, + system: str | NotGiven = NOT_GIVEN, + temperature: float | NotGiven = NOT_GIVEN, + tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, + top_k: int | NotGiven = NOT_GIVEN, + top_p: float | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = 600, + ) -> ToolsBetaMessage: + """ + Create a Message. + + Send a structured list of input messages with text and/or image content, and the + model will generate the next message in the conversation. + + The Messages API can be used for for either single queries or stateless + multi-turn conversations. + + Args: + max_tokens: The maximum number of tokens to generate before stopping. + + Note that our models may stop _before_ reaching this maximum. This parameter + only specifies the absolute maximum number of tokens to generate. + + Different models have different maximum values for this parameter. See + [models](https://docs.anthropic.com/claude/docs/models-overview) for details. + + messages: Input messages. + + Our models are trained to operate on alternating `user` and `assistant` + conversational turns. When creating a new `Message`, you specify the prior + conversational turns with the `messages` parameter, and the model then generates + the next `Message` in the conversation. + + Each input message must be an object with a `role` and `content`. You can + specify a single `user`-role message, or you can include multiple `user` and + `assistant` messages. The first message must always use the `user` role. + + If the final message uses the `assistant` role, the response content will + continue immediately from the content in that message. This can be used to + constrain part of the model's response. + + Example with a single `user` message: + + ```json + [{ "role": "user", "content": "Hello, Claude" }] + ``` + + Example with multiple conversational turns: + + ```json + [ + { "role": "user", "content": "Hello there." }, + { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + { "role": "user", "content": "Can you explain LLMs in plain English?" } + ] + ``` + + Example with a partially-filled response from Claude: + + ```json + [ + { + "role": "user", + "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + }, + { "role": "assistant", "content": "The best answer is (" } + ] + ``` + + Each input message `content` may be either a single `string` or an array of + content blocks, where each block has a specific `type`. Using a `string` for + `content` is shorthand for an array of one content block of type `"text"`. The + following input messages are equivalent: + + ```json + { "role": "user", "content": "Hello, Claude" } + ``` + + ```json + { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + ``` + + Starting with Claude 3 models, you can also send image content blocks: + + ```json + { + "role": "user", + "content": [ + { + "type": "image", + "source": { + "type": "base64", + "media_type": "image/jpeg", + "data": "/9j/4AAQSkZJRg..." + } + }, + { "type": "text", "text": "What is in this image?" } + ] + } + ``` + + We currently support the `base64` source type for images, and the `image/jpeg`, + `image/png`, `image/gif`, and `image/webp` media types. + + See [examples](https://docs.anthropic.com/claude/reference/messages-examples) + for more input examples. + + Note that if you want to include a + [system prompt](https://docs.anthropic.com/claude/docs/system-prompts), you can + use the top-level `system` parameter — there is no `"system"` role for input + messages in the Messages API. + + model: The model that will complete your prompt. + + See [models](https://docs.anthropic.com/claude/docs/models-overview) for + additional details and options. + + metadata: An object describing metadata about the request. + + stop_sequences: Custom text sequences that will cause the model to stop generating. + + Our models will normally stop when they have naturally completed their turn, + which will result in a response `stop_reason` of `"end_turn"`. + + If you want the model to stop generating when it encounters custom strings of + text, you can use the `stop_sequences` parameter. If the model encounters one of + the custom sequences, the response `stop_reason` value will be `"stop_sequence"` + and the response `stop_sequence` value will contain the matched stop sequence. + + stream: Whether to incrementally stream the response using server-sent events. + + See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming) + for details. + + system: System prompt. + + A system prompt is a way of providing context and instructions to Claude, such + as specifying a particular goal or role. See our + [guide to system prompts](https://docs.anthropic.com/claude/docs/system-prompts). + + temperature: Amount of randomness injected into the response. + + Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` + for analytical / multiple choice, and closer to `1.0` for creative and + generative tasks. + + Note that even with `temperature` of `0.0`, the results will not be fully + deterministic. + + tools: [beta] Definitions of tools that the model may use. + + If you include `tools` in your API request, the model may return `tool_use` + content blocks that represent the model's use of those tools. You can then run + those tools using the tool input generated by the model and then optionally + return results back to the model using `tool_result` content blocks. + + Each tool definition includes: + + - `name`: Name of the tool. + - `description`: Optional, but strongly-recommended description of the tool. + - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + shape that the model will produce in `tool_use` output content blocks. + + For example, if you defined `tools` as: + + ```json + [ + { + "name": "get_stock_price", + "description": "Get the current stock price for a given ticker symbol.", + "input_schema": { + "type": "object", + "properties": { + "ticker": { + "type": "string", + "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + } + }, + "required": ["ticker"] + } + } + ] + ``` + + And then asked the model "What's the S&P 500 at today?", the model might produce + `tool_use` content blocks in the response like this: + + ```json + [ + { + "type": "tool_use", + "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "name": "get_stock_price", + "input": { "ticker": "^GSPC" } + } + ] + ``` + + You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + input, and return the following back to the model in a subsequent `user` + message: + + ```json + [ + { + "type": "tool_result", + "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "content": "259.75 USD" + } + ] + ``` + + Tools can be used for workflows that include running client-side tools and + functions, or more generally whenever you want the model to produce a particular + JSON structure of output. + + See our [beta guide](https://docs.anthropic.com/claude/docs/tool-use) for more + details. + + top_k: Only sample from the top K options for each subsequent token. + + Used to remove "long tail" low probability responses. + [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + top_p: Use nucleus sampling. + + In nucleus sampling, we compute the cumulative distribution over all the options + for each subsequent token in decreasing probability order and cut it off once it + reaches a particular probability specified by `top_p`. You should either alter + `temperature` or `top_p`, but not both. + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + extra_headers: Send extra headers + + extra_query: Add additional query parameters to the request + + extra_body: Add additional JSON properties to the request + + timeout: Override the client-level default timeout for this request, in seconds + """ + ... + + @overload + def create( + self, + *, + max_tokens: int, + messages: Iterable[ToolsBetaMessageParam], + model: str, + stream: Literal[True], + metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN, + stop_sequences: List[str] | NotGiven = NOT_GIVEN, + system: str | NotGiven = NOT_GIVEN, + temperature: float | NotGiven = NOT_GIVEN, + tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, + top_k: int | NotGiven = NOT_GIVEN, + top_p: float | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = 600, + ) -> Stream[MessageStreamEvent]: + """ + Create a Message. + + Send a structured list of input messages with text and/or image content, and the + model will generate the next message in the conversation. + + The Messages API can be used for for either single queries or stateless + multi-turn conversations. + + Args: + max_tokens: The maximum number of tokens to generate before stopping. + + Note that our models may stop _before_ reaching this maximum. This parameter + only specifies the absolute maximum number of tokens to generate. + + Different models have different maximum values for this parameter. See + [models](https://docs.anthropic.com/claude/docs/models-overview) for details. + + messages: Input messages. + + Our models are trained to operate on alternating `user` and `assistant` + conversational turns. When creating a new `Message`, you specify the prior + conversational turns with the `messages` parameter, and the model then generates + the next `Message` in the conversation. + + Each input message must be an object with a `role` and `content`. You can + specify a single `user`-role message, or you can include multiple `user` and + `assistant` messages. The first message must always use the `user` role. + + If the final message uses the `assistant` role, the response content will + continue immediately from the content in that message. This can be used to + constrain part of the model's response. + + Example with a single `user` message: + + ```json + [{ "role": "user", "content": "Hello, Claude" }] + ``` + + Example with multiple conversational turns: + + ```json + [ + { "role": "user", "content": "Hello there." }, + { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + { "role": "user", "content": "Can you explain LLMs in plain English?" } + ] + ``` + + Example with a partially-filled response from Claude: + + ```json + [ + { + "role": "user", + "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + }, + { "role": "assistant", "content": "The best answer is (" } + ] + ``` + + Each input message `content` may be either a single `string` or an array of + content blocks, where each block has a specific `type`. Using a `string` for + `content` is shorthand for an array of one content block of type `"text"`. The + following input messages are equivalent: + + ```json + { "role": "user", "content": "Hello, Claude" } + ``` + + ```json + { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + ``` + + Starting with Claude 3 models, you can also send image content blocks: + + ```json + { + "role": "user", + "content": [ + { + "type": "image", + "source": { + "type": "base64", + "media_type": "image/jpeg", + "data": "/9j/4AAQSkZJRg..." + } + }, + { "type": "text", "text": "What is in this image?" } + ] + } + ``` + + We currently support the `base64` source type for images, and the `image/jpeg`, + `image/png`, `image/gif`, and `image/webp` media types. + + See [examples](https://docs.anthropic.com/claude/reference/messages-examples) + for more input examples. + + Note that if you want to include a + [system prompt](https://docs.anthropic.com/claude/docs/system-prompts), you can + use the top-level `system` parameter — there is no `"system"` role for input + messages in the Messages API. + + model: The model that will complete your prompt. + + See [models](https://docs.anthropic.com/claude/docs/models-overview) for + additional details and options. + + stream: Whether to incrementally stream the response using server-sent events. + + See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming) + for details. + + metadata: An object describing metadata about the request. + + stop_sequences: Custom text sequences that will cause the model to stop generating. + + Our models will normally stop when they have naturally completed their turn, + which will result in a response `stop_reason` of `"end_turn"`. + + If you want the model to stop generating when it encounters custom strings of + text, you can use the `stop_sequences` parameter. If the model encounters one of + the custom sequences, the response `stop_reason` value will be `"stop_sequence"` + and the response `stop_sequence` value will contain the matched stop sequence. + + system: System prompt. + + A system prompt is a way of providing context and instructions to Claude, such + as specifying a particular goal or role. See our + [guide to system prompts](https://docs.anthropic.com/claude/docs/system-prompts). + + temperature: Amount of randomness injected into the response. + + Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` + for analytical / multiple choice, and closer to `1.0` for creative and + generative tasks. + + Note that even with `temperature` of `0.0`, the results will not be fully + deterministic. + + tools: [beta] Definitions of tools that the model may use. + + If you include `tools` in your API request, the model may return `tool_use` + content blocks that represent the model's use of those tools. You can then run + those tools using the tool input generated by the model and then optionally + return results back to the model using `tool_result` content blocks. + + Each tool definition includes: + + - `name`: Name of the tool. + - `description`: Optional, but strongly-recommended description of the tool. + - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + shape that the model will produce in `tool_use` output content blocks. + + For example, if you defined `tools` as: + + ```json + [ + { + "name": "get_stock_price", + "description": "Get the current stock price for a given ticker symbol.", + "input_schema": { + "type": "object", + "properties": { + "ticker": { + "type": "string", + "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + } + }, + "required": ["ticker"] + } + } + ] + ``` + + And then asked the model "What's the S&P 500 at today?", the model might produce + `tool_use` content blocks in the response like this: + + ```json + [ + { + "type": "tool_use", + "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "name": "get_stock_price", + "input": { "ticker": "^GSPC" } + } + ] + ``` + + You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + input, and return the following back to the model in a subsequent `user` + message: + + ```json + [ + { + "type": "tool_result", + "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "content": "259.75 USD" + } + ] + ``` + + Tools can be used for workflows that include running client-side tools and + functions, or more generally whenever you want the model to produce a particular + JSON structure of output. + + See our [beta guide](https://docs.anthropic.com/claude/docs/tool-use) for more + details. + + top_k: Only sample from the top K options for each subsequent token. + + Used to remove "long tail" low probability responses. + [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + top_p: Use nucleus sampling. + + In nucleus sampling, we compute the cumulative distribution over all the options + for each subsequent token in decreasing probability order and cut it off once it + reaches a particular probability specified by `top_p`. You should either alter + `temperature` or `top_p`, but not both. + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + extra_headers: Send extra headers + + extra_query: Add additional query parameters to the request + + extra_body: Add additional JSON properties to the request + + timeout: Override the client-level default timeout for this request, in seconds + """ + ... + + @overload + def create( + self, + *, + max_tokens: int, + messages: Iterable[ToolsBetaMessageParam], + model: str, + stream: bool, + metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN, + stop_sequences: List[str] | NotGiven = NOT_GIVEN, + system: str | NotGiven = NOT_GIVEN, + temperature: float | NotGiven = NOT_GIVEN, + tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, + top_k: int | NotGiven = NOT_GIVEN, + top_p: float | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = 600, + ) -> ToolsBetaMessage | Stream[MessageStreamEvent]: + """ + Create a Message. + + Send a structured list of input messages with text and/or image content, and the + model will generate the next message in the conversation. + + The Messages API can be used for for either single queries or stateless + multi-turn conversations. + + Args: + max_tokens: The maximum number of tokens to generate before stopping. + + Note that our models may stop _before_ reaching this maximum. This parameter + only specifies the absolute maximum number of tokens to generate. + + Different models have different maximum values for this parameter. See + [models](https://docs.anthropic.com/claude/docs/models-overview) for details. + + messages: Input messages. + + Our models are trained to operate on alternating `user` and `assistant` + conversational turns. When creating a new `Message`, you specify the prior + conversational turns with the `messages` parameter, and the model then generates + the next `Message` in the conversation. + + Each input message must be an object with a `role` and `content`. You can + specify a single `user`-role message, or you can include multiple `user` and + `assistant` messages. The first message must always use the `user` role. + + If the final message uses the `assistant` role, the response content will + continue immediately from the content in that message. This can be used to + constrain part of the model's response. + + Example with a single `user` message: + + ```json + [{ "role": "user", "content": "Hello, Claude" }] + ``` + + Example with multiple conversational turns: + + ```json + [ + { "role": "user", "content": "Hello there." }, + { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + { "role": "user", "content": "Can you explain LLMs in plain English?" } + ] + ``` + + Example with a partially-filled response from Claude: + + ```json + [ + { + "role": "user", + "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + }, + { "role": "assistant", "content": "The best answer is (" } + ] + ``` + + Each input message `content` may be either a single `string` or an array of + content blocks, where each block has a specific `type`. Using a `string` for + `content` is shorthand for an array of one content block of type `"text"`. The + following input messages are equivalent: + + ```json + { "role": "user", "content": "Hello, Claude" } + ``` + + ```json + { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + ``` + + Starting with Claude 3 models, you can also send image content blocks: + + ```json + { + "role": "user", + "content": [ + { + "type": "image", + "source": { + "type": "base64", + "media_type": "image/jpeg", + "data": "/9j/4AAQSkZJRg..." + } + }, + { "type": "text", "text": "What is in this image?" } + ] + } + ``` + + We currently support the `base64` source type for images, and the `image/jpeg`, + `image/png`, `image/gif`, and `image/webp` media types. + + See [examples](https://docs.anthropic.com/claude/reference/messages-examples) + for more input examples. + + Note that if you want to include a + [system prompt](https://docs.anthropic.com/claude/docs/system-prompts), you can + use the top-level `system` parameter — there is no `"system"` role for input + messages in the Messages API. + + model: The model that will complete your prompt. + + See [models](https://docs.anthropic.com/claude/docs/models-overview) for + additional details and options. + + stream: Whether to incrementally stream the response using server-sent events. + + See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming) + for details. + + metadata: An object describing metadata about the request. + + stop_sequences: Custom text sequences that will cause the model to stop generating. + + Our models will normally stop when they have naturally completed their turn, + which will result in a response `stop_reason` of `"end_turn"`. + + If you want the model to stop generating when it encounters custom strings of + text, you can use the `stop_sequences` parameter. If the model encounters one of + the custom sequences, the response `stop_reason` value will be `"stop_sequence"` + and the response `stop_sequence` value will contain the matched stop sequence. + + system: System prompt. + + A system prompt is a way of providing context and instructions to Claude, such + as specifying a particular goal or role. See our + [guide to system prompts](https://docs.anthropic.com/claude/docs/system-prompts). + + temperature: Amount of randomness injected into the response. + + Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` + for analytical / multiple choice, and closer to `1.0` for creative and + generative tasks. + + Note that even with `temperature` of `0.0`, the results will not be fully + deterministic. + + tools: [beta] Definitions of tools that the model may use. + + If you include `tools` in your API request, the model may return `tool_use` + content blocks that represent the model's use of those tools. You can then run + those tools using the tool input generated by the model and then optionally + return results back to the model using `tool_result` content blocks. + + Each tool definition includes: + + - `name`: Name of the tool. + - `description`: Optional, but strongly-recommended description of the tool. + - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + shape that the model will produce in `tool_use` output content blocks. + + For example, if you defined `tools` as: + + ```json + [ + { + "name": "get_stock_price", + "description": "Get the current stock price for a given ticker symbol.", + "input_schema": { + "type": "object", + "properties": { + "ticker": { + "type": "string", + "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + } + }, + "required": ["ticker"] + } + } + ] + ``` + + And then asked the model "What's the S&P 500 at today?", the model might produce + `tool_use` content blocks in the response like this: + + ```json + [ + { + "type": "tool_use", + "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "name": "get_stock_price", + "input": { "ticker": "^GSPC" } + } + ] + ``` + + You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + input, and return the following back to the model in a subsequent `user` + message: + + ```json + [ + { + "type": "tool_result", + "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "content": "259.75 USD" + } + ] + ``` + + Tools can be used for workflows that include running client-side tools and + functions, or more generally whenever you want the model to produce a particular + JSON structure of output. + + See our [beta guide](https://docs.anthropic.com/claude/docs/tool-use) for more + details. + + top_k: Only sample from the top K options for each subsequent token. + + Used to remove "long tail" low probability responses. + [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + top_p: Use nucleus sampling. + + In nucleus sampling, we compute the cumulative distribution over all the options + for each subsequent token in decreasing probability order and cut it off once it + reaches a particular probability specified by `top_p`. You should either alter + `temperature` or `top_p`, but not both. + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + extra_headers: Send extra headers + + extra_query: Add additional query parameters to the request + + extra_body: Add additional JSON properties to the request + + timeout: Override the client-level default timeout for this request, in seconds + """ + ... + + @required_args(["max_tokens", "messages", "model"], ["max_tokens", "messages", "model", "stream"]) + def create( + self, + *, + max_tokens: int, + messages: Iterable[ToolsBetaMessageParam], + model: str, + metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN, + stop_sequences: List[str] | NotGiven = NOT_GIVEN, + stream: Literal[False] | Literal[True] | NotGiven = NOT_GIVEN, + system: str | NotGiven = NOT_GIVEN, + temperature: float | NotGiven = NOT_GIVEN, + tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, + top_k: int | NotGiven = NOT_GIVEN, + top_p: float | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = 600, + ) -> ToolsBetaMessage | Stream[MessageStreamEvent]: + extra_headers = {"anthropic-beta": "tools-2024-04-04", **(extra_headers or {})} + return self._post( + "/v1/messages?beta=tools", + body=maybe_transform( + { + "max_tokens": max_tokens, + "messages": messages, + "model": model, + "metadata": metadata, + "stop_sequences": stop_sequences, + "stream": stream, + "system": system, + "temperature": temperature, + "tools": tools, + "top_k": top_k, + "top_p": top_p, + }, + message_create_params.MessageCreateParams, + ), + options=make_request_options( + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout + ), + cast_to=ToolsBetaMessage, + stream=stream or False, + stream_cls=Stream[MessageStreamEvent], + ) + + +class AsyncMessages(AsyncAPIResource): + @cached_property + def with_raw_response(self) -> AsyncMessagesWithRawResponse: + return AsyncMessagesWithRawResponse(self) + + @cached_property + def with_streaming_response(self) -> AsyncMessagesWithStreamingResponse: + return AsyncMessagesWithStreamingResponse(self) + + @overload + async def create( + self, + *, + max_tokens: int, + messages: Iterable[ToolsBetaMessageParam], + model: str, + metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN, + stop_sequences: List[str] | NotGiven = NOT_GIVEN, + stream: Literal[False] | NotGiven = NOT_GIVEN, + system: str | NotGiven = NOT_GIVEN, + temperature: float | NotGiven = NOT_GIVEN, + tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, + top_k: int | NotGiven = NOT_GIVEN, + top_p: float | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = 600, + ) -> ToolsBetaMessage: + """ + Create a Message. + + Send a structured list of input messages with text and/or image content, and the + model will generate the next message in the conversation. + + The Messages API can be used for for either single queries or stateless + multi-turn conversations. + + Args: + max_tokens: The maximum number of tokens to generate before stopping. + + Note that our models may stop _before_ reaching this maximum. This parameter + only specifies the absolute maximum number of tokens to generate. + + Different models have different maximum values for this parameter. See + [models](https://docs.anthropic.com/claude/docs/models-overview) for details. + + messages: Input messages. + + Our models are trained to operate on alternating `user` and `assistant` + conversational turns. When creating a new `Message`, you specify the prior + conversational turns with the `messages` parameter, and the model then generates + the next `Message` in the conversation. + + Each input message must be an object with a `role` and `content`. You can + specify a single `user`-role message, or you can include multiple `user` and + `assistant` messages. The first message must always use the `user` role. + + If the final message uses the `assistant` role, the response content will + continue immediately from the content in that message. This can be used to + constrain part of the model's response. + + Example with a single `user` message: + + ```json + [{ "role": "user", "content": "Hello, Claude" }] + ``` + + Example with multiple conversational turns: + + ```json + [ + { "role": "user", "content": "Hello there." }, + { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + { "role": "user", "content": "Can you explain LLMs in plain English?" } + ] + ``` + + Example with a partially-filled response from Claude: + + ```json + [ + { + "role": "user", + "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + }, + { "role": "assistant", "content": "The best answer is (" } + ] + ``` + + Each input message `content` may be either a single `string` or an array of + content blocks, where each block has a specific `type`. Using a `string` for + `content` is shorthand for an array of one content block of type `"text"`. The + following input messages are equivalent: + + ```json + { "role": "user", "content": "Hello, Claude" } + ``` + + ```json + { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + ``` + + Starting with Claude 3 models, you can also send image content blocks: + + ```json + { + "role": "user", + "content": [ + { + "type": "image", + "source": { + "type": "base64", + "media_type": "image/jpeg", + "data": "/9j/4AAQSkZJRg..." + } + }, + { "type": "text", "text": "What is in this image?" } + ] + } + ``` + + We currently support the `base64` source type for images, and the `image/jpeg`, + `image/png`, `image/gif`, and `image/webp` media types. + + See [examples](https://docs.anthropic.com/claude/reference/messages-examples) + for more input examples. + + Note that if you want to include a + [system prompt](https://docs.anthropic.com/claude/docs/system-prompts), you can + use the top-level `system` parameter — there is no `"system"` role for input + messages in the Messages API. + + model: The model that will complete your prompt. + + See [models](https://docs.anthropic.com/claude/docs/models-overview) for + additional details and options. + + metadata: An object describing metadata about the request. + + stop_sequences: Custom text sequences that will cause the model to stop generating. + + Our models will normally stop when they have naturally completed their turn, + which will result in a response `stop_reason` of `"end_turn"`. + + If you want the model to stop generating when it encounters custom strings of + text, you can use the `stop_sequences` parameter. If the model encounters one of + the custom sequences, the response `stop_reason` value will be `"stop_sequence"` + and the response `stop_sequence` value will contain the matched stop sequence. + + stream: Whether to incrementally stream the response using server-sent events. + + See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming) + for details. + + system: System prompt. + + A system prompt is a way of providing context and instructions to Claude, such + as specifying a particular goal or role. See our + [guide to system prompts](https://docs.anthropic.com/claude/docs/system-prompts). + + temperature: Amount of randomness injected into the response. + + Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` + for analytical / multiple choice, and closer to `1.0` for creative and + generative tasks. + + Note that even with `temperature` of `0.0`, the results will not be fully + deterministic. + + tools: [beta] Definitions of tools that the model may use. + + If you include `tools` in your API request, the model may return `tool_use` + content blocks that represent the model's use of those tools. You can then run + those tools using the tool input generated by the model and then optionally + return results back to the model using `tool_result` content blocks. + + Each tool definition includes: + + - `name`: Name of the tool. + - `description`: Optional, but strongly-recommended description of the tool. + - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + shape that the model will produce in `tool_use` output content blocks. + + For example, if you defined `tools` as: + + ```json + [ + { + "name": "get_stock_price", + "description": "Get the current stock price for a given ticker symbol.", + "input_schema": { + "type": "object", + "properties": { + "ticker": { + "type": "string", + "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + } + }, + "required": ["ticker"] + } + } + ] + ``` + + And then asked the model "What's the S&P 500 at today?", the model might produce + `tool_use` content blocks in the response like this: + + ```json + [ + { + "type": "tool_use", + "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "name": "get_stock_price", + "input": { "ticker": "^GSPC" } + } + ] + ``` + + You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + input, and return the following back to the model in a subsequent `user` + message: + + ```json + [ + { + "type": "tool_result", + "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "content": "259.75 USD" + } + ] + ``` + + Tools can be used for workflows that include running client-side tools and + functions, or more generally whenever you want the model to produce a particular + JSON structure of output. + + See our [beta guide](https://docs.anthropic.com/claude/docs/tool-use) for more + details. + + top_k: Only sample from the top K options for each subsequent token. + + Used to remove "long tail" low probability responses. + [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + top_p: Use nucleus sampling. + + In nucleus sampling, we compute the cumulative distribution over all the options + for each subsequent token in decreasing probability order and cut it off once it + reaches a particular probability specified by `top_p`. You should either alter + `temperature` or `top_p`, but not both. + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + extra_headers: Send extra headers + + extra_query: Add additional query parameters to the request + + extra_body: Add additional JSON properties to the request + + timeout: Override the client-level default timeout for this request, in seconds + """ + ... + + @overload + async def create( + self, + *, + max_tokens: int, + messages: Iterable[ToolsBetaMessageParam], + model: str, + stream: Literal[True], + metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN, + stop_sequences: List[str] | NotGiven = NOT_GIVEN, + system: str | NotGiven = NOT_GIVEN, + temperature: float | NotGiven = NOT_GIVEN, + tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, + top_k: int | NotGiven = NOT_GIVEN, + top_p: float | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = 600, + ) -> AsyncStream[MessageStreamEvent]: + """ + Create a Message. + + Send a structured list of input messages with text and/or image content, and the + model will generate the next message in the conversation. + + The Messages API can be used for for either single queries or stateless + multi-turn conversations. + + Args: + max_tokens: The maximum number of tokens to generate before stopping. + + Note that our models may stop _before_ reaching this maximum. This parameter + only specifies the absolute maximum number of tokens to generate. + + Different models have different maximum values for this parameter. See + [models](https://docs.anthropic.com/claude/docs/models-overview) for details. + + messages: Input messages. + + Our models are trained to operate on alternating `user` and `assistant` + conversational turns. When creating a new `Message`, you specify the prior + conversational turns with the `messages` parameter, and the model then generates + the next `Message` in the conversation. + + Each input message must be an object with a `role` and `content`. You can + specify a single `user`-role message, or you can include multiple `user` and + `assistant` messages. The first message must always use the `user` role. + + If the final message uses the `assistant` role, the response content will + continue immediately from the content in that message. This can be used to + constrain part of the model's response. + + Example with a single `user` message: + + ```json + [{ "role": "user", "content": "Hello, Claude" }] + ``` + + Example with multiple conversational turns: + + ```json + [ + { "role": "user", "content": "Hello there." }, + { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + { "role": "user", "content": "Can you explain LLMs in plain English?" } + ] + ``` + + Example with a partially-filled response from Claude: + + ```json + [ + { + "role": "user", + "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + }, + { "role": "assistant", "content": "The best answer is (" } + ] + ``` + + Each input message `content` may be either a single `string` or an array of + content blocks, where each block has a specific `type`. Using a `string` for + `content` is shorthand for an array of one content block of type `"text"`. The + following input messages are equivalent: + + ```json + { "role": "user", "content": "Hello, Claude" } + ``` + + ```json + { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + ``` + + Starting with Claude 3 models, you can also send image content blocks: + + ```json + { + "role": "user", + "content": [ + { + "type": "image", + "source": { + "type": "base64", + "media_type": "image/jpeg", + "data": "/9j/4AAQSkZJRg..." + } + }, + { "type": "text", "text": "What is in this image?" } + ] + } + ``` + + We currently support the `base64` source type for images, and the `image/jpeg`, + `image/png`, `image/gif`, and `image/webp` media types. + + See [examples](https://docs.anthropic.com/claude/reference/messages-examples) + for more input examples. + + Note that if you want to include a + [system prompt](https://docs.anthropic.com/claude/docs/system-prompts), you can + use the top-level `system` parameter — there is no `"system"` role for input + messages in the Messages API. + + model: The model that will complete your prompt. + + See [models](https://docs.anthropic.com/claude/docs/models-overview) for + additional details and options. + + stream: Whether to incrementally stream the response using server-sent events. + + See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming) + for details. + + metadata: An object describing metadata about the request. + + stop_sequences: Custom text sequences that will cause the model to stop generating. + + Our models will normally stop when they have naturally completed their turn, + which will result in a response `stop_reason` of `"end_turn"`. + + If you want the model to stop generating when it encounters custom strings of + text, you can use the `stop_sequences` parameter. If the model encounters one of + the custom sequences, the response `stop_reason` value will be `"stop_sequence"` + and the response `stop_sequence` value will contain the matched stop sequence. + + system: System prompt. + + A system prompt is a way of providing context and instructions to Claude, such + as specifying a particular goal or role. See our + [guide to system prompts](https://docs.anthropic.com/claude/docs/system-prompts). + + temperature: Amount of randomness injected into the response. + + Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` + for analytical / multiple choice, and closer to `1.0` for creative and + generative tasks. + + Note that even with `temperature` of `0.0`, the results will not be fully + deterministic. + + tools: [beta] Definitions of tools that the model may use. + + If you include `tools` in your API request, the model may return `tool_use` + content blocks that represent the model's use of those tools. You can then run + those tools using the tool input generated by the model and then optionally + return results back to the model using `tool_result` content blocks. + + Each tool definition includes: + + - `name`: Name of the tool. + - `description`: Optional, but strongly-recommended description of the tool. + - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + shape that the model will produce in `tool_use` output content blocks. + + For example, if you defined `tools` as: + + ```json + [ + { + "name": "get_stock_price", + "description": "Get the current stock price for a given ticker symbol.", + "input_schema": { + "type": "object", + "properties": { + "ticker": { + "type": "string", + "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + } + }, + "required": ["ticker"] + } + } + ] + ``` + + And then asked the model "What's the S&P 500 at today?", the model might produce + `tool_use` content blocks in the response like this: + + ```json + [ + { + "type": "tool_use", + "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "name": "get_stock_price", + "input": { "ticker": "^GSPC" } + } + ] + ``` + + You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + input, and return the following back to the model in a subsequent `user` + message: + + ```json + [ + { + "type": "tool_result", + "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "content": "259.75 USD" + } + ] + ``` + + Tools can be used for workflows that include running client-side tools and + functions, or more generally whenever you want the model to produce a particular + JSON structure of output. + + See our [beta guide](https://docs.anthropic.com/claude/docs/tool-use) for more + details. + + top_k: Only sample from the top K options for each subsequent token. + + Used to remove "long tail" low probability responses. + [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + top_p: Use nucleus sampling. + + In nucleus sampling, we compute the cumulative distribution over all the options + for each subsequent token in decreasing probability order and cut it off once it + reaches a particular probability specified by `top_p`. You should either alter + `temperature` or `top_p`, but not both. + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + extra_headers: Send extra headers + + extra_query: Add additional query parameters to the request + + extra_body: Add additional JSON properties to the request + + timeout: Override the client-level default timeout for this request, in seconds + """ + ... + + @overload + async def create( + self, + *, + max_tokens: int, + messages: Iterable[ToolsBetaMessageParam], + model: str, + stream: bool, + metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN, + stop_sequences: List[str] | NotGiven = NOT_GIVEN, + system: str | NotGiven = NOT_GIVEN, + temperature: float | NotGiven = NOT_GIVEN, + tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, + top_k: int | NotGiven = NOT_GIVEN, + top_p: float | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = 600, + ) -> ToolsBetaMessage | AsyncStream[MessageStreamEvent]: + """ + Create a Message. + + Send a structured list of input messages with text and/or image content, and the + model will generate the next message in the conversation. + + The Messages API can be used for for either single queries or stateless + multi-turn conversations. + + Args: + max_tokens: The maximum number of tokens to generate before stopping. + + Note that our models may stop _before_ reaching this maximum. This parameter + only specifies the absolute maximum number of tokens to generate. + + Different models have different maximum values for this parameter. See + [models](https://docs.anthropic.com/claude/docs/models-overview) for details. + + messages: Input messages. + + Our models are trained to operate on alternating `user` and `assistant` + conversational turns. When creating a new `Message`, you specify the prior + conversational turns with the `messages` parameter, and the model then generates + the next `Message` in the conversation. + + Each input message must be an object with a `role` and `content`. You can + specify a single `user`-role message, or you can include multiple `user` and + `assistant` messages. The first message must always use the `user` role. + + If the final message uses the `assistant` role, the response content will + continue immediately from the content in that message. This can be used to + constrain part of the model's response. + + Example with a single `user` message: + + ```json + [{ "role": "user", "content": "Hello, Claude" }] + ``` + + Example with multiple conversational turns: + + ```json + [ + { "role": "user", "content": "Hello there." }, + { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + { "role": "user", "content": "Can you explain LLMs in plain English?" } + ] + ``` + + Example with a partially-filled response from Claude: + + ```json + [ + { + "role": "user", + "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + }, + { "role": "assistant", "content": "The best answer is (" } + ] + ``` + + Each input message `content` may be either a single `string` or an array of + content blocks, where each block has a specific `type`. Using a `string` for + `content` is shorthand for an array of one content block of type `"text"`. The + following input messages are equivalent: + + ```json + { "role": "user", "content": "Hello, Claude" } + ``` + + ```json + { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + ``` + + Starting with Claude 3 models, you can also send image content blocks: + + ```json + { + "role": "user", + "content": [ + { + "type": "image", + "source": { + "type": "base64", + "media_type": "image/jpeg", + "data": "/9j/4AAQSkZJRg..." + } + }, + { "type": "text", "text": "What is in this image?" } + ] + } + ``` + + We currently support the `base64` source type for images, and the `image/jpeg`, + `image/png`, `image/gif`, and `image/webp` media types. + + See [examples](https://docs.anthropic.com/claude/reference/messages-examples) + for more input examples. + + Note that if you want to include a + [system prompt](https://docs.anthropic.com/claude/docs/system-prompts), you can + use the top-level `system` parameter — there is no `"system"` role for input + messages in the Messages API. + + model: The model that will complete your prompt. + + See [models](https://docs.anthropic.com/claude/docs/models-overview) for + additional details and options. + + stream: Whether to incrementally stream the response using server-sent events. + + See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming) + for details. + + metadata: An object describing metadata about the request. + + stop_sequences: Custom text sequences that will cause the model to stop generating. + + Our models will normally stop when they have naturally completed their turn, + which will result in a response `stop_reason` of `"end_turn"`. + + If you want the model to stop generating when it encounters custom strings of + text, you can use the `stop_sequences` parameter. If the model encounters one of + the custom sequences, the response `stop_reason` value will be `"stop_sequence"` + and the response `stop_sequence` value will contain the matched stop sequence. + + system: System prompt. + + A system prompt is a way of providing context and instructions to Claude, such + as specifying a particular goal or role. See our + [guide to system prompts](https://docs.anthropic.com/claude/docs/system-prompts). + + temperature: Amount of randomness injected into the response. + + Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` + for analytical / multiple choice, and closer to `1.0` for creative and + generative tasks. + + Note that even with `temperature` of `0.0`, the results will not be fully + deterministic. + + tools: [beta] Definitions of tools that the model may use. + + If you include `tools` in your API request, the model may return `tool_use` + content blocks that represent the model's use of those tools. You can then run + those tools using the tool input generated by the model and then optionally + return results back to the model using `tool_result` content blocks. + + Each tool definition includes: + + - `name`: Name of the tool. + - `description`: Optional, but strongly-recommended description of the tool. + - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + shape that the model will produce in `tool_use` output content blocks. + + For example, if you defined `tools` as: + + ```json + [ + { + "name": "get_stock_price", + "description": "Get the current stock price for a given ticker symbol.", + "input_schema": { + "type": "object", + "properties": { + "ticker": { + "type": "string", + "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + } + }, + "required": ["ticker"] + } + } + ] + ``` + + And then asked the model "What's the S&P 500 at today?", the model might produce + `tool_use` content blocks in the response like this: + + ```json + [ + { + "type": "tool_use", + "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "name": "get_stock_price", + "input": { "ticker": "^GSPC" } + } + ] + ``` + + You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + input, and return the following back to the model in a subsequent `user` + message: + + ```json + [ + { + "type": "tool_result", + "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "content": "259.75 USD" + } + ] + ``` + + Tools can be used for workflows that include running client-side tools and + functions, or more generally whenever you want the model to produce a particular + JSON structure of output. + + See our [beta guide](https://docs.anthropic.com/claude/docs/tool-use) for more + details. + + top_k: Only sample from the top K options for each subsequent token. + + Used to remove "long tail" low probability responses. + [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + top_p: Use nucleus sampling. + + In nucleus sampling, we compute the cumulative distribution over all the options + for each subsequent token in decreasing probability order and cut it off once it + reaches a particular probability specified by `top_p`. You should either alter + `temperature` or `top_p`, but not both. + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + + extra_headers: Send extra headers + + extra_query: Add additional query parameters to the request + + extra_body: Add additional JSON properties to the request + + timeout: Override the client-level default timeout for this request, in seconds + """ + ... + + @required_args(["max_tokens", "messages", "model"], ["max_tokens", "messages", "model", "stream"]) + async def create( + self, + *, + max_tokens: int, + messages: Iterable[ToolsBetaMessageParam], + model: str, + metadata: message_create_params.Metadata | NotGiven = NOT_GIVEN, + stop_sequences: List[str] | NotGiven = NOT_GIVEN, + stream: Literal[False] | Literal[True] | NotGiven = NOT_GIVEN, + system: str | NotGiven = NOT_GIVEN, + temperature: float | NotGiven = NOT_GIVEN, + tools: Iterable[ToolParam] | NotGiven = NOT_GIVEN, + top_k: int | NotGiven = NOT_GIVEN, + top_p: float | NotGiven = NOT_GIVEN, + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. + # The extra values given here take precedence over values defined on the client or passed to this method. + extra_headers: Headers | None = None, + extra_query: Query | None = None, + extra_body: Body | None = None, + timeout: float | httpx.Timeout | None | NotGiven = 600, + ) -> ToolsBetaMessage | AsyncStream[MessageStreamEvent]: + extra_headers = {"anthropic-beta": "tools-2024-04-04", **(extra_headers or {})} + return await self._post( + "/v1/messages?beta=tools", + body=await async_maybe_transform( + { + "max_tokens": max_tokens, + "messages": messages, + "model": model, + "metadata": metadata, + "stop_sequences": stop_sequences, + "stream": stream, + "system": system, + "temperature": temperature, + "tools": tools, + "top_k": top_k, + "top_p": top_p, + }, + message_create_params.MessageCreateParams, + ), + options=make_request_options( + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout + ), + cast_to=ToolsBetaMessage, + stream=stream or False, + stream_cls=AsyncStream[MessageStreamEvent], + ) + + +class MessagesWithRawResponse: + def __init__(self, messages: Messages) -> None: + self._messages = messages + + self.create = _legacy_response.to_raw_response_wrapper( + messages.create, + ) + + +class AsyncMessagesWithRawResponse: + def __init__(self, messages: AsyncMessages) -> None: + self._messages = messages + + self.create = _legacy_response.async_to_raw_response_wrapper( + messages.create, + ) + + +class MessagesWithStreamingResponse: + def __init__(self, messages: Messages) -> None: + self._messages = messages + + self.create = to_streamed_response_wrapper( + messages.create, + ) + + +class AsyncMessagesWithStreamingResponse: + def __init__(self, messages: AsyncMessages) -> None: + self._messages = messages + + self.create = async_to_streamed_response_wrapper( + messages.create, + ) diff --git a/src/anthropic/resources/beta/tools/tools.py b/src/anthropic/resources/beta/tools/tools.py new file mode 100644 index 00000000..7fddb044 --- /dev/null +++ b/src/anthropic/resources/beta/tools/tools.py @@ -0,0 +1,80 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from .messages import ( + Messages, + AsyncMessages, + MessagesWithRawResponse, + AsyncMessagesWithRawResponse, + MessagesWithStreamingResponse, + AsyncMessagesWithStreamingResponse, +) +from ...._compat import cached_property +from ...._resource import SyncAPIResource, AsyncAPIResource + +__all__ = ["Tools", "AsyncTools"] + + +class Tools(SyncAPIResource): + @cached_property + def messages(self) -> Messages: + return Messages(self._client) + + @cached_property + def with_raw_response(self) -> ToolsWithRawResponse: + return ToolsWithRawResponse(self) + + @cached_property + def with_streaming_response(self) -> ToolsWithStreamingResponse: + return ToolsWithStreamingResponse(self) + + +class AsyncTools(AsyncAPIResource): + @cached_property + def messages(self) -> AsyncMessages: + return AsyncMessages(self._client) + + @cached_property + def with_raw_response(self) -> AsyncToolsWithRawResponse: + return AsyncToolsWithRawResponse(self) + + @cached_property + def with_streaming_response(self) -> AsyncToolsWithStreamingResponse: + return AsyncToolsWithStreamingResponse(self) + + +class ToolsWithRawResponse: + def __init__(self, tools: Tools) -> None: + self._tools = tools + + @cached_property + def messages(self) -> MessagesWithRawResponse: + return MessagesWithRawResponse(self._tools.messages) + + +class AsyncToolsWithRawResponse: + def __init__(self, tools: AsyncTools) -> None: + self._tools = tools + + @cached_property + def messages(self) -> AsyncMessagesWithRawResponse: + return AsyncMessagesWithRawResponse(self._tools.messages) + + +class ToolsWithStreamingResponse: + def __init__(self, tools: Tools) -> None: + self._tools = tools + + @cached_property + def messages(self) -> MessagesWithStreamingResponse: + return MessagesWithStreamingResponse(self._tools.messages) + + +class AsyncToolsWithStreamingResponse: + def __init__(self, tools: AsyncTools) -> None: + self._tools = tools + + @cached_property + def messages(self) -> AsyncMessagesWithStreamingResponse: + return AsyncMessagesWithStreamingResponse(self._tools.messages) diff --git a/src/anthropic/types/beta/__init__.py b/src/anthropic/types/beta/__init__.py new file mode 100644 index 00000000..f8ee8b14 --- /dev/null +++ b/src/anthropic/types/beta/__init__.py @@ -0,0 +1,3 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations diff --git a/src/anthropic/types/beta/tools/__init__.py b/src/anthropic/types/beta/tools/__init__.py new file mode 100644 index 00000000..3fc03434 --- /dev/null +++ b/src/anthropic/types/beta/tools/__init__.py @@ -0,0 +1,12 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from .tool_param import ToolParam as ToolParam +from .tool_use_block import ToolUseBlock as ToolUseBlock +from .tools_beta_message import ToolsBetaMessage as ToolsBetaMessage +from .tool_use_block_param import ToolUseBlockParam as ToolUseBlockParam +from .message_create_params import MessageCreateParams as MessageCreateParams +from .tool_result_block_param import ToolResultBlockParam as ToolResultBlockParam +from .tools_beta_content_block import ToolsBetaContentBlock as ToolsBetaContentBlock +from .tools_beta_message_param import ToolsBetaMessageParam as ToolsBetaMessageParam diff --git a/src/anthropic/types/beta/tools/message_create_params.py b/src/anthropic/types/beta/tools/message_create_params.py new file mode 100644 index 00000000..8eca73c5 --- /dev/null +++ b/src/anthropic/types/beta/tools/message_create_params.py @@ -0,0 +1,277 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing import List, Union, Iterable, Optional +from typing_extensions import Literal, Required, TypedDict + +from .tool_param import ToolParam +from .tools_beta_message_param import ToolsBetaMessageParam + +__all__ = ["MessageCreateParamsBase", "Metadata", "MessageCreateParamsNonStreaming", "MessageCreateParamsStreaming"] + + +class MessageCreateParamsBase(TypedDict, total=False): + max_tokens: Required[int] + """The maximum number of tokens to generate before stopping. + + Note that our models may stop _before_ reaching this maximum. This parameter + only specifies the absolute maximum number of tokens to generate. + + Different models have different maximum values for this parameter. See + [models](https://docs.anthropic.com/claude/docs/models-overview) for details. + """ + + messages: Required[Iterable[ToolsBetaMessageParam]] + """Input messages. + + Our models are trained to operate on alternating `user` and `assistant` + conversational turns. When creating a new `Message`, you specify the prior + conversational turns with the `messages` parameter, and the model then generates + the next `Message` in the conversation. + + Each input message must be an object with a `role` and `content`. You can + specify a single `user`-role message, or you can include multiple `user` and + `assistant` messages. The first message must always use the `user` role. + + If the final message uses the `assistant` role, the response content will + continue immediately from the content in that message. This can be used to + constrain part of the model's response. + + Example with a single `user` message: + + ```json + [{ "role": "user", "content": "Hello, Claude" }] + ``` + + Example with multiple conversational turns: + + ```json + [ + { "role": "user", "content": "Hello there." }, + { "role": "assistant", "content": "Hi, I'm Claude. How can I help you?" }, + { "role": "user", "content": "Can you explain LLMs in plain English?" } + ] + ``` + + Example with a partially-filled response from Claude: + + ```json + [ + { + "role": "user", + "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + }, + { "role": "assistant", "content": "The best answer is (" } + ] + ``` + + Each input message `content` may be either a single `string` or an array of + content blocks, where each block has a specific `type`. Using a `string` for + `content` is shorthand for an array of one content block of type `"text"`. The + following input messages are equivalent: + + ```json + { "role": "user", "content": "Hello, Claude" } + ``` + + ```json + { "role": "user", "content": [{ "type": "text", "text": "Hello, Claude" }] } + ``` + + Starting with Claude 3 models, you can also send image content blocks: + + ```json + { + "role": "user", + "content": [ + { + "type": "image", + "source": { + "type": "base64", + "media_type": "image/jpeg", + "data": "/9j/4AAQSkZJRg..." + } + }, + { "type": "text", "text": "What is in this image?" } + ] + } + ``` + + We currently support the `base64` source type for images, and the `image/jpeg`, + `image/png`, `image/gif`, and `image/webp` media types. + + See [examples](https://docs.anthropic.com/claude/reference/messages-examples) + for more input examples. + + Note that if you want to include a + [system prompt](https://docs.anthropic.com/claude/docs/system-prompts), you can + use the top-level `system` parameter — there is no `"system"` role for input + messages in the Messages API. + """ + + model: Required[str] + """The model that will complete your prompt. + + See [models](https://docs.anthropic.com/claude/docs/models-overview) for + additional details and options. + """ + + metadata: Metadata + """An object describing metadata about the request.""" + + stop_sequences: List[str] + """Custom text sequences that will cause the model to stop generating. + + Our models will normally stop when they have naturally completed their turn, + which will result in a response `stop_reason` of `"end_turn"`. + + If you want the model to stop generating when it encounters custom strings of + text, you can use the `stop_sequences` parameter. If the model encounters one of + the custom sequences, the response `stop_reason` value will be `"stop_sequence"` + and the response `stop_sequence` value will contain the matched stop sequence. + """ + + system: str + """System prompt. + + A system prompt is a way of providing context and instructions to Claude, such + as specifying a particular goal or role. See our + [guide to system prompts](https://docs.anthropic.com/claude/docs/system-prompts). + """ + + temperature: float + """Amount of randomness injected into the response. + + Defaults to `1.0`. Ranges from `0.0` to `1.0`. Use `temperature` closer to `0.0` + for analytical / multiple choice, and closer to `1.0` for creative and + generative tasks. + + Note that even with `temperature` of `0.0`, the results will not be fully + deterministic. + """ + + tools: Iterable[ToolParam] + """[beta] Definitions of tools that the model may use. + + If you include `tools` in your API request, the model may return `tool_use` + content blocks that represent the model's use of those tools. You can then run + those tools using the tool input generated by the model and then optionally + return results back to the model using `tool_result` content blocks. + + Each tool definition includes: + + - `name`: Name of the tool. + - `description`: Optional, but strongly-recommended description of the tool. + - `input_schema`: [JSON schema](https://json-schema.org/) for the tool `input` + shape that the model will produce in `tool_use` output content blocks. + + For example, if you defined `tools` as: + + ```json + [ + { + "name": "get_stock_price", + "description": "Get the current stock price for a given ticker symbol.", + "input_schema": { + "type": "object", + "properties": { + "ticker": { + "type": "string", + "description": "The stock ticker symbol, e.g. AAPL for Apple Inc." + } + }, + "required": ["ticker"] + } + } + ] + ``` + + And then asked the model "What's the S&P 500 at today?", the model might produce + `tool_use` content blocks in the response like this: + + ```json + [ + { + "type": "tool_use", + "id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "name": "get_stock_price", + "input": { "ticker": "^GSPC" } + } + ] + ``` + + You might then run your `get_stock_price` tool with `{"ticker": "^GSPC"}` as an + input, and return the following back to the model in a subsequent `user` + message: + + ```json + [ + { + "type": "tool_result", + "tool_use_id": "toolu_01D7FLrfh4GYq7yT1ULFeyMV", + "content": "259.75 USD" + } + ] + ``` + + Tools can be used for workflows that include running client-side tools and + functions, or more generally whenever you want the model to produce a particular + JSON structure of output. + + See our [beta guide](https://docs.anthropic.com/claude/docs/tool-use) for more + details. + """ + + top_k: int + """Only sample from the top K options for each subsequent token. + + Used to remove "long tail" low probability responses. + [Learn more technical details here](https://towardsdatascience.com/how-to-sample-from-language-models-682bceb97277). + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + """ + + top_p: float + """Use nucleus sampling. + + In nucleus sampling, we compute the cumulative distribution over all the options + for each subsequent token in decreasing probability order and cut it off once it + reaches a particular probability specified by `top_p`. You should either alter + `temperature` or `top_p`, but not both. + + Recommended for advanced use cases only. You usually only need to use + `temperature`. + """ + + +class Metadata(TypedDict, total=False): + user_id: Optional[str] + """An external identifier for the user who is associated with the request. + + This should be a uuid, hash value, or other opaque identifier. Anthropic may use + this id to help detect abuse. Do not include any identifying information such as + name, email address, or phone number. + """ + + +class MessageCreateParamsNonStreaming(MessageCreateParamsBase): + stream: Literal[False] + """Whether to incrementally stream the response using server-sent events. + + See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming) + for details. + """ + + +class MessageCreateParamsStreaming(MessageCreateParamsBase): + stream: Required[Literal[True]] + """Whether to incrementally stream the response using server-sent events. + + See [streaming](https://docs.anthropic.com/claude/reference/messages-streaming) + for details. + """ + + +MessageCreateParams = Union[MessageCreateParamsNonStreaming, MessageCreateParamsStreaming] diff --git a/src/anthropic/types/beta/tools/tool_param.py b/src/anthropic/types/beta/tools/tool_param.py new file mode 100644 index 00000000..19eeed52 --- /dev/null +++ b/src/anthropic/types/beta/tools/tool_param.py @@ -0,0 +1,34 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing import Optional +from typing_extensions import Literal, Required, TypedDict + +__all__ = ["ToolParam", "InputSchema"] + + +class InputSchema(TypedDict, total=False): + type: Required[Literal["object"]] + + properties: Optional[object] + + +class ToolParam(TypedDict, total=False): + input_schema: Required[InputSchema] + """[JSON schema](https://json-schema.org/) for this tool's input. + + This defines the shape of the `input` that your tool accepts and that the model + will produce. + """ + + name: Required[str] + + description: str + """Description of what this tool does. + + Tool descriptions should be as detailed as possible. The more information that + the model has about what the tool is and how to use it, the better it will + perform. You can use natural language descriptions to reinforce important + aspects of the tool input JSON schema. + """ diff --git a/src/anthropic/types/beta/tools/tool_result_block_param.py b/src/anthropic/types/beta/tools/tool_result_block_param.py new file mode 100644 index 00000000..b63f2bea --- /dev/null +++ b/src/anthropic/types/beta/tools/tool_result_block_param.py @@ -0,0 +1,20 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing import Iterable +from typing_extensions import Literal, Required, TypedDict + +from ...text_block_param import TextBlockParam + +__all__ = ["ToolResultBlockParam"] + + +class ToolResultBlockParam(TypedDict, total=False): + tool_use_id: Required[str] + + type: Required[Literal["tool_result"]] + + content: Iterable[TextBlockParam] + + is_error: bool diff --git a/src/anthropic/types/beta/tools/tool_use_block.py b/src/anthropic/types/beta/tools/tool_use_block.py new file mode 100644 index 00000000..7da68f56 --- /dev/null +++ b/src/anthropic/types/beta/tools/tool_use_block.py @@ -0,0 +1,17 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from typing_extensions import Literal + +from ...._models import BaseModel + +__all__ = ["ToolUseBlock"] + + +class ToolUseBlock(BaseModel): + id: str + + input: object + + name: str + + type: Literal["tool_use"] diff --git a/src/anthropic/types/beta/tools/tool_use_block_param.py b/src/anthropic/types/beta/tools/tool_use_block_param.py new file mode 100644 index 00000000..e0218476 --- /dev/null +++ b/src/anthropic/types/beta/tools/tool_use_block_param.py @@ -0,0 +1,17 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing_extensions import Literal, Required, TypedDict + +__all__ = ["ToolUseBlockParam"] + + +class ToolUseBlockParam(TypedDict, total=False): + id: Required[str] + + input: Required[object] + + name: Required[str] + + type: Required[Literal["tool_use"]] diff --git a/src/anthropic/types/beta/tools/tools_beta_content_block.py b/src/anthropic/types/beta/tools/tools_beta_content_block.py new file mode 100644 index 00000000..460b1cc3 --- /dev/null +++ b/src/anthropic/types/beta/tools/tools_beta_content_block.py @@ -0,0 +1,10 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from typing import Union + +from ...text_block import TextBlock +from .tool_use_block import ToolUseBlock + +__all__ = ["ToolsBetaContentBlock"] + +ToolsBetaContentBlock = Union[TextBlock, ToolUseBlock] diff --git a/src/anthropic/types/beta/tools/tools_beta_message.py b/src/anthropic/types/beta/tools/tools_beta_message.py new file mode 100644 index 00000000..ce9ec187 --- /dev/null +++ b/src/anthropic/types/beta/tools/tools_beta_message.py @@ -0,0 +1,104 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from typing import List, Optional +from typing_extensions import Literal + +from ...usage import Usage +from ...._models import BaseModel +from .tools_beta_content_block import ToolsBetaContentBlock + +__all__ = ["ToolsBetaMessage"] + + +class ToolsBetaMessage(BaseModel): + id: str + """Unique object identifier. + + The format and length of IDs may change over time. + """ + + content: List[ToolsBetaContentBlock] + """Content generated by the model. + + This is an array of content blocks, each of which has a `type` that determines + its shape. Currently, the only `type` in responses is `"text"`. + + Example: + + ```json + [{ "type": "text", "text": "Hi, I'm Claude." }] + ``` + + If the request input `messages` ended with an `assistant` turn, then the + response `content` will continue directly from that last turn. You can use this + to constrain the model's output. + + For example, if the input `messages` were: + + ```json + [ + { + "role": "user", + "content": "What's the Greek name for Sun? (A) Sol (B) Helios (C) Sun" + }, + { "role": "assistant", "content": "The best answer is (" } + ] + ``` + + Then the response `content` might be: + + ```json + [{ "type": "text", "text": "B)" }] + ``` + """ + + model: str + """The model that handled the request.""" + + role: Literal["assistant"] + """Conversational role of the generated message. + + This will always be `"assistant"`. + """ + + stop_reason: Optional[Literal["end_turn", "max_tokens", "stop_sequence", "tool_use"]] = None + """The reason that we stopped. + + This may be one the following values: + + - `"end_turn"`: the model reached a natural stopping point + - `"max_tokens"`: we exceeded the requested `max_tokens` or the model's maximum + - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated + - `"tool_use"`: (tools beta only) the model invoked one or more tools + + In non-streaming mode this value is always non-null. In streaming mode, it is + null in the `message_start` event and non-null otherwise. + """ + + stop_sequence: Optional[str] = None + """Which custom stop sequence was generated, if any. + + This value will be a non-null string if one of your custom stop sequences was + generated. + """ + + type: Literal["message"] + """Object type. + + For Messages, this is always `"message"`. + """ + + usage: Usage + """Billing and rate-limit usage. + + Anthropic's API bills and rate-limits by token counts, as tokens represent the + underlying cost to our systems. + + Under the hood, the API transforms requests into a format suitable for the + model. The model's output then goes through a parsing stage before becoming an + API response. As a result, the token counts in `usage` will not match one-to-one + with the exact visible content of an API request or response. + + For example, `output_tokens` will be non-zero, even for an empty string response + from Claude. + """ diff --git a/src/anthropic/types/beta/tools/tools_beta_message_param.py b/src/anthropic/types/beta/tools/tools_beta_message_param.py new file mode 100644 index 00000000..616dd78e --- /dev/null +++ b/src/anthropic/types/beta/tools/tools_beta_message_param.py @@ -0,0 +1,27 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +from typing import Union, Iterable +from typing_extensions import Literal, Required, TypedDict + +from ...text_block_param import TextBlockParam +from ...image_block_param import ImageBlockParam +from .tool_use_block_param import ToolUseBlockParam +from .tool_result_block_param import ToolResultBlockParam +from .tools_beta_content_block import ToolsBetaContentBlock + +__all__ = ["ToolsBetaMessageParam"] + + +class ToolsBetaMessageParam(TypedDict, total=False): + content: Required[ + Union[ + str, + Iterable[ + Union[TextBlockParam, ImageBlockParam, ToolUseBlockParam, ToolResultBlockParam, ToolsBetaContentBlock] + ], + ] + ] + + role: Required[Literal["user", "assistant"]] diff --git a/src/anthropic/types/message.py b/src/anthropic/types/message.py index 80c5f7d5..39b357b5 100644 --- a/src/anthropic/types/message.py +++ b/src/anthropic/types/message.py @@ -71,9 +71,6 @@ class Message(BaseModel): - `"max_tokens"`: we exceeded the requested `max_tokens` or the model's maximum - `"stop_sequence"`: one of your provided custom `stop_sequences` was generated - Note that these values are different than those in `/v1/complete`, where - `end_turn` and `stop_sequence` were not differentiated. - In non-streaming mode this value is always non-null. In streaming mode, it is null in the `message_start` event and non-null otherwise. """ diff --git a/tests/api_resources/beta/__init__.py b/tests/api_resources/beta/__init__.py new file mode 100644 index 00000000..fd8019a9 --- /dev/null +++ b/tests/api_resources/beta/__init__.py @@ -0,0 +1 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. diff --git a/tests/api_resources/beta/tools/__init__.py b/tests/api_resources/beta/tools/__init__.py new file mode 100644 index 00000000..fd8019a9 --- /dev/null +++ b/tests/api_resources/beta/tools/__init__.py @@ -0,0 +1 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. diff --git a/tests/api_resources/beta/tools/test_messages.py b/tests/api_resources/beta/tools/test_messages.py new file mode 100644 index 00000000..a053c3eb --- /dev/null +++ b/tests/api_resources/beta/tools/test_messages.py @@ -0,0 +1,530 @@ +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +from __future__ import annotations + +import os +from typing import Any, cast + +import pytest + +from anthropic import Anthropic, AsyncAnthropic +from tests.utils import assert_matches_type +from anthropic.types.beta.tools import ToolsBetaMessage + +base_url = os.environ.get("TEST_API_BASE_URL", "http://127.0.0.1:4010") + + +class TestMessages: + parametrize = pytest.mark.parametrize("client", [False, True], indirect=True, ids=["loose", "strict"]) + + @parametrize + def test_method_create_overload_1(self, client: Anthropic) -> None: + message = client.beta.tools.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + ) + assert_matches_type(ToolsBetaMessage, message, path=["response"]) + + @parametrize + def test_method_create_with_all_params_overload_1(self, client: Anthropic) -> None: + message = client.beta.tools.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + metadata={"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"}, + stop_sequences=["string", "string", "string"], + stream=False, + system="Today's date is 2024-01-01.", + temperature=1, + tools=[ + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + ], + top_k=5, + top_p=0.7, + ) + assert_matches_type(ToolsBetaMessage, message, path=["response"]) + + @parametrize + def test_raw_response_create_overload_1(self, client: Anthropic) -> None: + response = client.beta.tools.messages.with_raw_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + ) + + assert response.is_closed is True + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + message = response.parse() + assert_matches_type(ToolsBetaMessage, message, path=["response"]) + + @parametrize + def test_streaming_response_create_overload_1(self, client: Anthropic) -> None: + with client.beta.tools.messages.with_streaming_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + message = response.parse() + assert_matches_type(ToolsBetaMessage, message, path=["response"]) + + assert cast(Any, response.is_closed) is True + + @parametrize + def test_method_create_overload_2(self, client: Anthropic) -> None: + message_stream = client.beta.tools.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + stream=True, + ) + message_stream.response.close() + + @parametrize + def test_method_create_with_all_params_overload_2(self, client: Anthropic) -> None: + message_stream = client.beta.tools.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + stream=True, + metadata={"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"}, + stop_sequences=["string", "string", "string"], + system="Today's date is 2024-01-01.", + temperature=1, + tools=[ + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + ], + top_k=5, + top_p=0.7, + ) + message_stream.response.close() + + @parametrize + def test_raw_response_create_overload_2(self, client: Anthropic) -> None: + response = client.beta.tools.messages.with_raw_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + stream=True, + ) + + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + stream = response.parse() + stream.close() + + @parametrize + def test_streaming_response_create_overload_2(self, client: Anthropic) -> None: + with client.beta.tools.messages.with_streaming_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + stream=True, + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + stream = response.parse() + stream.close() + + assert cast(Any, response.is_closed) is True + + +class TestAsyncMessages: + parametrize = pytest.mark.parametrize("async_client", [False, True], indirect=True, ids=["loose", "strict"]) + + @parametrize + async def test_method_create_overload_1(self, async_client: AsyncAnthropic) -> None: + message = await async_client.beta.tools.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + ) + assert_matches_type(ToolsBetaMessage, message, path=["response"]) + + @parametrize + async def test_method_create_with_all_params_overload_1(self, async_client: AsyncAnthropic) -> None: + message = await async_client.beta.tools.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + metadata={"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"}, + stop_sequences=["string", "string", "string"], + stream=False, + system="Today's date is 2024-01-01.", + temperature=1, + tools=[ + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + ], + top_k=5, + top_p=0.7, + ) + assert_matches_type(ToolsBetaMessage, message, path=["response"]) + + @parametrize + async def test_raw_response_create_overload_1(self, async_client: AsyncAnthropic) -> None: + response = await async_client.beta.tools.messages.with_raw_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + ) + + assert response.is_closed is True + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + message = response.parse() + assert_matches_type(ToolsBetaMessage, message, path=["response"]) + + @parametrize + async def test_streaming_response_create_overload_1(self, async_client: AsyncAnthropic) -> None: + async with async_client.beta.tools.messages.with_streaming_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + message = await response.parse() + assert_matches_type(ToolsBetaMessage, message, path=["response"]) + + assert cast(Any, response.is_closed) is True + + @parametrize + async def test_method_create_overload_2(self, async_client: AsyncAnthropic) -> None: + message_stream = await async_client.beta.tools.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + stream=True, + ) + await message_stream.response.aclose() + + @parametrize + async def test_method_create_with_all_params_overload_2(self, async_client: AsyncAnthropic) -> None: + message_stream = await async_client.beta.tools.messages.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + stream=True, + metadata={"user_id": "13803d75-b4b5-4c3e-b2a2-6f21399b021b"}, + stop_sequences=["string", "string", "string"], + system="Today's date is 2024-01-01.", + temperature=1, + tools=[ + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + { + "description": "Get the current weather in a given location", + "name": "x", + "input_schema": { + "type": "object", + "properties": { + "location": { + "description": "The city and state, e.g. San Francisco, CA", + "type": "string", + }, + "unit": { + "description": "Unit for the output - one of (celsius, fahrenheit)", + "type": "string", + }, + }, + }, + }, + ], + top_k=5, + top_p=0.7, + ) + await message_stream.response.aclose() + + @parametrize + async def test_raw_response_create_overload_2(self, async_client: AsyncAnthropic) -> None: + response = await async_client.beta.tools.messages.with_raw_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + stream=True, + ) + + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + stream = response.parse() + await stream.close() + + @parametrize + async def test_streaming_response_create_overload_2(self, async_client: AsyncAnthropic) -> None: + async with async_client.beta.tools.messages.with_streaming_response.create( + max_tokens=1024, + messages=[ + { + "role": "user", + "content": "Hello, world", + } + ], + model="claude-3-opus-20240229", + stream=True, + ) as response: + assert not response.is_closed + assert response.http_request.headers.get("X-Stainless-Lang") == "python" + + stream = await response.parse() + await stream.close() + + assert cast(Any, response.is_closed) is True