-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add streamlit example of monitoring a manifold agent
- Loading branch information
1 parent
88bd7c6
commit 12dbb39
Showing
8 changed files
with
678 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,17 @@ | ||
from datetime import datetime, timedelta | ||
import pytz | ||
|
||
from prediction_market_agent_tooling.markets.manifold import get_authenticated_user | ||
from prediction_market_agent_tooling.monitor.monitor import ( | ||
from prediction_market_agent_tooling.monitor.markets.manifold import ( | ||
DeployedManifoldAgent, | ||
monitor_agent, | ||
) | ||
|
||
|
||
agent = DeployedManifoldAgent( | ||
name="foo", | ||
start_time=datetime.now() - timedelta(weeks=2), | ||
manifold_user=get_authenticated_user(), | ||
) | ||
from prediction_market_agent_tooling.monitor.monitor import monitor_agent | ||
|
||
monitor_agent(agent) | ||
if __name__ == "__main__": | ||
start_time = datetime.now() - timedelta(weeks=1) | ||
agent = DeployedManifoldAgent( | ||
name="foo", | ||
start_time=start_time.astimezone(pytz.UTC), | ||
manifold_user_id=get_authenticated_user().id, | ||
) | ||
monitor_agent(agent) |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
prediction_market_agent_tooling/monitor/markets/manifold.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from prediction_market_agent_tooling.markets.data_models import ResolvedBet | ||
from prediction_market_agent_tooling.markets.manifold import ( | ||
get_resolved_manifold_bets, | ||
manifold_to_generic_resolved_bet, | ||
) | ||
from prediction_market_agent_tooling.monitor.monitor import DeployedAgent | ||
|
||
|
||
class DeployedManifoldAgent(DeployedAgent): | ||
manifold_user_id: str | ||
|
||
def get_resolved_bets(self) -> list[ResolvedBet]: | ||
manifold_bets = get_resolved_manifold_bets( | ||
user_id=self.manifold_user_id, | ||
start_time=self.start_time, | ||
end_time=None, | ||
) | ||
return [manifold_to_generic_resolved_bet(b) for b in manifold_bets] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,68 @@ | ||
import altair as alt | ||
from datetime import datetime | ||
from pydantic import BaseModel | ||
import pandas as pd | ||
import pytz | ||
import streamlit as st | ||
import typing as t | ||
|
||
from prediction_market_agent_tooling.markets.data_models import Bet, ManifoldUser | ||
from prediction_market_agent_tooling.markets.manifold import get_bets | ||
from prediction_market_agent_tooling.markets.data_models import ResolvedBet | ||
|
||
|
||
class DeployedAgent(BaseModel): | ||
name: str | ||
start_time: datetime | ||
start_time: datetime = datetime.now().astimezone(tz=pytz.UTC) | ||
end_time: t.Optional[datetime] = None | ||
|
||
def get_bets(self) -> list[Bet]: | ||
def get_resolved_bets(self) -> list[ResolvedBet]: | ||
raise NotImplementedError("Subclasses must implement this method.") | ||
|
||
|
||
class DeployedManifoldAgent(DeployedAgent): | ||
manifold_user: ManifoldUser | ||
def monitor_agent(agent: DeployedAgent) -> None: | ||
agent_bets = agent.get_resolved_bets() | ||
bets_info = { | ||
"Market Question": [bet.market_question for bet in agent_bets], | ||
"Bet Amount": [bet.amount.amount for bet in agent_bets], | ||
"Bet Outcome": [bet.outcome for bet in agent_bets], | ||
"Created Time": [bet.created_time for bet in agent_bets], | ||
"Resolved Time": [bet.resolved_time for bet in agent_bets], | ||
"Is Correct": [bet.is_correct for bet in agent_bets], | ||
"Profit": [bet.profit.amount for bet in agent_bets], | ||
} | ||
bets_df = pd.DataFrame(bets_info).sort_values(by="Resolved Time") | ||
|
||
def get_bets(self) -> list[Bet]: | ||
return get_bets(self.manifold_user.id) | ||
st.set_page_config(layout="wide") | ||
st.title(f"Monitoring Agent: '{agent.name}'") | ||
|
||
# Metrics | ||
col1, col2 = st.columns(2) | ||
col1.metric(label="Number of bets", value=f"{len(agent_bets)}") | ||
col2.metric(label="% Correct", value=f"{100 * bets_df['Is Correct'].mean():.2f}%") | ||
|
||
def monitor_agent(agent: DeployedAgent) -> None: | ||
agent_bets = agent.get_bets() | ||
print(f"Agent {agent.name} has {len(agent_bets)} bets.") | ||
print(f"bet0: {agent_bets[0]}") | ||
# TODO Get the bets from agent.starttime to agent.endtime (or now if endtime is None) | ||
# TODO calculate the accuracy of last 10 bets for every day, and display it in a graph in streamlit app | ||
# Chart of cumulative profit per day | ||
profit_info = { | ||
"Time": bets_df["Resolved Time"], | ||
"Cumulative Profit": bets_df["Profit"].astype(float), | ||
} | ||
profit_df = pd.DataFrame(profit_info) | ||
profit_df["Date"] = pd.to_datetime(profit_df["Time"].dt.date) | ||
profit_df = ( | ||
profit_df.groupby("Date")["Cumulative Profit"].sum().cumsum().reset_index() | ||
) | ||
profit_df["Cumulative Profit"] = profit_df["Cumulative Profit"].astype(float) | ||
st.empty() | ||
st.altair_chart( | ||
alt.Chart(profit_df) | ||
.mark_line() | ||
.encode( | ||
x=alt.X("Date", axis=alt.Axis(format="%Y-%m-%d"), title=None), | ||
y=alt.Y("Cumulative Profit", axis=alt.Axis(format=".2f")), | ||
) | ||
.interactive(), | ||
use_container_width=True, | ||
) | ||
|
||
# Table of resolved bets | ||
st.empty() | ||
st.subheader("Resolved Bet History") | ||
st.table(bets_df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters