File size: 2,015 Bytes
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
64075d7
 
102dd4f
 
 
64075d7
102dd4f
 
 
96f3b64
 
102dd4f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc8cb49
 
 
 
102dd4f
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from typing import Any

from app.database.supabase import SupabaseRepository
from app.services.embedding_service import JinaEmbeddingService
from app.utils.departure import trip_departure_bucket, trip_departure_date


def build_trip_embedding_record(trip: dict[str, Any], embedding_model: str) -> dict[str, Any]:
    driver = _first_or_dict(trip.get("drivers")) or {}
    car = _first_or_dict(trip.get("driver_cars")) or {}
    trip_id = str(trip.get("id") or trip.get("trip_id"))
    departure_date = trip_departure_date(trip)
    departure_time = trip_departure_bucket(trip)
    available = trip.get("available_seats") or "unknown"
    total = trip.get("total_seats") or "unknown"
    chunk_text = (
        f"Trip {trip_id}: {trip.get('departure')} to {trip.get('destination')} "
        f"on {departure_date} during {departure_time}. "
        f"Available seats: {available} of {total}. "
        f"Vehicle: {car.get('car_type')}. Driver: {driver.get('name')}. "
        f"Price: {trip.get('price')}. Status: {trip.get('status')}."
    )
    if trip.get("use_driver_message") and trip.get("driver_message"):
        chunk_text += f" Driver message: {trip['driver_message']}"
    return {
        "trip_id": trip_id,
        "chunk_text": chunk_text,
        "embedding_model": embedding_model,
    }


async def index_trip(
    *,
    repository: SupabaseRepository,
    embeddings: JinaEmbeddingService,
    embedding_model: str,
    trip: dict[str, Any],
) -> None:
    record = build_trip_embedding_record(trip, embedding_model)
    record["embedding"] = (await embeddings.embed_passages([record["chunk_text"]]))[0]
    await repository.upsert_trip_embeddings([record])


async def unindex_trip(*, repository: SupabaseRepository, trip_id: str) -> None:
    await repository.delete_trip_embedding(trip_id)


def _first_or_dict(value: Any) -> dict[str, Any] | None:
    if isinstance(value, list):
        return value[0] if value else None
    if isinstance(value, dict):
        return value
    return None