Skip to content

Commit

Permalink
Added has_question_event_happened_in_the_past
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielfior committed Mar 26, 2024
1 parent 14153e8 commit 37650d2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 18 deletions.
50 changes: 33 additions & 17 deletions prediction_market_agent/agents/known_outcome_agent/deploy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import getpass
from decimal import Decimal

from dotenv import load_dotenv
from prediction_market_agent_tooling.config import APIKeys
from prediction_market_agent_tooling.deploy.agent import DeployableAgent
from prediction_market_agent_tooling.deploy.constants import OWNER_KEY
Expand All @@ -17,6 +18,7 @@
from prediction_market_agent.agents.known_outcome_agent.known_outcome_agent import (
Result,
get_known_outcome,
has_question_event_happened_in_the_past,
)


Expand All @@ -25,7 +27,7 @@ def market_is_saturated(market: AgentMarket) -> bool:


class DeployableKnownOutcomeAgent(DeployableAgent):
model = "gpt-4-1106-preview"
model = "gpt-3.5-turbo"

def load(self) -> None:
self.markets_with_known_outcomes: dict[str, Result] = {}
Expand All @@ -37,6 +39,13 @@ def pick_markets(self, markets: list[AgentMarket]) -> list[AgentMarket]:
# been correctly bet on, and therefore the value of betting on them
# is low.
if not market_is_saturated(market=market):

if not has_question_event_happened_in_the_past(model=self.model,
question=market.question):
# event happened
continue


answer = get_known_outcome(
model=self.model,
question=market.question,
Expand All @@ -62,22 +71,29 @@ def calculate_bet_amount(self, answer: bool, market: AgentMarket) -> BetAmount:

if __name__ == "__main__":
agent = DeployableKnownOutcomeAgent()
agent.deploy_gcp(
repository=f"git+{get_current_git_url()}@{get_current_git_commit_sha()}",
load_dotenv()
agent.deploy_local(
market_type=MarketType.OMEN,
labels={OWNER_KEY: getpass.getuser()},
secrets={
"TAVILY_API_KEY": "GNOSIS_AI_TAVILY_API_KEY:latest",
},
memory=1024,
api_keys=APIKeys(
BET_FROM_ADDRESS=verify_address(
"0xb611A9f02B318339049264c7a66ac3401281cc3c"
),
BET_FROM_PRIVATE_KEY=private_key_type("EVAN_OMEN_BETTER_0_PKEY:latest"),
OPENAI_API_KEY=SecretStr("EVAN_OPENAI_API_KEY:latest"),
MANIFOLD_API_KEY=None,
),
cron_schedule="0 */12 * * *",
sleep_time=10,
timeout=540,
place_bet=False
)
# agent.deploy_gcp(
# repository=f"git+{get_current_git_url()}@{get_current_git_commit_sha()}",
# market_type=MarketType.OMEN,
# labels={OWNER_KEY: getpass.getuser()},
# secrets={
# "TAVILY_API_KEY": "GNOSIS_AI_TAVILY_API_KEY:latest",
# },
# memory=1024,
# api_keys=APIKeys(
# BET_FROM_ADDRESS=verify_address(
# "0xb611A9f02B318339049264c7a66ac3401281cc3c"
# ),
# BET_FROM_PRIVATE_KEY=private_key_type("EVAN_OMEN_BETTER_0_PKEY:latest"),
# OPENAI_API_KEY=SecretStr("EVAN_OPENAI_API_KEY:latest"),
# MANIFOLD_API_KEY=None,
# ),
# cron_schedule="0 */12 * * *",
# timeout=540,
# )
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import json
import typing as t
from datetime import datetime
from datetime import datetime, timezone
from enum import Enum

from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from pydantic import BaseModel


from prediction_market_agent.tools.web_scrape.basic_summary import _summary
from prediction_market_agent.tools.web_scrape.markdown import web_scrape
from prediction_market_agent_tooling.tools.utils import utcnow
from prediction_market_agent.tools.web_search.tavily import web_search


Expand Down Expand Up @@ -42,6 +44,22 @@ def has_known_outcome(self) -> bool:
return self.result is not Result.UNKNOWN


HAS_QUESTION_HAPPENED_IN_THE_PAST_PROMPT = """
The current date is {date_str}. Your goal is to assert if a QUESTION references an event that is already finished (according to the current date and time) or if it will still take place in a later date.
For example, you should return 1 if given the event "Will Bitcoin have reached the price of $100 by 30 March 2023?", since the event ends on a data prior to the current date.
Your answer MUST be an integer and follow the logic below:
- If the event is already finished, return 1
- If the event has not yet finished, return 0
- If you are not sure, return -1
Answer with the single prompt only, and nothing else.
[QUESTION]
"{question}"
"""

GENERATE_SEARCH_QUERY_PROMPT = """
The current date is {date_str}. You are trying to determine whether the answer
to the following question has a definite answer. Generate a web search query
Expand Down Expand Up @@ -137,6 +155,28 @@ def summarize_if_required(content: str, model: str, question: str) -> str:
else:
return content

def has_question_event_happened_in_the_past(model: str, question: str) -> bool:
""" Asks the model if the event referenced by the question has finished (given the
current date) (returning 1), if the event has not yet finished (returning 0) or
if it cannot be sure (returning -1)."""
date_str = utcnow().strftime("%Y-%m-%d %H:%M:%S %Z")
llm = ChatOpenAI(model=model, temperature=0.0)
prompt = ChatPromptTemplate.from_template(
template=HAS_QUESTION_HAPPENED_IN_THE_PAST_PROMPT
).format_messages(
date_str=date_str,
question=question,
)
answer = str(llm.invoke(prompt).content)
try:
parsed_answer = int(answer)
if parsed_answer == 1:
return True
except Exception as e:
print ("Exception occured, cannot assert if title happened in the past. ", e)

return False


def get_known_outcome(model: str, question: str, max_tries: int) -> Answer:
"""
Expand Down

0 comments on commit 37650d2

Please sign in to comment.