Skip to content

Commit e449756

Browse files
authored
Stability (#5)
1 parent a262c0c commit e449756

File tree

5 files changed

+967
-971
lines changed

5 files changed

+967
-971
lines changed

labs_api/insights.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import fastapi
2+
from prediction_market_agent_tooling.loggers import logger
13
from prediction_market_agent_tooling.markets.omen.omen_subgraph_handler import (
24
HexAddress,
35
OmenSubgraphHandler,
@@ -19,17 +21,28 @@ def market_insights_cached(
1921

2022
else:
2123
new = market_insights(market_id)
22-
cache.save(new)
24+
if new.has_insights:
25+
cache.save(new)
2326
return new
2427

2528

2629
def market_insights(market_id: HexAddress) -> MarketInsightsResponse:
2730
"""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
2942
return MarketInsightsResponse.from_tavily_response(
3043
market_id=market_id,
3144
created_at=utcnow(),
32-
tavily_response=tavily_insights(market.question_title),
45+
tavily_response=insights,
3346
)
3447

3548

labs_api/main.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import typing as t
2+
13
import fastapi
24
import uvicorn
35
from config import Config
@@ -9,6 +11,15 @@
911
from labs_api.insights import MarketInsightsResponse, market_insights_cached
1012
from labs_api.insights_cache import MarketInsightsResponseCache
1113

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+
1223

1324
def create_app() -> fastapi.FastAPI:
1425
app = fastapi.FastAPI()
@@ -28,7 +39,7 @@ def _ping() -> str:
2839
return "pong"
2940

3041
@app.get("/market-insights/")
31-
def _market_insights(market_id: HexAddress) -> MarketInsightsResponse:
42+
def _market_insights(market_id: HEX_ADDRESS_VALIDATOR) -> MarketInsightsResponse:
3243
"""Returns market insights for a given market on Omen."""
3344
insights = market_insights_cached(market_id, market_insights_cache)
3445
logger.info(f"Insights for `{market_id}`: {insights.model_dump()}")

labs_api/models.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import typing as t
12
from datetime import datetime
23

34
from prediction_market_agent_tooling.gtypes import HexAddress
@@ -16,23 +17,31 @@ def from_tavily_result(tavily_result: "TavilyResult") -> "MarketInsightResult":
1617
class MarketInsightsResponse(BaseModel):
1718
market_id: HexAddress
1819
created_at: datetime
19-
summary: str
20+
summary: str | None
2021
results: list[MarketInsightResult]
2122

23+
@property
24+
def has_insights(self) -> bool:
25+
return bool(self.summary or self.results)
26+
2227
@staticmethod
2328
def from_tavily_response(
2429
market_id: HexAddress,
2530
created_at: datetime,
26-
tavily_response: "TavilyResponse",
31+
tavily_response: t.Union["TavilyResponse", None],
2732
) -> "MarketInsightsResponse":
2833
return MarketInsightsResponse(
2934
market_id=market_id,
3035
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+
),
3645
)
3746

3847

0 commit comments

Comments
 (0)