Skip to content

Commit

Permalink
Merge pull request #31 from valory-xyz/feat/aditional_params_to_request
Browse files Browse the repository at this point in the history
Added extra attributes to cli
  • Loading branch information
DavidMinarsch authored Mar 27, 2024
2 parents 5fb483f + c830673 commit 4e76e95
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 6 deletions.
19 changes: 17 additions & 2 deletions mech_client/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

"""Mech client CLI module."""

from typing import Optional
from typing import Any, Dict, List, Optional

import click

Expand All @@ -45,6 +45,13 @@ def cli() -> None:
type=str,
help="Name of the tool to be used",
)
@click.option(
"--extra-attribute",
type=str,
multiple=True,
help="Extra attribute (key=value) to be included in the request metadata",
metavar="KEY=VALUE",
)
@click.option(
"--key",
type=click.Path(exists=True, file_okay=True, dir_okay=False),
Expand Down Expand Up @@ -75,20 +82,28 @@ def cli() -> None:
def interact( # pylint: disable=too-many-arguments
prompt: str,
agent_id: int,
tool: Optional[str],
key: Optional[str],
tool: Optional[str],
extra_attribute: Optional[List[str]] = None,
confirm: Optional[str] = None,
retries: Optional[int] = None,
timeout: Optional[float] = None,
sleep: Optional[float] = None,
) -> None:
"""Interact with a mech specifying a prompt and tool."""
try:
extra_attributes_dict: Dict[str, Any] = {}
if extra_attribute:
for pair in extra_attribute:
k, v = pair.split("=")
extra_attributes_dict[k] = v

interact_(
prompt=prompt,
agent_id=agent_id,
private_key_path=key,
tool=tool,
extra_attributes=extra_attributes_dict,
confirmation_type=(
ConfirmationType(confirm)
if confirm is not None
Expand Down
11 changes: 10 additions & 1 deletion mech_client/interact.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ def send_request( # pylint: disable=too-many-arguments,too-many-locals
mech_contract: Web3Contract,
prompt: str,
tool: str,
extra_attributes: Optional[Dict[str, Any]] = None,
price: int = 10_000_000_000_000_000,
retries: Optional[int] = None,
timeout: Optional[float] = None,
Expand All @@ -250,6 +251,8 @@ def send_request( # pylint: disable=too-many-arguments,too-many-locals
:type prompt: str
:param tool: The requested tool.
:type tool: str
:param extra_attributes: Extra attributes to be included in the request metadata.
:type extra_attributes: Optional[Dict[str,Any]]
:param price: The price for the request (default: 10_000_000_000_000_000).
:type price: int
:param retries: Number of retries for sending a transaction
Expand All @@ -259,7 +262,9 @@ def send_request( # pylint: disable=too-many-arguments,too-many-locals
:param sleep: Amount of sleep before retrying the transaction
:type sleep: float
"""
v1_file_hash_hex_truncated, v1_file_hash_hex = push_metadata_to_ipfs(prompt, tool)
v1_file_hash_hex_truncated, v1_file_hash_hex = push_metadata_to_ipfs(
prompt, tool, extra_attributes
)
print(f"Prompt uploaded: https://gateway.autonolas.tech/ipfs/{v1_file_hash_hex}")
method_name = "request"
methord_args = {"data": v1_file_hash_hex_truncated}
Expand Down Expand Up @@ -377,6 +382,7 @@ def interact( # pylint: disable=too-many-arguments,too-many-locals
prompt: str,
agent_id: int,
tool: Optional[str] = None,
extra_attributes: Optional[Dict[str, Any]] = None,
private_key_path: Optional[str] = None,
confirmation_type: ConfirmationType = ConfirmationType.WAIT_FOR_BOTH,
retries: Optional[int] = None,
Expand All @@ -392,6 +398,8 @@ def interact( # pylint: disable=too-many-arguments,too-many-locals
:type agent_id: int
:param tool: The tool to interact with (optional).
:type tool: Optional[str]
:param extra_attributes: Extra attributes to be included in the request metadata (optional).
:type extra_attributes: Optional[Dict[str, Any]]
:param private_key_path: The path to the private key file (optional).
:type private_key_path: Optional[str]
:param confirmation_type: The confirmation type for the interaction (default: ConfirmationType.WAIT_FOR_BOTH).
Expand Down Expand Up @@ -438,6 +446,7 @@ def interact( # pylint: disable=too-many-arguments,too-many-locals
mech_contract=mech_contract,
prompt=prompt,
tool=tool,
extra_attributes=extra_attributes,
retries=retries,
timeout=timeout,
sleep=sleep,
Expand Down
10 changes: 8 additions & 2 deletions mech_client/prompt_to_ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,29 @@
import shutil
import tempfile
import uuid
from typing import Tuple
from typing import Any, Dict, Optional, Tuple

from mech_client.push_to_ipfs import push_to_ipfs


def push_metadata_to_ipfs(prompt: str, tool: str) -> Tuple[str, str]:
def push_metadata_to_ipfs(
prompt: str, tool: str, extra_attributes: Optional[Dict[str, Any]] = None
) -> Tuple[str, str]:
"""
Pushes metadata object to IPFS.
:param prompt: Prompt string.
:type prompt: str
:param tool: Tool string.
:type tool: str
:param extra_attributes: Extra attributes to be included in the request metadata.
:type extra_attributes: Optional[Dict[str,Any]]
:return: Tuple containing the IPFS hash and truncated IPFS hash.
:rtype: Tuple[str, str]
"""
metadata = {"prompt": prompt, "tool": tool, "nonce": str(uuid.uuid4())}
if extra_attributes:
metadata.update(extra_attributes)
dirpath = tempfile.mkdtemp()
file_name = dirpath + "metadata.json"
with open(file_name, "w", encoding="utf-8") as f:
Expand Down
2 changes: 1 addition & 1 deletion scripts/benchmark.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ for ((i=1; i<=$iterations; i++)); do
echo "- Iteration $i"

prompt="($i) Will arsenal win the Premier League in 2024"
mechx interact "$prompt" 6 --tool prediction-offline --confirm on-chain
mechx interact "$prompt" 6 --tool prediction-offline --confirm on-chain --extra-attribute key1=value1 --extra-attribute key2=value2

if [ $? -ne 0 ]; then
echo "Error: Command execution failed."
Expand Down

0 comments on commit 4e76e95

Please sign in to comment.