From 067993c9486c8a1bb31d2e9a10c404333b0620d5 Mon Sep 17 00:00:00 2001 From: KahanMajmudar Date: Thu, 21 Nov 2024 08:20:00 +0000 Subject: [PATCH] added script to create metadata hash for the mech --- .gitignore | 2 ++ .metadata_hash.json.example | 10 +++++++ setup_metadata_hash.py | 57 +++++++++++++++++++++++++++++++++++++ utils.py | 27 ++++++++++++++---- 4 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 .metadata_hash.json.example create mode 100644 setup_metadata_hash.py diff --git a/.gitignore b/.gitignore index 214c561..85d74e6 100644 --- a/.gitignore +++ b/.gitignore @@ -54,3 +54,5 @@ local_config.json mech.db /.api_keys.json + +.metadata_hash.json \ No newline at end of file diff --git a/.metadata_hash.json.example b/.metadata_hash.json.example new file mode 100644 index 0000000..e5e812e --- /dev/null +++ b/.metadata_hash.json.example @@ -0,0 +1,10 @@ +{ + "name": "Autonolas Mech Quickstart", + "description": "The mech executes AI tasks requested on-chain and delivers the results to the requester.", + "inputFormat": "ipfs-v0.1", + "outputFormat": "ipfs-v0.1", + "image": "https://gateway.autonolas.tech/ipfs/bafybeidzpenez565d7vp7jexfrwisa2wijzx6vwcffli57buznyyqkrceq", + "tools": [ + "openai-gpt-4" + ] +} \ No newline at end of file diff --git a/setup_metadata_hash.py b/setup_metadata_hash.py new file mode 100644 index 0000000..82429a0 --- /dev/null +++ b/setup_metadata_hash.py @@ -0,0 +1,57 @@ +from typing import Tuple + +import multibase +import multicodec +from aea.helpers.cid import to_v1 +from aea_cli_ipfs.ipfs_utils import IPFSTool +from utils import ( + print_title, + OPERATE_HOME, + MechQuickstartConfig, + input_with_default_value, + OperateApp, +) + + +def main() -> None: + """ + Push the metadata file to IPFS. + """ + + print_title("Mech Quickstart: Metadata hash setup") + print("This script will assist you in setting up the metadata hash for your mech.") + print() + + operate = OperateApp( + home=OPERATE_HOME, + ) + operate.setup() + + path = OPERATE_HOME / "local_config.json" + if path.exists(): + mech_quickstart_config = MechQuickstartConfig.load(path) + else: + mech_quickstart_config = MechQuickstartConfig(path) + + metadata_hash_path = input_with_default_value( + "Please provide the path to your metadata_hash.json file", + "./.metadata_hash.json", + ) + + response = IPFSTool().client.add( + metadata_hash_path, pin=True, recursive=True, wrap_with_directory=False + ) + v1_file_hash = to_v1(response["Hash"]) + cid_bytes = multibase.decode(v1_file_hash) + multihash_bytes = multicodec.remove_prefix(cid_bytes) + v1_file_hash_hex = "f01" + multihash_bytes.hex() + + mech_quickstart_config.metadata_hash = v1_file_hash_hex + mech_quickstart_config.store() + + print() + print_title("Metadata hash successfully generated and stored in config") + + +if __name__ == "__main__": + main() diff --git a/utils.py b/utils.py index aeb9449..88cc5e7 100644 --- a/utils.py +++ b/utils.py @@ -39,10 +39,8 @@ WARNING_ICON = colored("\u26A0", "yellow") OPERATE_HOME = Path.cwd() / ".mech_quickstart" DEFAULT_TOOLS_TO_PACKAGE_HASH = None -DEFAULT_MECH_TO_SUBSCRIPTION = None -DEFAULT_MECH_TO_CONFIG = None DEFAULT_MECH_HASH = "bafybeig544gw6i7ahlwj6d64djlwfltjuznz3p66kmwk4m6bzqtn2bjfbq" - +DEFAULT_MECH_METADATA_HASH = "f01701220caa53607238e340da63b296acab232c18a48e954f0af6ff2b835b2d93f1962f0" @dataclass class MechQuickstartConfig(LocalResource): """Local configuration.""" @@ -348,8 +346,27 @@ def get_local_config() -> MechQuickstartConfig: load_api_keys(mech_quickstart_config) if mech_quickstart_config.metadata_hash is None: - # TODO: default value is not a good idea here, we need to think of better ways to do this. - mech_quickstart_config.metadata_hash = input_with_default_value("Please provide the metadata hash", "f01701220caa53607238e340da63b296acab232c18a48e954f0af6ff2b835b2d93f1962f0") + metadata_hash = ( + input( + f"Do you want to update the metadata_hash str(set to {DEFAULT_MECH_METADATA_HASH})? (y/n): " + ).lower() + == "y" + ) + if metadata_hash: + while True: + user_input = input(f"Please enter the metadata_hash str: ") + if not isinstance(user_input, str): + print("Error: Please enter a valid str.") + continue + else: + mech_quickstart_config.metadata_hash = ( + user_input + ) + break + else: + mech_quickstart_config.metadata_hash = ( + DEFAULT_MECH_METADATA_HASH + ) if mech_quickstart_config.tools_to_packages_hash is None: tools_to_packages_hash = (