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

Added LLM call as a way to save Tavily credits #41

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (
kongzii marked this conversation as resolved.
Show resolved Hide resolved
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
gabrielfior marked this conversation as resolved.
Show resolved Hide resolved


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
Loading