diff --git a/.gitleaksignore b/.gitleaksignore index 1c42ce39..eb484528 100644 --- a/.gitleaksignore +++ b/.gitleaksignore @@ -20,4 +20,8 @@ a8e306437319e480ae967d24c27b7e0265a853c8:packages/valory/skills/registration_abc a8e306437319e480ae967d24c27b7e0265a853c8:packages/valory/skills/termination_abci/skill.yaml:generic-api-key:84 a8e306437319e480ae967d24c27b7e0265a853c8:packages/valory/skills/transaction_settlement_abci/skill.yaml:generic-api-key:92 ed97b16cdd270e228ecd3d837da4484c09357108:packages/valory/skills/reset_pause_abci/skill.yaml:generic-api-key:80 -0c8c92a4a5e3b94e0f8b8aadb7c104c644f23277:packages/valory/skills/subscription_abci/skill.yaml:generic-api-key:89 \ No newline at end of file +0c8c92a4a5e3b94e0f8b8aadb7c104c644f23277:packages/valory/skills/subscription_abci/skill.yaml:generic-api-key:89 +f92a447c4f4ecbfd32c1096f122bf6a0f555e22a:packages/valory/protocols/websocket_client/protocol.yaml:generic-api-key:15 +f92a447c4f4ecbfd32c1096f122bf6a0f555e22a:packages/valory/protocols/websocket_client/protocol.yaml:generic-api-key:16 +76e101cc83c5ee35d29227539c91071490c97bf5:packages/valory/protocols/websocket_client/protocol.yaml:generic-api-key:15 +76e101cc83c5ee35d29227539c91071490c97bf5:packages/valory/protocols/websocket_client/protocol.yaml:generic-api-key:16 diff --git a/packages/packages.json b/packages/packages.json index d36d70dd..82e0ff6a 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": "bafybeibwlyxqtitnfgmt2liuygv75pydlvih23e2ilwpr6xlffac5flyse", + "agent/valory/mech/0.1.0": "bafybeifcrgru4m3wsftglweasvybe7n74dwmyiisjs6kuieooeiagmhthi", "skill/valory/mech_abci/0.1.0": "bafybeieimp7xzxcnbzsuunf2xkcy5juulhmzsmkq2v3sw3o3lgssb53cnu", "contract/valory/agent_mech/0.1.0": "bafybeiepxumywg6z2zapqzc3bg3iey23cmlgjzxisqox5j74o5i2texr5e", - "service/valory/mech/0.1.0": "bafybeicw64tjmcx6fbffwhpgdabmnxfwbuk6cycwrcu4me3hplcleu4mze", + "service/valory/mech/0.1.0": "bafybeicodktlcyqz76qjmj24inrrqbbied2qeeluao35ajm62txykwli54", "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": "bafybeieercgbjemdjiovecetxadurwil26cs2swleupmbgc4py2rg6e2kq", + "skill/valory/task_execution/0.1.0": "bafybeidsq3mpnfmjp2jfrurrx3ck3ozwfh7jg4mzhc3742dqyoeijizocq", "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 9c72b415..97691dd7 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:bafybeieercgbjemdjiovecetxadurwil26cs2swleupmbgc4py2rg6e2kq +- valory/task_execution:0.1.0:bafybeidsq3mpnfmjp2jfrurrx3ck3ozwfh7jg4mzhc3742dqyoeijizocq - valory/task_submission_abci:0.1.0:bafybeib4m2bwgchloqss3wotsx4rz7qqkwydaesiqkls2zq7zbtp6jtpsi - valory/termination_abci:0.1.0:bafybeie4zvjfxvdu7qrulmur3chpjz3kpj5m4bjsxvpk4gvj5zbyyayfaa - valory/transaction_settlement_abci:0.1.0:bafybeiaefgqbs7zsn5xe5kdwrujj7ivygkn3ujpw6crnvi3knvxw75qmja @@ -203,6 +203,7 @@ models: agent_index: ${int:0} num_agents: ${int:4} from_block_range: ${int:50000} + mech_to_config: ${list:[["0x77af31De935740567Cf4fF1986D04B2c964A786a",["use_dynamic_pricing","false"]]]} timeout_limit: ${int:3} max_block_window: ${int:500} --- diff --git a/packages/valory/services/mech/service.yaml b/packages/valory/services/mech/service.yaml index f764171a..955330d0 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:bafybeibwlyxqtitnfgmt2liuygv75pydlvih23e2ilwpr6xlffac5flyse +agent: valory/mech:0.1.0:bafybeifcrgru4m3wsftglweasvybe7n74dwmyiisjs6kuieooeiagmhthi number_of_agents: 4 deployment: agent: @@ -177,6 +177,7 @@ type: skill agent_index: ${AGENT_INDEX_0:int:0} num_agents: ${NUM_AGENTS:int:4} timeout_limit: ${TIMEOUT_LIMIT:int:3} + mech_to_config: ${list:[["0xFf82123dFB52ab75C417195c5fDB87630145ae81",["use_dynamic_pricing","false"]],["0x77af31De935740567Cf4fF1986D04B2c964A786a",["use_dynamic_pricing","false"]]]} max_block_window: ${MAX_BLOCK_WINDOW:int:500} 1: models: @@ -189,6 +190,7 @@ type: skill polling_interval: ${POLLING_INTERVAL:float:30.0} agent_index: ${AGENT_INDEX_1:int:1} num_agents: ${NUM_AGENTS:int:4} + mech_to_config: ${list:[["0xFf82123dFB52ab75C417195c5fDB87630145ae81",["use_dynamic_pricing","false"]],["0x77af31De935740567Cf4fF1986D04B2c964A786a",["use_dynamic_pricing","false"]]]} timeout_limit: ${TIMEOUT_LIMIT:int:3} max_block_window: ${MAX_BLOCK_WINDOW:int:500} 2: @@ -202,6 +204,7 @@ type: skill polling_interval: ${POLLING_INTERVAL:float:30.0} agent_index: ${AGENT_INDEX_2:int:2} num_agents: ${NUM_AGENTS:int:4} + mech_to_config: ${list:[["0xFf82123dFB52ab75C417195c5fDB87630145ae81",["use_dynamic_pricing","false"]],["0x77af31De935740567Cf4fF1986D04B2c964A786a",["use_dynamic_pricing","false"]]]} timeout_limit: ${TIMEOUT_LIMIT:int:3} max_block_window: ${MAX_BLOCK_WINDOW:int:500} 3: @@ -216,6 +219,7 @@ type: skill agent_index: ${AGENT_INDEX_3:int:3} num_agents: ${NUM_AGENTS:int:4} timeout_limit: ${TIMEOUT_LIMIT:int:3} + mech_to_config: ${list:[["0xFf82123dFB52ab75C417195c5fDB87630145ae81",["use_dynamic_pricing","false"]],["0x77af31De935740567Cf4fF1986D04B2c964A786a",["use_dynamic_pricing","false"]]]} max_block_window: ${MAX_BLOCK_WINDOW:int:500} --- public_id: valory/ledger:0.19.0 @@ -228,6 +232,8 @@ type: connection chain_id: ${ETHEREUM_LEDGER_CHAIN_ID:int:1} poa_chain: ${ETHEREUM_LEDGER_IS_POA_CHAIN:bool:false} default_gas_price_strategy: ${ETHEREUM_LEDGER_PRICING:str:eip1559} + gnosis: + address: ${GNOSIS_RPC_0:str:http://host.docker.internal:8545} 1: config: ledger_apis: @@ -236,6 +242,8 @@ type: connection chain_id: ${ETHEREUM_LEDGER_CHAIN_ID:int:1} poa_chain: ${ETHEREUM_LEDGER_IS_POA_CHAIN:bool:false} default_gas_price_strategy: ${ETHEREUM_LEDGER_PRICING:str:eip1559} + gnosis: + address: ${GNOSIS_RPC_1:str:http://host.docker.internal:8545} 2: config: ledger_apis: @@ -244,6 +252,8 @@ type: connection chain_id: ${ETHEREUM_LEDGER_CHAIN_ID:int:1} poa_chain: ${ETHEREUM_LEDGER_IS_POA_CHAIN:bool:false} default_gas_price_strategy: ${ETHEREUM_LEDGER_PRICING:str:eip1559} + gnosis: + address: ${GNOSIS_RPC_2:str:http://host.docker.internal:8545} 3: config: ledger_apis: @@ -252,6 +262,8 @@ type: connection chain_id: ${ETHEREUM_LEDGER_CHAIN_ID:int:1} poa_chain: ${ETHEREUM_LEDGER_IS_POA_CHAIN:bool:false} default_gas_price_strategy: ${ETHEREUM_LEDGER_PRICING:str:eip1559} + gnosis: + address: ${GNOSIS_RPC_3:str:http://host.docker.internal:8545} --- public_id: valory/p2p_libp2p_client:0.1.0 type: connection diff --git a/packages/valory/skills/task_execution/behaviours.py b/packages/valory/skills/task_execution/behaviours.py index 38beb317..b28c33f6 100644 --- a/packages/valory/skills/task_execution/behaviours.py +++ b/packages/valory/skills/task_execution/behaviours.py @@ -31,6 +31,7 @@ from aea.protocols.base import Message from aea.protocols.dialogue.base import Dialogue from aea.skills.behaviours import SimpleBehaviour +from eth_abi import encode from packages.valory.connections.ipfs.connection import IpfsDialogues from packages.valory.connections.ipfs.connection import PUBLIC_ID as IPFS_CONNECTION_ID @@ -49,6 +50,9 @@ from packages.valory.protocols.ledger_api import LedgerApiMessage from packages.valory.skills.task_execution.models import Params from packages.valory.skills.task_execution.utils.benchmarks import TokenCounterCallback +from packages.valory.skills.task_execution.utils.cost_calculation import ( + get_cost_for_done_task, +) from packages.valory.skills.task_execution.utils.ipfs import ( ComponentPackageLoader, get_ipfs_file_hash, @@ -454,7 +458,17 @@ def _handle_store_response(self, message: IpfsMessage, dialogue: Dialogue) -> No data=ipfs_hash, ) done_task = cast(Dict[str, Any], self._done_task) - done_task["task_result"] = to_multihash(ipfs_hash) + task_result = to_multihash(ipfs_hash) + cost = get_cost_for_done_task(done_task) + self.context.logger.info(f"Cost for task {req_id}: {cost}") + mech_config = self.params.mech_to_config[done_task["mech_address"]] + if mech_config.use_dynamic_pricing: + self.context.logger.info(f"Dynamic pricing is enabled for task {req_id}.") + task_result = encode( + ["uint256", "bytes"], [cost, bytes.fromhex(task_result)] + ).hex() + + done_task["task_result"] = task_result # add to done tasks, in thread safe way with self.done_tasks_lock: self.done_tasks.append(done_task) diff --git a/packages/valory/skills/task_execution/models.py b/packages/valory/skills/task_execution/models.py index 3e81775e..d2bf7a3d 100644 --- a/packages/valory/skills/task_execution/models.py +++ b/packages/valory/skills/task_execution/models.py @@ -18,6 +18,7 @@ # ------------------------------------------------------------------------------ """This module contains the shared state for the abci skill of Mech.""" +import dataclasses from collections import defaultdict from typing import Any, Callable, Dict, List, Optional, cast @@ -25,6 +26,20 @@ from aea.skills.base import Model +@dataclasses.dataclass +class MechConfig: + """Mech config dataclass.""" + + use_dynamic_pricing: bool + + @staticmethod + def from_dict(raw_dict: Dict[str, Any]) -> "MechConfig": + """From dict.""" + return MechConfig( + use_dynamic_pricing=raw_dict["use_dynamic_pricing"].lower() == "true" + ) + + class Params(Model): """A model to represent params for multiple abci apps.""" @@ -66,6 +81,7 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: enforce(self.max_block_window is not None, "max_block_window must be set!") # maps the request id to the number of times it has timed out self.request_id_to_num_timeouts: Dict[int, int] = defaultdict(lambda: 0) + self.mech_to_config: Dict[str, MechConfig] = self._parse_mech_configs(kwargs) super().__init__(*args, **kwargs) def _nested_list_todict_workaround( @@ -78,3 +94,23 @@ def _nested_list_todict_workaround( if len(values) == 0: raise ValueError(f"No {key} specified!") return {value[0]: value[1] for value in values} + + def _parse_mech_configs(self, kwargs: Dict) -> Dict[str, MechConfig]: + """Parse the mech configs.""" + mech_configs_json = self._nested_list_todict_workaround( + kwargs, "mech_to_config" + ) + mech_configs_json = { + key: {value[0]: value[1]} for key, value in mech_configs_json.items() + } + + mech_configs = { + mech: MechConfig.from_dict(config) + for mech, config in mech_configs_json.items() + } + for address in self.agent_mech_contract_addresses: + enforce( + address in mech_configs, + f"agent_mech_contract_addresses {address} must be in mech_configs!", + ) + return mech_configs diff --git a/packages/valory/skills/task_execution/skill.yaml b/packages/valory/skills/task_execution/skill.yaml index 48e84c31..161f165f 100644 --- a/packages/valory/skills/task_execution/skill.yaml +++ b/packages/valory/skills/task_execution/skill.yaml @@ -7,13 +7,14 @@ license: Apache-2.0 aea_version: '>=1.0.0, <2.0.0' fingerprint: __init__.py: bafybeidqhvvlnthkbnmrdkdeyjyx2f2ab6z4xdgmagh7welqnh2v6wczx4 - behaviours.py: bafybeihprwot27csugwpoimsqcwprxu5bstqpvanot3nkmfgvhbr66ww4y + behaviours.py: bafybeiclizwd4icgkkof7zx4z5ckruckzlq7b5mho3flicpobahgsqguya dialogues.py: bafybeid4zxalqdlo5mw4yfbuf34hx4jp5ay5z6chm4zviwu4cj7fudtwca handlers.py: bafybeidbt5ezj74cgfogk3w4uw4si2grlnk5g54veyumw7g5yh6gdscywu - models.py: bafybeihgclxctyltuehj2f4fzj26edptqugrrm4phd6ovuulezrqot6qo4 + models.py: bafybeid6befxrrbiaw7nduz4zgbm5nfc246fn2eb6rfmja6v5hmq4wtcwe utils/__init__.py: bafybeiccdijaigu6e5p2iruwo5mkk224o7ywedc7nr6xeu5fpmhjqgk24e utils/benchmarks.py: bafybeibdwt4svz24ahok4x4h2rpeotlmlmvifccd27oizsz5bjwj6dqree - utils/ipfs.py: bafybeidinbdqkidix44ibz5hug7inkcbijooag53gr5mtbaa72tk335uqq + utils/cost_calculation.py: bafybeighafxied73w3mcmgziwfp3u2x6t4qlztw4kyekyq2ddgyhdge74q + utils/ipfs.py: bafybeic7cbuv3tomi2xv7h2qowrqnpoufpanngzlgzljl4ptimpss3meqm utils/task.py: bafybeieuziu7owtk543z3umgmayhjh67klftk7vrhz24l6rlaii5lvkqh4 fingerprint_ignore_patterns: [] connections: @@ -84,6 +85,10 @@ models: - stabilityai-stable-diffusion-768-v2-1 from_block_range: 50000 num_agents: 4 + mech_to_config: + - - '0x9A676e781A523b5d0C0e43731313A708CB607508' + - - - use_dynamic_pricing + - 'false' polling_interval: 30.0 task_deadline: 240.0 max_block_window: 500 @@ -111,4 +116,6 @@ dependencies: version: ==0.5.1 anthropic: version: ==0.3.11 + eth-abi: + version: ==4.0.0 is_abstract: false diff --git a/packages/valory/skills/task_execution/utils/cost_calculation.py b/packages/valory/skills/task_execution/utils/cost_calculation.py new file mode 100644 index 00000000..efd19acd --- /dev/null +++ b/packages/valory/skills/task_execution/utils/cost_calculation.py @@ -0,0 +1,52 @@ +# -*- 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. +# +# ------------------------------------------------------------------------------ +"""Calculate the cost for tools.""" +import logging +import math +from typing import Any, Dict, cast + +from packages.valory.skills.task_execution import PUBLIC_ID + + +_logger = logging.getLogger( + f"aea.packages.{PUBLIC_ID.author}.contracts.{PUBLIC_ID.name}.utils.cost_calculation" +) + +DEFAULT_PRICE = 1 + + +def get_cost_for_done_task( + done_task: Dict[str, Any], fallback_price: int = DEFAULT_PRICE +) -> int: + """Get the cost for a done task.""" + cost_dict = done_task.get("cost_dict", {}) + if cost_dict == {}: + _logger.warning(f"Cost dict not found in done task {done_task['request_id']}.") + return fallback_price + total_cost = cost_dict.get("total_cost", None) + if total_cost is None: + _logger.warning( + f"Total cost not found in cost dict {cost_dict} for {done_task['request_id']}." + ) + return fallback_price + + total_cost = cast(float, total_cost) + # 0.01 token (ex. xDAI/USDC) -> 1 NFT credit + cost_in_credits = math.ceil(total_cost * 100) + return cost_in_credits diff --git a/packages/valory/skills/task_execution/utils/ipfs.py b/packages/valory/skills/task_execution/utils/ipfs.py index 364a3a28..a6c12d8b 100644 --- a/packages/valory/skills/task_execution/utils/ipfs.py +++ b/packages/valory/skills/task_execution/utils/ipfs.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # ------------------------------------------------------------------------------ # -# Copyright 2023 Valory AG +# Copyright 2023-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. @@ -40,7 +40,7 @@ def get_ipfs_file_hash(data: bytes) -> str: return file_hash -def to_multihash(hash_string: str) -> bytes: +def to_multihash(hash_string: str) -> str: """To multihash string.""" # Decode the Base32 CID to bytes cid_bytes = multibase.decode(hash_string) diff --git a/tools/native_transfer_request/native_transfer_request.py b/tools/native_transfer_request/native_transfer_request.py index dd87799b..b0553327 100644 --- a/tools/native_transfer_request/native_transfer_request.py +++ b/tools/native_transfer_request/native_transfer_request.py @@ -119,7 +119,7 @@ def native_transfer( # parse the response to get the transaction object string itself parsed_txs = ast.literal_eval(response) except SyntaxError: - return response, None, None + return response, None, None, None # build the transaction object, unknowns are referenced from parsed_txs transaction = { diff --git a/tools/openai_request/openai_request.py b/tools/openai_request/openai_request.py index 5515c38f..5ea87827 100644 --- a/tools/openai_request/openai_request.py +++ b/tools/openai_request/openai_request.py @@ -61,7 +61,7 @@ def count_tokens(text: str, model: str) -> int: ALLOWED_TOOLS = [PREFIX + value for values in ENGINES.values() for value in values] -def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any]: +def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any, Any]: """Run the task""" with OpenAIClientManager(kwargs["api_keys"]["openai"]): max_tokens = kwargs.get("max_tokens", DEFAULT_OPENAI_SETTINGS["max_tokens"]) @@ -70,12 +70,12 @@ def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any]: tool = kwargs["tool"] counter_callback = kwargs.get("counter_callback", None) if tool not in ALLOWED_TOOLS: - return f"Tool {tool} is not in the list of supported tools.", None, None + return f"Tool {tool} is not in the list of supported tools.", None, None, None engine = tool.replace(PREFIX, "") moderation_result = client.moderations.create(input=prompt) if moderation_result.results[0].flagged: - return "Moderation flagged the prompt as in violation of terms.", None, None + return "Moderation flagged the prompt as in violation of terms.", None, None, None if engine in ENGINES["chat"]: messages = [ @@ -91,9 +91,9 @@ def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any]: timeout=120, stop=None, ) - return response.choices[0].message.content, prompt, None + return response.choices[0].message.content, prompt, None, None response = client.completions.create( - engine=engine, + model=engine, prompt=prompt, temperature=temperature, max_tokens=max_tokens, @@ -102,4 +102,4 @@ def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any]: timeout=120, presence_penalty=0, ) - return response.choices[0].text, prompt, counter_callback + return response.choices[0].text, prompt, None, counter_callback diff --git a/tools/optimization_by_prompting/optimization_by_prompting.py b/tools/optimization_by_prompting/optimization_by_prompting.py index 7759e24a..7e809744 100644 --- a/tools/optimization_by_prompting/optimization_by_prompting.py +++ b/tools/optimization_by_prompting/optimization_by_prompting.py @@ -387,7 +387,7 @@ def fetch_additional_information( return "\n".join(["- " + text for text in texts]) -def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: +def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any, Any]: """Run the task""" with OpenAIClientManager(kwargs["api_keys"]["openai"]): tool = kwargs["tool"] diff --git a/tools/prediction_request/prediction_request.py b/tools/prediction_request/prediction_request.py index 0017019e..8025c221 100644 --- a/tools/prediction_request/prediction_request.py +++ b/tools/prediction_request/prediction_request.py @@ -261,7 +261,7 @@ def fetch_additional_information( num_words: Optional[int], counter_callback: Optional[Callable] = None, source_links: Optional[List[str]] = None, -) -> str: +) -> Tuple[str, Any]: """Fetch additional information.""" url_query_prompt = URL_QUERY_PROMPT.format(user_prompt=prompt) moderation_result = client.moderations.create(input=url_query_prompt) @@ -363,7 +363,7 @@ def summarize(text: str, compression_factor: float, vocab: str) -> str: return summary_text -def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any]: +def run(**kwargs) -> Tuple[Optional[str], Any, Optional[Dict[str, Any]], Any]: """Run the task""" with OpenAIClientManager(kwargs["api_keys"]["openai"]): tool = kwargs["tool"] @@ -409,7 +409,7 @@ def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any]: ) moderation_result = client.moderations.create(input=prediction_prompt) if moderation_result.results[0].flagged: - return "Moderation flagged the prompt as in violation of terms.", None, None + return "Moderation flagged the prompt as in violation of terms.", None, None, None messages = [ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": prediction_prompt}, @@ -430,5 +430,5 @@ def run(**kwargs) -> Tuple[Optional[str], Optional[Dict[str, Any]], Any]: model=engine, token_counter=count_tokens, ) - return response.choices[0].message.content, prediction_prompt, counter_callback - return response.choices[0].message.content, prediction_prompt, None + return response.choices[0].message.content, prediction_prompt, None, counter_callback + return response.choices[0].message.content, prediction_prompt, None, None diff --git a/tools/prediction_request_claude/prediction_request_claude.py b/tools/prediction_request_claude/prediction_request_claude.py index e7682f78..a2461491 100644 --- a/tools/prediction_request_claude/prediction_request_claude.py +++ b/tools/prediction_request_claude/prediction_request_claude.py @@ -32,7 +32,7 @@ from googleapiclient.discovery import build NUM_URLS_EXTRACT = 5 -DEFAULT_NUM_WORDS: Dict[str, Optional[int]] = defaultdict(lambda: 300) +DEFAULT_NUM_WORDS = 300 DEFAULT_OPENAI_SETTINGS = { "max_tokens": 500, "temperature": 0.7, @@ -259,7 +259,7 @@ def fetch_additional_information( return "\n".join(["- " + text for text in texts]), None -def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: +def run(**kwargs) -> Tuple[Optional[str], Any, Optional[Dict[str, Any]], Any]: """Run the task""" tool = kwargs["tool"] prompt = kwargs["prompt"] @@ -308,6 +308,6 @@ def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: output_prompt=completion.completion, token_counter=count_tokens, ) - return completion.completion, prediction_prompt, counter_callback + return completion.completion, prediction_prompt, None, counter_callback - return completion.completion, prediction_prompt, None + return completion.completion, prediction_prompt, None, None diff --git a/tools/prediction_request_embedding/prediction_sentence_embedding.py b/tools/prediction_request_embedding/prediction_sentence_embedding.py index b8d133f4..d4262eeb 100644 --- a/tools/prediction_request_embedding/prediction_sentence_embedding.py +++ b/tools/prediction_request_embedding/prediction_sentence_embedding.py @@ -1105,7 +1105,7 @@ def fetch_additional_information( return additional_informations -def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: +def run(**kwargs) -> Tuple[Optional[str], Any, Optional[Dict[str, Any]], Any]: """ Run the task with the given arguments. @@ -1187,7 +1187,7 @@ def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: # Perform moderation moderation_result = client.moderations.create(input=prediction_prompt) if moderation_result.results[0].flagged: - return "Moderation flagged the prompt as in violation of terms.", None, None + return "Moderation flagged the prompt as in violation of terms.", None, None, None # Create messages for the OpenAI engine messages = [ @@ -1205,4 +1205,4 @@ def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: timeout=150, stop=None, ) - return response.choices[0].message.content, prediction_prompt, None + return response.choices[0].message.content, prediction_prompt, None, None diff --git a/tools/prediction_request_sme/prediction_request_sme.py b/tools/prediction_request_sme/prediction_request_sme.py index b99dca3b..363da469 100644 --- a/tools/prediction_request_sme/prediction_request_sme.py +++ b/tools/prediction_request_sme/prediction_request_sme.py @@ -303,7 +303,6 @@ def fetch_additional_information( json_data["queries"], api_key=google_api_key, engine=google_engine, - num_urls=num_urls, ) texts = extract_texts(urls, num_words) else: @@ -433,5 +432,5 @@ def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: model=engine, token_counter=count_tokens, ) - return response.choices[0].message.content, prediction_prompt, counter_callback - return response.choices[0].message.content, prediction_prompt, None + return response.choices[0].message.content, prediction_prompt, None, counter_callback + return response.choices[0].message.content, prediction_prompt, None, None diff --git a/tools/prediction_sum_url_content/prediction_sum_url_content.py b/tools/prediction_sum_url_content/prediction_sum_url_content.py index 68e11786..d411584c 100644 --- a/tools/prediction_sum_url_content/prediction_sum_url_content.py +++ b/tools/prediction_sum_url_content/prediction_sum_url_content.py @@ -1049,7 +1049,7 @@ def fetch_additional_information( return additional_informations -def run(**kwargs) -> Tuple[str, Optional[Dict[str, Any]], Any]: +def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: """ Run the task with the given arguments. @@ -1155,7 +1155,7 @@ def run(**kwargs) -> Tuple[str, Optional[Dict[str, Any]], Any]: # Perform moderation moderation_result = client.moderations.create(input=prediction_prompt) if moderation_result.results[0].flagged: - return "Moderation flagged the prompt as in violation of terms.", None, None + return "Moderation flagged the prompt as in violation of terms.", None, None, None # Create messages for the OpenAI engine messages = [ @@ -1174,4 +1174,4 @@ def run(**kwargs) -> Tuple[str, Optional[Dict[str, Any]], Any]: stop=None, ) print(f"RESPONSE: {response}") - return response.choices[0].message.content, None, None + return response.choices[0].message.content, None, None, None diff --git a/tools/stability_ai_request/stabilityai_request.py b/tools/stability_ai_request/stabilityai_request.py index c7703978..add4b651 100644 --- a/tools/stability_ai_request/stabilityai_request.py +++ b/tools/stability_ai_request/stabilityai_request.py @@ -68,14 +68,14 @@ class FinishReason(Enum): ERROR = 2 -def run(**kwargs: Any) -> Tuple[str, Optional[str], Optional[Dict[str, Any]]]: +def run(**kwargs) -> Tuple[str, Optional[str], Optional[Dict[str, Any]], Any]: """Run the task""" api_key = kwargs["api_keys"]["stabilityai"] tool = kwargs["tool"] prompt = kwargs["prompt"] if tool not in ALLOWED_TOOLS: - return f"Tool {tool} is not in the list of supported tools.", None + return f"Tool {tool} is not in the list of supported tools.", None, None, None # Place content moderation request here if needed engine = tool.replace(PREFIX, "") @@ -118,9 +118,10 @@ def run(**kwargs: Any) -> Tuple[str, Optional[str], Optional[Dict[str, Any]]]: json=json_params, ) if response.status_code == 200: - return json.dumps(response.json()), None, None + return json.dumps(response.json()), None, None, None return ( f"Error: Non-200 response ({response.status_code}): {response.text}", None, None, + None ) diff --git a/tox.ini b/tox.ini index 25718a95..c77c82db 100644 --- a/tox.ini +++ b/tox.ini @@ -370,6 +370,9 @@ ignore_missing_imports = True [mypy-certifi.*] ignore_missing_imports = True +[mypy-eth_abi.*] +ignore_missing_imports = True + [mypy-pandas.*] ignore_missing_imports = True