Skip to content

Commit

Permalink
beginning of an integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnicita committed Aug 2, 2024
1 parent 9a25723 commit 3619e64
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ archive.zip
.*.sw?
.cache
.DS_Store
local_db/
local_db*
1 change: 0 additions & 1 deletion application/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
class Creator:
def __init__(self):
self.polymarket = Polymarket()
self.newspaper = Newspaper()
self.agent = Agent()

def one_best_market(self):
Expand Down
56 changes: 43 additions & 13 deletions application/executor.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import os
import json
import pdb

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

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


class Executor:
Expand All @@ -18,10 +22,12 @@ def __init__(self):
model="gpt-3.5-turbo",
temperature=0,
)
self.client = GammaMarketClient()
self.gamma = Gamma()
self.chroma = Chroma()
self.polymarket = Polymarket()

def get_llm_response(self, user_input: str) -> str:
system_message = SystemMessage(content=str(prompts.market_analyst))
system_message = SystemMessage(content=str(self.prompter.market_analyst()))
human_message = HumanMessage(content=user_input)
messages = [system_message, human_message]
result = self.llm.invoke(messages)
Expand All @@ -30,33 +36,57 @@ def get_llm_response(self, user_input: str) -> str:
def get_superforecast(
self, event_title: str, market_question: str, outcome: str
) -> str:
messages = prompts.superforecaster(
messages = self.prompter.superforecaster(
event_title=event_title, market_question=market_question, outcome=outcome
)
result = self.llm.invoke(messages)
return result.content

def get_polymarket_llm(self, user_input: str) -> str:
data1 = self.client.get_current_events()
data2 = self.client.get_current_markets()
data1 = self.gamma.get_current_events()
data2 = self.gamma.get_current_markets()
system_message = SystemMessage(
content=str(prompts.prompts_polymarket(data1=data1, data2=data2))
content=str(self.prompter.prompts_polymarket(data1=data1, data2=data2))
)
human_message = HumanMessage(content=user_input)
messages = [system_message, human_message]
result = self.llm.invoke(messages)
return result.content

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

def filter_markets(self):
pass
def filter_events_with_rag(self, events: "list[SimpleEvent]"):
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):
markets = []
for e in filtered_events:
data = json.loads(e[0].json())
market_ids = data["metadata"]["markets"].split(",")
for market_id in market_ids:
market_data = self.gamma.get_market(market_id)
formatted_market_data = self.polymarket.map_api_to_market(market_data)
markets.append(formatted_market_data)
return markets

def filter_markets(self, markets):
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):
def source_best_trade(self, market):
pass

def format_trade_prompt_for_execution(self):
Expand Down
17 changes: 14 additions & 3 deletions application/prompts.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Prompter:

def generate_simple_ai_trader(market_description: str, relevant_info: str) -> str:
return f"""
Expand Down Expand Up @@ -121,10 +122,20 @@ def polymarket_analyst_api(self) -> str:

def filter_events(self) -> str:
return (
self.polymarket_analyst_api(self)
+ """
self.polymarket_analyst_api()
+ f"""
Filter these events for the ones you will be best at trading on profitably.
"""
)

def filter_markets(self) -> str:
return (
self.polymarket_analyst_api()
+ f"""
Filter events for the ones you will be best at trading on profitably.
Filter these markets for the ones you will be best at trading on profitably.
"""
)
44 changes: 33 additions & 11 deletions application/trade.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from application.executor import Executor as Agent
from connectors.gamma import GammaMarketClient as Gamma
from connectors.polymarket import Polymarket

import pdb
import json


class Trader:
def __init__(self):
self.polymarket = Polymarket()
self.gamma = Gamma()
self.agent = Agent()

def one_best_trade(self):
Expand All @@ -17,20 +22,37 @@ def one_best_trade(self):
then executes that trade without any human intervention
"""
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)
try:
events = self.polymarket.get_all_events()
print(f"1. FOUND {len(events)} EVENTS")

filtered_events = self.agent.filter_events_with_rag(events)
print(f"2. FILTERED {len(filtered_events)} EVENTS")

markets = self.agent.map_filtered_events_to_markets(filtered_events)
print(f"3. FOUND {len(filtered_events)} MARKETS")

filtered_markets = self.agent.filter_markets(markets)
print(f"4. FILTERED {len(filtered_markets)} MARKETS")

# orderbooks = [self.polymarket.get_orderbooks(m) for m in markets]
# orderbooks = self.agent.filter_orderbooks()

# best_trade = self.agent.source_best_trade(filtered_markets[0])
# formatted_best_trade = self.agent.format_trade_prompt_for_execution(best_trade)

# return self.polymarket.execute_order(**formatted_best_trade)
except Exception as e:
print(f"Error {e} \n \n Retrying")
self.one_best_trade()

def maintain_positions(self):
pass

def incentive_farm(self):
pass


if __name__ == "__main__":
t = Trader()
t.one_best_trade()
78 changes: 77 additions & 1 deletion connectors/chroma.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import json
import os
import time
import pdb

from langchain_chroma import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain_community.document_loaders import JSONLoader
from langchain_community.vectorstores.chroma import Chroma

from connectors.gamma import GammaMarketClient
from connectors.objects import SimpleEvent
from connectors.objects import SimpleMarket


class PolymarketRAG:
Expand Down Expand Up @@ -52,3 +55,76 @@ def query_local_markets_rag(self, local_directory=None, query=None):
)
response_docs = local_db.similarity_search_with_score(query=query)
return response_docs

def events(self, events: "list[SimpleEvent]", prompt: str):
# create local json file
local_events_directory: str = "./local_db_events"
if not os.path.isdir(local_events_directory):
os.mkdir(local_events_directory)
local_file_path = f"{local_events_directory}/events.json"
dict_events = [x.dict() for x in events]
with open(local_file_path, "w+") as output_file:
json.dump(dict_events, output_file)

# create vector db
def metadata_func(record: dict, metadata: dict) -> dict:

metadata["id"] = record.get("id")
metadata["markets"] = record.get("markets")

return metadata

loader = JSONLoader(
file_path=local_file_path,
jq_schema=".[]",
content_key="description",
text_content=False,
metadata_func=metadata_func,
)
loaded_docs = loader.load()
embedding_function = OpenAIEmbeddings(model="text-embedding-3-small")
vector_db_directory = f"{local_events_directory}/chroma"
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):
# create local json file
local_events_directory: str = "./local_db_markets"
if not os.path.isdir(local_events_directory):
os.mkdir(local_events_directory)
local_file_path = f"{local_events_directory}/markets.json"
with open(local_file_path, "w+") as output_file:
json.dump(markets, output_file)

# create vector db
def metadata_func(record: dict, metadata: dict) -> dict:

metadata["id"] = record.get("id")
metadata["outcomes"] = record.get("outcomes")
metadata["outcome_prices"] = record.get("outcome_prices")
metadata["question"] = record.get("question")

return metadata

loader = JSONLoader(
file_path=local_file_path,
jq_schema=".[]",
content_key="description",
text_content=False,
metadata_func=metadata_func,
)
loaded_docs = loader.load()
embedding_function = OpenAIEmbeddings(model="text-embedding-3-small")
vector_db_directory = f"{local_events_directory}/chroma"
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)
9 changes: 8 additions & 1 deletion connectors/gamma.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class GammaMarketClient:
def __init__(self):
self.gamma_url = "https://gamma-polymarket.com"
self.gamma_url = "https://gamma-api.polymarket.com"
self.gamma_markets_endpoint = self.gamma_url + "/markets"
self.gamma_events_endpoint = self.gamma_url + "/events"

Expand Down Expand Up @@ -175,3 +175,10 @@ def get_clob_tradable_markets(self, limit=2):
"enableOrderBook": True,
}
)

def get_market(self, market_id: int):
url = self.gamma_markets_endpoint + "/" + str(market_id)
print(url)
response = httpx.get(url)
return response.json()
# 902884
8 changes: 4 additions & 4 deletions connectors/polymarket.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

class Polymarket:
def __init__(self):
self.gamma_url = "https://gamma-com"
self.gamma_url = "https://gamma-api.polymarket.com"
self.gamma_markets_endpoint = self.gamma_url + "/markets"
self.gamma_events_endpoint = self.gamma_url + "/events"

Expand Down Expand Up @@ -205,18 +205,18 @@ def get_market(self, token_id: str) -> SimpleMarket:
market = data[0]
return self.map_api_to_market(market, token_id)

def map_api_to_market(self, market, token_id) -> SimpleMarket:
def map_api_to_market(self, market, token_id: str = "") -> SimpleMarket:
market = {
"id": int(market["id"]),
"question": market["question"],
"end": market["endDate"],
"description": market["description"],
"active": market["active"],
"deployed": market["deployed"],
# "deployed": market["deployed"],
"funded": market["funded"],
"rewardsMinSize": float(market["rewardsMinSize"]),
"rewardsMaxSpread": float(market["rewardsMaxSpread"]),
"volume": float(market["volume"]),
# "volume": float(market["volume"]),
"spread": float(market["spread"]),
"outcomes": str(market["outcomes"]),
"outcome_prices": str(market["outcomePrices"]),
Expand Down
Loading

0 comments on commit 3619e64

Please sign in to comment.