-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For more information see https://anthropic.com/news/message-batches-api
- Loading branch information
1 parent
59f2088
commit cd1ffcb
Showing
73 changed files
with
6,311 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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-286f00929e2a4d28d991e6a7e660fa801dca7ec91d8ecb2fc17654bb8173eb0d.yml | ||
configured_endpoints: 9 | ||
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/anthropic-aedee7570aa925baba404fc5bd3c8c1fffe8845517e492751db9b175c5cae9da.yml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import sys | ||
import time | ||
|
||
import rich | ||
|
||
from anthropic import Anthropic | ||
|
||
client = Anthropic() | ||
|
||
try: | ||
batch_id = sys.argv[1] | ||
except IndexError as exc: | ||
raise RuntimeError("must specify a message batch ID, `python examples/batch_results.py msgbatch_123`") from exc | ||
|
||
s = time.monotonic() | ||
|
||
result_stream = client.beta.messages.batches.results(batch_id) | ||
for result in result_stream: | ||
rich.print(result) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
from __future__ import annotations | ||
|
||
import json | ||
from typing_extensions import Generic, TypeVar, Iterator, AsyncIterator | ||
|
||
import httpx | ||
|
||
from .._models import construct_type_unchecked | ||
|
||
_T = TypeVar("_T") | ||
|
||
|
||
class JSONLDecoder(Generic[_T]): | ||
"""A decoder for [JSON Lines](https://jsonlines.org) format. | ||
This class provides an iterator over a byte-iterator that parses each JSON Line | ||
into a given type. | ||
""" | ||
|
||
http_response: httpx.Response | None | ||
"""The HTTP response this decoder was constructed from""" | ||
|
||
def __init__( | ||
self, *, raw_iterator: Iterator[bytes], line_type: type[_T], http_response: httpx.Response | None | ||
) -> None: | ||
super().__init__() | ||
self.http_response = http_response | ||
self._raw_iterator = raw_iterator | ||
self._line_type = line_type | ||
self._iterator = self.__decode__() | ||
|
||
def __decode__(self) -> Iterator[_T]: | ||
buf = b"" | ||
for chunk in self._raw_iterator: | ||
for line in chunk.splitlines(keepends=True): | ||
buf += line | ||
if buf.endswith((b"\r", b"\n", b"\r\n")): | ||
yield construct_type_unchecked( | ||
value=json.loads(buf), | ||
type_=self._line_type, | ||
) | ||
buf = b"" | ||
|
||
# flush | ||
if buf: | ||
yield construct_type_unchecked( | ||
value=json.loads(buf), | ||
type_=self._line_type, | ||
) | ||
|
||
def __next__(self) -> _T: | ||
return self._iterator.__next__() | ||
|
||
def __iter__(self) -> Iterator[_T]: | ||
for item in self._iterator: | ||
yield item | ||
|
||
|
||
class AsyncJSONLDecoder(Generic[_T]): | ||
"""A decoder for [JSON Lines](https://jsonlines.org) format. | ||
This class provides an async iterator over a byte-iterator that parses each JSON Line | ||
into a given type. | ||
""" | ||
|
||
http_response: httpx.Response | None | ||
|
||
def __init__( | ||
self, *, raw_iterator: AsyncIterator[bytes], line_type: type[_T], http_response: httpx.Response | None | ||
) -> None: | ||
super().__init__() | ||
self.http_response = http_response | ||
self._raw_iterator = raw_iterator | ||
self._line_type = line_type | ||
self._iterator = self.__decode__() | ||
|
||
async def __decode__(self) -> AsyncIterator[_T]: | ||
buf = b"" | ||
async for chunk in self._raw_iterator: | ||
for line in chunk.splitlines(keepends=True): | ||
buf += line | ||
if buf.endswith((b"\r", b"\n", b"\r\n")): | ||
yield construct_type_unchecked( | ||
value=json.loads(buf), | ||
type_=self._line_type, | ||
) | ||
buf = b"" | ||
|
||
# flush | ||
if buf: | ||
yield construct_type_unchecked( | ||
value=json.loads(buf), | ||
type_=self._line_type, | ||
) | ||
|
||
async def __anext__(self) -> _T: | ||
return await self._iterator.__anext__() | ||
|
||
async def __aiter__(self) -> AsyncIterator[_T]: | ||
async for item in self._iterator: | ||
yield item |
Oops, something went wrong.