-
Notifications
You must be signed in to change notification settings - Fork 2
Add AI wrapping for openai #397
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
Merged
Changes from 17 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ce26856
Add new "ai_op"
bitterpanda63 4d715a7
Update README.md to include section on AI SDKs
bitterpanda63 ca8c9c2
Add empty openai module
bitterpanda63 dac9412
Import openai as a sink
bitterpanda63 797dd65
Create new AIStatistics object with test cases
bitterpanda63 2b5592b
Move ai_statistics to simple storage
bitterpanda63 83ef070
Add AIStatistics to thread_cache
bitterpanda63 1ab7651
is_empty -> empty
bitterpanda63 4133f23
add ai stats to heartbeatr
bitterpanda63 10befa2
Add a global on_ai_call to ai_statisitcs.py
bitterpanda63 4120471
Sync AI stats and create a new merging function to facilitate
bitterpanda63 a683354
Add openai sink code
bitterpanda63 dc8816d
move on_ai_call global to helper function file
bitterpanda63 f10d86b
Add openai as a dev dependency
bitterpanda63 11859f8
Create new flask-openai sample app
bitterpanda63 d8a6383
Install openai and re-lock
bitterpanda63 07536c8
Add openai to flask-openai
bitterpanda63 1344621
Fix .get not existing on Response - openai wrapper
bitterpanda63 18aa06f
Update test cases for thread_cache_test and sink tests
bitterpanda63 177960b
Fix bug with import_list and add unit tests to test
bitterpanda63 b64e717
Delete unused sink test case
bitterpanda63 d3e8d17
Remove mentions of dogs in flask-openai readme
bitterpanda63 7b0877d
Add ^1.0 limit for openai
bitterpanda63 3e59acf
Add support for chat completions
bitterpanda63 fa9dcbb
Add extra wrapping to openai.py
bitterpanda63 0f5aa2f
Update sample app to include more example routes for openai
bitterpanda63 8eac181
Cleanup flask-openai app
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
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,7 @@ | ||
from aikido_zen.thread.thread_cache import get_cache | ||
|
||
|
||
def on_ai_call(provider, model, input_tokens, output_tokens): | ||
cache = get_cache() | ||
if cache: | ||
cache.ai_stats.on_ai_call(provider, model, input_tokens, output_tokens) |
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,25 @@ | ||
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 _create(func, instance, args, kwargs, return_value): | ||
op = "openai.resources.responses.responses.Responses.create" | ||
register_call(op, "ai_op") | ||
|
||
on_ai_call( | ||
provider="openai", | ||
model=return_value.get("model", ""), | ||
input_tokens=return_value.usage.input_tokens, | ||
output_tokens=return_value.usage.output_tokens, | ||
) | ||
|
||
|
||
@on_import("openai.resources.responses.responses") | ||
def patch(m): | ||
""" | ||
patching module openai | ||
- patches function create(...) on Responses class, to inspect response | ||
""" | ||
patch_function(m, "Responses.create", _create) | ||
bitterpanda63 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,56 @@ | ||
import copy | ||
|
||
|
||
class AIStatistics: | ||
def __init__(self): | ||
self.calls = {} | ||
|
||
def ensure_provider_stats(self, provider, model): | ||
key = get_provider_key(provider, model) | ||
|
||
if key not in self.calls: | ||
self.calls[key] = { | ||
"provider": provider, | ||
"model": model, | ||
"calls": 0, | ||
"tokens": { | ||
"input": 0, | ||
"output": 0, | ||
"total": 0, | ||
}, | ||
} | ||
|
||
return self.calls[key] | ||
|
||
def on_ai_call(self, provider, model, input_tokens, output_tokens): | ||
if not provider or not model: | ||
return | ||
|
||
provider_stats = self.ensure_provider_stats(provider, model) | ||
provider_stats["calls"] += 1 | ||
provider_stats["tokens"]["input"] += input_tokens | ||
provider_stats["tokens"]["output"] += output_tokens | ||
provider_stats["tokens"]["total"] += input_tokens + output_tokens | ||
|
||
def get_stats(self): | ||
return [copy.deepcopy(stats) for stats in self.calls.values()] | ||
|
||
def import_list(self, ai_stats_list): | ||
for new_entry in ai_stats_list: | ||
existing_entry = self.ensure_provider_stats( | ||
new_entry["provider"], new_entry["model"] | ||
) | ||
existing_entry["calls"] += new_entry["calls"] | ||
existing_entry["tokens"]["input"] = new_entry["tokens"]["input"] | ||
existing_entry["tokens"]["output"] = new_entry["tokens"]["output"] | ||
existing_entry["tokens"]["total"] = new_entry["tokens"]["total"] | ||
|
||
def clear(self): | ||
self.calls.clear() | ||
|
||
def empty(self): | ||
return len(self.calls) == 0 | ||
|
||
|
||
def get_provider_key(provider, model): | ||
return f"{provider}:{model}" |
Oops, something went wrong.
Oops, something went wrong.
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.