-
Notifications
You must be signed in to change notification settings - Fork 34
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
Feat/mech support #160
Merged
Merged
Feat/mech support #160
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5e8662c
fix: add lock check to workflow
angrybayblade 49e985d
Merge pull request #156 from valory-xyz/fix/workflow
DavidMinarsch 5d4af76
feat: bump openai to `v1.11.0`
0xArdi aa348b1
chore: lock openai to `v1.11.0`
0xArdi be025c9
feat: update tools to use `[email protected]`
0xArdi 0f36b71
Merge pull request #159 from valory-xyz/feat/bump-openai
0xArdi aeaf235
Merge branch 'main' into test/benchmark
0xArdi 7bd7a63
feat: add support for cost callback
0xArdi ad59a93
chore: generators
0xArdi be165a2
chore: lint
0xArdi 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 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,58 @@ | ||
[MASTER] | ||
ignore-patterns=.*_pb2.py,contract_dispatcher.py,test_abci_messages.py,test_tendermint_messages.py | ||
ignore=packages/valory/protocols/abci,packages/valory/connections/abci/gogoproto | ||
|
||
[MESSAGES CONTROL] | ||
disable=C0103,R0801,C0301,C0201,C0204,C0209,W1203,C0302,R1735,R1729,W0511,E0611 | ||
|
||
# See here for more options: https://www.codeac.io/documentation/pylint-configuration.html | ||
R1735: use-dict-literal | ||
R1729: use-a-generator | ||
C0103: invalid-name | ||
C0201: consider-iterating-dictionary | ||
W1203: logging-fstring-interpolation | ||
C0204: bad-mcs-classmethod-argument | ||
C0209: consider-using-f-string | ||
C0301: http://pylint-messages.wikidot.com/messages:c0301 > Line too long | ||
C0302: http://pylint-messages.wikidot.com/messages:c0302 > Too many lines in module | ||
R0801: similar lines | ||
E0611: no-name-in-module | ||
|
||
[IMPORTS] | ||
ignored-modules=pandas,numpy,aea_cli_ipfs,compose,multidict,gql,anthropic,tiktoken | ||
|
||
[DESIGN] | ||
# min-public-methods=1 | ||
max-public-methods=58 | ||
# max-returns=10 | ||
# max-bool-expr=7 | ||
max-args=6 | ||
# max-locals=31 | ||
# max-statements=80 | ||
max-parents=10 | ||
max-branches=36 | ||
max-attributes=8 | ||
|
||
[REFACTORING] | ||
# max-nested-blocks=6 | ||
|
||
[SPELLING] | ||
# uncomment to enable | ||
# spelling-dict=en_US | ||
|
||
# List of comma separated words that should not be checked. | ||
spelling-ignore-words=nocover,pragma,params,noqa,kwargs,str,async,json,boolean,config,pytest,args,url,tx,jsonschema,traceback,api,nosec | ||
|
||
[SIMILARITIES] | ||
|
||
# Minimum lines number of a similarity. | ||
min-similarity-lines=10 | ||
|
||
# Ignore comments when computing similarities. | ||
ignore-comments=yes | ||
|
||
# Ignore docstrings when computing similarities. | ||
ignore-docstrings=yes | ||
|
||
# Ignore imports when computing similarities. | ||
ignore-imports=no |
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
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
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
105 changes: 105 additions & 0 deletions
105
packages/valory/skills/task_execution/utils/benchmarks.py
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,105 @@ | ||
# -*- coding: utf-8 -*- | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Copyright 2024 Valory AG | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
# ------------------------------------------------------------------------------ | ||
"""Benchmarking for tools.""" | ||
|
||
import logging | ||
from typing import Any, Dict, Union | ||
|
||
import anthropic | ||
import tiktoken | ||
from tiktoken import Encoding | ||
|
||
|
||
PRICE_NUM_TOKENS = 1000 | ||
|
||
|
||
def encoding_for_model(model: str) -> Encoding: | ||
"""Get the encoding for a model.""" | ||
return tiktoken.encoding_for_model(model) | ||
|
||
|
||
def count_tokens(text: str, model: str) -> int: | ||
"""Count the number of tokens in a text.""" | ||
if "claude" in model: | ||
return anthropic.Anthropic().count_tokens(text) | ||
Comment on lines
+39
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The benchmark utils needs to be agnostic to this |
||
|
||
enc = encoding_for_model(model) | ||
return len(enc.encode(text)) | ||
|
||
|
||
class TokenCounterCallback: | ||
"""Callback to count the number of tokens used in a generation.""" | ||
|
||
TOKEN_PRICES = { | ||
"gpt-3.5-turbo": {"input": 0.001, "output": 0.002}, | ||
"gpt-4": {"input": 0.03, "output": 0.06}, | ||
"gpt-4-turbo": {"input": 0.01, "output": 0.03}, | ||
"claude-2": {"input": 0.008, "output": 0.024}, | ||
} | ||
|
||
def __init__(self) -> None: | ||
"""Initialize the callback.""" | ||
self.cost_dict: Dict[str, Union[int, float]] = { | ||
"input_tokens": 0, | ||
"output_tokens": 0, | ||
"total_tokens": 0, | ||
"input_cost": 0, | ||
"output_cost": 0, | ||
"total_cost": 0, | ||
} | ||
|
||
@staticmethod | ||
def token_to_cost(tokens: int, model: str, tokens_type: str) -> float: | ||
"""Converts a number of tokens to a cost in dollars.""" | ||
return ( | ||
tokens | ||
/ PRICE_NUM_TOKENS | ||
* TokenCounterCallback.TOKEN_PRICES[model][tokens_type] | ||
) | ||
|
||
def calculate_cost(self, tokens_type: str, model: str, **kwargs: Any) -> None: | ||
"""Calculate the cost of a generation.""" | ||
# Check if it its prompt or tokens are passed in | ||
prompt_key = f"{tokens_type}_prompt" | ||
token_key = f"{tokens_type}_tokens" | ||
if prompt_key in kwargs: | ||
tokens = count_tokens(kwargs[prompt_key], model) | ||
elif token_key in kwargs: | ||
tokens = kwargs[token_key] | ||
else: | ||
logging.warning(f"No {token_key}_tokens or {tokens_type}_prompt found.") | ||
cost = self.token_to_cost(tokens, model, tokens_type) | ||
self.cost_dict[token_key] += tokens | ||
self.cost_dict[f"{tokens_type}_cost"] += cost | ||
|
||
def __call__(self, model: str, **kwargs: Any) -> None: | ||
"""Callback to count the number of tokens used in a generation.""" | ||
if model not in list(TokenCounterCallback.TOKEN_PRICES.keys()): | ||
raise ValueError(f"Model {model} not supported.") | ||
try: | ||
self.calculate_cost("input", model, **kwargs) | ||
self.calculate_cost("output", model, **kwargs) | ||
self.cost_dict["total_tokens"] = ( | ||
self.cost_dict["input_tokens"] + self.cost_dict["output_tokens"] | ||
) | ||
self.cost_dict["total_cost"] = ( | ||
self.cost_dict["input_cost"] + self.cost_dict["output_cost"] | ||
) | ||
except Exception as e: | ||
logging.error(f"Error in TokenCounterCallback: {e}") |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why? That seems strange to include here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its copied from here https://github.com/valory-xyz/olas-predict-benchmark/blob/main/benchmark/benchmark/utils.py