Skip to content

Commit

Permalink
added script to create metadata hash for the mech
Browse files Browse the repository at this point in the history
  • Loading branch information
KahanMajmudar committed Nov 21, 2024
1 parent 1964950 commit 067993c
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,5 @@ local_config.json
mech.db

/.api_keys.json

.metadata_hash.json
10 changes: 10 additions & 0 deletions .metadata_hash.json.example
Original file line number Diff line number Diff line change
@@ -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"
]
}
57 changes: 57 additions & 0 deletions setup_metadata_hash.py
Original file line number Diff line number Diff line change
@@ -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()
27 changes: 22 additions & 5 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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 = (
Expand Down

0 comments on commit 067993c

Please sign in to comment.