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

469 missing information in ipfsagentresult #521

Merged
merged 10 commits into from
Nov 5, 2024
24 changes: 17 additions & 7 deletions prediction_market_agent_tooling/deploy/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,10 +153,10 @@ def langfuse_update_current_trace(
input=input,
output=output,
user_id=user_id or getpass.getuser(),
session_id=session_id
or self.session_id, # All traces within a single run execution will be grouped under a single session.
version=version
or APIKeys().LANGFUSE_DEPLOYMENT_VERSION, # Optionally, mark the current deployment with version (e.g. add git commit hash during docker building).
session_id=session_id or self.session_id,
# All traces within a single run execution will be grouped under a single session.
version=version or APIKeys().LANGFUSE_DEPLOYMENT_VERSION,
# Optionally, mark the current deployment with version (e.g. add git commit hash during docker building).
release=release,
metadata=metadata,
tags=tags,
Expand Down Expand Up @@ -342,6 +342,10 @@ def update_langfuse_trace_by_processed_market(
]
)

@property
def agent_name(self) -> str:
return self.__class__.__name__

Comment on lines +345 to +348
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Consider making agent_name more configurable

While the current implementation works, consider making it more flexible by allowing subclasses to customize their agent name, either through constructor parameter or class attribute.

-    @property
-    def agent_name(self) -> str:
-        return self.__class__.__name__
+    def __init__(
+        self,
+        enable_langfuse: bool = APIKeys().default_enable_langfuse,
+        store_prediction: bool = True,
+        agent_name: str | None = None,
+    ) -> None:
+        super().__init__(enable_langfuse=enable_langfuse)
+        self.store_prediction = store_prediction
+        self._agent_name = agent_name
+
+    @property
+    def agent_name(self) -> str:
+        return self._agent_name or self.__class__.__name__

Committable suggestion skipped: line range outside the PR's diff.

def check_min_required_balance_to_operate(self, market_type: MarketType) -> None:
api_keys = APIKeys()

Expand Down Expand Up @@ -444,7 +448,9 @@ def after_process_market(
) -> None:
keys = APIKeys()
if self.store_prediction:
market.store_prediction(processed_market=processed_market, keys=keys)
market.store_prediction(
processed_market=processed_market, keys=keys, agent_name=self.agent_name
)
else:
logger.info(
f"Prediction {processed_market} not stored because {self.store_prediction=}."
Expand Down Expand Up @@ -613,10 +619,14 @@ def after_process_market(
processed_market: ProcessedMarket | None,
) -> None:
api_keys = APIKeys()
super().after_process_market(market_type, market, processed_market)
super().after_process_market(
market_type,
market,
processed_market,
)
if isinstance(processed_market, ProcessedTradedMarket):
if self.store_trades:
market.store_trades(processed_market, api_keys)
market.store_trades(processed_market, api_keys, self.agent_name)
else:
logger.info(
f"Trades {processed_market.trades} not stored because {self.store_trades=}."
Expand Down
10 changes: 8 additions & 2 deletions prediction_market_agent_tooling/markets/agent_market.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,21 @@ def verify_operational_balance(api_keys: APIKeys) -> bool:
raise NotImplementedError("Subclasses must implement this method")

def store_prediction(
self, processed_market: ProcessedMarket | None, keys: APIKeys
self,
processed_market: ProcessedMarket | None,
keys: APIKeys,
agent_name: str,
gabrielfior marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
"""
If market allows to upload predictions somewhere, implement it in this method.
"""
raise NotImplementedError("Subclasses must implement this method")

def store_trades(
self, traded_market: ProcessedTradedMarket | None, keys: APIKeys
self,
traded_market: ProcessedTradedMarket | None,
keys: APIKeys,
agent_name: str,
gabrielfior marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
"""
If market allows to upload trades somewhere, implement it in this method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def get_binary_markets(
return [MetaculusAgentMarket.from_data_model(q) for q in all_questions[:limit]]

def store_prediction(
self, processed_market: ProcessedMarket | None, keys: APIKeys
self, processed_market: ProcessedMarket | None, keys: APIKeys, agent_name: str
) -> None:
if processed_market is not None:
make_prediction(self.id, processed_market.answer.p_yes)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ def from_tuple(values: tuple[t.Any]) -> "ContractPrediction":

class IPFSAgentResult(BaseModel):
reasoning: str

agent_name: str
model_config = ConfigDict(
extra="forbid",
)
22 changes: 14 additions & 8 deletions prediction_market_agent_tooling/markets/omen/omen.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,17 +413,21 @@ def get_trade_balance(api_keys: APIKeys, web3: Web3 | None = None) -> xDai:
@staticmethod
def verify_operational_balance(api_keys: APIKeys) -> bool:
return get_total_balance(
api_keys.public_key, # Use `public_key`, not `bet_from_address` because transaction costs are paid from the EOA wallet.
api_keys.public_key,
# Use `public_key`, not `bet_from_address` because transaction costs are paid from the EOA wallet.
sum_wxdai=False,
) > xdai_type(0.001)

def store_prediction(
self, processed_market: ProcessedMarket | None, keys: APIKeys
self, processed_market: ProcessedMarket | None, keys: APIKeys, agent_name: str
) -> None:
"""On Omen, we have to store predictions along with trades, see `store_trades`."""

def store_trades(
self, traded_market: ProcessedTradedMarket | None, keys: APIKeys
self,
traded_market: ProcessedTradedMarket | None,
keys: APIKeys,
agent_name: str,
) -> None:
if traded_market is None:
logger.warning(f"No prediction for market {self.id}, not storing anything.")
Expand All @@ -437,7 +441,7 @@ def store_trades(
if keys.enable_ipfs_upload:
logger.info("Storing prediction on IPFS.")
ipfs_hash = IPFSHandler(keys).store_agent_result(
IPFSAgentResult(reasoning=reasoning)
IPFSAgentResult(reasoning=reasoning, agent_name=agent_name)
)
ipfs_hash_decoded = ipfscidv0_to_byte32(ipfs_hash)

Expand Down Expand Up @@ -1237,12 +1241,12 @@ def redeem_from_all_user_positions(

if not conditional_token_contract.is_condition_resolved(condition_id):
logger.info(
f"[{index+1} / {len(user_positions)}] Skipping redeem, {user_position.id=} isn't resolved yet."
f"[{index + 1} / {len(user_positions)}] Skipping redeem, {user_position.id=} isn't resolved yet."
)
continue

logger.info(
f"[{index+1} / {len(user_positions)}] Processing redeem from {user_position.id=}."
f"[{index + 1} / {len(user_positions)}] Processing redeem from {user_position.id=}."
)

original_balances = get_balances(public_key, web3)
Expand All @@ -1263,9 +1267,11 @@ def redeem_from_all_user_positions(
def get_binary_market_p_yes_history(market: OmenAgentMarket) -> list[Probability]:
history: list[Probability] = []
trades = sorted(
OmenSubgraphHandler().get_trades( # We need to look at price both after buying or selling, so get trades, not bets.
OmenSubgraphHandler().get_trades(
# We need to look at price both after buying or selling, so get trades, not bets.
market_id=market.market_maker_contract_address_checksummed,
end_time=market.close_time, # Even after market is closed, there can be many `Sell` trades which will converge the probability to the true one.
end_time=market.close_time,
# Even after market is closed, there can be many `Sell` trades which will converge the probability to the true one.
),
key=lambda x: x.creation_datetime,
)
Expand Down
Loading