-
Notifications
You must be signed in to change notification settings - Fork 177
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(tokens): incorrect caching of async tokenizer (#47)
The async tokenizer would crash if it was called multiple times in the same process
- Loading branch information
1 parent
31c7256
commit 089a3f4
Showing
2 changed files
with
47 additions
and
11 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,12 +1,32 @@ | ||
#!/usr/bin/env poetry run python | ||
|
||
from anthropic import Anthropic | ||
import asyncio | ||
|
||
client = Anthropic() | ||
from anthropic import Anthropic, AsyncAnthropic | ||
|
||
text = "hello world!" | ||
|
||
tokens = client.count_tokens(text) | ||
print(f"'{text}' is {tokens} tokens") | ||
def sync_tokens() -> None: | ||
client = Anthropic() | ||
|
||
assert tokens == 3 | ||
text = "hello world!" | ||
|
||
tokens = client.count_tokens(text) | ||
print(f"'{text}' is {tokens} tokens") | ||
|
||
assert tokens == 3 | ||
|
||
|
||
async def async_tokens() -> None: | ||
anthropic = AsyncAnthropic() | ||
|
||
text = "fist message" | ||
tokens = await anthropic.count_tokens(text) | ||
print(f"'{text}' is {tokens} tokens") | ||
|
||
text = "second message" | ||
tokens = await anthropic.count_tokens(text) | ||
print(f"'{text}' is {tokens} tokens") | ||
|
||
|
||
sync_tokens() | ||
asyncio.run(async_tokens()) |
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