Skip to content

Commit

Permalink
init: polymarket agents (2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexnicita committed Aug 13, 2024
1 parent d447be4 commit 172b98c
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 59 deletions.
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ This repo is inteded for use with Python 3.9

6. Load your wallet with USDC.

7. Trade!
7. Try the command line interface...

```
python application/trade.py
python scripts/python/cli.py
```

Or just go trade!

```
python agents/application/trade.py
```

8. Note: If running the command outside of docker, please set the following env var:
Expand All @@ -89,18 +95,18 @@ This repo is inteded for use with Python 3.9

The Polymarket Agents architecture features modular components that can be maintained and extended by individual community members.

### Connectors
### APIs

Polymarket Agents connectors standardize data sources and order types.

- `Polymarket.py`: defines a Polymarket class that interacts with the Polymarket API to retrieve and manage market and event data, and to execute orders on the Polymarket DEX. It includes methods for API key initialization, market and event data retrieval, and trade execution. The file also provides utility functions for building and signing orders, as well as examples for testing API interactions.

- `Objects.py`: data models using Pydantic; representations for trades, markets, events, and related entities.

- `Chroma.py`: chroma DB for vectorizing news sources and other API data. Developers are able to add their own vector database implementations.

- `Gamma.py`: defines `GammaMarketClient` class, which interfaces with the Polymarket Gamma API to fetch and parse market and event metadata. Methods to retrieve current and tradable markets, as well as defined information on specific markets and events.

- `Polymarket.py`: defines a Polymarket class that interacts with the Polymarket API to retrieve and manage market and event data, and to execute orders on the Polymarket DEX. It includes methods for API key initialization, market and event data retrieval, and trade execution. The file also provides utility functions for building and signing orders, as well as examples for testing API interactions.

- `Objects.py`: data models using Pydantic; representations for trades, markets, events, and related entities.

### Scripts

Files for managing your local environment, server set-up to run the application remotely, and cli for end-user commands.
Expand Down Expand Up @@ -164,4 +170,4 @@ Enjoy using the CLI application! If you encounter any issues, feel free to open

# Terms of Service

[Terms of Service](https://polymarket.com/tos) prohibit US persons and persons from certain other jurisdictions from trading on Polymarket (via UI & API and including agents developed by persons in restricted jurisdictions), although data and information is viewable globally.
[Terms of Service](https://polymarket.com/tos) prohibit US persons and persons from certain other jurisdictions from trading on Polymarket (via UI & API and including agents developed by persons in restricted jurisdictions), although data and information is viewable globally.
8 changes: 3 additions & 5 deletions application/creator.py → agents/application/creator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from application.executor import Executor as Agent
from connectors.gamma import GammaMarketClient as Gamma
from connectors.polymarket import Polymarket

import time
from agents.application.executor import Executor as Agent
from agents.polymarket.gamma import GammaMarketClient as Gamma
from agents.polymarket.polymarket import Polymarket


class Creator:
Expand Down
3 changes: 2 additions & 1 deletion application/cron.py → agents/application/cron.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from agents.application.trade import Trader

import time
from application.trade import Trader

from scheduler import Scheduler
from scheduler.trigger import Monday
Expand Down
14 changes: 6 additions & 8 deletions application/executor.py → agents/application/executor.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import os
import json
import pdb
import ast
import time
import re

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

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


class Executor:
Expand All @@ -40,7 +38,7 @@ def get_superforecast(
self, event_title: str, market_question: str, outcome: str
) -> str:
messages = self.prompter.superforecaster(
event_title=event_title, market_question=market_question, outcome=outcome
description=event_title, question=market_question, outcome=outcome
)
result = self.llm.invoke(messages)
return result.content
Expand Down
13 changes: 6 additions & 7 deletions application/prompts.py → agents/application/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def market_analyst(self) -> str:
Assign a probability estimate to the event occurring described by the user
"""

def sentiment_analyzer(question: str, outcome: str) -> float:
def sentiment_analyzer(self, question: str, outcome: str) -> float:
return f"""
You are a political scientist trained in media analysis.
You are given a question: {question}.
Expand All @@ -34,7 +34,7 @@ def sentiment_analyzer(question: str, outcome: str) -> float:
"""

def prompts_polymarket(
data1: str, data2: str, market_question: str, outcome: str
self, data1: str, data2: str, market_question: str, outcome: str
) -> str:
current_market_data = str(data1)
current_event_data = str(data2)
Expand All @@ -52,7 +52,7 @@ def prompts_polymarket(
I believe {market_question} has a likelihood {float} for outcome of {outcome}.
"""

def prompts_polymarket(data1: str, data2: str, user_input: str) -> str:
def prompts_polymarket(self, data1: str, data2: str) -> str:
current_market_data = str(data1)
current_event_data = str(data2)
return f"""
Expand All @@ -63,13 +63,12 @@ def prompts_polymarket(data1: str, data2: str, user_input: str) -> str:
current Polymarket events {current_event_data}.
Help users identify markets to trade based on their interests or queries.
Provide specific information for markets including probabilities of outcomes.
"""

def routing(system_message: str) -> str:
return f"""You are an expert at routing a user question to the appropriate data source. """
def routing(self, system_message: str) -> str:
return f"""You are an expert at routing a user question to the appropriate data source. System message: ${system_message}"""

def multiquery(question: str) -> str:
def multiquery(self, question: str) -> str:
return f"""
You're an AI assistant. Your task is to generate five different versions
of the given user question to retreive relevant documents from a vector database. By generating
Expand Down
6 changes: 3 additions & 3 deletions application/trade.py → agents/application/trade.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from application.executor import Executor as Agent
from connectors.gamma import GammaMarketClient as Gamma
from connectors.polymarket import Polymarket
from agents.application.executor import Executor as Agent
from agents.polymarket.gamma import GammaMarketClient as Gamma
from agents.polymarket.polymarket import Polymarket

import shutil

Expand Down
6 changes: 2 additions & 4 deletions connectors/chroma.py → agents/connectors/chroma.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import json
import os
import time
import pdb

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
from agents.polymarket.gamma import GammaMarketClient
from agents.utils.objects import SimpleEvent, SimpleMarket


class PolymarketRAG:
Expand Down
2 changes: 1 addition & 1 deletion connectors/news.py → agents/connectors/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from newsapi import NewsApiClient

from connectors.objects import Article
from agents.utils.objects import Article


class News:
Expand Down
File renamed without changes.
7 changes: 2 additions & 5 deletions connectors/gamma.py → agents/polymarket/gamma.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import httpx
import json

from connectors.objects import Market, PolymarketEvent
from connectors.objects import ClobReward
from connectors.objects import Tag

from connectors.polymarket import Polymarket
from agents.polymarket.polymarket import Polymarket
from agents.utils.objects import Market, PolymarketEvent, ClobReward, Tag


class GammaMarketClient:
Expand Down
15 changes: 10 additions & 5 deletions connectors/polymarket.py → agents/polymarket/polymarket.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
)
from py_clob_client.order_builder.constants import BUY

from connectors.objects import SimpleMarket, SimpleEvent
from agents.utils.objects import SimpleMarket, SimpleEvent

load_dotenv()

Expand Down Expand Up @@ -194,14 +194,15 @@ def get_all_markets(self) -> "list[SimpleMarket]":
try:
market_data = self.map_api_to_market(market)
markets.append(SimpleMarket(**market_data))
except:
except Exception as e:
print(e)
pass
return markets

def filter_markets_for_trading(self, markets: "list[SimpleMarket]"):
tradeable_markets = []
for market in markets:
if market.active and market.deployed:
if market.active:
tradeable_markets.append(market)
return tradeable_markets

Expand Down Expand Up @@ -238,21 +239,25 @@ def get_all_events(self) -> "list[SimpleEvent]":
events = []
res = httpx.get(self.gamma_events_endpoint)
if res.status_code == 200:
print(len(res.json()))
for event in res.json():
try:
print(1)
event_data = self.map_api_to_event(event)
events.append(SimpleEvent(**event_data))
except:
except Exception as e:
print(e)
pass
return events

def map_api_to_event(self, event) -> SimpleEvent:
description = event["description"] if "description" in event.keys() else ""
return {
"id": int(event["id"]),
"ticker": event["ticker"],
"slug": event["slug"],
"title": event["title"],
"description": event["description"],
"description": description,
"active": event["active"],
"closed": event["closed"],
"archived": event["archived"],
Expand Down
4 changes: 2 additions & 2 deletions connectors/objects.py → agents/utils/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class SimpleMarket(BaseModel):
end: str
description: str
active: bool
deployed: bool
# deployed: Optional[bool]
funded: bool
# orderMinSize: float
# orderPriceMinTickSize: float
rewardsMinSize: float
rewardsMaxSpread: float
volume: float
# volume: Optional[float]
spread: float
outcomes: str
outcome_prices: str
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion connectors/recorder.py

This file was deleted.

File renamed without changes.
Binary file added docs/images/cli.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 12 additions & 9 deletions scripts/python/cli.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import typer
from devtools import pprint

from connectors.polymarket import Polymarket
from application import executor, prompts
from connectors.chroma import PolymarketRAG
from connectors.news import News
from application.trade import Trader
from application.creator import Creator
from agents.polymarket.polymarket import Polymarket
from agents.connectors.chroma import PolymarketRAG
from agents.connectors.news import News
from agents.application.trade import Trader
from agents.application.executor import Executor
from agents.application.creator import Creator

app = typer.Typer()
polymarket = Polymarket()
Expand All @@ -15,15 +15,15 @@


@app.command()
def get_all_markets(limit: int = 5, sort_by: str = "volume") -> None:
def get_all_markets(limit: int = 5, sort_by: str = "spread") -> None:
"""
Query Polymarket's markets
"""
print(f"limit: int = {limit}, sort_by: str = {sort_by}")
markets = polymarket.get_all_markets()
markets = polymarket.filter_markets_for_trading(markets)
if sort_by == "volume":
markets = sorted(markets, key=lambda x: x.volume, reverse=True)
if sort_by == "spread":
markets = sorted(markets, key=lambda x: x.spread, reverse=True)
markets = markets[:limit]
pprint(markets)

Expand Down Expand Up @@ -78,6 +78,7 @@ def ask_superforecaster(event_title: str, market_question: str, outcome: str) ->
print(
f"event: str = {event_title}, question: str = {market_question}, outcome (usually yes or no): str = {outcome}"
)
executor = Executor()
response = executor.get_superforecast(
event_title=event_title, market_question=market_question, outcome=outcome
)
Expand All @@ -99,6 +100,7 @@ def ask_llm(user_input: str) -> None:
"""
Ask a question to the LLM and get a response.
"""
executor = Executor()
response = executor.get_llm_response(user_input)
print(f"LLM Response: {response}")

Expand All @@ -108,6 +110,7 @@ def ask_polymarket_llm(user_input: str) -> None:
"""
What types of markets do you want trade?
"""
executor = Executor()
response = executor.get_polymarket_llm(user_input=user_input)
print(f"LLM + current markets&events response: {response}")

Expand Down
File renamed without changes.

0 comments on commit 172b98c

Please sign in to comment.