prediction-market-agent-tooling 0.64.7__py3-none-any.whl → 0.64.9__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -69,13 +69,16 @@ def _handle_exception(
69
69
  sys.__excepthook__(exc_type, exc_value, exc_traceback)
70
70
  return
71
71
 
72
- info: BaseException | None = exc_value
72
+ exp_details: BaseException | None = exc_value
73
73
  if isinstance(exc_value, RetryError):
74
74
  # In case of RetryError from tenacity, add last attempt's exp to the log, otherwise we won't see the exception's message in the logs.
75
- info = exc_value.last_attempt.exception()
75
+ exp_details = exc_value.last_attempt.exception()
76
76
 
77
77
  logger.error(
78
- f"Uncaught exception: {info}",
78
+ # Do not use f-string here, loguru internally calls `message.format(*args, **kwargs)` and in case you put {} into the message (for example, dict-formatted exception from evm),
79
+ # it would throw an error.
80
+ "Uncaught exception: {exp_details}",
81
+ exp_details=exp_details,
79
82
  exc_info=(exc_type, exc_value, exc_traceback),
80
83
  )
81
84
 
@@ -0,0 +1,117 @@
1
+ import math
2
+ from itertools import product
3
+ from typing import Any, Tuple
4
+
5
+ from pydantic import BaseModel
6
+
7
+ from prediction_market_agent_tooling.loggers import logger
8
+
9
+
10
+ class LogprobKey(BaseModel):
11
+ name: str
12
+ key_type: type
13
+ valid_values: set[Any] | None
14
+
15
+
16
+ class LogprobsParser:
17
+ def _get_logprobs_key_index(
18
+ self, logprobs: list[dict[str, Any]], key: LogprobKey
19
+ ) -> int:
20
+ key_candidate = ""
21
+ for i, token in enumerate(logprobs):
22
+ if token["token"] in key.name:
23
+ key_candidate = key_candidate + token["token"]
24
+ else:
25
+ key_candidate = ""
26
+ if key_candidate == key.name:
27
+ return i
28
+
29
+ return -1
30
+
31
+ def _get_logprobs_indexes_for_result(
32
+ self, logprobs: list[dict[str, Any]], key_index: int
33
+ ) -> Tuple[int, int]:
34
+ result_start_index = next(
35
+ (
36
+ i
37
+ for i in range(key_index + 1, len(logprobs))
38
+ if logprobs[i]["token"] in {":", ",", " ", ' "', '"', "\t", "\u00A0"}
39
+ ),
40
+ -1,
41
+ )
42
+ result_end_index = next(
43
+ (
44
+ i
45
+ for i in range(result_start_index, len(logprobs))
46
+ if logprobs[i]["token"] in {",", '"', ",\n", "\",\n'", '",\n'}
47
+ ),
48
+ -1,
49
+ )
50
+ return result_start_index + 1, result_end_index
51
+
52
+ def _is_correct_type(self, token: str, key_type: type) -> bool:
53
+ try:
54
+ key_type(token)
55
+ return True
56
+ except ValueError:
57
+ return False
58
+
59
+ def _parse_valid_tokens_with__agg_probs(
60
+ self, logprobs_list: list[tuple[dict[str, Any]]], key: LogprobKey
61
+ ) -> list[dict[str, Any]]:
62
+ results: list[dict[str, Any]] = [
63
+ {
64
+ "token": "".join(str(logprob["token"]) for logprob in logprobs),
65
+ "logprob": sum(float(logprob["logprob"]) for logprob in logprobs),
66
+ "prob": math.exp(
67
+ sum(float(logprob["logprob"]) for logprob in logprobs)
68
+ ),
69
+ }
70
+ for logprobs in logprobs_list
71
+ ]
72
+
73
+ results_filtered: list[dict[str, Any]] = [
74
+ result
75
+ for result in results
76
+ if self._is_correct_type(result["token"], key.key_type)
77
+ and (key.valid_values is None or result["token"] in key.valid_values)
78
+ ]
79
+
80
+ return sorted(results_filtered, key=lambda x: x["logprob"], reverse=True)[
81
+ : len(logprobs_list[0])
82
+ ]
83
+
84
+ def parse_logprobs(
85
+ self, logprobs: list[dict[str, Any]], keys: list[LogprobKey]
86
+ ) -> list[dict[str, Any]]:
87
+ results_for_keys = []
88
+
89
+ for key in keys:
90
+ key_index = self._get_logprobs_key_index(logprobs, key)
91
+ if key_index < 0:
92
+ logger.warning(f"Key {key.name} not found in logprobs")
93
+ continue
94
+
95
+ (
96
+ result_start_index,
97
+ result_end_index,
98
+ ) = self._get_logprobs_indexes_for_result(logprobs, key_index)
99
+ if result_start_index < 0 or result_end_index < 0:
100
+ logger.warning(f"Error in parsing result for {key.name} in logprobs")
101
+ continue
102
+
103
+ valid_logprobs = [
104
+ logprobs[i]["top_logprobs"]
105
+ for i in range(result_start_index, result_end_index)
106
+ ]
107
+
108
+ results_for_keys.append(
109
+ {
110
+ "key": key.name,
111
+ "logprobs": self._parse_valid_tokens_with__agg_probs(
112
+ list(product(*valid_logprobs)), key
113
+ ),
114
+ }
115
+ )
116
+
117
+ return results_for_keys
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: prediction-market-agent-tooling
3
- Version: 0.64.7
3
+ Version: 0.64.9
4
4
  Summary: Tools to benchmark, deploy and monitor prediction market agents.
5
5
  Author: Gnosis
6
6
  Requires-Python: >=3.10,<3.13
@@ -35,7 +35,8 @@ prediction_market_agent_tooling/gtypes.py,sha256=bUIZfZIGvIi3aiZNu5rVE9kRevw8sfM
35
35
  prediction_market_agent_tooling/jobs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
36
36
  prediction_market_agent_tooling/jobs/jobs_models.py,sha256=8vYafsK1cqMWQtjBoq9rruroF84xAVD00vBTMWH6QMg,2166
37
37
  prediction_market_agent_tooling/jobs/omen/omen_jobs.py,sha256=Pf6QxPXGyie-2l_wZUjaGPTjZTlpv50_JhP40mULBaU,5048
38
- prediction_market_agent_tooling/loggers.py,sha256=_4ccdpSU3Kxmf4wYDZR5fPHXKhPxpUAjj_AISWKFJLM,4915
38
+ prediction_market_agent_tooling/loggers.py,sha256=hF_n-E5iMSqh3dY5G6LkQRHyReMYGPNTLu82dDFh1PU,5187
39
+ prediction_market_agent_tooling/logprobs_parser.py,sha256=sXIwkA5O_fpBP3Civ891rTa0yRcbs3qNcNsY5MJpL68,3734
39
40
  prediction_market_agent_tooling/markets/agent_market.py,sha256=1NomilM0GCXcRq_1N_cr2AbSK5ONTowFeRbrhc7V5zE,14929
40
41
  prediction_market_agent_tooling/markets/base_subgraph_handler.py,sha256=7RaYO_4qAmQ6ZGM8oPK2-CkiJfKmV9MxM-rJlduaecU,1971
41
42
  prediction_market_agent_tooling/markets/blockchain_utils.py,sha256=qm21scopQ6dfewkoqQF6lWLDGg2BblsKUdC9aG93Hmc,2249
@@ -124,8 +125,8 @@ prediction_market_agent_tooling/tools/tokens/usd.py,sha256=yuW8iPPtcpP4eLH2nORMD
124
125
  prediction_market_agent_tooling/tools/transaction_cache.py,sha256=K5YKNL2_tR10Iw2TD9fuP-CTGpBbZtNdgbd0B_R7pjg,1814
125
126
  prediction_market_agent_tooling/tools/utils.py,sha256=AC2a68jwASMWuQi-w8twl8b_M52YwrEJ81abmuEaqMY,6661
126
127
  prediction_market_agent_tooling/tools/web3_utils.py,sha256=zRq-eeBGWt8uUGN9G_WfjmJ0eVvO8aWE9S0Pz_Y6AOA,12342
127
- prediction_market_agent_tooling-0.64.7.dist-info/LICENSE,sha256=6or154nLLU6bELzjh0mCreFjt0m2v72zLi3yHE0QbeE,7650
128
- prediction_market_agent_tooling-0.64.7.dist-info/METADATA,sha256=1iMPY2iyUlk1LofPDU32Lr98VeW9XWkcOHaUtTAoMhc,8741
129
- prediction_market_agent_tooling-0.64.7.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
130
- prediction_market_agent_tooling-0.64.7.dist-info/entry_points.txt,sha256=m8PukHbeH5g0IAAmOf_1Ahm-sGAMdhSSRQmwtpmi2s8,81
131
- prediction_market_agent_tooling-0.64.7.dist-info/RECORD,,
128
+ prediction_market_agent_tooling-0.64.9.dist-info/LICENSE,sha256=6or154nLLU6bELzjh0mCreFjt0m2v72zLi3yHE0QbeE,7650
129
+ prediction_market_agent_tooling-0.64.9.dist-info/METADATA,sha256=_1FdDou19Th75LG9Jud3dHCXv8Ca3EaiUNKkSygE1cI,8741
130
+ prediction_market_agent_tooling-0.64.9.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
131
+ prediction_market_agent_tooling-0.64.9.dist-info/entry_points.txt,sha256=m8PukHbeH5g0IAAmOf_1Ahm-sGAMdhSSRQmwtpmi2s8,81
132
+ prediction_market_agent_tooling-0.64.9.dist-info/RECORD,,