From 04cddc0a581a17465b2d79c773b2964df37ecbd4 Mon Sep 17 00:00:00 2001 From: evangriffiths Date: Thu, 15 Feb 2024 11:20:01 +0000 Subject: [PATCH 1/3] Add isort and autoflake to CI --- .github/workflows/python_ci.yaml | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/.github/workflows/python_ci.yaml b/.github/workflows/python_ci.yaml index 4ed70cdb..fa7f9343 100644 --- a/.github/workflows/python_ci.yaml +++ b/.github/workflows/python_ci.yaml @@ -50,3 +50,23 @@ jobs: - uses: ./.github/actions/python_prepare - name: Check with black run: poetry run black --check . + + autoflake: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./.github/actions/python_prepare + - name: Check with autoflake + run: | + poetry run autoflake --in-place --remove-all-unused-imports --remove-unused-variables --recursive . + git diff --exit-code --quiet || exit 1 + + isort: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - uses: ./.github/actions/python_prepare + - name: Check with isort + run: | + poetry run isort --profile black . + git diff --exit-code --quiet || exit 1 From abe93113d24a669e6105eab6b73eae081a7382c6 Mon Sep 17 00:00:00 2001 From: evangriffiths Date: Thu, 15 Feb 2024 11:23:11 +0000 Subject: [PATCH 2/3] Add autoflake and run on repo --- main.py | 10 ++++---- poetry.lock | 28 +++++++++++++++++++++- prediction_market_agent/__init__.py | 2 -- prediction_market_agent/agents/__init__.py | 7 ------ pyproject.toml | 1 + 5 files changed, 33 insertions(+), 15 deletions(-) diff --git a/main.py b/main.py index 9564a58a..bfacf232 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,6 @@ -import typer -import prediction_market_agent as pma from decimal import Decimal -from prediction_market_agent.agents.all_agents import AgentType, get_agent + +import typer from prediction_market_agent_tooling.markets.markets import ( MarketType, get_bet_amount, @@ -9,6 +8,9 @@ place_bet, ) +import prediction_market_agent as pma +from prediction_market_agent.agents.all_agents import AgentType, get_agent + def main( market_type: MarketType = MarketType.MANIFOLD, @@ -18,8 +20,6 @@ def main( """ Picks one market and answers it, optionally placing a bet. """ - keys = pma.utils.APIKeys() - # Pick a market market = get_binary_markets(market_type)[0].to_agent_market() diff --git a/poetry.lock b/poetry.lock index 2e7b866e..f6d1d83c 100644 --- a/poetry.lock +++ b/poetry.lock @@ -173,6 +173,21 @@ tests = ["attrs[tests-no-zope]", "zope-interface"] tests-mypy = ["mypy (>=1.6)", "pytest-mypy-plugins"] tests-no-zope = ["attrs[tests-mypy]", "cloudpickle", "hypothesis", "pympler", "pytest (>=4.3.0)", "pytest-xdist[psutil]"] +[[package]] +name = "autoflake" +version = "2.2.1" +description = "Removes unused imports and unused variables" +optional = false +python-versions = ">=3.8" +files = [ + {file = "autoflake-2.2.1-py3-none-any.whl", hash = "sha256:265cde0a43c1f44ecfb4f30d95b0437796759d07be7706a2f70e4719234c0f79"}, + {file = "autoflake-2.2.1.tar.gz", hash = "sha256:62b7b6449a692c3c9b0c916919bbc21648da7281e8506bcf8d3f8280e431ebc1"}, +] + +[package.dependencies] +pyflakes = ">=3.0.0" +tomli = {version = ">=2.0.1", markers = "python_version < \"3.11\""} + [[package]] name = "beautifulsoup4" version = "4.12.3" @@ -3511,6 +3526,17 @@ files = [ pydantic = ">=2.3.0" python-dotenv = ">=0.21.0" +[[package]] +name = "pyflakes" +version = "3.2.0" +description = "passive checker of Python programs" +optional = false +python-versions = ">=3.8" +files = [ + {file = "pyflakes-3.2.0-py2.py3-none-any.whl", hash = "sha256:84b5be138a2dfbb40689ca07e2152deb896a65c3a3e24c251c5c62489568074a"}, + {file = "pyflakes-3.2.0.tar.gz", hash = "sha256:1c61603ff154621fb2a9172037d84dca3500def8c8b630657d1701f026f8af3f"}, +] + [[package]] name = "pyproject-hooks" version = "1.0.0" @@ -4981,4 +5007,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.10" -content-hash = "55056af30c8b9acb85280d1442ab5b0f2af061d7c89df7c14ed470a389650f55" +content-hash = "7c6bfc5b16504c0556ddfb95409b1283f4ea7b7f08e87ae2e960bc01bf707ff3" diff --git a/prediction_market_agent/__init__.py b/prediction_market_agent/__init__.py index 7792fdc9..e69de29b 100644 --- a/prediction_market_agent/__init__.py +++ b/prediction_market_agent/__init__.py @@ -1,2 +0,0 @@ -from . import utils -from . import agents diff --git a/prediction_market_agent/agents/__init__.py b/prediction_market_agent/agents/__init__.py index eeefcf8c..e69de29b 100644 --- a/prediction_market_agent/agents/__init__.py +++ b/prediction_market_agent/agents/__init__.py @@ -1,7 +0,0 @@ -from . import langchain_agent -from . import autogen_agent -from . import always_yes -from . import llamaindex_agent -from . import metagpt_agent -from . import crewai_agent -from . import custom_agent diff --git a/pyproject.toml b/pyproject.toml index cb81ab58..e9aaacbd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -38,6 +38,7 @@ 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" } pydantic-settings = "^2.1.0" +autoflake = "^2.2.1" [build-system] requires = ["poetry-core"] From 7dec7d0e96d82ca106df3b32b72eacd3caa158b8 Mon Sep 17 00:00:00 2001 From: evangriffiths Date: Thu, 15 Feb 2024 11:25:51 +0000 Subject: [PATCH 3/3] Add isort and run on repo --- poetry.lock | 16 ++++++++++++- prediction_market_agent/agents/all_agents.py | 11 +++++---- prediction_market_agent/agents/always_yes.py | 3 ++- .../agents/autogen_agent.py | 4 ++-- .../agents/crewai_agent.py | 7 +++--- .../agents/custom_agent.py | 18 +++++++------- .../agents/langchain_agent.py | 6 ++--- .../agents/llamaindex_agent.py | 4 ++-- .../agents/metagpt_agent.py | 7 +++--- .../ai_models/abstract_ai_models.py | 1 + .../ai_models/llama_ai_models.py | 10 ++++---- .../ai_models/openai_ai_models.py | 12 ++++++---- .../tools/betting_strategies.py | 24 ++++++++----------- prediction_market_agent/tools/web_scrape.py | 3 +-- .../tools/web_scrape_structured.py | 1 + prediction_market_agent/utils.py | 3 ++- pyproject.toml | 1 + tests/ai_models/test_llama_ai_models.py | 4 ++-- tests/tools/test_betting_strategies.py | 15 ++++++------ 19 files changed, 86 insertions(+), 64 deletions(-) diff --git a/poetry.lock b/poetry.lock index f6d1d83c..07d98b4d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1985,6 +1985,20 @@ files = [ {file = "installer-0.7.0.tar.gz", hash = "sha256:a26d3e3116289bb08216e0d0f7d925fcef0b0194eedfa0c944bcaaa106c4b631"}, ] +[[package]] +name = "isort" +version = "5.13.2" +description = "A Python utility / library to sort Python imports." +optional = false +python-versions = ">=3.8.0" +files = [ + {file = "isort-5.13.2-py3-none-any.whl", hash = "sha256:8ca5e72a8d85860d5a3fa69b8745237f2939afe12dbf656afbcb47fe72d947a6"}, + {file = "isort-5.13.2.tar.gz", hash = "sha256:48fdfcb9face5d58a4f6dde2e72a1fb8dcaf8ab26f95ab49fab84c2ddefb0109"}, +] + +[package.extras] +colors = ["colorama (>=0.4.6)"] + [[package]] name = "itsdangerous" version = "2.1.2" @@ -5007,4 +5021,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.10" -content-hash = "7c6bfc5b16504c0556ddfb95409b1283f4ea7b7f08e87ae2e960bc01bf707ff3" +content-hash = "eb4f86cc45ef2e410b05a046f2e08e1d96db537e15d2322663d8667d894a105c" diff --git a/prediction_market_agent/agents/all_agents.py b/prediction_market_agent/agents/all_agents.py index ad305a33..edc7e500 100644 --- a/prediction_market_agent/agents/all_agents.py +++ b/prediction_market_agent/agents/all_agents.py @@ -1,16 +1,17 @@ import typing as t from enum import Enum + from prediction_market_agent.agents.abstract import AbstractAgent -from prediction_market_agent.agents.langchain_agent import LangChainAgent -from prediction_market_agent.agents.autogen_agent import AutoGenAgent from prediction_market_agent.agents.always_yes import AlwaysYesAgent -from prediction_market_agent.agents.llamaindex_agent import LlamaIndexAgent -from prediction_market_agent.agents.metagpt_agent import MetaGPTAgent +from prediction_market_agent.agents.autogen_agent import AutoGenAgent from prediction_market_agent.agents.crewai_agent import CrewAIAgent from prediction_market_agent.agents.custom_agent import ( - CustomAgentOpenAi, CustomAgentLlama, + CustomAgentOpenAi, ) +from prediction_market_agent.agents.langchain_agent import LangChainAgent +from prediction_market_agent.agents.llamaindex_agent import LlamaIndexAgent +from prediction_market_agent.agents.metagpt_agent import MetaGPTAgent class AgentType(str, Enum): diff --git a/prediction_market_agent/agents/always_yes.py b/prediction_market_agent/agents/always_yes.py index 56d58769..0392b6d7 100644 --- a/prediction_market_agent/agents/always_yes.py +++ b/prediction_market_agent/agents/always_yes.py @@ -1,6 +1,7 @@ -from prediction_market_agent.agents.abstract import AbstractAgent from prediction_market_agent_tooling.markets.data_models import AgentMarket +from prediction_market_agent.agents.abstract import AbstractAgent + class AlwaysYesAgent(AbstractAgent): """ diff --git a/prediction_market_agent/agents/autogen_agent.py b/prediction_market_agent/agents/autogen_agent.py index ef9490d0..a96a8d71 100644 --- a/prediction_market_agent/agents/autogen_agent.py +++ b/prediction_market_agent/agents/autogen_agent.py @@ -1,14 +1,14 @@ -import autogen import re import typing as t +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 import utils from prediction_market_agent.agents.abstract import AbstractAgent from prediction_market_agent.tools.google_search import GoogleSearchTool from prediction_market_agent.tools.web_scrape import WebScrapingTool -from prediction_market_agent_tooling.markets.data_models import AgentMarket class AutoGenAgent(AbstractAgent): diff --git a/prediction_market_agent/agents/crewai_agent.py b/prediction_market_agent/agents/crewai_agent.py index b5f09f1c..82edf7fa 100644 --- a/prediction_market_agent/agents/crewai_agent.py +++ b/prediction_market_agent/agents/crewai_agent.py @@ -1,8 +1,9 @@ import json -from prediction_market_agent.agents.abstract import AbstractAgent -from prediction_market_agent_tooling.markets.data_models import AgentMarket from langchain_community.tools import DuckDuckGoSearchRun +from prediction_market_agent_tooling.markets.data_models import AgentMarket + +from prediction_market_agent.agents.abstract import AbstractAgent # TODO can use langchain's @tool decorator on our own tool methods to create a # tool useable by a crew agent @@ -32,7 +33,7 @@ def __init__(self) -> None: ) def answer_binary_market(self, market: AgentMarket) -> bool: - from crewai import Task, Crew + from crewai import Crew, Task task1 = Task( description=( diff --git a/prediction_market_agent/agents/custom_agent.py b/prediction_market_agent/agents/custom_agent.py index 0e99523c..3ead855a 100644 --- a/prediction_market_agent/agents/custom_agent.py +++ b/prediction_market_agent/agents/custom_agent.py @@ -1,24 +1,26 @@ import json -import requests from typing import Optional + +import requests +from prediction_market_agent_tooling.markets.data_models import AgentMarket +from prediction_market_agent_tooling.tools.utils import ( + check_not_none, + should_not_happen, +) + from prediction_market_agent import utils from prediction_market_agent.agents.abstract import AbstractAgent from prediction_market_agent.ai_models.abstract_ai_models import ( - Message, AbstractAiChatModel, + Message, ) from prediction_market_agent.ai_models.llama_ai_models import ChatReplicateLLamaModel from prediction_market_agent.ai_models.openai_ai_models import ChatOpenAIModel from prediction_market_agent.tools.google_search import google_search +from prediction_market_agent.tools.tool_exception_handler import tool_exception_handler from prediction_market_agent.tools.web_scrape_structured import ( web_scrape_structured_and_summarized, ) -from prediction_market_agent.tools.tool_exception_handler import tool_exception_handler -from prediction_market_agent_tooling.tools.utils import ( - check_not_none, - should_not_happen, -) -from prediction_market_agent_tooling.markets.data_models import AgentMarket class CustomAgent(AbstractAgent): diff --git a/prediction_market_agent/agents/langchain_agent.py b/prediction_market_agent/agents/langchain_agent.py index 8a9f9321..400b2542 100644 --- a/prediction_market_agent/agents/langchain_agent.py +++ b/prediction_market_agent/agents/langchain_agent.py @@ -1,11 +1,9 @@ -from langchain.agents import load_tools -from langchain.agents import initialize_agent -from langchain.agents import AgentType +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 import utils from prediction_market_agent.agents.abstract import AbstractAgent -from prediction_market_agent_tooling.markets.data_models import AgentMarket class LangChainAgent(AbstractAgent): diff --git a/prediction_market_agent/agents/llamaindex_agent.py b/prediction_market_agent/agents/llamaindex_agent.py index e80f3328..1da3dca5 100644 --- a/prediction_market_agent/agents/llamaindex_agent.py +++ b/prediction_market_agent/agents/llamaindex_agent.py @@ -1,12 +1,12 @@ 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 import utils from prediction_market_agent.agents.abstract import AbstractAgent from prediction_market_agent.tools.google_search import google_search from prediction_market_agent.tools.web_scrape import web_scrape -from prediction_market_agent import utils -from prediction_market_agent_tooling.markets.data_models import AgentMarket class LlamaIndexAgent(AbstractAgent): diff --git a/prediction_market_agent/agents/metagpt_agent.py b/prediction_market_agent/agents/metagpt_agent.py index ce6d99db..6cb9a570 100644 --- a/prediction_market_agent/agents/metagpt_agent.py +++ b/prediction_market_agent/agents/metagpt_agent.py @@ -1,11 +1,12 @@ import asyncio -import dotenv import json import os -from prediction_market_agent.agents.abstract import AbstractAgent -from prediction_market_agent_tooling.tools.utils import check_not_none +import dotenv from prediction_market_agent_tooling.markets.data_models import AgentMarket +from prediction_market_agent_tooling.tools.utils import check_not_none + +from prediction_market_agent.agents.abstract import AbstractAgent class MetaGPTAgent(AbstractAgent): diff --git a/prediction_market_agent/ai_models/abstract_ai_models.py b/prediction_market_agent/ai_models/abstract_ai_models.py index fae96ed5..82132e54 100644 --- a/prediction_market_agent/ai_models/abstract_ai_models.py +++ b/prediction_market_agent/ai_models/abstract_ai_models.py @@ -1,4 +1,5 @@ from typing import Optional + from pydantic import BaseModel diff --git a/prediction_market_agent/ai_models/llama_ai_models.py b/prediction_market_agent/ai_models/llama_ai_models.py index f728b3b4..6f62871e 100644 --- a/prediction_market_agent/ai_models/llama_ai_models.py +++ b/prediction_market_agent/ai_models/llama_ai_models.py @@ -1,11 +1,13 @@ +from enum import Enum +from typing import Literal, Optional + +import replicate +from prediction_market_agent_tooling.tools.utils import should_not_happen + from prediction_market_agent.ai_models.abstract_ai_models import ( AbstractAiChatModel, Message, ) -from enum import Enum -from typing import Optional, Literal -from prediction_market_agent_tooling.tools.utils import should_not_happen -import replicate class LlamaRole(Enum): diff --git a/prediction_market_agent/ai_models/openai_ai_models.py b/prediction_market_agent/ai_models/openai_ai_models.py index 07686af8..951b1f57 100644 --- a/prediction_market_agent/ai_models/openai_ai_models.py +++ b/prediction_market_agent/ai_models/openai_ai_models.py @@ -1,10 +1,7 @@ -from prediction_market_agent.ai_models.abstract_ai_models import ( - AbstractAiChatModel, - Message, -) from enum import Enum +from typing import Literal, Optional + from openai import OpenAI -from typing import Optional, Literal from openai.types.chat.chat_completion import ChatCompletion from openai.types.chat.chat_completion_message_param import ( ChatCompletionMessageParam, @@ -12,6 +9,11 @@ ChatCompletionUserMessageParam, ) +from prediction_market_agent.ai_models.abstract_ai_models import ( + AbstractAiChatModel, + Message, +) + ROLE_KEY = "role" CONTENT_KEY = "content" diff --git a/prediction_market_agent/tools/betting_strategies.py b/prediction_market_agent/tools/betting_strategies.py index 08b873d8..86725b0d 100644 --- a/prediction_market_agent/tools/betting_strategies.py +++ b/prediction_market_agent/tools/betting_strategies.py @@ -1,27 +1,23 @@ +import typing as t from decimal import Decimal from functools import reduce -import numpy as np -import typing as t -from web3 import Web3 -from prediction_market_agent_tooling.markets.data_models import ( - BetAmount, - Currency, -) +import numpy as np +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 ( - omen_calculate_buy_amount, OmenMarket, + omen_calculate_buy_amount, ) -from prediction_market_agent_tooling.markets.markets import MarketType from prediction_market_agent_tooling.tools.gnosis_rpc import GNOSIS_RPC_URL +from prediction_market_agent_tooling.tools.utils import check_not_none from prediction_market_agent_tooling.tools.web3_utils import ( - xdai_to_wei, - wei_to_xdai, ONE_XDAI, + wei_to_xdai, + xdai_to_wei, ) -from prediction_market_agent_tooling.gtypes import Probability, xDai, wei_type -from prediction_market_agent_tooling.tools.utils import check_not_none - +from web3 import Web3 OutcomeIndex = t.Literal[0, 1] diff --git a/prediction_market_agent/tools/web_scrape.py b/prediction_market_agent/tools/web_scrape.py index 1163ebdb..621d5274 100644 --- a/prediction_market_agent/tools/web_scrape.py +++ b/prediction_market_agent/tools/web_scrape.py @@ -1,10 +1,9 @@ import bs4 import requests - from langchain.chains.summarize import load_summarize_chain -from langchain_openai import ChatOpenAI from langchain.prompts import PromptTemplate from langchain.text_splitter import RecursiveCharacterTextSplitter +from langchain_openai import ChatOpenAI def _summary(objective: str, content: str) -> str: diff --git a/prediction_market_agent/tools/web_scrape_structured.py b/prediction_market_agent/tools/web_scrape_structured.py index a69ad142..4145c99f 100644 --- a/prediction_market_agent/tools/web_scrape_structured.py +++ b/prediction_market_agent/tools/web_scrape_structured.py @@ -1,5 +1,6 @@ import requests from bs4 import BeautifulSoup, Comment, Tag + from prediction_market_agent.tools.web_scrape import _summary diff --git a/prediction_market_agent/utils.py b/prediction_market_agent/utils.py index 6682160f..07f0a8b3 100644 --- a/prediction_market_agent/utils.py +++ b/prediction_market_agent/utils.py @@ -1,9 +1,10 @@ import typing as t + +from prediction_market_agent_tooling.config import APIKeys as APIKeysBase from prediction_market_agent_tooling.tools.utils import ( check_not_none, should_not_happen, ) -from prediction_market_agent_tooling.config import APIKeys as APIKeysBase class APIKeys(APIKeysBase): diff --git a/pyproject.toml b/pyproject.toml index e9aaacbd..a258f696 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -39,6 +39,7 @@ cron-validator = "^1.0.8" prediction-market-agent-tooling = { git = "https://github.com/gnosis/prediction-market-agent-tooling.git", branch = "peter/fix-apikeys" } pydantic-settings = "^2.1.0" autoflake = "^2.2.1" +isort = "^5.13.2" [build-system] requires = ["poetry-core"] diff --git a/tests/ai_models/test_llama_ai_models.py b/tests/ai_models/test_llama_ai_models.py index 98f7e600..78252279 100644 --- a/tests/ai_models/test_llama_ai_models.py +++ b/tests/ai_models/test_llama_ai_models.py @@ -1,7 +1,7 @@ from prediction_market_agent.ai_models.llama_ai_models import ( - construct_llama_prompt, - Message, LlamaRole, + Message, + construct_llama_prompt, ) diff --git a/tests/tools/test_betting_strategies.py b/tests/tools/test_betting_strategies.py index 457ab460..b68dd6f7 100644 --- a/tests/tools/test_betting_strategies.py +++ b/tests/tools/test_betting_strategies.py @@ -1,17 +1,18 @@ -import pytest import numpy as np -from prediction_market_agent_tooling.markets.markets import omen +import pytest from prediction_market_agent_tooling.gtypes import ( - Probability, - xdai_type, - wei_type, - usd_type, HexAddress, - OmenOutcomeToken, HexStr, + OmenOutcomeToken, + Probability, Wei, + usd_type, + wei_type, xDai, + xdai_type, ) +from prediction_market_agent_tooling.markets.markets import omen + from prediction_market_agent.tools.betting_strategies import ( get_kelly_criterion_bet, get_market_moving_bet,