Skip to content

Commit

Permalink
Fix think thoroughly final question (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored May 3, 2024
1 parent bf32eab commit c407608
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@
You should determine the probability of the SCENARIO SCENARIO_TO_ASSESS being true,
considering the probabilities of the other related SCENARIOs.
SCENARIOS_WITH_PROBABILITIES:
{scenarios_with_probabilities}
SCENARIO_TO_ASSESS: {scenario_to_assess}
SCENARIOS_WITH_PROBABILITIES: {scenarios_with_probabilities}
"""

PROBABILITY_CLASS_OUTPUT = """
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def generate_prediction_for_one_outcome(
inputs = {"sentence": sentence}
if previous_scenarios_and_answers:
inputs["previous_scenarios_with_probabilities"] = "\n".join(
f"- Scenario '{s}' has probability of happening {a.p_yes * 100:.2f}%, because {a.reasoning}"
f"- Scenario '{s}' has probability of happening {a.p_yes * 100:.2f}% with confidence {a.confidence * 100:.2f}%, because {a.reasoning}"
for s, a in previous_scenarios_and_answers
)

Expand All @@ -171,7 +171,7 @@ def generate_prediction_for_one_outcome(
return None

def generate_final_decision(
self, scenarios_with_probabilities: list[t.Tuple[str, Answer]]
self, question: str, scenarios_with_probabilities: list[t.Tuple[str, Answer]]
) -> Answer:
predictor = self._get_predictor()

Expand All @@ -188,13 +188,15 @@ def generate_final_decision(
verbose=2,
)

logger.info(f"Starting to generate final decision for '{question}'.")
crew.kickoff(
inputs={
"scenarios_with_probabilities": [
(i[0], i[1].model_dump()) for i in scenarios_with_probabilities
],
"scenarios_with_probabilities": "\n".join(
f"- Scenario '{s}' has probability of happening {a.p_yes * 100:.2f}% with confidence {a.confidence * 100:.2f}%, because {a.reasoning}"
for s, a in scenarios_with_probabilities
),
"number_of_scenarios": len(scenarios_with_probabilities),
"scenario_to_assess": scenarios_with_probabilities[0][0],
"scenario_to_assess": question,
}
)
output = Answer.model_validate_json(task_final_decision.output.raw_output)
Expand Down Expand Up @@ -233,7 +235,7 @@ def answer_binary_market(
)

final_answer = (
self.generate_final_decision(scenarios_with_probs)
self.generate_final_decision(question, scenarios_with_probs)
if scenarios_with_probs
else None
)
Expand Down
18 changes: 13 additions & 5 deletions prediction_market_agent/tools/streamlit_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@ def loguru_streamlit_sink(log: "Message") -> None:
record = log.record
level = record["level"].name

message = record["message"]
# Replace escaped newlines with actual newlines.
message = message.replace("\\n", "\n")
# Fix malformed dollar signs in the messages.
message = message.replace("$", "\$")
message = streamlit_escape(record["message"])

if level == "ERROR":
st.error(message, icon="❌")
Expand All @@ -35,3 +31,15 @@ def add_sink_to_logger() -> None:
Needs to be behind a cache decorator, so it only runs once per streamlit session (otherwise we would see duplicated messages).
"""
logger.add(loguru_streamlit_sink)


def streamlit_escape(message: str) -> str:
"""
Escapes the string for streamlit writes.
"""
# Replace escaped newlines with actual newlines.
message = message.replace("\\n", "\n")
# Fix malformed dollar signs in the messages.
message = message.replace("$", "\$")

return message
11 changes: 9 additions & 2 deletions scripts/agent_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
from prediction_market_agent.agents.think_thoroughly_agent.deploy import (
DeployableThinkThoroughlyAgent,
)
from prediction_market_agent.tools.streamlit_utils import add_sink_to_logger
from prediction_market_agent.tools.streamlit_utils import (
add_sink_to_logger,
streamlit_escape,
)

AGENTS: list[
t.Type[DeployableKnownOutcomeAgent] | t.Type[DeployableThinkThoroughlyAgent]
Expand Down Expand Up @@ -113,4 +116,8 @@
bet_amount = agent.calculate_bet_amount(answer, market)

st.warning(f"Took {costs.time / 60:.2f} minutes and {costs.cost:.2f} USD.")
st.success(f"Would bet {bet_amount.amount} {bet_amount.currency} on {answer}!")
st.success(
streamlit_escape(
f"Would bet {bet_amount.amount} {bet_amount.currency} on {answer}!"
)
)

0 comments on commit c407608

Please sign in to comment.