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/persist tool accuracy #343

Open
wants to merge 26 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4b2828c
feat: persisting local tool info
annasambrook Oct 22, 2024
e98709e
changes to storage manager
annasambrook Oct 25, 2024
07c1c34
add updated_ts to EGreedyPolicy
annasambrook Oct 25, 2024
4be7e8b
storage manager changes
annasambrook Oct 25, 2024
b123430
refactor storage manager changes to remove redundant code
annasambrook Oct 25, 2024
553fc63
tomte check-code
annasambrook Oct 25, 2024
24eac66
chore: generators
annasambrook Oct 25, 2024
dce1f33
Merge branch 'refs/heads/main' into feat/persist-tool-accuracy
annasambrook Oct 25, 2024
2380698
chore: generators
annasambrook Oct 25, 2024
729f0f0
make type optional
annasambrook Oct 25, 2024
fb6daa1
add suggested changes
annasambrook Nov 1, 2024
d574ce1
refactor: _check_local_policy_store_overwrite
annasambrook Nov 14, 2024
427c678
refactor: constants
annasambrook Nov 14, 2024
857e999
implemented requested changes
annasambrook Nov 14, 2024
041eb72
chore: generators
annasambrook Nov 14, 2024
633c2ab
Merge remote-tracking branch 'refs/remotes/origin/main' into feat/per…
annasambrook Nov 14, 2024
ad023f6
chore: generators
annasambrook Nov 14, 2024
d38e7ab
fix if not accuracy_information
annasambrook Nov 15, 2024
d954db0
fix policy_store_update_offset not overridden at service level
annasambrook Nov 15, 2024
6b07270
remove elif is_first_period
annasambrook Nov 15, 2024
134b4ca
chore: generators
annasambrook Nov 15, 2024
31930a1
remove unused import
annasambrook Nov 15, 2024
acf1e8f
chore: generators
annasambrook Nov 15, 2024
5cce568
policy_store_update_offset override
annasambrook Nov 15, 2024
8f462e5
Merge branch 'refs/heads/main' into feat/persist-tool-accuracy
annasambrook Nov 15, 2024
1e23ec2
chore: generators
annasambrook Nov 15, 2024
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
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": "bafybeiaizbnlz46lba6tcwmajpo2z37cpxuobeqmlgdqbdxnt65jr6rely",
"skill/valory/trader_abci/0.1.0": "bafybeido4dpfcvpyrobs2veoliuwaz2ltuzvo2gqfbsqxgd5tdr7zllyru",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeieszb6lxyy5gt4m4f75fjicuzf3ewsxgxcc5dh4l2tjjau7wcrgxe",
"skill/valory/decision_maker_abci/0.1.0": "bafybeif7mb7jzcxm2jowee53avcljgfjlzzixq3u5fgwa75ga3ikol2g3m",
"skill/valory/trader_abci/0.1.0": "bafybeige67doi3tsjstkobsnafa2d5rqcm4tpw2vkej755t3knqhu3qdtq",
"skill/valory/tx_settlement_multiplexer_abci/0.1.0": "bafybeigkuozh6m4l6qrvott7olopblaiezqlbq2geaqplb5lzspyo2v2ey",
"skill/valory/staking_abci/0.1.0": "bafybeictd5pxhscuhqntvctb7l5lfjausxt2m22rg5mkaiuj4cwwcxpvne",
"skill/valory/check_stop_trading_abci/0.1.0": "bafybeifmi64g4ki6zwbcncb35ovhd4sllw4xrszrkturpeqdhgf5bkiini",
"agent/valory/trader/0.1.0": "bafybeifaw3dk5uklcgj5iftcdhj4p2ymkidoli3uxibq4olddd3eboey7e",
"service/valory/trader/0.1.0": "bafybeiaqqlwqjiofrezpnk4hq6doiccbquzgmd6pcgtfx7akmfhhvyoipi",
"service/valory/trader_pearl/0.1.0": "bafybeidzv544ive2oozjk2szmengrcszea7ai54qts7ehdjdkwevwtwfba"
"agent/valory/trader/0.1.0": "bafybeialurh44ecpu5wgttfgv2ofmrmzgyxuok4utdxucp4mbqnp7nspay",
"service/valory/trader/0.1.0": "bafybeiasj27q7vwyqkxg4mcojrndzwhakoy3sd7o6ukee3sb4pdakzmu7e",
"service/valory/trader_pearl/0.1.0": "bafybeickeykdynteoeu7vcmmrz3gcf266qbryhkk6y5oilpdsrwcxhxp34"
},
"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:bafybeieszb6lxyy5gt4m4f75fjicuzf3ewsxgxcc5dh4l2tjjau7wcrgxe
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeigkuozh6m4l6qrvott7olopblaiezqlbq2geaqplb5lzspyo2v2ey
- valory/market_manager_abci:0.1.0:bafybeicia7itulcrxlwbmhfzxppgo5i33tb2nmcycwizftw7gps4dareka
- valory/decision_maker_abci:0.1.0:bafybeiaizbnlz46lba6tcwmajpo2z37cpxuobeqmlgdqbdxnt65jr6rely
- valory/trader_abci:0.1.0:bafybeido4dpfcvpyrobs2veoliuwaz2ltuzvo2gqfbsqxgd5tdr7zllyru
- valory/decision_maker_abci:0.1.0:bafybeif7mb7jzcxm2jowee53avcljgfjlzzixq3u5fgwa75ga3ikol2g3m
- valory/trader_abci:0.1.0:bafybeige67doi3tsjstkobsnafa2d5rqcm4tpw2vkej755t3knqhu3qdtq
- 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}
Adamantios marked this conversation as resolved.
Show resolved Hide resolved
mech_marketplace_config:
mech_marketplace_address: ${str:0x0000000000000000000000000000000000000000}
priority_mech_address: ${str:0x0000000000000000000000000000000000000000}
Expand Down
2 changes: 1 addition & 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:bafybeifaw3dk5uklcgj5iftcdhj4p2ymkidoli3uxibq4olddd3eboey7e
agent: valory/trader:0.1.0:bafybeialurh44ecpu5wgttfgv2ofmrmzgyxuok4utdxucp4mbqnp7nspay
number_of_agents: 4
deployment:
agent:
Expand Down
2 changes: 1 addition & 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:bafybeifaw3dk5uklcgj5iftcdhj4p2ymkidoli3uxibq4olddd3eboey7e
agent: valory/trader:0.1.0:bafybeialurh44ecpu5wgttfgv2ofmrmzgyxuok4utdxucp4mbqnp7nspay
number_of_agents: 1
deployment:
agent:
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 @@ -36,6 +37,7 @@
from packages.valory.skills.decision_maker_abci.models import AgentToolsSpecs
from packages.valory.skills.decision_maker_abci.policy import (
AccuracyInfo,
DataclassEncoder,
EGreedyPolicy,
)

Expand All @@ -45,6 +47,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 Down Expand Up @@ -267,6 +271,62 @@ 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.accuracy_information

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)

max_transaction_date = None

# try to read the maximum transaction date in the remote accuracy info
try:
for row in reader:
Adamantios marked this conversation as resolved.
Show resolved Hide resolved
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...")
Expand Down Expand Up @@ -312,11 +372,18 @@ 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()

elif self.is_first_period:
policy_json = json.dumps(self.policy, cls=DataclassEncoder)
self.accuracy_information = StringIO(policy_json)
Adamantios marked this conversation as resolved.
Show resolved Hide resolved

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
annasambrook marked this conversation as resolved.
Show resolved Hide resolved

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: bafybeid4ref74rphfzpox4vbsfrhmo4i7vmvx6jk5pnjarogpn25xjrfbm
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 @@ -231,6 +231,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:bafybeiaizbnlz46lba6tcwmajpo2z37cpxuobeqmlgdqbdxnt65jr6rely
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeieszb6lxyy5gt4m4f75fjicuzf3ewsxgxcc5dh4l2tjjau7wcrgxe
- valory/decision_maker_abci:0.1.0:bafybeif7mb7jzcxm2jowee53avcljgfjlzzixq3u5fgwa75ga3ikol2g3m
- valory/tx_settlement_multiplexer_abci:0.1.0:bafybeigkuozh6m4l6qrvott7olopblaiezqlbq2geaqplb5lzspyo2v2ey
- 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:bafybeiaizbnlz46lba6tcwmajpo2z37cpxuobeqmlgdqbdxnt65jr6rely
- valory/decision_maker_abci:0.1.0:bafybeif7mb7jzcxm2jowee53avcljgfjlzzixq3u5fgwa75ga3ikol2g3m
- valory/staking_abci:0.1.0:bafybeictd5pxhscuhqntvctb7l5lfjausxt2m22rg5mkaiuj4cwwcxpvne
- valory/mech_interact_abci:0.1.0:bafybeid6m3i5ofq7vuogqapdnoshhq7mswmudhvfcr2craw25fdwtoe3lm
behaviours:
Expand Down
Loading