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

Use PMAT from Pypi #30

Merged
merged 2 commits into from
Feb 22, 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
12 changes: 4 additions & 8 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
import typer
from prediction_market_agent_tooling.markets.markets import (
MarketType,
get_bet_amount,
get_binary_markets,
place_bet,
)

import prediction_market_agent as pma
Expand All @@ -21,7 +19,7 @@ def main(
Picks one market and answers it, optionally placing a bet.
"""
# Pick a market
market = get_binary_markets(market_type)[0].to_agent_market()
market = get_binary_markets(market_type)[0]

# Create the agent and run it
agent = get_agent(agent_type)
Expand All @@ -44,13 +42,11 @@ def main(
f"Placing bet with position {pma.utils.parse_result_to_str(result)} on market '{market.question}'"
)
amount = Decimal(
input(f"How much do you want to bet? (in {market.bet_amount_currency}): ")
input(f"How much do you want to bet? (in {market.currency}): ")
)
place_bet(
market=market.original_market,
amount=get_bet_amount(amount, market_type),
market.place_bet(
amount=market.get_bet_amount(amount),
outcome=result,
omen_auto_deposit=True,
)


Expand Down
1,419 changes: 1,041 additions & 378 deletions poetry.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion prediction_market_agent/agents/abstract.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from prediction_market_agent_tooling.markets.data_models import AgentMarket
from prediction_market_agent_tooling.markets.agent_market import AgentMarket


class AbstractAgent:
Expand Down
2 changes: 1 addition & 1 deletion prediction_market_agent/agents/always_yes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from prediction_market_agent_tooling.markets.data_models import AgentMarket
from prediction_market_agent_tooling.markets.agent_market import AgentMarket

from prediction_market_agent.agents.abstract import AbstractAgent

Expand Down
2 changes: 1 addition & 1 deletion prediction_market_agent/agents/autogen_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import autogen
from autogen.agentchat.contrib.gpt_assistant_agent import GPTAssistantAgent
from prediction_market_agent_tooling.markets.data_models import AgentMarket
from prediction_market_agent_tooling.markets.agent_market import AgentMarket

from prediction_market_agent import utils
from prediction_market_agent.agents.abstract import AbstractAgent
Expand Down
2 changes: 1 addition & 1 deletion prediction_market_agent/agents/crewai_agent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json

from langchain_community.tools import DuckDuckGoSearchRun
from prediction_market_agent_tooling.markets.data_models import AgentMarket
from prediction_market_agent_tooling.markets.agent_market import AgentMarket

from prediction_market_agent.agents.abstract import AbstractAgent

Expand Down
8 changes: 5 additions & 3 deletions prediction_market_agent/agents/custom_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional

import requests
from prediction_market_agent_tooling.markets.data_models import AgentMarket
from prediction_market_agent_tooling.markets.agent_market import AgentMarket
from prediction_market_agent_tooling.tools.utils import (
check_not_none,
should_not_happen,
Expand Down Expand Up @@ -118,15 +118,17 @@ def answer_binary_market(self, market: AgentMarket) -> bool:
elif tool_name := completion_dict.get("tool_name"):
tool_params = completion_dict.get("tool_params")
self.verbose_print(f"Agent: Using {tool_name=} with {tool_params=}.")
tool_output = (
tool_output = ( # type: ignore # Untyped for the sake of simplicity when the LLM is used.
self.google_search
if tool_name == "GoogleSearchTool"
else (
self.web_scrap_and_summarize
if tool_name == "WebScrapingTool"
else should_not_happen("Unknown tool requested from the LLM.")
)
)(**tool_params)
)(
**tool_params
)
self.verbose_print(f"Tool: {tool_name=} returns {tool_output=}.")
messages.append(
Message(
Expand Down
6 changes: 4 additions & 2 deletions prediction_market_agent/agents/langchain_agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from langchain.agents import AgentType, initialize_agent, load_tools
from langchain_community.llms import OpenAI
from prediction_market_agent_tooling.markets.data_models import AgentMarket
from prediction_market_agent_tooling.markets.agent_market import AgentMarket

from prediction_market_agent import utils
from prediction_market_agent.agents.abstract import AbstractAgent
Expand All @@ -13,7 +13,9 @@ def __init__(self) -> None:
# Can use pre-defined search tool
# TODO: Tavily tool could give better results
# https://docs.tavily.com/docs/tavily-api/langchain
tools = load_tools(["serpapi", "llm-math"], llm=llm, serpapi_api_key=keys.serp)
tools = load_tools(
["serpapi", "llm-math"], llm=llm, serpapi_api_key=keys.serp_api_key
)
self._agent = initialize_agent(
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
Expand Down
2 changes: 1 addition & 1 deletion prediction_market_agent/agents/llamaindex_agent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from llama_index.agent import OpenAIAgent
from llama_index.llms import OpenAI
from llama_index.tools import FunctionTool
from prediction_market_agent_tooling.markets.data_models import AgentMarket
from prediction_market_agent_tooling.markets.agent_market import AgentMarket

from prediction_market_agent import utils
from prediction_market_agent.agents.abstract import AbstractAgent
Expand Down
2 changes: 1 addition & 1 deletion prediction_market_agent/agents/metagpt_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

import dotenv
from prediction_market_agent_tooling.markets.data_models import AgentMarket
from prediction_market_agent_tooling.markets.agent_market import AgentMarket
from prediction_market_agent_tooling.tools.utils import check_not_none

from prediction_market_agent.agents.abstract import AbstractAgent
Expand Down
10 changes: 6 additions & 4 deletions prediction_market_agent/tools/betting_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
from prediction_market_agent_tooling.gtypes import Probability, wei_type, xDai
from prediction_market_agent_tooling.markets.data_models import BetAmount, Currency
from prediction_market_agent_tooling.markets.markets import MarketType
from prediction_market_agent_tooling.markets.omen import (
OmenMarket,
from prediction_market_agent_tooling.markets.omen.data_models import OmenMarket
from prediction_market_agent_tooling.markets.omen.omen import (
OmenAgentMarket,
omen_calculate_buy_amount,
)
from prediction_market_agent_tooling.tools.gnosis_rpc import GNOSIS_RPC_URL
Expand Down Expand Up @@ -57,6 +58,7 @@ def get_market_moving_bet(
new_product - fixed_product = dx * na_y
dx = (new_product - fixed_product) / na_y
"""
market_agent = OmenAgentMarket.from_data_model(market)
amounts = market.outcomeTokenAmounts
prices = check_not_none(
market.outcomeTokenProbabilities, "No probabilities, is marked closed?"
Expand Down Expand Up @@ -98,7 +100,7 @@ def get_market_moving_bet(
if check_vs_contract:
expected_trade = omen_calculate_buy_amount(
web3=Web3(Web3.HTTPProvider(GNOSIS_RPC_URL)),
market=market,
market=market_agent,
investment_amount=wei_type(bet_amount),
outcome_index=bet_outcome_index,
)
Expand All @@ -114,7 +116,7 @@ def get_market_moving_bet(
if verbose:
outcome = market.get_outcome_str(bet_outcome_index)
print(
f"Target p_yes: {target_p_yes:.2f}, bet: {wei_to_xdai(bet_amount_wei):.2f}{market.BET_AMOUNT_CURRENCY} for {outcome}, new p_yes: {new_p_yes:.2f}"
f"Target p_yes: {target_p_yes:.2f}, bet: {wei_to_xdai(bet_amount_wei):.2f}{market_agent.currency} for {outcome}, new p_yes: {new_p_yes:.2f}"
)
if abs(target_p_yes - new_p_yes) < 0.01:
break
Expand Down
2 changes: 1 addition & 1 deletion prediction_market_agent/tools/google_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def google_search(query: str) -> list[str]:
params = {"q": query, "api_key": utils.APIKeys().serp, "num": 4}
params = {"q": query, "api_key": utils.APIKeys().serp_api_key, "num": 4}
search = serpapi.GoogleSearch(params)
urls = [result["link"] for result in search.get_dict()["organic_results"]]
return urls
Expand Down
6 changes: 3 additions & 3 deletions prediction_market_agent/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class APIKeys(APIKeysBase):

@property
def serp_api_key(self) -> str:
return check_not_none( # type: ignore # Remove once PMAT is correctly released and this doesn't ignore his typing.
return check_not_none(
self.SERP_API_KEY, "SERP_API_KEY missing in the environment."
)

@property
def openai_api_key(self) -> str:
return check_not_none( # type: ignore # Remove once PMAT is correctly released and this doesn't ignore his typing.
return check_not_none(
self.OPENAI_API_KEY, "OPENAI_API_KEY missing in the environment."
)

Expand All @@ -34,7 +34,7 @@ def get_market_prompt(question: str) -> str:


def parse_result_to_boolean(result: str) -> bool:
return ( # type: ignore # Remove once PMAT is correctly released and this doesn't ignore his typing.
return (
True
if result.lower() == "yes"
else (
Expand Down
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = ["Gnosis"]
readme = "README.md"

[tool.poetry.dependencies]
python = ">=3.9,<3.10"
python = "~3.10.0"
python-dotenv = "*"
openai = "*"
langchain = "~0.1.1"
Expand Down Expand Up @@ -35,8 +35,7 @@ poetry = "^1.7.1"
poetry-plugin-export = "^1.6.0"
functions-framework = "^3.5.0"
cron-validator = "^1.0.8"
# TODO: Change to the correct version once it's released.
prediction-market-agent-tooling = { git = "https://github.com/gnosis/prediction-market-agent-tooling.git", branch = "peter/fix-apikeys" }
prediction-market-agent-tooling = "0.2.6"
pydantic-settings = "^2.1.0"
autoflake = "^2.2.1"
isort = "^5.13.2"
Expand Down
6 changes: 3 additions & 3 deletions tests/tools/test_betting_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
xDai,
xdai_type,
)
from prediction_market_agent_tooling.markets.markets import omen
from prediction_market_agent_tooling.markets.omen import omen

from prediction_market_agent.tools.betting_strategies import (
get_kelly_criterion_bet,
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_get_market_moving_bet_0(
float(expected_buying_xdai_amount),
atol=2.0, # We don't expect it to be 100% accurate, but close enough.
), f"To move this martket to ~{wanted_p_yes_on_the_market}% for yes, the amount should be {expected_buying_xdai_amount}xDai, according to aiomen website."
assert outcome_index == TEST_OMEN_MARKET.get_outcome_index(
assert outcome_index == TEST_OMEN_MARKET.outcomes.index(
expected_buying_outcome
), f"The buying outcome index should `{expected_buying_outcome}`."

Expand All @@ -79,4 +79,4 @@ def test_kelly_criterion_bet_0(est_p_yes: Probability, expected_outcome: str) ->
)
# Kelly estimates the best bet for maximizing the expected value of the logarithm of the wealth.
# We don't know the real best xdai_amount, but at least we know which outcome index makes sense.
assert outcome_index == TEST_OMEN_MARKET.get_outcome_index(expected_outcome)
assert outcome_index == TEST_OMEN_MARKET.outcomes.index(expected_outcome)
Loading