-
Notifications
You must be signed in to change notification settings - Fork 2
Add anthropic AI Usage #404
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
Merged
+96
−1
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a4a8082
Create new anthropic sink file
bitterpanda63 49edeea
Add start of anthropic sink
bitterpanda63 3a92849
Add anthropic to README
bitterpanda63 5381c65
Import anthropic sink in protect()
bitterpanda63 ae3f349
Install anthropic
bitterpanda63 7c9f0f2
lint on sinks file
bitterpanda63 c8da916
Add test cases for anthropic
bitterpanda63 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,21 @@ | ||
from aikido_zen.helpers.on_ai_call import on_ai_call | ||
from aikido_zen.helpers.register_call import register_call | ||
from aikido_zen.sinks import on_import, patch_function, after | ||
|
||
|
||
@after | ||
def _messages_create(func, instance, args, kwargs, return_value): | ||
op = f"anthropic.resources.messages.messages.Messages.create" | ||
register_call(op, "ai_op") | ||
|
||
on_ai_call( | ||
provider="anthropic", | ||
model=return_value.model, | ||
input_tokens=return_value.usage.input_tokens, | ||
output_tokens=return_value.usage.output_tokens, | ||
) | ||
|
||
|
||
@on_import("anthropic.resources.messages") | ||
def patch(m): | ||
patch_function(m, "messages.Messages.create", _messages_create) |
This file contains hidden or 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,46 @@ | ||
import os | ||
|
||
import pytest | ||
import aikido_zen.sinks.anthropic | ||
import anthropic | ||
|
||
from aikido_zen.thread.thread_cache import get_cache | ||
|
||
skip_no_api_key = pytest.mark.skipif( | ||
"ANTHROPIC_API_KEY" not in os.environ, | ||
reason="ANTHROPIC_API_KEY environment variable not set", | ||
) | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def setup(): | ||
get_cache().reset() | ||
yield | ||
get_cache().reset() | ||
|
||
|
||
def get_ai_stats(): | ||
return get_cache().ai_stats.get_stats() | ||
|
||
|
||
@skip_no_api_key | ||
def test_anthropic_messages_create(): | ||
client = anthropic.Anthropic() | ||
response = client.messages.create( | ||
model="claude-3-opus-20240229", | ||
max_tokens=20, | ||
messages=[ | ||
{ | ||
"role": "user", | ||
"content": "Write the longest response possible, just as I am writing a long content", | ||
} | ||
], | ||
) | ||
print(response) | ||
|
||
assert get_ai_stats()[0]["model"] == "claude-3-opus-20240229" | ||
assert get_ai_stats()[0]["calls"] == 1 | ||
assert get_ai_stats()[0]["provider"] == "anthropic" | ||
assert get_ai_stats()[0]["tokens"]["input"] == 21 | ||
assert get_ai_stats()[0]["tokens"]["output"] == 20 | ||
assert get_ai_stats()[0]["tokens"]["total"] == 41 | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.