`scraped_items.py`: In-memory vector similarity instead of Azure AI Search
- Linguagem predominante
- TypeScript
- Estrelas
- 0
- Forks
- 0
- Merge médio
- 16min
- PRs com merge (30d)
- 1
Descrição
**File:** `PluckIt.Processor/agents/tools/scraped_items.py:87`
### Problem
`search_scraped_items` loads 200 candidate documents from Cosmos — including their full embedding vectors — and computes cosine similarity in Python:
```python
_CANDIDATE_LIMIT = 200 # how many docs to load for in-memory ranking
async for doc in container.query_items(
query="... ORDER BY c.scoreSignal DESC OFFSET 0 LIMIT 200"
):
candidates.append(doc)
for doc in candidates:
emb = doc.get("embedding") or []
sim = _cosine_similarity(query_embedding, emb)
```
This approach has two compounding problems:
1. **200 Cosmos reads per search** — each document includes its full embedding vector (~6KB for `text-embedding-3-small`). That's ~1.2 MB of data transferred and ~2,000 RU per tool call.
2. **Candidate selection is by `scoreSignal`, not semantic relevance** — the 200 items sent for ranking are chosen by a scraper quality score, not by closeness to the query. A semantically perfect match ranked 201st is invisible.
### Impact
| Users | Stylist sessions/month | Scraped-item searches | Monthly Cosmos RU cost |
|-------|----------------------|----------------------|----------------------|
| 10 | 300 | ~300 | ~600K RU (negligible) |
| 100 | 3,000 | ~3,000 | ~6M RU — requires autoscale |
| 1,000 | 30,000 | ~30,000 | ~60M RU — ~$90/month in Cosmos alone |
At 100+ users this becomes the single largest Cosmos cost driver.
### Proposed Fix
Move similarity search to **Azure AI Search** with a vector index on the `embedding` field. The query becomes a single ANN (approximate nearest-neighbour) search call returning the top-K results directly, with no client-side scoring loop and no full-vector document reads from Cosmos.
This is already the infrastructure direction implied by the Azure stack — Azure AI Search is already provisioned in the Terraform config.
### Functionality Impact
Results improve: ANN search considers the full corpus rather than the top-200 by score signal, so relevant items that scored low on the scraper heuristic are no longer invisible. Latency also drops — one search API call vs 200 Cosmos reads.
Guia de contribuição
Direção de pesquisa
The issue is in `PluckIt.Processor/agents/tools/scraped_items.py` at line 87. Examine the `search_scraped_items` function and its current Cosmos DB query. Research the existing Azure AI Search setup in the Terraform config. The goal is to replace the in-memory similarity loop with an ANN search call to Azure AI Search, moving the vector index there. 'Done' means the function uses the search service, returns top-K results, and the Cosmos reads are eliminated.
Escrita pelo modelo de indexação a partir do texto da issue.
Avaliação
- Stack de tecnologia
- azure, python
- Domínio
- backend, databases, search
- Tipo de issue
- Refatoração
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Status de atividade
- Estagnada
- Clareza
- Claramente especificada
- Facilidade para iniciantes
- 45/100