Skip to content

Commit

Permalink
feat(api): add tool_choice param, image block params inside `tool_r…
Browse files Browse the repository at this point in the history
…esult.content`, and streaming for `tool_use` blocks (#502)
  • Loading branch information
stainless-bot committed May 16, 2024
1 parent 5d896a2 commit e0c78af
Show file tree
Hide file tree
Showing 18 changed files with 1,192 additions and 258 deletions.
2 changes: 1 addition & 1 deletion .stats.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
configured_endpoints: 3
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-4742de59ec06077403336bc26e26390e57888e5eef313bf27eab241dbb905f06.yml
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-0017013a270564e5cdfb7b8ffe474c962f4b806c862cbcc33c905504897fabbe.yml
5 changes: 5 additions & 0 deletions api.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,21 @@ Types:

```python
from anthropic.types.beta.tools import (
InputJsonDelta,
Tool,
ToolResultBlockParam,
ToolUseBlock,
ToolUseBlockParam,
ToolsBetaContentBlock,
ToolsBetaContentBlockDeltaEvent,
ToolsBetaContentBlockStartEvent,
ToolsBetaMessage,
ToolsBetaMessageParam,
ToolsBetaMessageStreamEvent,
)
```

Methods:

- <code title="post /v1/messages?beta=tools">client.beta.tools.messages.<a href="./src/anthropic/resources/beta/tools/messages.py">create</a>(\*\*<a href="src/anthropic/types/beta/tools/message_create_params.py">params</a>) -> <a href="./src/anthropic/types/beta/tools/tools_beta_message.py">ToolsBetaMessage</a></code>
- <code>client.beta.tools.messages.<a href="./src/anthropic/resources/beta/tools/messages.py">stream</a>(\*args) -> ToolsBetaMessageStreamManager[ToolsBetaMessageStream] | ToolsBetaMessageStreamManager[ToolsBetaMessageStreamT]</code>
48 changes: 48 additions & 0 deletions examples/tools_stream.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import asyncio
from typing_extensions import override

from anthropic import AsyncAnthropic
from anthropic.lib.streaming.beta import AsyncToolsBetaMessageStream

client = AsyncAnthropic()


class MyHandler(AsyncToolsBetaMessageStream):
@override
async def on_input_json(self, delta: str, snapshot: object) -> None:
print(f"delta: {repr(delta)}")
print(f"snapshot: {snapshot}")
print()


async def main() -> None:
async with client.beta.tools.messages.stream(
max_tokens=1024,
model="claude-3-haiku-20240307",
tools=[
{
"name": "get_weather",
"description": "Get the weather at a specific location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "Unit for the output",
},
},
"required": ["location"],
},
}
],
messages=[{"role": "user", "content": "What is the weather in SF?"}],
event_handler=MyHandler,
) as stream:
await stream.until_done()

print()


asyncio.run(main())
8 changes: 8 additions & 0 deletions src/anthropic/lib/streaming/beta/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from ._tools import (
ToolsBetaMessageStream as ToolsBetaMessageStream,
ToolsBetaMessageStreamT as ToolsBetaMessageStreamT,
AsyncToolsBetaMessageStream as AsyncToolsBetaMessageStream,
AsyncToolsBetaMessageStreamT as AsyncToolsBetaMessageStreamT,
ToolsBetaMessageStreamManager as ToolsBetaMessageStreamManager,
AsyncToolsBetaMessageStreamManager as AsyncToolsBetaMessageStreamManager,
)
Loading

0 comments on commit e0c78af

Please sign in to comment.