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

Feat/make tools as packages #141

Merged
merged 5 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": "bafybeiezyvels4ylygglro3isx7cvjaku2e5cfhb2vjrw6b3gxjioa4h4a",
"agent/valory/mech/0.1.0": "bafybeierifcna6tju6gblkcbudekz7qtwey5d5zklkrdrnwbgu4fha3mda",
"skill/valory/mech_abci/0.1.0": "bafybeifktlrkncnb5hjtwc7xqjag2pzmtciogp6nsm4t5umrqhmkmzyl5i",
"contract/valory/agent_mech/0.1.0": "bafybeicgt3bwogpwv7t57xuxzcq7vxmoq4glcjoprabb6d4ajtgjwzsrzi",
"service/valory/mech/0.1.0": "bafybeicme3nvcsgvid2q4k2xuhg4y4h6hzfzws4a2ikuowsuquoehjy6re",
"service/valory/mech/0.1.0": "bafybeiab5duxsqoidikglede3vdauj6zizvn5jnsro3razlhiphhnzusji",
"protocol/valory/acn_data_share/0.1.0": "bafybeih5ydonnvrwvy2ygfqgfabkr47s4yw3uqxztmwyfprulwfsoe7ipq",
"skill/valory/task_submission_abci/0.1.0": "bafybeia6b7rzrbrhijkctxpwzeywqvsdqm2j5u5ee6fbmrp6t3x2afdase",
"skill/valory/task_execution/0.1.0": "bafybeidmgp7dwelaxbul532vsebllmb6tyqbnvrifosmxwkzwgvyekyrve",
"skill/valory/task_execution/0.1.0": "bafybeiffc4nqpk3ri7kq5tuljbgnxqmhfj3brpznr6ujxqaif24tj64kcq",
"contract/valory/agent_registry/0.1.0": "bafybeiargayav6yiztdnwzejoejstcx4idssch2h4f5arlgtzj3tgsgfmu",
"protocol/valory/websocket_client/0.1.0": "bafybeih43mnztdv3v2hetr2k3gezg7d3yj4ur7cxdvcyaqhg65e52s5sf4",
"skill/valory/websocket_client/0.1.0": "bafybeidwntmkk4b2ixq5454ycbkknclqx7a6vpn7aqpm2nw3duszqrxvta",
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/agents/mech/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ skills:
- valory/abstract_round_abci:0.1.0:bafybeiaqcl7h2famylusiffigwem7tevkcsyocdu5xd42jkmgq6kvowzgq
- valory/contract_subscription:0.1.0:bafybeicyugrkx5glat4p4ezwf6i7oduh26eycfie6ftd4uxrknztzl3ik4
- valory/mech_abci:0.1.0:bafybeifktlrkncnb5hjtwc7xqjag2pzmtciogp6nsm4t5umrqhmkmzyl5i
- valory/task_execution:0.1.0:bafybeidmgp7dwelaxbul532vsebllmb6tyqbnvrifosmxwkzwgvyekyrve
- valory/task_execution:0.1.0:bafybeiffc4nqpk3ri7kq5tuljbgnxqmhfj3brpznr6ujxqaif24tj64kcq
- valory/registration_abci:0.1.0:bafybeic2ynseiak7jpta7jfwuqwyp453b4p7lolr4wihxmpn633uekv5am
- valory/reset_pause_abci:0.1.0:bafybeidzajbe3erygeh2xbd6lrjv7nsptznjuzrt24ykgvhgotdeyhfnba
- valory/task_submission_abci:0.1.0:bafybeia6b7rzrbrhijkctxpwzeywqvsdqm2j5u5ee6fbmrp6t3x2afdase
Expand Down
2 changes: 1 addition & 1 deletion packages/valory/services/mech/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeif7ia4jdlazy6745ke2k2x5yoqlwsgwr6sbztbgqtwvs3ndm2p7ba
fingerprint_ignore_patterns: []
agent: valory/mech:0.1.0:bafybeiezyvels4ylygglro3isx7cvjaku2e5cfhb2vjrw6b3gxjioa4h4a
agent: valory/mech:0.1.0:bafybeierifcna6tju6gblkcbudekz7qtwey5d5zklkrdrnwbgu4fha3mda
number_of_agents: 4
deployment:
agent:
Expand Down
12 changes: 8 additions & 4 deletions packages/valory/skills/task_execution/behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
from packages.valory.protocols.ledger_api import LedgerApiMessage
from packages.valory.skills.task_execution.models import Params
from packages.valory.skills.task_execution.utils.ipfs import (
ComponentPackageLoader,
get_ipfs_file_hash,
to_multihash,
)
Expand All @@ -73,7 +74,7 @@ def __init__(self, **kwargs: Any):
self._executor = ProcessPoolExecutor(max_workers=1)
self._executing_task: Optional[Dict[str, Any]] = None
self._tools_to_file_hash: Dict[str, str] = {}
self._all_tools: Dict[str, str] = {}
self._all_tools: Dict[str, Tuple[str, str]] = {}
self._inflight_tool_req: Optional[str] = None
self._done_task: Optional[Dict[str, Any]] = None
self._last_polling: Optional[float] = None
Expand Down Expand Up @@ -183,9 +184,11 @@ def _download_tools(self) -> None:

def _handle_get_tool(self, message: IpfsMessage, dialogue: Dialogue) -> None:
"""Handle get tool response"""
tool_py = list(message.files.values())[0]
_component_yaml, tool_py, callable_method = ComponentPackageLoader.load(
message.files
)
tool_req = cast(str, self._inflight_tool_req)
self._all_tools[tool_req] = tool_py
self._all_tools[tool_req] = tool_py, callable_method
self._inflight_tool_req = None

def _populate_from_block(self) -> None:
Expand Down Expand Up @@ -361,8 +364,9 @@ def _submit_task(self, fn: Any, *args: Any, **kwargs: Any) -> Future:
def _prepare_task(self, task_data: Dict[str, Any]) -> None:
"""Prepare the task."""
tool_task = AnyToolAsTask()
tool_py = self._all_tools[task_data["tool"]]
tool_py, callable_method = self._all_tools[task_data["tool"]]
task_data["tool_py"] = tool_py
task_data["callable_method"] = callable_method
task_data["api_keys"] = self.params.api_keys
future = self._submit_task(tool_task.execute, **task_data)
executing_task = cast(Dict[str, Any], self._executing_task)
Expand Down
8 changes: 5 additions & 3 deletions packages/valory/skills/task_execution/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidqhvvlnthkbnmrdkdeyjyx2f2ab6z4xdgmagh7welqnh2v6wczx4
behaviours.py: bafybeiahzfpgcjpvbhpfkbifrkzb6qaj2gesnkswdi2lj7yszhe27dvjtm
behaviours.py: bafybeia4ohotkawmpee6k7jr3jin2cnuoikxnm2r7vd3mgapewbt6ea6vy
dialogues.py: bafybeid4zxalqdlo5mw4yfbuf34hx4jp5ay5z6chm4zviwu4cj7fudtwca
handlers.py: bafybeidbt5ezj74cgfogk3w4uw4si2grlnk5g54veyumw7g5yh6gdscywu
models.py: bafybeihc2kmymmh5oousjddbc7xujqbk5niermuqak2dhtgryukzq5wxeq
utils/__init__.py: bafybeiccdijaigu6e5p2iruwo5mkk224o7ywedc7nr6xeu5fpmhjqgk24e
utils/ipfs.py: bafybeicuaj23qrcdv6ly4j7yo6il2r5plozhd6mwvcp5acwqbjxb2t3u2i
utils/task.py: bafybeiakokty64m5cqp72drrpvfckhruldlwcge5hcc2bsy2ujk6nnrazq
utils/ipfs.py: bafybeidinbdqkidix44ibz5hug7inkcbijooag53gr5mtbaa72tk335uqq
utils/task.py: bafybeieuziu7owtk543z3umgmayhjh67klftk7vrhz24l6rlaii5lvkqh4
fingerprint_ignore_patterns: []
connections:
- valory/ledger:0.19.0:bafybeia47rr37ianvwsh77tjjpv3nwif5sywhhy2fbdshnz4a2icwln76a
Expand Down Expand Up @@ -104,4 +104,6 @@ dependencies:
version: ==1.0.3
py-multicodec:
version: ==0.2.1
pyyaml:
version: <=6.0.1,>=3.10
is_abstract: false
46 changes: 46 additions & 0 deletions packages/valory/skills/task_execution/utils/ipfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#
# ------------------------------------------------------------------------------
"""This module contains helpers for IPFS interaction."""
from typing import Any, Dict, Tuple

import yaml
from aea.helpers.cid import CID
from multibase import multibase
from multicodec import multicodec
Expand Down Expand Up @@ -46,3 +49,46 @@ def to_multihash(hash_string: str) -> bytes:
# Convert the multihash bytes to a hexadecimal string
hex_multihash = multihash_bytes.hex()
return hex_multihash[6:]


class ComponentPackageLoader:
"""Component package loader."""

@staticmethod
def load(serialized_objects: Dict[str, str]) -> Tuple[Dict[str, Any], str, str]:
"""
Load a custom component package.

:param serialized_objects: the serialized objects.
:return: the component.yaml, entry_point.py and callable as tuple.
"""
# the package MUST contain a component.yaml file
if "component.yaml" not in serialized_objects:
raise ValueError(
"Invalid component package. "
"The package MUST contain a component.yaml."
)

# load the component.yaml file
component_yaml = yaml.safe_load(serialized_objects["component.yaml"])
if "entry_point" not in component_yaml or "callable" not in component_yaml:
raise ValueError(
"Invalid component package. "
"The component.yaml file MUST contain the 'entry_point' and 'callable' keys."
)

# the name of the script that needs to be executed
entry_point_name = component_yaml["entry_point"]

# load the script
if entry_point_name not in serialized_objects:
raise ValueError(
f"Invalid component package. "
f"{entry_point_name} is not present in the component package."
)
entry_point = serialized_objects[entry_point_name]

# the method that needs to be called
callable_method = component_yaml["callable"]

return component_yaml, entry_point, callable_method
7 changes: 4 additions & 3 deletions packages/valory/skills/task_execution/utils/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ class AnyToolAsTask:
def execute(self, *args: Any, **kwargs: Any) -> Any:
"""Execute the task."""
tool_py = kwargs.pop("tool_py")
if "run" in globals():
del globals()["run"]
callable_method = kwargs.pop("callable_method")
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this going to work with multiple packages having the same callable name?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yes

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

all tools had run as their callable and it worked

if callable_method in globals():
del globals()[callable_method]
exec(tool_py, globals()) # pylint: disable=W0122 # nosec
method = globals()["run"]
method = globals()[callable_method]
return method(*args, **kwargs)
20 changes: 20 additions & 0 deletions tools/native_transfer_request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 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.
#
# ------------------------------------------------------------------------------

"""This module contains the bet amount per threshold strategy."""
14 changes: 14 additions & 0 deletions tools/native_transfer_request/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: native_transfer_request
author: valory
version: 0.1.0
type: custom_component
description: A tool to prepare a native transfer request.
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidey4syohls5hxmso6qsp5p4uhtzle5txv2mlbym6ktjzknich6oa
native_transfer_request.py: bafybeibqwl52cnz64cysjd2jnjijuakdvyrffapxq65cdzx6g65gu42deq
fingerprint_ignore_patterns: []
entry_point: native_transfer_request.py
callable: run
dependencies: {}
20 changes: 20 additions & 0 deletions tools/openai_request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 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.
#
# ------------------------------------------------------------------------------

"""This module contains the bet amount per threshold strategy."""
14 changes: 14 additions & 0 deletions tools/openai_request/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: openai_request
author: valory
version: 0.1.0
type: custom_component
description: A tool that runs a prompt against the OpenAI API.
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidey4syohls5hxmso6qsp5p4uhtzle5txv2mlbym6ktjzknich6oa
openai_request.py: bafybeibqwl52cnz64cysjd2jnjijuakdvyrffapxq65cdzx6g65gu42deq
fingerprint_ignore_patterns: []
entry_point: openai_request.py
callable: run
dependencies: {}
File renamed without changes.
20 changes: 20 additions & 0 deletions tools/optimization_by_prompting/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 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.
#
# ------------------------------------------------------------------------------

"""This module contains the bet amount per threshold strategy."""
14 changes: 14 additions & 0 deletions tools/optimization_by_prompting/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: optimization_by_prompting
author: psouranis
version: 0.1.0
type: custom_component
description: A tool that implements the optimization by prompting methodology.
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidey4syohls5hxmso6qsp5p4uhtzle5txv2mlbym6ktjzknich6oa
optimization_by_prompting.py: bafybeibqwl52cnz64cysjd2jnjijuakdvyrffapxq65cdzx6g65gu42deq
fingerprint_ignore_patterns: []
entry_point: optimization_by_prompting.py
callable: run
dependencies: {}
20 changes: 20 additions & 0 deletions tools/prediction_request/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 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.
#
# ------------------------------------------------------------------------------

"""This module contains the bet amount per threshold strategy."""
14 changes: 14 additions & 0 deletions tools/prediction_request/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: prediction_request
author: valory
version: 0.1.0
type: custom_component
description: A tool for making binary predictions on markets using OpenAI API.
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidey4syohls5hxmso6qsp5p4uhtzle5txv2mlbym6ktjzknich6oa
prediction_request.py: bafybeibqwl52cnz64cysjd2jnjijuakdvyrffapxq65cdzx6g65gu42deq
fingerprint_ignore_patterns: []
entry_point: prediction_request.py
callable: run
dependencies: {}
20 changes: 20 additions & 0 deletions tools/prediction_request_claude/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 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.
#
# ------------------------------------------------------------------------------

"""This module contains the bet amount per threshold strategy."""
14 changes: 14 additions & 0 deletions tools/prediction_request_claude/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: prediction_request_claude
author: valory
version: 0.1.0
type: custom_component
description: A tool for making binary predictions on markets using claude.
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidey4syohls5hxmso6qsp5p4uhtzle5txv2mlbym6ktjzknich6oa
prediction_request_claude.py: bafybeibqwl52cnz64cysjd2jnjijuakdvyrffapxq65cdzx6g65gu42deq
fingerprint_ignore_patterns: []
entry_point: prediction_request_claude.py
callable: run
dependencies: {}
20 changes: 20 additions & 0 deletions tools/prediction_request_embedding/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 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.
#
# ------------------------------------------------------------------------------

"""This module contains the bet amount per threshold strategy."""
14 changes: 14 additions & 0 deletions tools/prediction_request_embedding/component.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: prediction_request_embedding
author: valory
version: 0.1.0
type: custom_component
description: A tool to establish Subject Matter Expert (SME) roles for specific market-related questions.
license: Apache-2.0
aea_version: '>=1.0.0, <2.0.0'
fingerprint:
__init__.py: bafybeidey4syohls5hxmso6qsp5p4uhtzle5txv2mlbym6ktjzknich6oa
prediction_request_embedding.py: bafybeibqwl52cnz64cysjd2jnjijuakdvyrffapxq65cdzx6g65gu42deq
fingerprint_ignore_patterns: []
entry_point: prediction_request_embedding.py
callable: run
dependencies: {}
Loading
Loading