File size: 15,016 Bytes
cd451ea 3adb1b6 cd451ea 3adb1b6 cd451ea 3adb1b6 cd451ea 3adb1b6 cd451ea d1bdad3 cd451ea bb6067e cd451ea 2f097fe cd451ea 2f097fe cd451ea 2f097fe cd451ea 2f097fe cd451ea 2f097fe cd451ea 2f097fe cd451ea 3adb1b6 cd451ea 3adb1b6 d363a89 3adb1b6 d363a89 3adb1b6 cd451ea 2f097fe 75bb2cf 2f097fe |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 |
# -*- coding: utf-8 -*-
# ------------------------------------------------------------------------------
#
# Copyright 2023 Valory AG
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ------------------------------------------------------------------------------
import json
import ijson
from typing import (
Optional,
List,
Dict,
Union,
Any,
)
import pandas as pd
import requests
from datetime import datetime
from gnosis_timestamps import transform_timestamp_to_datetime
from requests.adapters import HTTPAdapter
from tqdm import tqdm
from urllib3 import Retry
from markets import add_market_creator
from concurrent.futures import ThreadPoolExecutor, as_completed
from web3_utils import (
N_IPFS_RETRIES,
)
from utils import (
clean,
BLOCK_FIELD,
limit_text,
ROOT_DIR,
TMP_DIR,
JSON_DATA_DIR,
MechEvent,
MechEventName,
MechRequest,
MechResponse,
EVENT_TO_MECH_STRUCT,
REQUEST_ID,
HTTP,
HTTPS,
get_result_values,
get_vote,
get_win_probability,
get_prediction_values,
save_json_file,
)
CONTRACTS_PATH = "contracts"
MECH_TO_INFO = {
# this block number is when the creator had its first tx ever, and after this mech's creation
"0xff82123dfb52ab75c417195c5fdb87630145ae81": ("old_mech_abi.json", 28911547),
# this block number is when this mech was created
"0x77af31de935740567cf4ff1986d04b2c964a786a": ("new_mech_abi.json", 30776879),
}
# optionally set the latest block to stop searching for the delivered events
EVENT_ARGUMENTS = "args"
DATA = "data"
IPFS_LINKS_SERIES_NAME = "ipfs_links"
BACKOFF_FACTOR = 1
STATUS_FORCELIST = [404, 500, 502, 503, 504]
DEFAULT_FILENAME = "tools.parquet"
ABI_ERROR = "The event signature did not match the provided ABI"
# HTTP_TIMEOUT = 10
# Increasing when ipfs is slow
HTTP_TIMEOUT = 15
IRRELEVANT_TOOLS = [
"openai-text-davinci-002",
"openai-text-davinci-003",
"openai-gpt-3.5-turbo",
"openai-gpt-4",
"stabilityai-stable-diffusion-v1-5",
"stabilityai-stable-diffusion-xl-beta-v2-2-2",
"stabilityai-stable-diffusion-512-v2-1",
"stabilityai-stable-diffusion-768-v2-1",
"deepmind-optimization-strong",
"deepmind-optimization",
"prediction-url-cot-claude",
"prediction-url-cot",
]
# this is how frequently we will keep a snapshot of the progress so far in terms of blocks' batches
# for example, the value 1 means that for every `BLOCKS_CHUNK_SIZE` blocks that we search,
# we also store the snapshot
SNAPSHOT_RATE = 10
NUM_WORKERS = 30
GET_CONTENTS_BATCH_SIZE = 1000
class TimestampedRetry(Retry):
def increment(self, *args, **kwargs):
print(f"Retry attempt at {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
return super().increment(*args, **kwargs)
def create_session() -> requests.Session:
"""Create a session with a retry strategy."""
session = requests.Session()
retry_strategy = TimestampedRetry(
total=N_IPFS_RETRIES,
backoff_factor=BACKOFF_FACTOR,
status_forcelist=STATUS_FORCELIST,
)
adapter = HTTPAdapter(max_retries=retry_strategy)
for protocol in (HTTP, HTTPS):
session.mount(protocol, adapter)
return session
def request(
session: requests.Session, url: str, timeout: int = HTTP_TIMEOUT
) -> Optional[requests.Response]:
"""Perform a request with a session."""
try:
response = session.get(url, timeout=timeout)
response.raise_for_status()
except requests.exceptions.HTTPError as exc:
tqdm.write(f"HTTP error occurred: {exc}.")
except Exception as exc:
tqdm.write(f"Unexpected error occurred: {exc}.")
else:
return response
return None
def parse_ipfs_response(
session: requests.Session,
url: str,
event: MechEvent,
event_name: MechEventName,
response: requests.Response,
) -> Optional[Dict[str, str]]:
"""Parse a response from IPFS."""
try:
return response.json()
except requests.exceptions.JSONDecodeError:
# this is a workaround because the `metadata.json` file was introduced and removed multiple times
if event_name == MechEvent.REQUEST and url != event.ipfs_request_link:
url = event.ipfs_request_link
response = request(session, url)
if response is None:
tqdm.write(f"Skipping {event=}.")
return None
try:
return response.json()
except requests.exceptions.JSONDecodeError:
pass
tqdm.write(f"Failed to parse response into json for {url=}.")
return None
def parse_ipfs_tools_content(
raw_content: Dict[str, str], event: MechEvent, event_name: MechEventName
) -> Optional[Union[MechRequest, MechResponse]]:
"""Parse tools content from IPFS."""
struct = EVENT_TO_MECH_STRUCT.get(event_name)
raw_content[REQUEST_ID] = str(event.requestId)
raw_content[BLOCK_FIELD] = str(event.for_block)
raw_content["sender"] = str(event.sender)
try:
mech_response = struct(**raw_content)
except (ValueError, TypeError, KeyError):
tqdm.write(f"Could not parse {limit_text(str(raw_content))}")
return None
if event_name == MechEventName.REQUEST and mech_response.tool in IRRELEVANT_TOOLS:
return None
return mech_response
def parse_json_events(json_events: dict, keys_to_traverse: List[int]) -> pd.DataFrame:
"""Function to parse the mech info in a json format"""
all_records = []
for key in keys_to_traverse:
try:
json_input = json_events[key]
output = {}
output["request_id"] = json_input["requestId"]
output["request_block"] = json_input["blockNumber"]
output["request_time"] = transform_timestamp_to_datetime(
int(json_input["blockTimestamp"])
)
output["tx_hash"] = json_input["transactionHash"]
output["prompt_request"] = json_input["ipfsContents"].get("prompt", None)
output["tool"] = json_input["ipfsContents"].get("tool", None)
output["nonce"] = json_input["ipfsContents"].get("nonce", None)
output["trader_address"] = json_input.get("sender", None)
output["deliver_block"] = json_input["deliver"].get("blockNumber", None)
error_message = json_input.get("error_message", None)
error_value = -1
if error_message is None:
error_value, error_message, prediction_params = get_result_values(
json_input["deliver"]["ipfsContents"]["result"]
)
# error_message_value = json_input.get("error_message", error_message)
output["error"] = error_value
output["error_message"] = error_message
output["prompt_response"] = json_input["deliver"]["ipfsContents"].get(
"prompt", None
)
output["mech_address"] = json_input["deliver"].get("mech", None)
p_yes_value, p_no_value, confidence_value, info_utility_value = (
None,
None,
None,
None,
)
if error_value == 0:
p_yes_value, p_no_value, confidence_value, info_utility_value = (
get_prediction_values(prediction_params)
)
output["p_yes"] = p_yes_value
output["p_no"] = p_no_value
output["confidence"] = confidence_value
output["info_utility"] = info_utility_value
output["vote"] = get_vote(p_yes_value, p_no_value)
if error_value == 0:
output["win_probability"] = get_win_probability(p_yes_value, p_no_value)
else:
output["win_probability"] = None
all_records.append(output)
except Exception as e:
print(e)
print(f"Error parsing the key ={key}. Noted as mech request error")
output["error"] = -1
output["error_message"] = "Mech request error"
output["p_yes"] = None
output["p_no"] = None
output["confidence"] = None
output["info_utility"] = None
output["vote"] = None
output["win_probability"] = None
all_records.append(output)
return pd.DataFrame.from_dict(all_records, orient="columns")
def transform_request(contents: pd.DataFrame) -> pd.DataFrame:
"""Transform the requests dataframe."""
return clean(contents)
def transform_deliver(contents: pd.DataFrame) -> pd.DataFrame:
"""Transform the delivers dataframe."""
unpacked_result = pd.json_normalize(contents.result)
# # drop result column if it exists
if "result" in unpacked_result.columns:
unpacked_result.drop(columns=["result"], inplace=True)
# drop prompt column if it exists
if "prompt" in unpacked_result.columns:
unpacked_result.drop(columns=["prompt"], inplace=True)
# rename prompt column to prompt_deliver
unpacked_result.rename(columns={"prompt": "prompt_deliver"}, inplace=True)
contents = pd.concat((contents, unpacked_result), axis=1)
if "result" in contents.columns:
contents.drop(columns=["result"], inplace=True)
if "prompt" in contents.columns:
contents.drop(columns=["prompt"], inplace=True)
return clean(contents)
def parse_store_json_events_parallel(json_events: Dict[str, Any], output_filename: str):
total_nr_events = len(json_events)
ids_to_traverse = list(json_events.keys())
print(f"Parsing {total_nr_events} events")
contents = []
with ThreadPoolExecutor(max_workers=NUM_WORKERS) as executor:
futures = []
for i in range(0, total_nr_events, GET_CONTENTS_BATCH_SIZE):
futures.append(
executor.submit(
parse_json_events,
json_events,
ids_to_traverse[i : i + GET_CONTENTS_BATCH_SIZE],
)
)
for future in tqdm(
as_completed(futures),
total=len(futures),
desc=f"Fetching json contents",
):
current_mech_contents = future.result()
contents.append(current_mech_contents)
tools = pd.concat(contents, ignore_index=True)
print(f"Adding market creators info. Length of the tools file = {len(tools)}")
tools = add_market_creator(tools)
print(
f"Length of the tools dataframe after adding market creators info= {len(tools)}"
)
print(tools.info())
try:
if "result" in tools.columns:
tools = tools.drop(columns=["result"])
tools.to_parquet(ROOT_DIR / output_filename, index=False)
except Exception as e:
print(f"Failed to write tools data: {e}")
return tools
def filter_json_events(
json_events: dict,
keys_to_traverse: List[str],
input_file: str,
min_block_number: int,
) -> dict:
"""Function to parse the mech info in a json format"""
output = {}
for key in keys_to_traverse:
# print(f"key={key}")
try:
json_input = json_events[key]
# print(f"json_input={json_input}")
if input_file == "mech_delivers.json":
# the json input is a list
new_list = []
for item in json_input:
if int(item["blockNumber"]) >= min_block_number:
new_list.append(item)
if len(new_list) > 0:
output[key] = new_list
else:
# the json input is a dictionary
if int(json_input["blockNumber"]) >= int(min_block_number):
output[key] = json_input
except Exception as e:
print(f"Error while filtering the json events {e}")
break
return output
def filter_json_events_parallel(
input_file: str, json_events: Dict[str, Any], min_block_number: int
) -> dict:
total_nr_events = len(json_events)
ids_to_traverse = list(json_events.keys())
print(f"Parsing {total_nr_events} events")
contents = {}
with ThreadPoolExecutor(max_workers=NUM_WORKERS) as executor:
futures = []
for i in range(0, total_nr_events, GET_CONTENTS_BATCH_SIZE):
futures.append(
executor.submit(
filter_json_events,
json_events,
ids_to_traverse[i : i + GET_CONTENTS_BATCH_SIZE],
input_file,
min_block_number,
)
)
for future in tqdm(
as_completed(futures),
total=len(futures),
desc=f"Fetching json contents",
):
filtered_json_entries = future.result()
contents.update(filtered_json_entries)
return contents
def generate_tools_file(input_filename: str, output_filename: str):
"""Function to parse the json mech events and generate the parquet tools file"""
try:
with open(JSON_DATA_DIR / input_filename, "r") as file:
file_contents = json.load(file)
parse_store_json_events_parallel(file_contents, output_filename)
except Exception as e:
print(f"An Exception happened while parsing the json events {e}")
def remove_old_entries(
input_file: str, output_file: str, min_block_number: int = 38228872
):
print(f"Removing older entries than {min_block_number} in {input_file}")
try:
output_content = {}
with open(JSON_DATA_DIR / input_file, "r") as file:
input_contents = json.load(file)
print(f"Initial lenght of items = {len(input_contents)}")
output_content = filter_json_events_parallel(
input_file, input_contents, min_block_number
)
print(f"Final lenght of filtered items = {len(output_content)}")
if len(output_content) > 0:
save_json_file(output_content, filename=output_file)
except Exception as e:
print(f"An Exception happened while filtering the json events {e}")
if __name__ == "__main__":
generate_tools_file(
input_filename="tools_info.json",
output_filename="tools_updated.parquet",
)
# remove_old_entries(
# input_file="tools_info.json", output_file="cleaned_tools_info.json"
# )
|