Skip to content

Commit

Permalink
chore: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
0xArdi committed Feb 6, 2024
1 parent ad59a93 commit be165a2
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 17 deletions.
58 changes: 58 additions & 0 deletions .pylintrc
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
6 changes: 3 additions & 3 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
"dev": {
"connection/valory/websocket_client/0.1.0": "bafybeiflmystocxaqblhpzqlcop2vkhsknpzjx2jomohomaxamwskeokzm",
"skill/valory/contract_subscription/0.1.0": "bafybeicyugrkx5glat4p4ezwf6i7oduh26eycfie6ftd4uxrknztzl3ik4",
"agent/valory/mech/0.1.0": "bafybeifvlviooupr5g7hcn4gwdjmgls24zpk3uwmduq2nfnsuqmm2f5ndq",
"agent/valory/mech/0.1.0": "bafybeiepdstu23qid2pcrugkpwtibh7jhlshmnmclwwbgjmefio27cp4im",
"skill/valory/mech_abci/0.1.0": "bafybeieimp7xzxcnbzsuunf2xkcy5juulhmzsmkq2v3sw3o3lgssb53cnu",
"contract/valory/agent_mech/0.1.0": "bafybeiepxumywg6z2zapqzc3bg3iey23cmlgjzxisqox5j74o5i2texr5e",
"service/valory/mech/0.1.0": "bafybeiaqwakcncrsflwx65jvzje3vhx6udfmtkzuxkgth2egi2llvfwmd4",
"service/valory/mech/0.1.0": "bafybeifzkrmgejdce5nvp7s63dpvthde6wn6etolesh4dmf5pno7jplzcy",
"protocol/valory/acn_data_share/0.1.0": "bafybeih5ydonnvrwvy2ygfqgfabkr47s4yw3uqxztmwyfprulwfsoe7ipq",
"skill/valory/task_submission_abci/0.1.0": "bafybeib4m2bwgchloqss3wotsx4rz7qqkwydaesiqkls2zq7zbtp6jtpsi",
"skill/valory/task_execution/0.1.0": "bafybeifmy2lat3otl6yf2c7uxlhbwxl25ymzciyuifc7xwx4ce2erycfe4",
"skill/valory/task_execution/0.1.0": "bafybeicthrgfdv6q56htrsradow445smojjk2zqqizm4cdxyfxfor22vyy",
"contract/valory/agent_registry/0.1.0": "bafybeiargayav6yiztdnwzejoejstcx4idssch2h4f5arlgtzj3tgsgfmu",
"protocol/valory/websocket_client/0.1.0": "bafybeih43mnztdv3v2hetr2k3gezg7d3yj4ur7cxdvcyaqhg65e52s5sf4",
"skill/valory/websocket_client/0.1.0": "bafybeidwntmkk4b2ixq5454ycbkknclqx7a6vpn7aqpm2nw3duszqrxvta",
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/agents/mech/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ skills:
- valory/registration_abci:0.1.0:bafybeic2ynseiak7jpta7jfwuqwyp453b4p7lolr4wihxmpn633uekv5am
- valory/reset_pause_abci:0.1.0:bafybeidzajbe3erygeh2xbd6lrjv7nsptznjuzrt24ykgvhgotdeyhfnba
- valory/subscription_abci:0.1.0:bafybeigaxq7m2dqv2huhg5jvb4jx3rysqwvvjj2xhojow3t3zzuwq2k4ie
- valory/task_execution:0.1.0:bafybeifmy2lat3otl6yf2c7uxlhbwxl25ymzciyuifc7xwx4ce2erycfe4
- valory/task_execution:0.1.0:bafybeicthrgfdv6q56htrsradow445smojjk2zqqizm4cdxyfxfor22vyy
- valory/task_submission_abci:0.1.0:bafybeib4m2bwgchloqss3wotsx4rz7qqkwydaesiqkls2zq7zbtp6jtpsi
- valory/termination_abci:0.1.0:bafybeie4zvjfxvdu7qrulmur3chpjz3kpj5m4bjsxvpk4gvj5zbyyayfaa
- valory/transaction_settlement_abci:0.1.0:bafybeiaefgqbs7zsn5xe5kdwrujj7ivygkn3ujpw6crnvi3knvxw75qmja
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/services/mech/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeif7ia4jdlazy6745ke2k2x5yoqlwsgwr6sbztbgqtwvs3ndm2p7ba
fingerprint_ignore_patterns: []
agent: valory/mech:0.1.0:bafybeifvlviooupr5g7hcn4gwdjmgls24zpk3uwmduq2nfnsuqmm2f5ndq
agent: valory/mech:0.1.0:bafybeiepdstu23qid2pcrugkpwtibh7jhlshmnmclwwbgjmefio27cp4im
number_of_agents: 4
deployment:
agent:
Expand Down
7 changes: 6 additions & 1 deletion packages/valory/skills/task_execution/behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,12 @@ def _handle_done_task(self, task_result: Any) -> None:
cost_dict = {}
if counter_callback is not None:
cost_dict = cast(TokenCounterCallback, counter_callback).cost_dict
response = {**response, "result": deliver_msg, "prompt": prompt, "cost_dict": cost_dict}
response = {
**response,
"result": deliver_msg,
"prompt": prompt,
"cost_dict": cost_dict,
}
self._done_task["transaction"] = transaction

self.context.logger.info(f"Task result for request {req_id}: {task_result}")
Expand Down
4 changes: 2 additions & 2 deletions packages/valory/skills/task_execution/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidqhvvlnthkbnmrdkdeyjyx2f2ab6z4xdgmagh7welqnh2v6wczx4
behaviours.py: bafybeigp5kzbug44juyq2vv5ofo2dpwqvzsncvydebyreajy7drmf2s2ka
behaviours.py: bafybeihprwot27csugwpoimsqcwprxu5bstqpvanot3nkmfgvhbr66ww4y
dialogues.py: bafybeid4zxalqdlo5mw4yfbuf34hx4jp5ay5z6chm4zviwu4cj7fudtwca
handlers.py: bafybeidbt5ezj74cgfogk3w4uw4si2grlnk5g54veyumw7g5yh6gdscywu
models.py: bafybeihgclxctyltuehj2f4fzj26edptqugrrm4phd6ovuulezrqot6qo4
utils/__init__.py: bafybeiccdijaigu6e5p2iruwo5mkk224o7ywedc7nr6xeu5fpmhjqgk24e
utils/benchmarks.py: bafybeiauwamdldntwrxcyssycimwphcbrvpefhkzprl3txta2ymi4j2fve
utils/benchmarks.py: bafybeihdutp44ds4cupszbd34gsmcw6fsdda2tzkh5b27fpg65ejbpdvdm
utils/ipfs.py: bafybeidinbdqkidix44ibz5hug7inkcbijooag53gr5mtbaa72tk335uqq
utils/task.py: bafybeieuziu7owtk543z3umgmayhjh67klftk7vrhz24l6rlaii5lvkqh4
fingerprint_ignore_patterns: []
Expand Down
32 changes: 23 additions & 9 deletions packages/valory/skills/task_execution/utils/benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,26 @@
# limitations under the License.
#
# ------------------------------------------------------------------------------
"""Benchmarking for tools."""

import logging
import tiktoken
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)

Expand All @@ -49,21 +55,26 @@ class TokenCounterCallback:

def __init__(self) -> None:
"""Initialize the callback."""
self.cost_dict = {
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
"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]
return (
tokens
/ PRICE_NUM_TOKENS
* TokenCounterCallback.TOKEN_PRICES[model][tokens_type]
)

def calculate_cost(self, tokens_type: str, model: str, **kwargs) -> None:
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"
Expand All @@ -77,15 +88,18 @@ def calculate_cost(self, tokens_type: str, model: str, **kwargs) -> None:
self.cost_dict[token_key] += tokens
self.cost_dict[f"{tokens_type}_cost"] += cost

def __call__(self, model: str, **kwargs) -> None:
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"]
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}")

6 changes: 6 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ exclude=(.*_pb2|.*custom_types)
[mypy-aea.*]
ignore_missing_imports = True

[mypy-anthropic.*]
ignore_missing_imports = True

[mypy-tiktoken.*]
ignore_missing_imports = True

[mypy-packages.open_aea.*]
ignore_errors=True

Expand Down

0 comments on commit be165a2

Please sign in to comment.