From 2bffebd881b0af7b29d0e6e201e98f2d7efde441 Mon Sep 17 00:00:00 2001 From: Andrew-Forman Date: Mon, 5 Aug 2024 13:05:40 -0400 Subject: [PATCH] Updated Readme --- README.md | 131 +++++++++++++++++++++++++++++++++++++- ai/llm/prompt_executor.py | 6 ++ ai/llm/prompts.py | 25 ++------ ai/llm/tools.py | 2 + cli.py | 7 +- 5 files changed, 146 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 4781811..a94151b 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,133 @@ -# Agents +# Polymarket Agents + +Polymarket Agents is an advanced developer framework and set of utilities for building AI agents for Polymarket. estimating outcomes in information market, or bots that can trade in Polymarkets. + +This code is free and publicly available under MIT License open source license! + +## Features + +- Integration with Polymarket API +- AI agent utilities for prediction markets +- Local and remote RAG (Retrieval-Augmented Generation) support +- Data sourcing from betting services, news providers, and web search +- Comphrehensive LLM tools for prompt engineering + +# Getting started This repo is inteded for use with Python 3.9 +1. Clone the repository + + ``` + git clone https://github.com/yourusername/polymarket-agents.git` + cd polymarket-agents + ``` + +2. Create the virtual environment + ```` + python -m venv venv + ``` + ```` +3. Activate the virtual environment + + - On Windows: + + ``` + venv\Scripts\activate + ``` + + - On macOS and Linux: + + ``` + source venv/bin/activate + ``` + +4. Install the required dependencies: + + ``` + pip install -r requirements.txt + ``` + +5. Set up your environment variables: + + - Create a `.env` file in the project root directory + - Add the following environment variables: + + ``` + POLYMARKET_API_KEY=your_polymarket_api_key + POLYMARKET_SECRET=your_polymarket_secret + POLYMARKET_PASS_PHRASE=your_polymarket_pass_phrase + ``` + +6. Load wallet with USDC + +## Architecture + +The Polymarket Agents architecture features modular components that can be maintained and extended by individual community members. + +### Connectors + +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 data. Methods to retrieve current and tradable markets, as well as defined information on specific markets and events. + +### Scripts + +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. + +Commands should follow this format: + +`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. + ``` -virtualenv --python=python3.9 .venv -source .venv/bin/activate +python cli.py get_all_markets --limit --sort-by ``` + +- limit: The number of markets to retrieve (default: 5). +- sort_by: The sorting criterion, either volume (default) or another valid attribute. + +# Contributing + +If you would like to contribute to this project, please follow these steps: + +1. Fork the repository. +2. Create a new branch. +3. Make your changes. +4. Submit a pull request. + +# Related Repos + +- [py-clob-client](https://github.com/Polymarket/py-clob-client): Python client for the Polymarket CLOB +- [python-order-utils](https://github.com/Polymarket/python-order-utils): Python utilities to generate and sign orders from Polymarket's CLOB +- [Polymarket CLOB client](https://github.com/Polymarket/clob-client): Typescript client for Polymarket CLOB +- [Langchain](https://github.com/langchain-ai/langchain): Utility for building context-aware reasoning applications +- [Chroma](https://docs.trychroma.com/getting-started): Chroma is an AI-native open-source vector database + +# Prediction markets reading + +- Prediction Markets: Bottlenecks and the Next Major Unlocks, Mikey 0x: https://mirror.xyz/1kx.eth/jnQhA56Kx9p3RODKiGzqzHGGEODpbskivUUNdd7hwh0 +- The promise and challenges of crypto + AI applications, Vitalik Buterin: https://vitalik.eth.limo/general/2024/01/30/cryptoai.html +- Superforecasting: How to Upgrade Your Company's Judgement, Schoemaker and Tetlock: https://hbr.org/2016/05/superforecasting-how-to-upgrade-your-companys-judgment + +# License + +This project is licensed under the MIT License. See the LICENSE file for details. + +# Contact + +For any questions or inquiries, please contact name@polymarket.com. + +Enjoy using the CLI application! If you encounter any issues, feel free to open an issue on the repository. diff --git a/ai/llm/prompt_executor.py b/ai/llm/prompt_executor.py index 0b2e9d6..4a530c2 100644 --- a/ai/llm/prompt_executor.py +++ b/ai/llm/prompt_executor.py @@ -5,6 +5,7 @@ from langchain_core.messages import HumanMessage, SystemMessage from langchain_openai import ChatOpenAI from langchain_core.runnables import RunnablePassthrough +from langchain.prompts import ChatPromptTemplate from ai.llm import prompts from api.polymarket.gamma_market_client import GammaMarketClient @@ -47,4 +48,9 @@ def get_polymarket_llm(user_input: str) -> str: return result.content +def generate_queries(user_input: str) -> str: + prompt_perspectives = ChatPromptTemplate.from_template(prompts.decomposition_prompt) + generate_queries = prompt_perspectives | ChatOpenAI(temperature=0) + + # add prompts on market data and news data diff --git a/ai/llm/prompts.py b/ai/llm/prompts.py index cf3f08e..f292d16 100644 --- a/ai/llm/prompts.py +++ b/ai/llm/prompts.py @@ -48,7 +48,7 @@ def superforecaster(event_title: str, market_question: str, outcome: str) -> str - Seek out diverse sources of information. - Look for both quantitative data and qualitative insights. - Stay updated on relevant news and expert analyses. - 3. Considere Base Rates: + 3. Consider Base Rates: - Use statistical baselines or historical averages as a starting point. - Compare the current situation to similar past events to establish a benchmark probability. 4. Identify and Evaluate Factors: @@ -68,24 +68,6 @@ def superforecaster(event_title: str, market_question: str, outcome: str) -> str """ -def prompts_polymarket(data1: str, data2: str, user_input: str) -> str: - current_market_data = str(data1) - current_event_data = str(data2) - return f""" - You are an AI assistant for users of a prediction market called Polymarket. - Users want to place bets based on their beliefs of market outcomes such as political or sports events. - - Here is data for current Polymarket markets {current_market_data} and - 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. - Give your response in the following format: - - I believe {market_question} has a likelihood {float} for outcome of {outcome}. - """ - - def prompts_polymarket(data1: str, data2: str, user_input: str) -> str: current_market_data = str(data1) current_event_data = str(data2) @@ -115,6 +97,11 @@ def multiquery(question: str) -> str: """ +def decomposition_prompt(question: str) -> str: + return f"""You are a helpful assistant that generates multiple sub-questions related to an input question. \n + The goal is to break down the input into a set of sub-problems / sub-questions that can be answers in isolation. \n + Generate multiple search queries related to: {question} \n + Output (3 queries):""" """ def read_polymarket() -> str: return f""" diff --git a/ai/llm/tools.py b/ai/llm/tools.py index bda68ac..2d59806 100644 --- a/ai/llm/tools.py +++ b/ai/llm/tools.py @@ -2,3 +2,5 @@ from typing import Annotated from json import tool from langchain.tools import tool + +# def get_market_news(market_question) diff --git a/cli.py b/cli.py index 80670a6..22c4bb4 100644 --- a/cli.py +++ b/cli.py @@ -7,7 +7,7 @@ from ai.llm import prompt_executor, prompts from data_sources.news_providers.newsapi_org.newsapi_caller import NewsApiCaller from langchain_core.output_parsers import StrOutputParser -from data_sources.news_providers.newsapi_org.newsapi_caller import NewsApiCaller +from langchain.prompts import ChatPromptTemplate from local_rag import run_query_on_local_data app = typer.Typer() @@ -95,10 +95,11 @@ def ask_llm(user_input: str): @app.command() -def ask_polymarket_llm(user_input: str): +def advanced_rag(user_input: str): """ - What types of markets do you want trade? + Ask a question, reference Polymarket data, send to LLM, get a response. """ + prompt_perspectives = Chat rag_chain = ( {"context": retriever, "question": user_input}