Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnicita committed Aug 1, 2024
1 parent d94a48f commit e1131f9
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 43 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@ Run the following commands from the agents directory to run an ephemeral docker
./scripts/bash/build-docker.sh
./scripts/bash/run-docker-dev.sh
```

# Future

This repo is intended to be an experimental developer toolkit. In order to productionize the system with capital, we recommend the following major projects begin to be developed:

[ ] Backtesting
[ ] Papertrading

Other major development endeavors that would support the current state of the system include:

[ ] External embeddings
[ ] More news api sources
41 changes: 38 additions & 3 deletions ai/llm/executor.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import os
import time
import json

from dotenv import load_dotenv
from langchain_core.messages import HumanMessage, SystemMessage
from langchain_openai import ChatOpenAI
from langchain_openai import OpenAIEmbeddings

from ai.llm import prompts
from api.gamma import GammaMarketClient
from polymarket.agents.ai.llm.prompts import Prompter
from ai.llm.prompts import Prompter
from ai.rag.chroma import Chroma


class Executor:

def __init__(self):
load_dotenv()
self.prompter = Prompter()
Expand All @@ -18,7 +23,13 @@ def __init__(self):
model="gpt-3.5-turbo",
temperature=0,
)
self.llm_embedding_function = OpenAIEmbeddings(model="text-embedding-3-small")
self.client = GammaMarketClient()
self.chroma = Chroma()

self.local_data_directory = "./localDb"
if not os.path.isdir(self.local_data_directory):
os.mkdir(self.local_data_directory)

def get_llm_response(self, user_input: str) -> str:
system_message = SystemMessage(content=str(prompts.market_analyst))
Expand Down Expand Up @@ -47,8 +58,32 @@ def get_polymarket_llm(self, user_input: str) -> str:
result = self.llm.invoke(messages)
return result.content

def filter_events(self):
pass
def filter_events(self, events):
if not self.chroma:
# create local embedding for events
local_events_embedding_path = f"{self.local_data_directory}/events.json"
if os.path.isfile(self.local_data_directory):
os.remove(local_events_embedding_path)
with open(local_events_embedding_path, "w+") as output_file:
json.dump(events, output_file)

# load embedding into chroma
local_db = Chroma(
persist_directory=self.local_data_directory,
embedding_function=self.llm_embedding_function,
)

# query using a prompt
prompt = self.prompter.filter_events()
print(prompt)
response_docs = local_db.similarity_search_with_score(query=prompt)
print(response_docs)
return response_docs

else:
self.chroma.create_local_markets_rag()
prompt = self.prompter.filter_events()
return self.chroma.query_local_markets_rag(prompt)

def filter_markets(self):
pass
Expand Down
2 changes: 1 addition & 1 deletion ai/llm/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def polymarket_analyst_api(self) -> str:

def filter_events(self) -> str:
return (
self.polymarket_analyst_api(self)
self.polymarket_analyst_api()
+ """
Filter events for the ones you will be best at trading on profitably.
Expand Down
8 changes: 4 additions & 4 deletions ai/rag/chroma.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
from api.gamma import GammaMarketClient


class PolymarketRAG:
class Chroma:
def __init__(self, local_db_directory=None, embedding_function=None) -> None:
self.gamma_client = GammaMarketClient()
self.local_db_directory = local_db_directory
self.embedding_function = embedding_function

def load_json_from_local(
self, json_file_path=None, vector_db_directory="./local_db"
self, json_file_path=None, vector_db_directory="./localDb"
):
loader = JSONLoader(
file_path=json_file_path, jq_schema=".[].description", text_content=False
Expand All @@ -29,7 +29,7 @@ def load_json_from_local(

return db2

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

if not os.path.isdir(local_directory):
Expand All @@ -44,7 +44,7 @@ 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, query=None, local_directory="./local_db"):
embedding_function = OpenAIEmbeddings(model="text-embedding-3-small")
local_db = Chroma(
persist_directory=local_directory, embedding_function=embedding_function
Expand Down
8 changes: 4 additions & 4 deletions api/polymarket.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
from py_clob_client.clob_types import OrderArgs
from py_clob_client.order_builder.constants import BUY

from api.polymarket.types import SimpleMarket
from api.polymarket.types import SimpleEvent
from api.types import SimpleMarket
from api.types import SimpleEvent

load_dotenv()

Expand All @@ -45,8 +45,8 @@ def __init__(self):
self.exchange_address = "0x4bfb41d5b3570defd03c39a9a4d8de6bd8b8982e"
self.neg_risk_exchange_address = "0xC5d563A36AE78145C45a50134d48A1215220f80a"

self._init_api_keys()
self._init_approvals(True)
# self._init_api_keys()
# self._init_approvals(True)

def _init_api_keys(self):
self.client = ClobClient(
Expand Down
19 changes: 11 additions & 8 deletions jobs/scheduler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import datetime as dt
import time
from polymarket.agents.lib.trade import Trader
from lib.trade import Trader

from polymarket.agents.lib.history import record_history
from polymarket.agents.lib.refresh import refresh_trades
from lib.history import record_history
from lib.refresh import refresh_trades

from scheduler import Scheduler
from scheduler.trigger import Monday


class Scheduler:
class MetaScheduler:
def __init__(self):
self.trader = Trader()
self.schedule = Scheduler()
Expand All @@ -22,11 +22,14 @@ def start(self):

class TradingAgent(Scheduler):
def __init__(self):
super()
self.trader = Trader()
self.weekly(Monday(), self.trader.one_best_trade)
self.daily(dt.time(hour=12), refresh_trades)
self.hourly(dt.time(minute=30), record_history)
self.schedule = Scheduler()
self.schedule.weekly(Monday(), self.trader.one_best_trade)
self.schedule.daily(dt.time(hour=12), refresh_trades)
self.schedule.hourly(dt.time(minute=30), record_history)

def trade(self):
self.trader.one_best_trade()


# TODO: add task objects for generalized schedule management infrastructure
6 changes: 3 additions & 3 deletions lib/creator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from polymarket.agents.ai.llm.executor import Executor as Agent
from polymarket.agents.api.polymarket import Polymarket
from polymarket.agents.data.newspaper import Newspaper
from ai.llm.executor import Executor as Agent
from api.polymarket import Polymarket
from data.newspaper import Newspaper


class Creator:
Expand Down
34 changes: 21 additions & 13 deletions lib/trade.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from polymarket.agents.ai.llm.executor import Executor as Agent
from polymarket.agents.api.polymarket import Polymarket
from polymarket.agents.data.newspaper import Newspaper
from ai.llm.executor import Executor as Agent
from api.polymarket import Polymarket
from data.newspaper import Newspaper

import pdb


class Trader:
Expand All @@ -19,17 +21,23 @@ def one_best_trade(self):
then executes that trade without any human intervention
"""

print("1. GETTING ALL EVENTS polymarket.get_all_events()")
events = self.polymarket.get_all_events()
events = self.agent.filter_events()
markets = [self.polymarket.get_market(e) for e in events]
markets = self.agent.filter_markets()
orderbooks = [self.polymarket.get_orderbooks(m) for m in markets]
orderbooks = self.agent.filter_orderbooks()
best_trade = self.agent.source_best_trade(
events, markets, orderbooks, self.newspaper
)
formatted_best_trade = self.agent.format_trade_prompt_for_execution(best_trade)
return self.polymarket.execute_order(**formatted_best_trade)
print("2. GOT EVENTS. len(events) = ", len(events))
print("3. FILTERING EVENTS ...")
pdb.set_trace()
events = self.agent.filter_events([x.json() for x in events])
print("4. GOT BEST EVENTS: ", events)
# markets = [self.polymarket.get_market(e) for e in events]
# markets = self.agent.filter_markets()
# orderbooks = [self.polymarket.get_orderbooks(m) for m in markets]
# orderbooks = self.agent.filter_orderbooks()
# best_trade = self.agent.source_best_trade(
# events, markets, orderbooks, self.newspaper
# )
# formatted_best_trade = self.agent.format_trade_prompt_for_execution(best_trade)
# return self.polymarket.execute_order(**formatted_best_trade)

def maintain_positions(self):
pass
Expand Down
8 changes: 4 additions & 4 deletions scripts/python/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@

from api.polymarket import Polymarket
from ai.llm import executor, prompts
from ai.rag.chroma import PolymarketRAG
from ai.rag.chroma import Chroma
from data.news.newsapiorg.api import API as NewsApiCaller
from jobs.scheduler import TradingAgent

app = typer.Typer()
polymarket = Polymarket()
newsapi_client = NewsApiCaller()
polymarket_rag = PolymarketRAG()
polymarket_rag = Chroma()


@app.command()
Expand Down Expand Up @@ -118,12 +118,12 @@ def ask_polymarket_llm(user_input: str):


@app.command()
def run_autonomous_trader(user_input: str):
def run_autonomous_trader():
"""
Let an autonomous system trade for you.
"""
trader = TradingAgent()
trader.start()
trader.trade()


if __name__ == "__main__":
Expand Down
13 changes: 10 additions & 3 deletions scripts/python/server.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
"""
We envision decentralized agents being able to
communicate with one another as they decide trades.
This server could also be used for observability of
decentralized agents trading performance.
"""

from typing import Union
from fastapi import FastAPI

Expand All @@ -22,6 +32,3 @@ def read_trade(trade_id: int, q: Union[str, None] = None):
@app.get("/markets/{market_id}")
def read_market(market_id: int, q: Union[str, None] = None):
return {"market_id": market_id, "q": q}


# post new prompt

0 comments on commit e1131f9

Please sign in to comment.