Skip to content

Commit

Permalink
Added LLM call as a way to save Tavily credits (#41)
Browse files Browse the repository at this point in the history
* Added has_question_event_happened_in_the_past

* Fixed CI steps

* Reverted model back to gpt4

* Added PR comments
  • Loading branch information
gabrielfior authored Mar 27, 2024
1 parent 14153e8 commit 2a7a8b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
7 changes: 6 additions & 1 deletion prediction_market_agent/agents/known_outcome_agent/deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 @@ -36,7 +37,11 @@ def pick_markets(self, markets: list[AgentMarket]) -> list[AgentMarket]:
# Assume very high probability markets are already known, and have
# been correctly bet on, and therefore the value of betting on them
# is low.
if not market_is_saturated(market=market):
if not market_is_saturated(
market=market
) and has_question_event_happened_in_the_past(
model=self.model, question=market.question
):
answer = get_known_outcome(
model=self.model,
question=market.question,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from prediction_market_agent_tooling.tools.utils import utcnow
from pydantic import BaseModel

from prediction_market_agent.tools.web_scrape.basic_summary import _summary
Expand Down Expand Up @@ -42,6 +43,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 1, 0 or -1 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 @@ -138,6 +155,29 @@ def summarize_if_required(content: str, model: str, question: str) -> str:
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:
"""
In a loop, perform web search and scrape to find if the answer to the
Expand Down

0 comments on commit 2a7a8b9

Please sign in to comment.