Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added script to create metadata hash for the mech #3

Merged
merged 10 commits into from
Nov 22, 2024
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"
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The metadata is missing here. Check example here.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not seperate the tool metadata into the tool's yaml?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a future version yes, atm no. It will take too much time.

}
1 change: 0 additions & 1 deletion run_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,6 @@ def main() -> None:
# TODO: no way to update this atm after its provided, user is expected to update the file itself.
"API_KEYS": json.dumps(api_keys, separators=(',', ':')),
"AGENT_ID": str(mech_quickstart_config.agent_id),
# TODO this will be very unclear for the general user how to come up with
"METADATA_HASH": mech_quickstart_config.metadata_hash,
"MECH_TO_CONFIG": json.dumps(mech_to_config, separators=(',', ':')),
"ON_CHAIN_SERVICE_ID": service.chain_configs[home_chain_id].chain_data.token,
Expand Down
57 changes: 57 additions & 0 deletions setup_metadata_hash.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it makes sense that this gets called upon setup of local config on run_service.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",
)
Comment on lines +62 to +65
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should do some validation as well. Basically needs to follow the schema like here.


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: ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we just call the other function here

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
Loading