Skip to content

Commit

Permalink
more python typing
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnicita committed Aug 6, 2024
1 parent f4bb309 commit d6bccaa
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 118 deletions.
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ This repo is inteded for use with Python 3.9
python application/trade.py
```

Note: If running the command outside of docker, please set the following env var:

```
export PYTHONPATH="."
```

If running with docker is preferred, we provide the following scripts:

```
./scripts/bash/build-docker.sh
./scripts/bash/run-docker-dev.sh
```

## Architecture

The Polymarket Agents architecture features modular components that can be maintained and extended by individual community members.
Expand All @@ -92,19 +105,19 @@ Polymarket Agents connectors standardize data sources and order types.

Files for managing your local environment, server set-up to run the application remotely, and cli for end-user commands.

`Cli.py` is the primary user interface for the repo. Users can run various commands to interact with the Polymarket API, retrieve relevant news articles, query local data, send data/prompts to LLMs, and execute trades in Polymarkets.
`cli.py` is the primary user interface for the repo. Users can run various commands to interact with the Polymarket API, retrieve relevant news articles, query local data, send data/prompts to LLMs, and execute trades in Polymarkets.

Commands should follow this format:

`python cli.py command_name [attribute value] [attribute value]`
`python scripts/python/cli.py command_name [attribute value] [attribute value]`

Example:

`get_all_markets`
Retrieve and display a list of markets from Polymarket, sorted by volume.

```
python cli.py get_all_markets --limit <LIMIT> --sort-by <SORT_BY>
python scripts/python/cli.py get_all_markets --limit <LIMIT> --sort-by <SORT_BY>
```

- limit: The number of markets to retrieve (default: 5).
Expand Down
2 changes: 0 additions & 2 deletions application/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from connectors.gamma import GammaMarketClient as Gamma
from connectors.polymarket import Polymarket

import pdb
import time


Expand Down Expand Up @@ -33,7 +32,6 @@ def one_best_market(self):
print()
print(f"3. FOUND {len(markets)} MARKETS")

time.sleep(5)
print()
filtered_markets = self.agent.filter_markets(markets)
print(f"4. FILTERED {len(filtered_markets)} MARKETS")
Expand Down
10 changes: 3 additions & 7 deletions application/cron.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import datetime as dt
import time
from application.trade import Trader

Expand All @@ -7,21 +6,18 @@


class Scheduler:
def __init__(self):
def __init__(self) -> None:
self.trader = Trader()
self.schedule = Scheduler()

def start(self):
def start(self) -> None:
while True:
self.schedule.exec_jobs()
time.sleep(1)


class TradingAgent(Scheduler):
def __init__(self):
def __init__(self) -> None:
super()
self.trader = Trader()
self.weekly(Monday(), self.trader.one_best_trade)


# TODO: add task objects for generalized schedule management infrastructure
25 changes: 12 additions & 13 deletions application/executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

from connectors.gamma import GammaMarketClient as Gamma
from connectors.chroma import PolymarketRAG as Chroma
from connectors.objects import SimpleEvent
from connectors.objects import SimpleEvent, SimpleMarket
from application.prompts import Prompter
from connectors.polymarket import Polymarket


class Executor:
def __init__(self):
def __init__(self) -> None:
load_dotenv()
self.prompter = Prompter()
self.openai_api_key = os.getenv("OPENAI_API_KEY")
Expand Down Expand Up @@ -56,19 +56,21 @@ def get_polymarket_llm(self, user_input: str) -> str:
result = self.llm.invoke(messages)
return result.content

def filter_events(self, events: "list[SimpleEvent]"):
def filter_events(self, events: "list[SimpleEvent]") -> str:
prompt = self.prompter.filter_events(events)
result = self.llm.invoke(prompt)
return result.content

def filter_events_with_rag(self, events: "list[SimpleEvent]"):
def filter_events_with_rag(self, events: "list[SimpleEvent]") -> str:
prompt = self.prompter.filter_events()
print()
print("... prompting ... ", prompt)
print()
return self.chroma.events(events, prompt)

def map_filtered_events_to_markets(self, filtered_events):
def map_filtered_events_to_markets(
self, filtered_events: "list[SimpleEvent]"
) -> "list[SimpleMarket]":
markets = []
for e in filtered_events:
data = json.loads(e[0].json())
Expand All @@ -79,17 +81,14 @@ def map_filtered_events_to_markets(self, filtered_events):
markets.append(formatted_market_data)
return markets

def filter_markets(self, markets):
def filter_markets(self, markets) -> "list[tuple]":
prompt = self.prompter.filter_markets()
print()
print("... prompting ... ", prompt)
print()
return self.chroma.markets(markets, prompt)

def filter_orderbooks(self):
pass

def source_best_trade(self, market_object):
def source_best_trade(self, market_object) -> str:
market_document = market_object[0].dict()
market = market_document["metadata"]
outcome_prices = ast.literal_eval(market["outcome_prices"])
Expand All @@ -103,15 +102,15 @@ def source_best_trade(self, market_object):
print()
result = self.llm.invoke(prompt)
content = result.content
time.sleep(5)

print("result: ", content)
print()
prompt = self.prompter.one_best_trade(content, outcomes, outcome_prices)
print("... prompting ... ", prompt)
print()
result = self.llm.invoke(prompt)
content = result.content
time.sleep(5)

print("result: ", content)
print()
return content
Expand All @@ -123,7 +122,7 @@ def format_trade_prompt_for_execution(self, best_trade: str) -> float:
usdc_balance = self.polymarket.get_usdc_balance()
return float(size) * usdc_balance

def source_best_market_to_create(self, filtered_markets):
def source_best_market_to_create(self, filtered_markets) -> str:
prompt = self.prompter.create_new_market(filtered_markets)
print()
print("... prompting ... ", prompt)
Expand Down
5 changes: 2 additions & 3 deletions application/trade.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self):
self.gamma = Gamma()
self.agent = Agent()

def one_best_trade(self):
def one_best_trade(self) -> None:
"""
one_best_trade is a strategy that evaluates all events, markets, and orderbooks
Expand All @@ -33,16 +33,15 @@ def one_best_trade(self):
print()
print(f"3. FOUND {len(markets)} MARKETS")

time.sleep(5)
print()
filtered_markets = self.agent.filter_markets(markets)
pdb.set_trace()
print(f"4. FILTERED {len(filtered_markets)} MARKETS")

# TODO: use even more data to build even better models!
# orderbooks = [self.polymarket.get_orderbooks(m) for m in markets]
# orderbooks = self.agent.filter_orderbooks()

time.sleep(5)
market = filtered_markets[0]
best_trade = self.agent.source_best_trade(market)
print(f"5. CALCULATED TRADE {best_trade}")
Expand Down
6 changes: 3 additions & 3 deletions application/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json


def parse_camel_case(key):
def parse_camel_case(key) -> str:
output = ""
for char in key:
if char.isupper():
Expand All @@ -12,7 +12,7 @@ def parse_camel_case(key):
return output


def preprocess_market_object(market_object):
def preprocess_market_object(market_object: dict) -> dict:
description = market_object["description"]

for k, v in market_object.items():
Expand All @@ -32,7 +32,7 @@ def preprocess_market_object(market_object):
return market_object


def preprocess_local_json(file_path, preprocessor_function):
def preprocess_local_json(file_path: str, preprocessor_function: function) -> None:
with open(file_path, "r+") as open_file:
data = json.load(open_file)

Expand Down
18 changes: 8 additions & 10 deletions connectors/chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,18 @@ def __init__(self, local_db_directory=None, embedding_function=None) -> None:

def load_json_from_local(
self, json_file_path=None, vector_db_directory="./local_db"
):
) -> None:
loader = JSONLoader(
file_path=json_file_path, jq_schema=".[].description", text_content=False
)
loaded_docs = loader.load()

embedding_function = OpenAIEmbeddings(model="text-embedding-3-small")
db2 = Chroma.from_documents(
Chroma.from_documents(
loaded_docs, embedding_function, persist_directory=vector_db_directory
)

return db2

def create_local_markets_rag(self, local_directory="./local_db"):
def create_local_markets_rag(self, local_directory="./local_db") -> None:
all_markets = self.gamma_client.get_all_current_markets()

if not os.path.isdir(local_directory):
Expand All @@ -48,15 +46,17 @@ def create_local_markets_rag(self, local_directory="./local_db"):
json_file_path=local_file_path, vector_db_directory=local_directory
)

def query_local_markets_rag(self, local_directory=None, query=None):
def query_local_markets_rag(
self, local_directory=None, query=None
) -> "list[tuple]":
embedding_function = OpenAIEmbeddings(model="text-embedding-3-small")
local_db = Chroma(
persist_directory=local_directory, embedding_function=embedding_function
)
response_docs = local_db.similarity_search_with_score(query=query)
return response_docs

def events(self, events: "list[SimpleEvent]", prompt: str):
def events(self, events: "list[SimpleEvent]", prompt: str) -> "list[tuple]":
# create local json file
local_events_directory: str = "./local_db_events"
if not os.path.isdir(local_events_directory):
Expand Down Expand Up @@ -87,12 +87,11 @@ def metadata_func(record: dict, metadata: dict) -> dict:
local_db = Chroma.from_documents(
loaded_docs, embedding_function, persist_directory=vector_db_directory
)
# local_db.persist()

# query
return local_db.similarity_search_with_score(query=prompt)

def markets(self, markets: "list[SimpleMarket]", prompt: str):
def markets(self, markets: "list[SimpleMarket]", prompt: str) -> "list[tuple]":
# create local json file
local_events_directory: str = "./local_db_markets"
if not os.path.isdir(local_events_directory):
Expand Down Expand Up @@ -125,7 +124,6 @@ def metadata_func(record: dict, metadata: dict) -> dict:
local_db = Chroma.from_documents(
loaded_docs, embedding_function, persist_directory=vector_db_directory
)
# local_db.persist()

# query
return local_db.similarity_search_with_score(query=prompt)
Loading

0 comments on commit d6bccaa

Please sign in to comment.