Skip to content

Commit

Permalink
Use latest PMAT (mostly caching stuff) (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
kongzii authored Nov 6, 2024
1 parent 6ee0103 commit f53d72f
Show file tree
Hide file tree
Showing 11 changed files with 1,320 additions and 1,523 deletions.
100 changes: 0 additions & 100 deletions labs_api/cache.py

This file was deleted.

40 changes: 11 additions & 29 deletions labs_api/insights/insights.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import timedelta

import fastapi
from langchain.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
Expand All @@ -9,44 +11,22 @@
OmenMarket,
OmenSubgraphHandler,
)
from prediction_market_agent_tooling.tools.caches.db_cache import db_cache
from prediction_market_agent_tooling.tools.langfuse_ import (
get_langfuse_langchain_config,
observe,
)
from prediction_market_agent_tooling.tools.tavily_storage.tavily_models import (
TavilyResponse,
TavilyStorage,
)
from prediction_market_agent_tooling.tools.tavily_storage.tavily_storage import (
TavilyStorage,
tavily_search,
)
from prediction_market_agent_tooling.tools.tavily.tavily_models import TavilyResponse
from prediction_market_agent_tooling.tools.tavily.tavily_search import tavily_search
from prediction_market_agent_tooling.tools.utils import (
LLM_SUPER_LOW_TEMPERATURE,
utcnow,
)

from labs_api.insights.insights_cache import (
MarketInsightsResponse,
MarketInsightsResponseCache,
)


# Don't observe the cached version, as it will always return the same thing that's already been observed.
def market_insights_cached(
market_id: HexAddress, cache: MarketInsightsResponseCache
) -> MarketInsightsResponse:
"""Returns `market_insights`, but cached daily."""
if (cached := cache.find(market_id)) is not None:
return cached

else:
new = market_insights(market_id)
if new.has_insights:
cache.save(new)
return new
from labs_api.insights.insights_models import MarketInsightsResponse


@db_cache(max_age=timedelta(days=3))
@observe()
def market_insights(market_id: HexAddress) -> MarketInsightsResponse:
"""Returns market insights for a given market on Omen."""
Expand All @@ -56,17 +36,19 @@ def market_insights(market_id: HexAddress) -> MarketInsightsResponse:
raise fastapi.HTTPException(
status_code=404, detail=f"Market with id `{market_id}` not found."
)
except Exception as e:
logger.error(f"Failed to fetch market for `{market_id}`: {e}")
raise fastapi.HTTPException(status_code=500)
try:
tavily_response = tavily_search(
market.question_title,
search_depth="basic",
include_answer=True,
max_results=5,
tavily_storage=TavilyStorage("market_insights"),
)
except Exception as e:
logger.error(f"Failed to get tavily_response for market `{market_id}`: {e}")
tavily_response = None
raise fastapi.HTTPException(status_code=500)
try:
summary = (
tavily_response_to_summary(market, tavily_response)
Expand Down
61 changes: 0 additions & 61 deletions labs_api/insights/insights_cache.py

This file was deleted.

40 changes: 40 additions & 0 deletions labs_api/insights/insights_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from prediction_market_agent_tooling.gtypes import HexAddress
from prediction_market_agent_tooling.tools.datetime_utc import DatetimeUTC
from prediction_market_agent_tooling.tools.tavily.tavily_models import (
TavilyResponse,
TavilyResult,
)
from pydantic import BaseModel


class MarketInsightResult(BaseModel):
url: str
title: str

@staticmethod
def from_tavily_result(tavily_result: TavilyResult) -> "MarketInsightResult":
return MarketInsightResult(url=tavily_result.url, title=tavily_result.title)


class MarketInsightsResponse(BaseModel):
market_id: HexAddress
created_at: DatetimeUTC
summary: str | None
results: list[MarketInsightResult]

@staticmethod
def from_tavily_response(
market_id: HexAddress,
created_at: DatetimeUTC,
summary: str | None,
tavily_response: TavilyResponse,
) -> "MarketInsightsResponse":
return MarketInsightsResponse(
market_id=market_id,
created_at=created_at,
summary=summary,
results=[
MarketInsightResult.from_tavily_result(result)
for result in tavily_response.results
],
)
38 changes: 12 additions & 26 deletions labs_api/invalid/invalid.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,15 @@
HexAddress,
OmenSubgraphHandler,
)
from prediction_market_agent_tooling.tools.caches.db_cache import db_cache
from prediction_market_agent_tooling.tools.is_invalid import is_invalid
from prediction_market_agent_tooling.tools.langfuse_ import observe
from prediction_market_agent_tooling.tools.utils import utcnow

from labs_api.invalid.invalid_cache import (
MarketInvalidResponse,
MarketInvalidResponseCache,
)


# Don't observe the cached version, as it will always return the same thing that's already been observed.
def market_invalid_cached(
market_id: HexAddress, cache: MarketInvalidResponseCache
) -> MarketInvalidResponse:
"""Returns `market_invalid`, but cached daily."""
if (cached := cache.find(market_id)) is not None:
return cached

else:
new = market_invalid(market_id)
if new.has_invalid:
cache.save(new)
return new
from labs_api.invalid.invalid_models import MarketInvalidResponse


@db_cache
@observe()
def market_invalid(market_id: HexAddress) -> MarketInvalidResponse:
"""Returns market invalid for a given market on Omen."""
Expand All @@ -39,13 +23,15 @@ def market_invalid(market_id: HexAddress) -> MarketInvalidResponse:
raise fastapi.HTTPException(
status_code=404, detail=f"Market with id `{market_id}` not found."
)
except Exception as e:
logger.error(f"Failed to fetch market for `{market_id}`: {e}")
raise fastapi.HTTPException(status_code=500)
try:
invalid = is_invalid(market.question_title)
return MarketInvalidResponse(
market_id=market_id,
created_at=utcnow(),
invalid=is_invalid(market.question_title),
)
except Exception as e:
logger.error(f"Failed to get is_invalid for market `{market_id}`: {e}")
invalid = None
return MarketInvalidResponse(
market_id=market_id,
created_at=utcnow(),
invalid=invalid,
)
raise fastapi.HTTPException(status_code=500)
20 changes: 0 additions & 20 deletions labs_api/invalid/invalid_cache.py

This file was deleted.

11 changes: 11 additions & 0 deletions labs_api/invalid/invalid_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from prediction_market_agent_tooling.markets.omen.omen_subgraph_handler import (
HexAddress,
)
from prediction_market_agent_tooling.tools.datetime_utc import DatetimeUTC
from pydantic import BaseModel


class MarketInvalidResponse(BaseModel):
market_id: HexAddress
created_at: DatetimeUTC
invalid: bool
Loading

0 comments on commit f53d72f

Please sign in to comment.