-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector.py
More file actions
582 lines (512 loc) · 20.2 KB
/
Copy pathvector.py
File metadata and controls
582 lines (512 loc) · 20.2 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
import json
import os
import re
from dataclasses import dataclass
from datetime import date
from hashlib import sha256
from importlib.resources import files
from pathlib import Path
from typing import IO, Any, TypeAlias, cast
import pandas as pd
from langchain_chroma import Chroma
from langchain_core.documents import Document
from langchain_ollama import OllamaEmbeddings
PROJECT_ROOT = Path(__file__).resolve().parent
DEFAULT_DATA_PATH = Path(
str(files("local_ai_agent.data").joinpath("realistic_restaurant_reviews.csv"))
)
DEFAULT_STORAGE_ROOT = Path(
os.getenv(
"LOCAL_AI_STORAGE_ROOT",
str(Path.home() / ".local" / "share" / "local-ai-agent"),
)
)
DEFAULT_DATABASE_PATH = DEFAULT_STORAGE_ROOT / "chroma"
DEFAULT_COLLECTION_NAME = "restaurant_reviews"
DEFAULT_EMBEDDING_MODEL = "mxbai-embed-large"
CANONICAL_COLUMNS = (
"Title",
"Date",
"Rating",
"Sentiment",
"Restaurant",
"Country",
"Review",
)
ROLE_ALIASES: dict[str, tuple[str, ...]] = {
"review": (
"review",
"reviewtext",
"customerreview",
"feedback",
"customerfeedback",
"comment",
"comments",
"body",
"content",
),
"title": ("title", "reviewtitle", "headline", "subject"),
"date": (
"date",
"reviewdate",
"createdat",
"publishedat",
"publishedon",
"timestamp",
"submissiondate",
),
"rating": ("rating", "stars", "star", "score", "reviewscore"),
"sentiment": ("sentiment", "polarity", "sentimentlabel", "label"),
"restaurant": (
"restaurant",
"restaurantname",
"venue",
"business",
"businessname",
"locationname",
),
"country": ("country", "nation", "market", "region"),
}
ROLE_TO_CANONICAL = {
"review": "Review",
"title": "Title",
"date": "Date",
"rating": "Rating",
"sentiment": "Sentiment",
"restaurant": "Restaurant",
"country": "Country",
}
ReviewSource: TypeAlias = str | Path | IO[str] | IO[bytes] | pd.DataFrame
class ReviewDataError(ValueError):
"""Raised when a review dataset cannot be indexed safely."""
@dataclass(frozen=True)
class ColumnMapping:
review: str
title: str | None = None
date: str | None = None
rating: str | None = None
sentiment: str | None = None
restaurant: str | None = None
country: str | None = None
def as_dict(self) -> dict[str, str | None]:
return {role: getattr(self, role) for role in ROLE_TO_CANONICAL}
@dataclass(frozen=True)
class ReviewSummary:
total_reviews: int
average_rating: float | None
high_rated: int
low_rated: int
rating_counts: dict[int, int]
sentiment_counts: dict[str, int]
first_date: date | None
last_date: date | None
restaurant_count: int
country_count: int
@dataclass(frozen=True)
class ReviewMatch:
document: Document
score: float
def _normalize_column_name(value: object) -> str:
return re.sub(r"[^a-z0-9]", "", str(value).casefold())
def _alias_score(column: str, aliases: tuple[str, ...]) -> int:
normalized = _normalize_column_name(column)
return max(
(1000 + len(alias) for alias in aliases if normalized == alias),
default=0,
)
def suggest_column_mapping(columns: Any) -> dict[str, str | None]:
"""Suggest semantic roles without guessing when no useful alias exists."""
available = [str(column) for column in columns]
suggestions: dict[str, str | None] = {}
claimed: set[str] = set()
for role, aliases in ROLE_ALIASES.items():
ranked = sorted(
(
(_alias_score(column, aliases), -index, column)
for index, column in enumerate(available)
if column not in claimed
),
reverse=True,
)
score, _, column = ranked[0] if ranked else (0, 0, None)
suggestions[role] = column if score >= 500 else None
if score >= 500 and column is not None:
claimed.add(column)
return suggestions
def detect_column_mapping(columns: Any) -> ColumnMapping:
suggestions = suggest_column_mapping(columns)
review_column = suggestions["review"]
if review_column is None:
raise ReviewDataError(
"Could not detect a review text column. Select one in the column mapping."
)
return ColumnMapping(
review=review_column,
title=suggestions["title"],
date=suggestions["date"],
rating=suggestions["rating"],
sentiment=suggestions["sentiment"],
restaurant=suggestions["restaurant"],
country=suggestions["country"],
)
def _read_dataframe(source: ReviewSource) -> pd.DataFrame:
if isinstance(source, pd.DataFrame):
return cast(pd.DataFrame, source).copy()
return pd.read_csv(source)
def _validate_mapping(mapping: ColumnMapping, columns: Any) -> None:
available = {str(column) for column in columns}
selected = [column for column in mapping.as_dict().values() if column is not None]
missing = [column for column in selected if column not in available]
if missing:
raise ReviewDataError(f"Mapped columns do not exist: {', '.join(missing)}")
duplicates = sorted({column for column in selected if selected.count(column) > 1})
if duplicates:
raise ReviewDataError(
"A source column cannot be assigned to more than one role: "
+ ", ".join(duplicates)
)
def _optional_text(series: pd.Series) -> pd.Series:
values = series.astype("string").str.strip()
return values.mask(values == "", pd.NA)
def load_reviews(
source: ReviewSource = DEFAULT_DATA_PATH,
*,
mapping: ColumnMapping | None = None,
) -> pd.DataFrame:
"""Map a review dataset into a stable internal schema."""
dataframe = _read_dataframe(source)
if set(CANONICAL_COLUMNS).issubset(dataframe.columns) and "_extra" in dataframe:
return dataframe.reset_index(drop=True)
resolved_mapping = mapping or detect_column_mapping(dataframe.columns)
_validate_mapping(resolved_mapping, dataframe.columns)
review_values = _optional_text(dataframe[resolved_mapping.review])
invalid_reviews = review_values.isna()
if invalid_reviews.any():
rows = ", ".join(str(index) for index in dataframe.index[invalid_reviews])
raise ReviewDataError(f"Review cannot be empty; invalid rows: {rows}")
normalized = pd.DataFrame(index=dataframe.index)
normalized["Review"] = review_values
if resolved_mapping.title:
normalized["Title"] = _optional_text(dataframe[resolved_mapping.title])
else:
normalized["Title"] = pd.Series(pd.NA, index=dataframe.index, dtype="string")
if resolved_mapping.rating:
raw_ratings = dataframe[resolved_mapping.rating]
ratings = pd.to_numeric(raw_ratings, errors="coerce")
supplied = raw_ratings.notna() & (
raw_ratings.astype("string").str.strip() != ""
)
invalid_ratings = supplied & (
ratings.isna() | (ratings % 1 != 0) | ~ratings.between(1, 5)
)
if invalid_ratings.any():
rows = ", ".join(str(index) for index in dataframe.index[invalid_ratings])
raise ReviewDataError(
f"Rating must be an integer from 1 to 5; invalid rows: {rows}"
)
normalized["Rating"] = ratings.astype("Int64")
else:
normalized["Rating"] = pd.Series(pd.NA, index=dataframe.index, dtype="Int64")
if resolved_mapping.date:
raw_dates = dataframe[resolved_mapping.date]
dates = pd.to_datetime(raw_dates, errors="coerce", format="mixed")
supplied = raw_dates.notna() & (raw_dates.astype("string").str.strip() != "")
invalid_dates = supplied & dates.isna()
if invalid_dates.any():
rows = ", ".join(str(index) for index in dataframe.index[invalid_dates])
raise ReviewDataError(f"Date must be a valid date; invalid rows: {rows}")
normalized["Date"] = dates.dt.strftime("%Y-%m-%d").astype("string")
else:
normalized["Date"] = pd.Series(pd.NA, index=dataframe.index, dtype="string")
for role in ("sentiment", "restaurant", "country"):
canonical = ROLE_TO_CANONICAL[role]
source_column = getattr(resolved_mapping, role)
if source_column:
normalized[canonical] = _optional_text(dataframe[source_column])
else:
normalized[canonical] = pd.Series(
pd.NA, index=dataframe.index, dtype="string"
)
normalized = normalized.loc[:, CANONICAL_COLUMNS]
used_columns = {
column for column in resolved_mapping.as_dict().values() if column is not None
}
extra_columns = [
column for column in dataframe.columns if column not in used_columns
]
normalized["_extra"] = [
{
str(column): str(dataframe.at[index, column])
for column in extra_columns
if pd.notna(dataframe.at[index, column])
and str(dataframe.at[index, column]).strip()
}
for index in dataframe.index
]
normalized.attrs["column_mapping"] = resolved_mapping.as_dict()
return normalized.reset_index(drop=True)
def dataset_summary(dataframe: pd.DataFrame) -> ReviewSummary:
"""Calculate metrics from whichever semantic fields are available."""
normalized = load_reviews(dataframe)
ratings = normalized["Rating"].dropna().astype(int)
rating_values = ratings.value_counts().to_dict()
rating_counts = {
rating: int(rating_values.get(rating, 0)) for rating in range(1, 6)
}
sentiments = normalized["Sentiment"].dropna().astype(str)
sentiment_values = sentiments.value_counts().sort_index().to_dict()
dates = pd.to_datetime(normalized["Date"], errors="coerce").dropna()
return ReviewSummary(
total_reviews=len(normalized),
average_rating=float(ratings.mean()) if len(ratings) else None,
high_rated=int((ratings >= 4).sum()),
low_rated=int((ratings <= 2).sum()),
rating_counts=rating_counts,
sentiment_counts={
str(key): int(value) for key, value in sentiment_values.items()
},
first_date=dates.min().date() if len(dates) else None,
last_date=dates.max().date() if len(dates) else None,
restaurant_count=int(normalized["Restaurant"].nunique(dropna=True)),
country_count=int(normalized["Country"].nunique(dropna=True)),
)
def _casefold_values(values: tuple[str, ...] | list[str]) -> set[str]:
return {value.casefold() for value in values}
def filter_reviews(
dataframe: pd.DataFrame,
*,
min_rating: int | None = None,
max_rating: int | None = None,
start_date: date | None = None,
end_date: date | None = None,
sentiments: tuple[str, ...] | list[str] = (),
restaurants: tuple[str, ...] | list[str] = (),
countries: tuple[str, ...] | list[str] = (),
) -> pd.DataFrame:
"""Filter normalized reviews using only requested semantic fields."""
normalized = load_reviews(dataframe)
if min_rating is not None and max_rating is not None and min_rating > max_rating:
raise ValueError("min_rating cannot exceed max_rating")
if start_date is not None and end_date is not None and start_date > end_date:
raise ValueError("start_date cannot be after end_date")
mask = pd.Series(True, index=normalized.index)
if min_rating is not None or max_rating is not None:
ratings = normalized["Rating"]
mask &= ratings.notna()
if min_rating is not None:
mask &= ratings >= min_rating
if max_rating is not None:
mask &= ratings <= max_rating
if start_date is not None or end_date is not None:
dates = pd.to_datetime(normalized["Date"], errors="coerce")
mask &= dates.notna()
if start_date is not None:
mask &= dates >= pd.Timestamp(start_date)
if end_date is not None:
mask &= dates <= pd.Timestamp(end_date)
for canonical, selected in (
("Sentiment", sentiments),
("Restaurant", restaurants),
("Country", countries),
):
if selected:
allowed = _casefold_values(selected)
mask &= normalized[canonical].astype("string").str.casefold().isin(allowed)
return normalized.loc[mask].reset_index(drop=True)
def _metadata_value(value: Any) -> str | int | float | bool | None:
if pd.isna(value):
return None
if hasattr(value, "item"):
value = value.item()
if isinstance(value, (str, int, float, bool)):
return value
return str(value)
def _record_payload(row: pd.Series) -> str:
canonical = {column: _metadata_value(row[column]) for column in CANONICAL_COLUMNS}
extras = sorted((str(key), str(value)) for key, value in row["_extra"].items())
return json.dumps(
{"canonical": canonical, "extras": extras},
ensure_ascii=False,
sort_keys=True,
separators=(",", ":"),
)
def _record_digest(row: pd.Series) -> str:
return sha256(_record_payload(row).encode("utf-8")).hexdigest()
def dataset_fingerprint(dataframe: pd.DataFrame) -> str:
"""Return an order-insensitive digest of normalized review content."""
required = set(CANONICAL_COLUMNS) | {"_extra"}
if (
required <= set(dataframe.columns)
and dataframe["_extra"].map(lambda value: isinstance(value, dict)).all()
):
normalized = dataframe
else:
normalized = load_reviews(dataframe)
record_digests = sorted(_record_digest(row) for _, row in normalized.iterrows())
payload = json.dumps(record_digests, separators=(",", ":")).encode("utf-8")
return sha256(payload).hexdigest()
def dataset_storage(
dataframe: pd.DataFrame,
*,
storage_root: str | Path | None = None,
) -> tuple[Path, str]:
"""Derive an isolated persistence path and collection from dataset content."""
digest = dataset_fingerprint(dataframe)
root = Path(storage_root) if storage_root is not None else DEFAULT_STORAGE_ROOT
return root / "chroma" / digest, f"reviews_{digest[:16]}"
def _documents_and_ids(dataframe: pd.DataFrame) -> tuple[list[Document], list[str]]:
documents: list[Document] = []
ids: list[str] = []
occurrences: dict[str, int] = {}
metadata_fields = {
"Title": "title",
"Date": "date",
"Rating": "rating",
"Sentiment": "sentiment",
"Restaurant": "restaurant",
"Country": "country",
}
for _, row in dataframe.iterrows():
digest = _record_digest(row)
occurrence = occurrences.get(digest, 0) + 1
occurrences[digest] = occurrence
suffix = f"_{occurrence}" if occurrence > 1 else ""
item_id = f"review_{digest[:32]}{suffix}"
metadata: dict[str, str | int | float | bool] = {"source_id": item_id}
content_lines: list[str] = []
for canonical, key in metadata_fields.items():
value = _metadata_value(row[canonical])
if value is not None:
metadata[key] = value
if canonical in {"Title", "Sentiment", "Restaurant", "Country"}:
content_lines.append(f"{canonical}: {value}")
for source_column, raw_value in row["_extra"].items():
base_key = f"extra_{_normalize_column_name(source_column)}"
if base_key == "extra_":
continue
safe_key = base_key
collision_suffix = 2
while safe_key in metadata:
safe_key = f"{base_key}_{collision_suffix}"
collision_suffix += 1
metadata[safe_key] = str(raw_value)
content_lines.append(f"{source_column}: {raw_value}")
content_lines.append(f"Review: {row['Review']}")
documents.append(
Document(
page_content="\n".join(content_lines),
metadata=metadata,
id=item_id,
)
)
ids.append(item_id)
return documents, ids
def create_embeddings(
*,
model: str = DEFAULT_EMBEDDING_MODEL,
base_url: str | None = None,
) -> OllamaEmbeddings:
return OllamaEmbeddings(model=model, base_url=base_url)
def create_vector_store(
source: ReviewSource = DEFAULT_DATA_PATH,
*,
mapping: ColumnMapping | None = None,
database_path: str | Path | None = None,
collection_name: str | None = None,
embeddings: Any | None = None,
embedding_model: str = DEFAULT_EMBEDDING_MODEL,
ollama_host: str | None = None,
) -> Chroma:
"""Open an isolated Chroma collection and reconcile it with the source."""
dataframe = load_reviews(source, mapping=mapping)
derived_database, derived_collection = dataset_storage(dataframe)
resolved_database = Path(database_path) if database_path else derived_database
resolved_collection = collection_name or derived_collection
documents, ids = _documents_and_ids(dataframe)
embedding_function = embeddings or create_embeddings(
model=embedding_model,
base_url=ollama_host,
)
vector_store = Chroma(
collection_name=resolved_collection,
persist_directory=str(resolved_database),
embedding_function=embedding_function,
)
desired_ids = set(ids)
existing_ids = set(vector_store.get(include=[])["ids"])
stale_ids = sorted(existing_ids - desired_ids)
if stale_ids:
vector_store.delete(ids=stale_ids)
missing_indexes = [
index for index, item_id in enumerate(ids) if item_id not in existing_ids
]
if missing_indexes:
vector_store.add_documents(
documents=[documents[index] for index in missing_indexes],
ids=[ids[index] for index in missing_indexes],
)
return vector_store
def index_count(vector_store: Any) -> int:
return len(vector_store.get(include=[])["ids"])
def _matches_category(
metadata: dict[str, Any], key: str, selected: tuple[str, ...] | list[str]
) -> bool:
if not selected:
return True
value = metadata.get(key)
return value is not None and str(value).casefold() in _casefold_values(selected)
def search_reviews(
vector_store: Any,
query: str,
*,
limit: int = 5,
min_rating: int | None = None,
max_rating: int | None = None,
start_date: date | None = None,
end_date: date | None = None,
sentiments: tuple[str, ...] | list[str] = (),
restaurants: tuple[str, ...] | list[str] = (),
countries: tuple[str, ...] | list[str] = (),
) -> list[ReviewMatch]:
"""Search semantically, then apply filters supported by the dataset."""
if limit < 1:
raise ValueError("limit must be at least 1")
if min_rating is not None and max_rating is not None and min_rating > max_rating:
raise ValueError("min_rating cannot exceed max_rating")
if start_date is not None and end_date is not None and start_date > end_date:
raise ValueError("start_date cannot be after end_date")
total = index_count(vector_store)
if total == 0:
return []
results = vector_store.similarity_search_with_score(query, k=total)
matches: list[ReviewMatch] = []
for document, score in results:
metadata = document.metadata
if min_rating is not None or max_rating is not None:
try:
rating = int(metadata["rating"])
except (KeyError, TypeError, ValueError):
continue
if min_rating is not None and rating < min_rating:
continue
if max_rating is not None and rating > max_rating:
continue
if start_date is not None or end_date is not None:
try:
review_date = date.fromisoformat(str(metadata["date"]))
except (KeyError, ValueError):
continue
if start_date is not None and review_date < start_date:
continue
if end_date is not None and review_date > end_date:
continue
if not _matches_category(metadata, "sentiment", sentiments):
continue
if not _matches_category(metadata, "restaurant", restaurants):
continue
if not _matches_category(metadata, "country", countries):
continue
matches.append(ReviewMatch(document=document, score=float(score)))
if len(matches) == limit:
break
return matches