File tree Expand file tree Collapse file tree 5 files changed +967
-971
lines changed Expand file tree Collapse file tree 5 files changed +967
-971
lines changed Original file line number Diff line number Diff line change
1
+ import fastapi
2
+ from prediction_market_agent_tooling .loggers import logger
1
3
from prediction_market_agent_tooling .markets .omen .omen_subgraph_handler import (
2
4
HexAddress ,
3
5
OmenSubgraphHandler ,
@@ -19,17 +21,28 @@ def market_insights_cached(
19
21
20
22
else :
21
23
new = market_insights (market_id )
22
- cache .save (new )
24
+ if new .has_insights :
25
+ cache .save (new )
23
26
return new
24
27
25
28
26
29
def market_insights (market_id : HexAddress ) -> MarketInsightsResponse :
27
30
"""Returns market insights for a given market on Omen."""
28
- market = OmenSubgraphHandler ().get_omen_market_by_market_id (market_id )
31
+ try :
32
+ market = OmenSubgraphHandler ().get_omen_market_by_market_id (market_id )
33
+ except ValueError :
34
+ raise fastapi .HTTPException (
35
+ status_code = 404 , detail = f"Market with id `{ market_id } ` not found."
36
+ )
37
+ try :
38
+ insights = tavily_insights (market .question_title )
39
+ except Exception as e :
40
+ logger .error (f"Failed to get insights for market `{ market_id } `: { e } " )
41
+ insights = None
29
42
return MarketInsightsResponse .from_tavily_response (
30
43
market_id = market_id ,
31
44
created_at = utcnow (),
32
- tavily_response = tavily_insights ( market . question_title ) ,
45
+ tavily_response = insights ,
33
46
)
34
47
35
48
Original file line number Diff line number Diff line change
1
+ import typing as t
2
+
1
3
import fastapi
2
4
import uvicorn
3
5
from config import Config
9
11
from labs_api .insights import MarketInsightsResponse , market_insights_cached
10
12
from labs_api .insights_cache import MarketInsightsResponseCache
11
13
14
+ HEX_ADDRESS_VALIDATOR = t .Annotated [
15
+ HexAddress ,
16
+ fastapi .Query (
17
+ ...,
18
+ description = "Hex address of the market on Omen." ,
19
+ pattern = "^0x[a-fA-F0-9]{40}$" ,
20
+ ),
21
+ ]
22
+
12
23
13
24
def create_app () -> fastapi .FastAPI :
14
25
app = fastapi .FastAPI ()
@@ -28,7 +39,7 @@ def _ping() -> str:
28
39
return "pong"
29
40
30
41
@app .get ("/market-insights/" )
31
- def _market_insights (market_id : HexAddress ) -> MarketInsightsResponse :
42
+ def _market_insights (market_id : HEX_ADDRESS_VALIDATOR ) -> MarketInsightsResponse :
32
43
"""Returns market insights for a given market on Omen."""
33
44
insights = market_insights_cached (market_id , market_insights_cache )
34
45
logger .info (f"Insights for `{ market_id } `: { insights .model_dump ()} " )
Original file line number Diff line number Diff line change
1
+ import typing as t
1
2
from datetime import datetime
2
3
3
4
from prediction_market_agent_tooling .gtypes import HexAddress
@@ -16,23 +17,31 @@ def from_tavily_result(tavily_result: "TavilyResult") -> "MarketInsightResult":
16
17
class MarketInsightsResponse (BaseModel ):
17
18
market_id : HexAddress
18
19
created_at : datetime
19
- summary : str
20
+ summary : str | None
20
21
results : list [MarketInsightResult ]
21
22
23
+ @property
24
+ def has_insights (self ) -> bool :
25
+ return bool (self .summary or self .results )
26
+
22
27
@staticmethod
23
28
def from_tavily_response (
24
29
market_id : HexAddress ,
25
30
created_at : datetime ,
26
- tavily_response : "TavilyResponse" ,
31
+ tavily_response : t . Union [ "TavilyResponse" , None ] ,
27
32
) -> "MarketInsightsResponse" :
28
33
return MarketInsightsResponse (
29
34
market_id = market_id ,
30
35
created_at = created_at ,
31
- summary = tavily_response .answer ,
32
- results = [
33
- MarketInsightResult .from_tavily_result (result )
34
- for result in tavily_response .results
35
- ],
36
+ summary = tavily_response .answer if tavily_response else None ,
37
+ results = (
38
+ [
39
+ MarketInsightResult .from_tavily_result (result )
40
+ for result in tavily_response .results
41
+ ]
42
+ if tavily_response
43
+ else []
44
+ ),
36
45
)
37
46
38
47
You can’t perform that action at this time.
0 commit comments