Skip to content

Commit

Permalink
Merge pull request #343 from valory-xyz/feat/persist-tool-accuracy
Browse files Browse the repository at this point in the history
Feat/persist tool accuracy
  • Loading branch information
annasambrook authored Nov 18, 2024
2 parents e750d12 + 1e23ec2 commit f4e122b
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 31 deletions.
12 changes: 6 additions & 6 deletions packages/packages.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
"contract/valory/staking_token/0.1.0": "bafybeiep4r6qyilbfgzdvx6t7zvpgaioxqktmxm7puwtnbpb2ftlib43gy",
"contract/valory/relayer/0.1.0": "bafybeicawmds6czx7db2lcktvexwrp245jpekgulndtos5s5zdid3ilvq4",
"skill/valory/market_manager_abci/0.1.0": "bafybeicia7itulcrxlwbmhfzxppgo5i33tb2nmcycwizftw7gps4dareka",
"skill/valory/decision_maker_abci/0.1.0": "bafybeia5qzqac2g444myog3tlwagthcknmanqunnytbjfsuk2msep2rnna",
"skill/valory/trader_abci/0.1.0": "bafybeigz6mm7xzhe334iuyy6nw4tdymxfjp6fp37firbzznxg7mowtvcoe",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeigt42cdisy2z53u5pqq3qmjk5bb32cm5oegftptv56m7yq5dbdoyi",
"skill/valory/decision_maker_abci/0.1.0": "bafybeies6fzgumlvqvxs2hl2ib5sogid5pxyyuvdlyahobtfxnekyxlb4a",
"skill/valory/trader_abci/0.1.0": "bafybeiduheiehpzyhh4jccdqno3bu3wmmmuoi5udewky3jjzyncbqee35a",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeieu2l6dkg53f5eyvuttk2xywe3uln2h4n6uoadpoariznejoizj7a",
"skill/valory/staking_abci/0.1.0": "bafybeictd5pxhscuhqntvctb7l5lfjausxt2m22rg5mkaiuj4cwwcxpvne",
"skill/valory/check_stop_trading_abci/0.1.0": "bafybeifmi64g4ki6zwbcncb35ovhd4sllw4xrszrkturpeqdhgf5bkiini",
"agent/valory/trader/0.1.0": "bafybeidcn54f67yhqaa7aycsmurm4tvmx2dj2e2zir6keft3ubxchcu6hu",
"service/valory/trader/0.1.0": "bafybeig6jsofnzrst72r3qannxrpbrbfd7c4nvdcvacsplcx7gnyroqeuu",
"service/valory/trader_pearl/0.1.0": "bafybeih456qaxmfhx7bhhk2wdzjq4s3mbxmkfdveyucgwdh7ilqio4ruvm"
"agent/valory/trader/0.1.0": "bafybeie52jxu5w4xuoiy3ottfdojnr6pism6xiaat4vipj56mhs2paa2uu",
"service/valory/trader/0.1.0": "bafybeies7dwitrixe2ooxyapkd5zagzb47c44wgok5zjlcmosjdqco6uf4",
"service/valory/trader_pearl/0.1.0": "bafybeiejqghxhnskc2gecekjc4eth2yg4csd2af6uhboa3vvqpvwdtjdgq"
},
"third_party": {
"protocol/open_aea/signing/1.0.0": "bafybeihv62fim3wl2bayavfcg3u5e5cxu3b7brtu4cn5xoxd6lqwachasi",
Expand Down
7 changes: 4 additions & 3 deletions packages/valory/agents/trader/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ skills:
- valory/reset_pause_abci:0.1.0:bafybeigrdlxed3xlsnxtjhnsbl3cojruihxcqx4jxhgivkd5i2fkjncgba
- valory/termination_abci:0.1.0:bafybeib5l7jhew5ic6iq24dd23nidcoimzqkrk556gqywhoziatj33zvwm
- valory/transaction_settlement_abci:0.1.0:bafybeic7q7recyka272udwcupblwbkc3jkodgp74fvcdxb7urametg5dae
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeigt42cdisy2z53u5pqq3qmjk5bb32cm5oegftptv56m7yq5dbdoyi
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeieu2l6dkg53f5eyvuttk2xywe3uln2h4n6uoadpoariznejoizj7a
- valory/market_manager_abci:0.1.0:bafybeicia7itulcrxlwbmhfzxppgo5i33tb2nmcycwizftw7gps4dareka
- valory/decision_maker_abci:0.1.0:bafybeia5qzqac2g444myog3tlwagthcknmanqunnytbjfsuk2msep2rnna
- valory/trader_abci:0.1.0:bafybeigz6mm7xzhe334iuyy6nw4tdymxfjp6fp37firbzznxg7mowtvcoe
- valory/decision_maker_abci:0.1.0:bafybeies6fzgumlvqvxs2hl2ib5sogid5pxyyuvdlyahobtfxnekyxlb4a
- valory/trader_abci:0.1.0:bafybeiduheiehpzyhh4jccdqno3bu3wmmmuoi5udewky3jjzyncbqee35a
- valory/staking_abci:0.1.0:bafybeictd5pxhscuhqntvctb7l5lfjausxt2m22rg5mkaiuj4cwwcxpvne
- valory/check_stop_trading_abci:0.1.0:bafybeifmi64g4ki6zwbcncb35ovhd4sllw4xrszrkturpeqdhgf5bkiini
- valory/mech_interact_abci:0.1.0:bafybeid6m3i5ofq7vuogqapdnoshhq7mswmudhvfcr2craw25fdwtoe3lm
Expand Down Expand Up @@ -237,6 +237,7 @@ models:
rebet_chance: ${float:0.6}
mech_interaction_sleep_time: ${int:10}
use_mech_marketplace: ${bool:false}
policy_store_update_offset: ${int:259200}
mech_marketplace_config:
mech_marketplace_address: ${str:0x0000000000000000000000000000000000000000}
priority_mech_address: ${str:0x0000000000000000000000000000000000000000}
Expand Down
6 changes: 5 additions & 1 deletion packages/valory/services/trader/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: bafybeigtuothskwyvrhfosps2bu6suauycolj67dpuxqvnicdrdu7yhtvq
fingerprint_ignore_patterns: []
agent: valory/trader:0.1.0:bafybeidcn54f67yhqaa7aycsmurm4tvmx2dj2e2zir6keft3ubxchcu6hu
agent: valory/trader:0.1.0:bafybeie52jxu5w4xuoiy3ottfdojnr6pism6xiaat4vipj56mhs2paa2uu
number_of_agents: 4
deployment:
agent:
Expand Down Expand Up @@ -144,6 +144,7 @@ type: skill
safe_voting_range: ${SAFE_VOTING_RANGE:int:600}
rebet_chance: ${REBET_CHANCE:float:0.6}
mech_interaction_sleep_time: ${MECH_INTERACTION_SLEEP_TIME:int:10}
policy_store_update_offset: ${POLICY_STORE_UPDATE_OFFSET:int:259200}
benchmark_tool: &id004
args:
log_dir: ${LOG_DIR:str:/benchmarks}
Expand Down Expand Up @@ -366,6 +367,7 @@ type: skill
safe_voting_range: ${SAFE_VOTING_RANGE:int:600}
rebet_chance: ${REBET_CHANCE:float:0.6}
mech_interaction_sleep_time: ${MECH_INTERACTION_SLEEP_TIME:int:10}
policy_store_update_offset: ${POLICY_STORE_UPDATE_OFFSET:int:259200}
benchmark_tool: *id004
acc_info_fields: *id005
network_subgraph: *id006
Expand Down Expand Up @@ -482,6 +484,7 @@ type: skill
safe_voting_range: ${SAFE_VOTING_RANGE:int:600}
rebet_chance: ${REBET_CHANCE:float:0.6}
mech_interaction_sleep_time: ${MECH_INTERACTION_SLEEP_TIME:int:10}
policy_store_update_offset: ${POLICY_STORE_UPDATE_OFFSET:int:259200}
benchmark_tool: *id004
acc_info_fields: *id005
network_subgraph: *id006
Expand Down Expand Up @@ -598,6 +601,7 @@ type: skill
safe_voting_range: ${SAFE_VOTING_RANGE:int:600}
rebet_chance: ${REBET_CHANCE:float:0.6}
mech_interaction_sleep_time: ${MECH_INTERACTION_SLEEP_TIME:int:10}
policy_store_update_offset: ${POLICY_STORE_UPDATE_OFFSET:int:259200}
benchmark_tool: *id004
acc_info_fields: *id005
network_subgraph: *id006
Expand Down
3 changes: 2 additions & 1 deletion packages/valory/services/trader_pearl/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license: Apache-2.0
fingerprint:
README.md: bafybeibg7bdqpioh4lmvknw3ygnllfku32oca4eq5pqtvdrdsgw6buko7e
fingerprint_ignore_patterns: []
agent: valory/trader:0.1.0:bafybeidcn54f67yhqaa7aycsmurm4tvmx2dj2e2zir6keft3ubxchcu6hu
agent: valory/trader:0.1.0:bafybeie52jxu5w4xuoiy3ottfdojnr6pism6xiaat4vipj56mhs2paa2uu
number_of_agents: 1
deployment:
agent:
Expand Down Expand Up @@ -102,6 +102,7 @@ models:
priority_mech_service_id: ${PRIORITY_MECH_SERVICE_ID:int:0}
requester_staking_instance_address: ${REQUESTER_STAKING_INSTANCE_ADDRESS:str:0x0000000000000000000000000000000000000000}
response_timeout: ${RESPONSE_TIMEOUT:int:300}
policy_store_update_offset: ${POLICY_STORE_UPDATE_OFFSET:int:259200}
benchmark_tool:
args:
log_dir: /benchmarks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import csv
import json
from abc import ABC
from datetime import datetime
from io import StringIO
from typing import Any, Dict, Generator, List, Optional

Expand All @@ -45,6 +46,8 @@
UTILIZED_TOOLS_STORE = "utilized_tools.json"
GET = "GET"
OK_CODE = 200
MAX_STR = "max"
DATETIME_FORMAT_STR = "%Y-%m-%d %H:%M:%S"


class StorageManagerBehaviour(DecisionMakerBaseBehaviour, ABC):
Expand All @@ -57,7 +60,7 @@ def __init__(self, **kwargs: Any) -> None:
self._mech_hash: str = ""
self._utilized_tools: Dict[str, str] = {}
self._mech_tools: Optional[List[str]] = None
self._accuracy_information: StringIO = StringIO()
self._remote_accuracy_information: StringIO = StringIO()

@property
def mech_tools(self) -> List[str]:
Expand All @@ -72,14 +75,14 @@ def mech_tools(self, mech_tools: List[str]) -> None:
self._mech_tools = mech_tools

@property
def accuracy_information(self) -> StringIO:
def remote_accuracy_information(self) -> StringIO:
"""Get the accuracy information."""
return self._accuracy_information
return self._remote_accuracy_information

@accuracy_information.setter
def accuracy_information(self, accuracy_information: StringIO) -> None:
@remote_accuracy_information.setter
def remote_accuracy_information(self, accuracy_information: StringIO) -> None:
"""Set the accuracy information."""
self._accuracy_information = accuracy_information
self._remote_accuracy_information = accuracy_information

@property
def mech_id(self) -> int:
Expand Down Expand Up @@ -257,7 +260,7 @@ def _fetch_accuracy_info(self) -> Generator[None, None, bool]:

self.context.logger.info("Parsing accuracy information of the tools...")
try:
self.accuracy_information = StringIO(response.body.decode())
self.remote_accuracy_information = StringIO(response.body.decode())
except (ValueError, TypeError) as e:
self.context.logger.error(
f"Could not parse response from ipfs server, "
Expand All @@ -267,12 +270,68 @@ def _fetch_accuracy_info(self) -> Generator[None, None, bool]:

return True

def _fetch_remote_tool_date(self) -> int:
"""Fetch the max transaction date from the remote accuracy storage."""
self.context.logger.info("Checking remote accuracy information date... ")
self.context.logger.info("Trying to read max date in file...")
accuracy_information = self.remote_accuracy_information

max_transaction_date = None

if accuracy_information:
sep = self.acc_info_fields.sep
accuracy_information.seek(0) # Ensure we’re at the beginning
reader = csv.DictReader(accuracy_information.readlines(), delimiter=sep)

# try to read the maximum transaction date in the remote accuracy info
try:
for row in reader:
current_transaction_date = row.get(MAX_STR)
if (
max_transaction_date is None
or current_transaction_date > max_transaction_date
):
max_transaction_date = current_transaction_date

except TypeError:
self.context.logger.warning(
"Invalid transaction date found. Continuing with local accuracy information..."
)
return 0

if max_transaction_date:
self.context.logger.info(f"Maximum date found: {max_transaction_date}")
max_datetime = datetime.strptime(max_transaction_date, DATETIME_FORMAT_STR)
unix_timestamp = int(max_datetime.timestamp())
return unix_timestamp

self.context.logger.info("No maximum date found.")
return 0

def _check_local_policy_store_overwrite(self) -> bool:
"""Compare the local and remote policy store dates and decide which to use."""

local_policy_store_date = self.policy.updated_ts
remote_policy_store_date = self._fetch_remote_tool_date()
policy_store_update_offset = self.params.policy_store_update_offset

self.context.logger.info("Comparing tool accuracy dates...")

overwrite = (
True
if remote_policy_store_date
> (local_policy_store_date - policy_store_update_offset)
else False
)
self.context.logger.info(f"Local policy store overwrite: {overwrite}.")
return overwrite

def _update_accuracy_store(self) -> None:
"""Update the accuracy store file with the latest information available"""
self.context.logger.info("Updating accuracy information of the policy...")
sep = self.acc_info_fields.sep
reader: csv.DictReader = csv.DictReader(
self.accuracy_information, delimiter=sep
self.remote_accuracy_information, delimiter=sep
)
accuracy_store = self.policy.accuracy_store

Expand Down Expand Up @@ -312,11 +371,14 @@ def _set_policy(self) -> Generator:
)
self._policy = self.synchronized_data.policy

if self.is_first_period:
yield from self.wait_for_condition_with_sleep(
self._fetch_accuracy_info, sleep_time_override=self.params.sleep_time
)
self._update_accuracy_store()
yield from self.wait_for_condition_with_sleep(
self._fetch_accuracy_info, sleep_time_override=self.params.sleep_time
)
overwrite_local_store = self._check_local_policy_store_overwrite()

if self.is_first_period and overwrite_local_store:
self.policy.updated_ts = int(datetime.now().timestamp())
self._update_accuracy_store()

def _try_recover_utilized_tools(self) -> Dict[str, str]:
"""Try to recover the utilized tools from the tools store."""
Expand Down
3 changes: 3 additions & 0 deletions packages/valory/skills/decision_maker_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.service_endpoint = self._ensure("service_endpoint", kwargs, str)
self.safe_voting_range = self._ensure("safe_voting_range", kwargs, int)
self.rebet_chance = self._ensure("rebet_chance", kwargs, float)
self.policy_store_update_offset = self._ensure(
"policy_store_update_offset", kwargs, int
)
super().__init__(*args, **kwargs)

@property
Expand Down
9 changes: 8 additions & 1 deletion packages/valory/skills/decision_maker_abci/policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,13 @@ def hook(
"""Perform the custom decoding."""
for cls_ in (AccuracyInfo, EGreedyPolicy):
cls_attributes = cls_.__annotations__.keys() # pylint: disable=no-member
if sorted(cls_attributes) == sorted(data.keys()):
if sorted(cls_attributes) == sorted(data.keys()) or (
cls_ == EGreedyPolicy
and sorted(cls_attributes - {"updated_ts"}) == sorted(data.keys())
):
# If EGreedyPolicy and 'updated_ts' is missing, set it to 0
if cls_ == EGreedyPolicy and "updated_ts" not in data:
data["updated_ts"] = 0
# if the attributes match the ones of the current class, use it to perform the deserialization
return cls_(**data)

Expand All @@ -89,6 +95,7 @@ class EGreedyPolicy:
eps: float
accuracy_store: Dict[str, AccuracyInfo] = field(default_factory=dict)
weighted_accuracy: Dict[str, float] = field(default_factory=dict)
updated_ts: int = 0

def __post_init__(self) -> None:
"""Perform post-initialization checks."""
Expand Down
7 changes: 4 additions & 3 deletions packages/valory/skills/decision_maker_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ fingerprint:
behaviours/reedem.py: bafybeiaxwp4lx62owcaqfp6xcqh6567f5yvwnl4rage2f5hmq4nltkzjjy
behaviours/round_behaviour.py: bafybeih63hpia2bwwzu563hxs5yd3t5ycvxvkfnhvxbzghbyy3mw3xjl3i
behaviours/sampling.py: bafybeiekxzublnsvgjb2y2uvtye4zkethvmsxh4itonnwmb3sbviyyhsgq
behaviours/storage_manager.py: bafybeic7c23rtp3dvgstcusmyxkcm22bpf2bfydljiam2nf55tleym5j3u
behaviours/storage_manager.py: bafybeignur4cwwkdmoj33ll3pmylnddbaqd6gpn6m2n7idieyyti7m3jg4
behaviours/tool_selection.py: bafybeienlxcgjs3ogyofli3d7q3p5rst3mcxxcnwqf7qolqjeefjtixeke
dialogues.py: bafybeigpwuzku3we7axmxeamg7vn656maww6emuztau5pg3ebsoquyfdqm
fsm_specification.yaml: bafybeiabt2fonv63hozgr7bt4d5auom76iufzh6etpnmbwekktckz7644a
handlers.py: bafybeigod6gbjrxy4mbmulbzsbokeaoycoqys64vqtxnumishfukzf73za
io_/__init__.py: bafybeifxgmmwjqzezzn3e6keh2bfo4cyo7y5dq2ept3stfmgglbrzfl5rq
io_/loader.py: bafybeih3sdsx5dhe4kzhtoafexjgkutsujwqy3zcdrlrkhtdks45bc7exa
models.py: bafybeicpwbvlrj4ag3kcbhjtp6ojvy4g5ggh7zjw47iwilmmhye72lt3fy
models.py: bafybeifhmwxoix5fqka4phtmv6324osei52zwirg3ogziondvlq33brnby
payloads.py: bafybeicloiy4ax7dlipwp4czlueflgjgtlev4a6vhn2m7ztoehnemiiko4
policy.py: bafybeihcltocxg7zsmvjtx44ilal4zir4fz2rupgmhacktzv5neb2lmas4
policy.py: bafybeihlzs4o5e7yfmfzcvvrzkf4bhxfsg5gxnzsrpepwgfugh45gafye4
redeem_info.py: bafybeifiiix4gihfo4avraxt34sfw35v6dqq45do2drrssei2shbps63mm
rounds.py: bafybeihczrvkid4lqoai7myqqs4f3g5diqsnjotxtqhxhm5c7oowzifbqy
states/__init__.py: bafybeid23llnyp6j257dluxmrnztugo5llsrog7kua53hllyktz4dqhqoy
Expand Down Expand Up @@ -247,6 +247,7 @@ models:
policy_epsilon: 0.1
use_subgraph_for_redeeming: true
use_nevermined: true
policy_store_update_offset: 259200
mech_to_subscription_params:
- - base_url
- https://marketplace-api.gnosis.nevermined.app/api/v1/metadata/assets/ddo
Expand Down
5 changes: 3 additions & 2 deletions packages/valory/skills/trader_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ skills:
- valory/transaction_settlement_abci:0.1.0:bafybeic7q7recyka272udwcupblwbkc3jkodgp74fvcdxb7urametg5dae
- valory/termination_abci:0.1.0:bafybeib5l7jhew5ic6iq24dd23nidcoimzqkrk556gqywhoziatj33zvwm
- valory/market_manager_abci:0.1.0:bafybeicia7itulcrxlwbmhfzxppgo5i33tb2nmcycwizftw7gps4dareka
- valory/decision_maker_abci:0.1.0:bafybeia5qzqac2g444myog3tlwagthcknmanqunnytbjfsuk2msep2rnna
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeigt42cdisy2z53u5pqq3qmjk5bb32cm5oegftptv56m7yq5dbdoyi
- valory/decision_maker_abci:0.1.0:bafybeies6fzgumlvqvxs2hl2ib5sogid5pxyyuvdlyahobtfxnekyxlb4a
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeieu2l6dkg53f5eyvuttk2xywe3uln2h4n6uoadpoariznejoizj7a
- valory/staking_abci:0.1.0:bafybeictd5pxhscuhqntvctb7l5lfjausxt2m22rg5mkaiuj4cwwcxpvne
- valory/check_stop_trading_abci:0.1.0:bafybeifmi64g4ki6zwbcncb35ovhd4sllw4xrszrkturpeqdhgf5bkiini
- valory/mech_interact_abci:0.1.0:bafybeid6m3i5ofq7vuogqapdnoshhq7mswmudhvfcr2craw25fdwtoe3lm
Expand Down Expand Up @@ -178,6 +178,7 @@ models:
slippage: 0.01
policy_epsilon: 0.1
store_path: /data/
policy_store_update_offset: 259200
use_subgraph_for_redeeming: true
irrelevant_tools:
- openai-text-davinci-002
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protocols:
- valory/ledger_api:1.0.0:bafybeihdk6psr4guxmbcrc26jr2cbgzpd5aljkqvpwo64bvaz7tdti2oni
skills:
- valory/abstract_round_abci:0.1.0:bafybeib733xfbndtpvkf44mtk7oyodnficgloo6xhn7xmqxxeos33es65u
- valory/decision_maker_abci:0.1.0:bafybeia5qzqac2g444myog3tlwagthcknmanqunnytbjfsuk2msep2rnna
- valory/decision_maker_abci:0.1.0:bafybeies6fzgumlvqvxs2hl2ib5sogid5pxyyuvdlyahobtfxnekyxlb4a
- valory/staking_abci:0.1.0:bafybeictd5pxhscuhqntvctb7l5lfjausxt2m22rg5mkaiuj4cwwcxpvne
- valory/mech_interact_abci:0.1.0:bafybeid6m3i5ofq7vuogqapdnoshhq7mswmudhvfcr2craw25fdwtoe3lm
behaviours:
Expand Down

0 comments on commit f4e122b

Please sign in to comment.