From be165a20ec5b8ebf7030c5e7ac6de962a7ee173e Mon Sep 17 00:00:00 2001 From: Ardian Date: Tue, 6 Feb 2024 20:57:32 +0100 Subject: [PATCH] chore: lint --- .pylintrc | 58 +++++++++++++++++++ packages/packages.json | 6 +- packages/valory/agents/mech/aea-config.yaml | 2 +- packages/valory/services/mech/service.yaml | 2 +- .../skills/task_execution/behaviours.py | 7 ++- .../valory/skills/task_execution/skill.yaml | 4 +- .../skills/task_execution/utils/benchmarks.py | 32 +++++++--- tox.ini | 6 ++ 8 files changed, 100 insertions(+), 17 deletions(-) create mode 100644 .pylintrc diff --git a/.pylintrc b/.pylintrc new file mode 100644 index 00000000..1cd3079c --- /dev/null +++ b/.pylintrc @@ -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 \ No newline at end of file diff --git a/packages/packages.json b/packages/packages.json index 83f65d3c..a95ae6bf 100644 --- a/packages/packages.json +++ b/packages/packages.json @@ -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", diff --git a/packages/valory/agents/mech/aea-config.yaml b/packages/valory/agents/mech/aea-config.yaml index cedc6eb1..4d83fc06 100644 --- a/packages/valory/agents/mech/aea-config.yaml +++ b/packages/valory/agents/mech/aea-config.yaml @@ -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 diff --git a/packages/valory/services/mech/service.yaml b/packages/valory/services/mech/service.yaml index e3adaa07..4d98d254 100644 --- a/packages/valory/services/mech/service.yaml +++ b/packages/valory/services/mech/service.yaml @@ -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: diff --git a/packages/valory/skills/task_execution/behaviours.py b/packages/valory/skills/task_execution/behaviours.py index 82d5fabe..38beb317 100644 --- a/packages/valory/skills/task_execution/behaviours.py +++ b/packages/valory/skills/task_execution/behaviours.py @@ -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}") diff --git a/packages/valory/skills/task_execution/skill.yaml b/packages/valory/skills/task_execution/skill.yaml index 043a1a73..73b2439b 100644 --- a/packages/valory/skills/task_execution/skill.yaml +++ b/packages/valory/skills/task_execution/skill.yaml @@ -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: [] diff --git a/packages/valory/skills/task_execution/utils/benchmarks.py b/packages/valory/skills/task_execution/utils/benchmarks.py index f4d3a31d..8793c5c8 100644 --- a/packages/valory/skills/task_execution/utils/benchmarks.py +++ b/packages/valory/skills/task_execution/utils/benchmarks.py @@ -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) @@ -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" @@ -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}") - diff --git a/tox.ini b/tox.ini index 80437ca9..25718a95 100644 --- a/tox.ini +++ b/tox.ini @@ -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