Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions app/services/ai/cloudflare_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

50K credits available for inference at the edge.

Model Selection Strategy (consistent with workers/src/config/models.ts):
Model Selection Strategy (aligned with the Workers model tier selection in workers/src/lib/workers-ai.ts):
- SPEED: llama-3.2-3b-instruct (ultra-fast, <100ms, simple queries)
- BALANCED: llama-3.3-70b-instruct-fp8-fast (best quality/speed balance)
- QUALITY: gpt-oss-120b (highest quality, production use)
Expand Down Expand Up @@ -110,7 +110,8 @@ class ModelTier(str, Enum):
"""
Model tiers for automatic selection based on query complexity.

Consistent with workers/src/config/models.ts TIER_MODELS mapping.
Keep in sync with Workers model tier selection logic in
workers/src/lib/workers-ai.ts.
"""
SPEED = "speed" # Ultra-fast, simple queries (<100ms)
BALANCED = "balanced" # Best quality/speed balance (default)
Expand All @@ -119,7 +120,7 @@ class ModelTier(str, Enum):


# Tier to model mapping
# Consistent with workers/src/config/models.ts TIER_MODELS
# consistent with Workers model tier selection logic in workers/src/lib/workers-ai.ts
TIER_MODELS = {
ModelTier.SPEED: CFModel.LLAMA_3_2_3B, # Ultra-fast for simple tasks
ModelTier.BALANCED: CFModel.LLAMA_3_3_70B_FAST, # Best quality/speed balance
Expand Down
30 changes: 16 additions & 14 deletions app/services/search/multi_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self):
"""Initialize multi-provider search service."""
self.client = httpx.AsyncClient(timeout=30)
self.providers = self._configure_providers()
self.provider_stats = {provider: {"requests": 0, "errors": 0, "avg_latency": 0.0}
self.provider_stats = {provider: {"requests": 0, "errors": 0,"total_latency":0.0, "avg_latency": 0.0}
for provider in SearchProvider}

def _configure_providers(self) -> Dict[SearchProvider, SearchProviderConfig]:
Expand Down Expand Up @@ -128,19 +128,21 @@ async def search(self, options: SearchOptions) -> List[SearchResult]:
start_time = asyncio.get_event_loop().time()
results = await self._search_with_provider(provider_type, options)

if results:
# Update stats
latency = asyncio.get_event_loop().time() - start_time
self.provider_stats[provider_type]["requests"] += 1
self.provider_stats[provider_type]["avg_latency"] = (
(self.provider_stats[provider_type]["avg_latency"] + latency) / 2
)

logger.info("search_successful",
provider=config.name,
results=len(results),
latency=latency)
return results
if results:
# Update stats
latency = asyncio.get_event_loop().time() - start_time
stats = self.provider_stats[provider_type]
stats["requests"] += 1
stats["total_latency"] += latency
stats["avg_latency"] = (
stats["total_latency"] / stats["requests"]
)

logger.info("search_successful",
provider=config.name,
results=len(results),
latency=latency)
return results

except Exception as e:
self.provider_stats[provider_type]["errors"] += 1
Expand Down