Skip to content

Commit

Permalink
chore: update parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jmoreira-valory committed Sep 14, 2023
1 parent 8bee4e2 commit 378ef2b
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 35 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,14 @@ export SAFE_CONTRACT_ADDRESS="YOUR_SAFE_ADDRESS"

export NEWSAPI_ENDPOINT=https://newsapi.org/v2/top-headlines
export NEWSAPI_API_KEY=YOUR_NEWSAPI_API_KEY
export NEWSAPI_CATEGORIES='["business","entertainment","general","health","science","sports","technology"]'
export OPENAI_API_KEY=YOUR_OPENAI_API_KEY
export ENGINE="gpt-4"
export MARKET_APPROVAL_SERVER_URL=YOUR_MARKET_APPROVAL_SERVER_URL
export MARKET_APPROVAL_SERVER_API_KEY=YOUR_MARKET_APPROVAL_SERVER_API_KEY

export NUM_MARKETS=10
export TOPICS='["business","science","technology","politics","arts","weather"]'
export NUM_PROPOSED_MARKETS_PER_DAY=80
export DAYS_AHEAD_TO_PROPOSE_MARKETS=5
export MARKET_FEE=2
export INITIAL_FUNDS=1
export MINIMUM_MARKET_TIME=1
Expand Down
7 changes: 4 additions & 3 deletions packages/valory/agents/market_maker/aea-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,13 @@ models:
outcome of the market to be verified.\n - topic; One word description of the
topic of the news and it should be one of; {topics}.\n* Output only the JSON
object. Do not include any other contents in your response.}
num_markets: ${int:1}
num_proposed_markets_per_day: ${int:80}
days_ahead_to_propose_markets: ${int:5}
market_approval_server_url: ${str:http://127.0.0.1:5000}
market_approval_server_api_key: ${str:api_key}
newsapi_endpoint: ${str:https://newsapi.org/v2/everything}
newsapi_endpoint: ${str:https://newsapi.org/v2/top-headlines}
newsapi_api_key: ${str:f1ea36ae6175474bbcbae11eca64d0b9}
topics: ${list:["business","cryptocurrency","politics","science","technology"]}
newsapi_categories: ${list:["business","entertainment","general","health","science","sports","technology"]}
market_fee: ${float:2.0}
initial_funds: ${float:1.0}
market_timeout: ${int:1}
Expand Down
7 changes: 4 additions & 3 deletions packages/valory/services/market_maker/service.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ models:
outcome of the market to be verified.\n - topic; One word description of the
topic of the news and it should be one of; {topics}.\n* Output only the JSON
object. Do not include any other contents in your response.}
num_markets: ${NUM_MARKETS:int:1}
num_proposed_markets_per_day: ${NUM_PROPOSED_MARKETS_PER_DAY:int:80}
days_ahead_to_propose_markets: ${DAYS_AHEAD_TO_PROPOSE_MARKETS:int:5}
market_approval_server_url: ${MARKET_APPROVAL_SERVER_URL:str:http://127.0.0.1:5000}
market_approval_server_api_key: ${MARKET_APPROVAL_SERVER_API_KEY:str:api_key}
newsapi_endpoint: ${NEWSAPI_ENDPOINT:str:https://newsapi.org/v2/everything}
newsapi_endpoint: ${NEWSAPI_ENDPOINT:str:https://newsapi.org/v2/top-headlines}
newsapi_api_key: ${NEWSAPI_API_KEY:str:f1ea36ae6175474bbcbae11eca64d0b9}
topics: ${TOPICS:list:["business","cryptocurrency","politics","science","technology"]}
newsapi_categories: ${NEWSAPI_CATEGORIES:list:["business","entertainment","general","health","science","sports","technology"]}
market_fee: ${MARKET_FEE:float:2.0}
initial_funds: ${INITIAL_FUNDS:float:1.0}
market_timeout: ${MARKET_TIMEOUT:int:1}
Expand Down
25 changes: 11 additions & 14 deletions packages/valory/skills/market_creation_manager_abci/behaviours.py
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ def async_act(self) -> Generator:
with self.context.benchmark_tool.measure(self.behaviour_id).local():
sender = self.context.agent_address
if self.synchronized_data.markets_created < cast(
int, self.params.num_markets
int, self.params.num_proposed_markets_per_day
):
gathered_data = yield from self._gather_data()
else:
Expand All @@ -812,17 +812,10 @@ def async_act(self) -> Generator:
def _gather_data(self) -> Generator[None, None, str]:
"""Auxiliary method to collect data from endpoint."""
headers = {"X-Api-Key": self.params.newsapi_api_key}
today = datetime.date.today()
from_date = today - datetime.timedelta(days=7)
to_date = today
topics_string = " OR ".join(self.params.topics)

parameters = {
"q": topics_string,
"language": "en",
"sortBy": "popularity",
"from": from_date.strftime("%y-%m-%d"),
"to": to_date.strftime("%y-%m-%d"),
"sources": "bbc-news,bbc-sports,abc-news,cnn,the-guardian,reuters",
"pageSize": 100,
}
response = yield from self.get_http_response(
method="GET",
Expand Down Expand Up @@ -894,6 +887,8 @@ def _sender_act(self) -> Generator:

with self.context.benchmark_tool.measure(self.behaviour_id).local():
payload_data = yield from self._get_llm_response()


if payload_data is None:
return

Expand Down Expand Up @@ -925,7 +920,7 @@ def _get_llm_response(self) -> Generator[None, None, Optional[dict]]:
input_news += f"- ({date}) {title}\n {content}\n\n"

event_day = self._get_event_day()
topics = ", ".join(self.params.topics)
topics = ", ".join(self.params.newsapi_categories)
prompt_template = self.params.market_identification_prompt
prompt_values = {
"input_news": input_news,
Expand Down Expand Up @@ -955,6 +950,8 @@ def _get_llm_response(self) -> Generator[None, None, Optional[dict]]:
self.context.logger.info(f"Got LLM response: {result}")
data = json.loads(result)
valid_responses = []

# TODO - Clarify if needed.
# Opening date for realitio oracle contract and closing date
# for answering question on omen market
minimum_opening_date = datetime.datetime.fromtimestamp(
Expand All @@ -970,6 +967,7 @@ def _get_llm_response(self) -> Generator[None, None, Optional[dict]]:
"Cannot parse datestring " + q["resolution_date"]
)
continue
# TODO - Clarify if needed.
if resolution_date < minimum_opening_date:
self.context.logger.error(
"Invalid resolution date " + q["resolution_date"]
Expand All @@ -989,9 +987,8 @@ def _get_llm_response(self) -> Generator[None, None, Optional[dict]]:
f"Error converting question object {q} with error {e}"
)
continue
if len(valid_responses) == 0:
return None
return valid_responses[0]

return valid_responses

def _get_event_day(self) -> str:
# Get the current date
Expand Down
9 changes: 5 additions & 4 deletions packages/valory/skills/market_creation_manager_abci/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
DEFAULT_INITIAL_FUNDS = 1.0
DEFAULT_MARKET_TIMEOUT = 1 # days
DEFAULT_MINIMUM_MARKET_TIME = 7 # days
DEFAULT_MAX_ALLOWED_MARKETS = 1

DEFAULT_NUM_PROPOSED_MARKETS_PER_DAY = 80
DEFAULT_DAYS_AHEAD_TO_PROPOSE_MARKETS = 5

class SharedState(BaseSharedState):
"""Keep the current shared state of the skill."""
Expand Down Expand Up @@ -70,8 +70,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
self.market_identification_prompt = self._ensure(
key="market_identification_prompt", kwargs=kwargs, type_=str
)
self.topics = self._ensure(key="topics", kwargs=kwargs, type_=List[str])
self.num_markets = kwargs.get("num_markets", DEFAULT_MAX_ALLOWED_MARKETS)
self.newsapi_categories = self._ensure(key="newsapi_categories", kwargs=kwargs, type_=List[str])
self.num_proposed_markets_per_day = kwargs.get("num_proposed_markets_per_day", DEFAULT_NUM_PROPOSED_MARKETS_PER_DAY)
self.days_ahead_to_propose_markets = kwargs.get("days_ahead_to_propose_markets", DEFAULT_DAYS_AHEAD_TO_PROPOSE_MARKETS)
self.realitio_contract = self._ensure(
key="realitio_contract",
kwargs=kwargs,
Expand Down
11 changes: 7 additions & 4 deletions packages/valory/skills/market_creation_manager_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,19 @@ models:
params:
args:
market_identification_prompt: market_identification_prompt
num_markets: 1
num_proposed_markets_per_day: 80
days_ahead_to_propose_markets: 5
market_approval_server_url: http://127.0.0.1:5000
market_approval_server_api_key: api_key
newsapi_endpoint: https://newsapi.org/v2/top-headlines
newsapi_api_key: f1ea36ae6175474bbcbae11eca64d0b9
topics:
newsapi_categories:
- business
- cryptocurrency
- politics
- entertainment
- general
- health
- science
- sports
- technology
market_fee: 2.0
initial_funds: 1.0
Expand Down
11 changes: 7 additions & 4 deletions packages/valory/skills/market_maker_abci/skill.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,19 @@ models:
params:
args:
market_identification_prompt: market_identification_prompt
num_markets: 1
num_proposed_markets_per_day: 80
days_ahead_to_propose_markets: 5
market_approval_server_url: http://127.0.0.1:5000
market_approval_server_api_key: api_key
newsapi_endpoint: https://newsapi.org/v2/top-headlines
newsapi_api_key: f1ea36ae6175474bbcbae11eca64d0b9
topics:
newsapi_categories:
- business
- cryptocurrency
- politics
- entertainment
- general
- health
- science
- sports
- technology
market_fee: 2.0
initial_funds: 1.0
Expand Down
58 changes: 58 additions & 0 deletions scripts/generate_csv_for_day.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import argparse
import requests
import json
import csv
from datetime import datetime
from typing import Dict, Optional

def get_markets(url: str, day: Optional[str] = None) -> Dict[str, Dict]:
# Send a GET request to the specified URL
response = requests.get(url)

if response.status_code == 200:
data = response.json()

# If day is specified, filter markets by resolution_time
if day:
timestamp_start = datetime.strptime(day, '%d-%m-%Y').timestamp()
timestamp_end = timestamp_start + 24 * 60 * 60 # 24 hours
filtered_markets = {
key: market for key, market in data["proposed_markets"].items()
if timestamp_start <= market["resolution_time"] < timestamp_end
}
else:
filtered_markets = data["proposed_markets"]

return filtered_markets
else:
raise Exception(f"Failed to fetch data from {url} (HTTP {response.status_code})")

def export_to_csv(markets: Dict[str, Dict]) -> None:
# Generate the CSV filename
current_datetime = datetime.now().strftime('%d-%m-%Y_%H-%M')
csv_filename = f"output_{current_datetime}.csv"

with open(csv_filename, mode='w', newline='') as csv_file:
fieldnames = ["id", "language", "question", "resolution_time", "topic"]
writer = csv.DictWriter(csv_file, fieldnames=fieldnames)

for market in markets.values():
writer.writerow({
"id": market["id"],
"language": market["language"],
"question": market["question"],
"resolution_time": market["resolution_time"],
"topic": market["topic"]
})

print(f"Data exported to {csv_filename}")

if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Fetch and process JSON data from a URL.")
parser.add_argument("url", help="The URL to fetch JSON data from.")
parser.add_argument("--day", help="Filter markets by UTC timestamp in the format dd-mm-aaaa.")

args = parser.parse_args()

markets = get_markets(args.url, args.day)
export_to_csv(markets)
2 changes: 1 addition & 1 deletion scripts/market_creator_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def run( # pylint: disable=too-many-locals
input_news += f"- ({date}) {title}\n {content}\n\n"

market_creation_prompt = MARKET_CREATION_PROMPT.format(
input_news=input_news, from_date=from_date, to_date=to_date, topics=TOPICS
input_news=input_news, topics=TOPICS
)

print(market_creation_prompt)
Expand Down

0 comments on commit 378ef2b

Please sign in to comment.