Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: 0.8.1 #288

Merged
merged 8 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.8.0"
".": "0.8.1"
}
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## 0.8.1 (2023-12-22)

Full Changelog: [v0.8.0...v0.8.1](https://github.com/anthropics/anthropic-sdk-python/compare/v0.8.0...v0.8.1)

### Chores

* **internal:** add bin script ([#292](https://github.com/anthropics/anthropic-sdk-python/issues/292)) ([ba2953d](https://github.com/anthropics/anthropic-sdk-python/commit/ba2953dcaa8a8fcebaa7e8891304687c95b17499))
* **internal:** fix typos ([#287](https://github.com/anthropics/anthropic-sdk-python/issues/287)) ([4ffbcdf](https://github.com/anthropics/anthropic-sdk-python/commit/4ffbcdf1d3c8c2fbaf7152d207b24cdb0ea82ac9))
* **internal:** use ruff instead of black for formatting ([#294](https://github.com/anthropics/anthropic-sdk-python/issues/294)) ([1753887](https://github.com/anthropics/anthropic-sdk-python/commit/1753887a776f41bdc2d648329cfe6f20c91125e5))
* **package:** bump minimum typing-extensions to 4.7 ([#290](https://github.com/anthropics/anthropic-sdk-python/issues/290)) ([9ec5c57](https://github.com/anthropics/anthropic-sdk-python/commit/9ec5c57ba9a14a769d540e48755b05a1c190b45b))


### Documentation

* **messages:** improvements to helpers reference + typos ([#291](https://github.com/anthropics/anthropic-sdk-python/issues/291)) ([d18a895](https://github.com/anthropics/anthropic-sdk-python/commit/d18a895d380fc0c6610443486d73247b0cd97376))
* **readme:** remove old migration guide ([#289](https://github.com/anthropics/anthropic-sdk-python/issues/289)) ([eec4574](https://github.com/anthropics/anthropic-sdk-python/commit/eec4574f1f6668804c88bda67b901db10400fbc3))

## 0.8.0 (2023-12-19)

Full Changelog: [v0.7.8...v0.8.0](https://github.com/anthropics/anthropic-sdk-python/compare/v0.7.8...v0.8.0)
Expand Down
71 changes: 2 additions & 69 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,6 @@ and offers both synchronous and asynchronous clients powered by [httpx](https://

For the AWS Bedrock API, see [`anthropic-bedrock`](https://github.com/anthropics/anthropic-bedrock-python).

## Migration from v0.2.x and below

In `v0.3.0`, we introduced a fully rewritten SDK.

The new version uses separate sync and async clients, unified streaming, typed params and structured response objects, and resource-oriented methods:

**Sync before/after:**

```diff
- client = anthropic.Client(os.environ["ANTHROPIC_API_KEY"])
+ client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
# or, simply provide an ANTHROPIC_API_KEY environment variable:
+ client = anthropic.Anthropic()

- rsp = client.completion(**params)
- rsp["completion"]
+ rsp = client.completions.create(**params)
+ rsp.completion
```

**Async before/after:**

```diff
- client = anthropic.Client(os.environ["ANTHROPIC_API_KEY"])
+ client = anthropic.AsyncAnthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

- await client.acompletion(**params)
+ await client.completions.create(**params)
```

The `.completion_stream()` and `.acompletion_stream()` methods have been removed;
simply pass `stream=True`to `.completions.create()`.

Streaming responses are now incremental; the full text is not sent in each message,
as v0.3 sends the `Anthropic-Version: 2023-06-01` header.

<details>
<summary>Example streaming diff</summary>

```diff py
import anthropic

- client = anthropic.Client(os.environ["ANTHROPIC_API_KEY"])
+ client = anthropic.Anthropic()

# Streams are now incremental diffs of text
# rather than sending the whole message every time:
text = "
- stream = client.completion_stream(**params)
- for data in stream:
- diff = data["completion"].replace(text, "")
- text = data["completion"]
+ stream = client.completions.create(**params, stream=True)
+ for data in stream:
+ diff = data.completion # incremental text
+ text += data.completion
print(diff, end="")

print("Done. Final text is:")
print(text)
```

</details>

## Documentation

The API documentation can be found [here](https://docs.anthropic.com/claude/reference/).
Expand Down Expand Up @@ -195,11 +131,8 @@ async def main() -> None:
print(text, end="", flush=True)
print()

# you can still get the accumulated final message outside of
# the context manager, as long as the entire stream was consumed
# inside of the context manager
accumulated = await stream.get_final_message()
print(accumulated.model_dump_json(indent=2))
message = await stream.get_final_message()
print(message.model_dump_json(indent=2))

asyncio.run(main())
```
Expand Down
40 changes: 40 additions & 0 deletions bin/check-env-state.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Script that exits 1 if the current environment is not
in sync with the `requirements-dev.lock` file.
"""

from pathlib import Path

import importlib_metadata


def should_run_sync() -> bool:
dev_lock = Path(__file__).parent.parent.joinpath("requirements-dev.lock")

for line in dev_lock.read_text().splitlines():
if not line or line.startswith("#") or line.startswith("-e"):
continue

dep, lock_version = line.split("==")

try:
version = importlib_metadata.version(dep)

if lock_version != version:
print(f"mismatch for {dep} current={version} lock={lock_version}")
return True
except Exception:
print(f"could not import {dep}")
return True

return False


def main() -> None:
if should_run_sync():
exit(1)
else:
exit(0)


if __name__ == "__main__":
main()
130 changes: 23 additions & 107 deletions bin/blacken-docs.py → bin/ruffen-docs.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
# fork of https://github.com/asottile/blacken-docs implementing https://github.com/asottile/blacken-docs/issues/170
# fork of https://github.com/asottile/blacken-docs adapted for ruff
from __future__ import annotations

import re
import sys
import argparse
import textwrap
import contextlib
import subprocess
from typing import Match, Optional, Sequence, Generator, NamedTuple, cast

import black
from black.mode import TargetVersion
from black.const import DEFAULT_LINE_LENGTH

MD_RE = re.compile(
r"(?P<before>^(?P<indent> *)```\s*python\n)" r"(?P<code>.*?)" r"(?P<after>^(?P=indent)```\s*$)",
re.DOTALL | re.MULTILINE,
Expand All @@ -19,55 +17,12 @@
r"(?P<before>^(?P<indent> *)```\s*pycon\n)" r"(?P<code>.*?)" r"(?P<after>^(?P=indent)```.*$)",
re.DOTALL | re.MULTILINE,
)
RST_PY_LANGS = frozenset(("python", "py", "sage", "python3", "py3", "numpy"))
BLOCK_TYPES = "(code|code-block|sourcecode|ipython)"
DOCTEST_TYPES = "(testsetup|testcleanup|testcode)"
RST_RE = re.compile(
rf"(?P<before>"
rf"^(?P<indent> *)\.\. ("
rf"jupyter-execute::|"
rf"{BLOCK_TYPES}:: (?P<lang>\w+)|"
rf"{DOCTEST_TYPES}::.*"
rf")\n"
rf"((?P=indent) +:.*\n)*"
rf"\n*"
rf")"
rf"(?P<code>(^((?P=indent) +.*)?\n)+)",
re.MULTILINE,
)
RST_PYCON_RE = re.compile(
r"(?P<before>"
r"(?P<indent> *)\.\. ((code|code-block):: pycon|doctest::.*)\n"
r"((?P=indent) +:.*\n)*"
r"\n*"
r")"
r"(?P<code>(^((?P=indent) +.*)?(\n|$))+)",
re.MULTILINE,
)
PYCON_PREFIX = ">>> "
PYCON_CONTINUATION_PREFIX = "..."
PYCON_CONTINUATION_RE = re.compile(
rf"^{re.escape(PYCON_CONTINUATION_PREFIX)}( |$)",
)
LATEX_RE = re.compile(
r"(?P<before>^(?P<indent> *)\\begin{minted}{python}\n)"
r"(?P<code>.*?)"
r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
re.DOTALL | re.MULTILINE,
)
LATEX_PYCON_RE = re.compile(
r"(?P<before>^(?P<indent> *)\\begin{minted}{pycon}\n)" r"(?P<code>.*?)" r"(?P<after>^(?P=indent)\\end{minted}\s*$)",
re.DOTALL | re.MULTILINE,
)
PYTHONTEX_LANG = r"(?P<lang>pyblock|pycode|pyconsole|pyverbatim)"
PYTHONTEX_RE = re.compile(
rf"(?P<before>^(?P<indent> *)\\begin{{{PYTHONTEX_LANG}}}\n)"
rf"(?P<code>.*?)"
rf"(?P<after>^(?P=indent)\\end{{(?P=lang)}}\s*$)",
re.DOTALL | re.MULTILINE,
)
INDENT_RE = re.compile("^ +(?=[^ ])", re.MULTILINE)
TRAILING_NL_RE = re.compile(r"\n+\Z", re.MULTILINE)
DEFAULT_LINE_LENGTH = 100


class CodeBlockError(NamedTuple):
Expand All @@ -77,7 +32,6 @@ class CodeBlockError(NamedTuple):

def format_str(
src: str,
black_mode: black.FileMode,
) -> tuple[str, Sequence[CodeBlockError]]:
errors: list[CodeBlockError] = []

Expand All @@ -91,24 +45,10 @@ def _collect_error(match: Match[str]) -> Generator[None, None, None]:
def _md_match(match: Match[str]) -> str:
code = textwrap.dedent(match["code"])
with _collect_error(match):
code = black.format_str(code, mode=black_mode)
code = format_code_block(code)
code = textwrap.indent(code, match["indent"])
return f'{match["before"]}{code}{match["after"]}'

def _rst_match(match: Match[str]) -> str:
lang = match["lang"]
if lang is not None and lang not in RST_PY_LANGS:
return match[0]
min_indent = min(INDENT_RE.findall(match["code"]))
trailing_ws_match = TRAILING_NL_RE.search(match["code"])
assert trailing_ws_match
trailing_ws = trailing_ws_match.group()
code = textwrap.dedent(match["code"])
with _collect_error(match):
code = black.format_str(code, mode=black_mode)
code = textwrap.indent(code, min_indent)
return f'{match["before"]}{code.rstrip()}{trailing_ws}'

def _pycon_match(match: Match[str]) -> str:
code = ""
fragment = cast(Optional[str], None)
Expand All @@ -119,7 +59,7 @@ def finish_fragment() -> None:

if fragment is not None:
with _collect_error(match):
fragment = black.format_str(fragment, mode=black_mode)
fragment = format_code_block(fragment)
fragment_lines = fragment.splitlines()
code += f"{PYCON_PREFIX}{fragment_lines[0]}\n"
for line in fragment_lines[1:]:
Expand Down Expand Up @@ -159,42 +99,33 @@ def _md_pycon_match(match: Match[str]) -> str:
code = textwrap.indent(code, match["indent"])
return f'{match["before"]}{code}{match["after"]}'

def _rst_pycon_match(match: Match[str]) -> str:
code = _pycon_match(match)
min_indent = min(INDENT_RE.findall(match["code"]))
code = textwrap.indent(code, min_indent)
return f'{match["before"]}{code}'

def _latex_match(match: Match[str]) -> str:
code = textwrap.dedent(match["code"])
with _collect_error(match):
code = black.format_str(code, mode=black_mode)
code = textwrap.indent(code, match["indent"])
return f'{match["before"]}{code}{match["after"]}'

def _latex_pycon_match(match: Match[str]) -> str:
code = _pycon_match(match)
code = textwrap.indent(code, match["indent"])
return f'{match["before"]}{code}{match["after"]}'

src = MD_RE.sub(_md_match, src)
src = MD_PYCON_RE.sub(_md_pycon_match, src)
src = RST_RE.sub(_rst_match, src)
src = RST_PYCON_RE.sub(_rst_pycon_match, src)
src = LATEX_RE.sub(_latex_match, src)
src = LATEX_PYCON_RE.sub(_latex_pycon_match, src)
src = PYTHONTEX_RE.sub(_latex_match, src)
return src, errors


def format_code_block(code: str) -> str:
return subprocess.check_output(
[
sys.executable,
"-m",
"ruff",
"format",
"--stdin-filename=script.py",
f"--line-length={DEFAULT_LINE_LENGTH}",
],
encoding="utf-8",
input=code,
)


def format_file(
filename: str,
black_mode: black.FileMode,
skip_errors: bool,
) -> int:
with open(filename, encoding="UTF-8") as f:
contents = f.read()
new_contents, errors = format_str(contents, black_mode)
new_contents, errors = format_str(contents)
for error in errors:
lineno = contents[: error.offset].count("\n") + 1
print(f"{filename}:{lineno}: code block parse error {error.exc}")
Expand All @@ -217,15 +148,6 @@ def main(argv: Sequence[str] | None = None) -> int:
type=int,
default=DEFAULT_LINE_LENGTH,
)
parser.add_argument(
"-t",
"--target-version",
action="append",
type=lambda v: TargetVersion[v.upper()],
default=[],
help=f"choices: {[v.name.lower() for v in TargetVersion]}",
dest="target_versions",
)
parser.add_argument(
"-S",
"--skip-string-normalization",
Expand All @@ -235,15 +157,9 @@ def main(argv: Sequence[str] | None = None) -> int:
parser.add_argument("filenames", nargs="*")
args = parser.parse_args(argv)

black_mode = black.FileMode(
target_versions=set(args.target_versions),
line_length=args.line_length,
string_normalization=not args.skip_string_normalization,
)

retv = 0
for filename in args.filenames:
retv |= format_file(filename, black_mode, skip_errors=args.skip_errors)
retv |= format_file(filename, skip_errors=args.skip_errors)
return retv


Expand Down
3 changes: 1 addition & 2 deletions examples/messages_stream_handler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import asyncio
from typing_extensions import override

from anthropic import AsyncAnthropic
from anthropic import AsyncAnthropic, AsyncMessageStream
from anthropic.types.beta import MessageStreamEvent
from anthropic.lib.streaming import AsyncMessageStream

client = AsyncAnthropic()

Expand Down
Loading