-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloader.py
More file actions
747 lines (604 loc) · 25.6 KB
/
Copy pathloader.py
File metadata and controls
747 lines (604 loc) · 25.6 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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
from __future__ import annotations
import hashlib
import json
import logging
import os
import re
import time
from datetime import datetime, timezone
from pathlib import Path
from typing import Any, List
from dotenv import load_dotenv
from fastembed import TextEmbedding
from fastembed.common.model_description import ModelSource, PoolingType
from groq import Groq
from qdrant_client import QdrantClient
from qdrant_client.http import models
try:
import boto3
except ImportError:
boto3 = None
BASE_DIR = Path(__file__).resolve().parent
load_dotenv(BASE_DIR / ".env")
SOURCE_ROOT = Path(os.getenv("MARCAS_ROOT", "/Users/angellollerena/Documents/MARCAS"))
DATA_DIR = Path(os.getenv("RAG_CARS_DATA_DIR", str(BASE_DIR / "data")))
AWS_REGION = os.getenv("AWS_REGION", "sa-east-1").strip()
AWS_S3_BUCKET = os.getenv("AWS_S3_BUCKET", "rag-autos-prod").strip()
AWS_S3_PREFIX = os.getenv("AWS_S3_PREFIX", "rag-autos-prod").strip().strip("/")
ENABLE_S3_SYNC = os.getenv("ENABLE_S3_SYNC", "").strip().lower() in {"1", "true", "yes", "on"}
QDRANT_PATH = Path(os.getenv("QDRANT_PATH", str(BASE_DIR / "qdrant_db")))
COLLECTION_NAME = os.getenv("QDRANT_COLLECTION", "automotive_catalogs")
VECTOR_NAME = "bge_m3"
FASTEMBED_MODEL_NAME = os.getenv("FASTEMBED_MODEL_NAME", "BAAI/bge-m3")
FASTEMBED_CACHE_DIR = os.getenv("FASTEMBED_CACHE_DIR", str(BASE_DIR / ".fastembed_cache"))
FASTEMBED_BATCH_SIZE = int(os.getenv("FASTEMBED_BATCH_SIZE", "8"))
EMBEDDING_DIM = int(os.getenv("FASTEMBED_EMBEDDING_DIM", "1024"))
GROQ_MODEL = os.getenv("GROQ_MODEL", "meta-llama/llama-4-scout-17b-16e-instruct")
LLAMAPARSE_TIER = os.getenv("LLAMAPARSE_TIER", "agentic")
LLAMAPARSE_VERSION = os.getenv("LLAMAPARSE_VERSION", "latest")
LLAMAPARSE_PARSE_MODE = os.getenv("LLAMAPARSE_PARSE_MODE", "").strip()
FORCE_REPARSE_MARKDOWN = os.getenv("FORCE_REPARSE_MARKDOWN", "").strip().lower() in {"1", "true", "yes", "on"}
logger = logging.getLogger(__name__)
logging.basicConfig(level=os.getenv("LOG_LEVEL", "INFO").upper(), format="%(levelname)s: %(message)s")
_EMBEDDING_MODEL: TextEmbedding | None = None
#Devuelve la fecha utc
def _utc_now_iso() -> str:
return datetime.now(timezone.utc).isoformat()
#Construye la clave S3 para el objeto
def _s3_key(*parts: str) -> str:
clean_parts = []
if AWS_S3_PREFIX:
clean_parts.append(AWS_S3_PREFIX)
for part in parts:
clean = str(part).strip().strip("/")
if clean:
clean_parts.append(clean)
return "/".join(clean_parts)
#Obtiene el cliente S3 por boto3
def _get_s3_client():
if not ENABLE_S3_SYNC:
return None
if not AWS_S3_BUCKET:
raise RuntimeError("AWS S3 Bucket es requerido cuando tengas ENABLE_S3_SYNC= true.")
if boto3 is None:
raise RuntimeError(" boto3 es requerido cuando tengas ENABLE_S3_SYNC= true.")
if AWS_REGION:
return boto3.client("s3", region_name=AWS_REGION)#La region es br
return boto3.client("s3")
def upload_file_to_s3(local_path: str | Path, key: str) -> str | None:
client = _get_s3_client()
if client is None:
return None
path = Path(local_path).expanduser().resolve()
if not path.exists() or not path.is_file():
raise FileNotFoundError(f" No se puede cargar a S3 debido a que falta archivo: {path}")
try:
client.upload_file(str(path), AWS_S3_BUCKET, key)
except Exception as exc:
raise RuntimeError(f" Fallo la carga del archivo a s3://{AWS_S3_BUCKET}/{key}: {exc}") from exc
return f"s3://{AWS_S3_BUCKET}/{key}"
def upload_text_to_s3(text: str, key: str, content_type: str) -> str | None:
client = _get_s3_client()
if client is None:
return None
try:
client.put_object(
Bucket=AWS_S3_BUCKET,
Key=key,
Body=text.encode("utf-8"),
ContentType=content_type,
)
except Exception as exc:
raise RuntimeError(f"Fallo la carga del texto a s3://{AWS_S3_BUCKET}/{key}: {exc}") from exc
return f"s3://{AWS_S3_BUCKET}/{key}"
def _s3_markdown_key(brand: str, model_name: str) -> str:
return _s3_key("silver", "parsed_markdown", brand, model_name, f"{model_name}.md")
def _s3_markdown_uri(brand: str, model_name: str) -> str:
if not ENABLE_S3_SYNC:
return ""
return f"s3://{AWS_S3_BUCKET}/{_s3_markdown_key(brand, model_name)}"
def s3_object_exists(key: str) -> bool:
client = _get_s3_client()
if client is None:
return False
try:
client.head_object(Bucket=AWS_S3_BUCKET, Key=key)
return True
except Exception as exc:
error_code = getattr(exc, "response", {}).get("Error", {}).get("Code")
if error_code in {"404", "NoSuchKey", "NotFound"}:
return False
raise RuntimeError(f"No se pudo verificar s3://{AWS_S3_BUCKET}/{key}: {exc}") from exc
def get_all_pdf_paths(root_path: str) -> List[dict]:
root = Path(root_path).expanduser().resolve()
pdfs: List[dict] = []
if not root.exists():
logger.warning("Source root does not exist: %s", root)
return pdfs
for current_dir, _, filenames in os.walk(root):
folder = Path(current_dir)
try:
relative_parts = folder.relative_to(root).parts
except ValueError:
continue
if len(relative_parts) < 2:
continue
brand, model_name = relative_parts[0], relative_parts[1]
for filename in sorted(filenames):
if filename.lower().endswith(".pdf"):
pdfs.append(
{
"pdf_path": str((folder / filename).resolve()),
"brand": brand,
"model": model_name,
}
)
return sorted(pdfs, key=lambda item: (item["brand"], item["model"], item["pdf_path"]))
def _get_llama_parser():
api_key = os.getenv("LLAMA_CLOUD_API_KEY", "").strip()
if not api_key:
raise RuntimeError("LLAMA_CLOUD_API_KEY es requerido para parsear PDFs con LlamaParse.")
try:
from llama_cloud_services import LlamaParse
except ImportError:
from llama_parse import LlamaParse
parser_options = {
"api_key": api_key,
"result_type": "markdown",
"language": "es",
"compact_markdown_table": True,
"merge_tables_across_pages_in_markdown": True,
"markdown_table_multiline_header_separator": "<br />",
"continuous_mode": True,
"aggressive_table_extraction": True,
"guess_xlsx_sheet_name": True,
"high_res_ocr": True,
"disable_ocr": False,
"invalidate_cache": FORCE_REPARSE_MARKDOWN,
"ignore_errors": False,
"verbose": True,
}
if LLAMAPARSE_PARSE_MODE:
parser_options["parse_mode"] = LLAMAPARSE_PARSE_MODE
else:
parser_options["tier"] = LLAMAPARSE_TIER
parser_options["version"] = LLAMAPARSE_VERSION
return LlamaParse(
**parser_options,
)
def _extract_markdown(parse_result: Any) -> str:
if isinstance(parse_result, str):
return parse_result
if isinstance(parse_result, list):
parts = []
for item in parse_result:
text = getattr(item, "text", None) or getattr(item, "markdown", None)
if text:
parts.append(str(text))
if parts:
return "\n\n".join(parts)
text = getattr(parse_result, "text", None) or getattr(parse_result, "markdown", None)
if text:
return str(text)
raise RuntimeError("LlamaParse returned an unsupported response shape.")
def parse_pdf_to_markdown(pdf_path: str, output_path: str, force_reparse: bool = False) -> str:
output = Path(output_path).expanduser().resolve()
if output.exists() and output.stat().st_size > 0 and not force_reparse:
logger.info("Skipeamos debido a que ya existe el Markdown: %s", output)
brand = output.parent.name
model_name = output.stem
s3_markdown_path = upload_text_to_s3(
output.read_text(encoding="utf-8"),
_s3_markdown_key(brand, model_name),
"text/markdown; charset=utf-8",
)
if s3_markdown_path:
logger.info(" CargaUploaded existing Markdown to S3 Silver: %s", s3_markdown_path)
return str(output)
pdf = Path(pdf_path).expanduser().resolve()
if not pdf.exists():
raise FileNotFoundError(f"PDF not found: {pdf}")
output.parent.mkdir(parents=True, exist_ok=True)
logger.info("Parsing PDF with LlamaParse: %s", pdf)
parser = _get_llama_parser()
try:
if hasattr(parser, "load_data"):
parsed = parser.load_data(str(pdf))
elif hasattr(parser, "parse"):
parsed = parser.parse(str(pdf))
else:
raise RuntimeError("Installed LlamaParse client has no load_data or parse method.")
except Exception as exc:
raise RuntimeError(f"Failed to parse {pdf}: {exc}") from exc
markdown = _extract_markdown(parsed).strip()
if not markdown:
raise RuntimeError(f"LlamaParse produced empty Markdown for {pdf}")
output.write_text(markdown + "\n", encoding="utf-8")
logger.info("Saved Markdown: %s", output)
brand = output.parent.name
model_name = output.stem
markdown_key = _s3_markdown_key(brand, model_name)
s3_markdown_path = upload_text_to_s3(
markdown + "\n",
markdown_key,
"text/markdown; charset=utf-8",
)
if s3_markdown_path:
logger.info("Uploaded Markdown to S3 Silver: %s", s3_markdown_path)
return str(output)
def load_markdown(md_path: str) -> str:
path = Path(md_path).expanduser().resolve()
if not path.exists():
raise FileNotFoundError(f"Markdown file not found: {path}")
return path.read_text(encoding="utf-8")
def _is_table_line(line: str) -> bool:
stripped = line.strip()
return stripped.startswith("|") and stripped.endswith("|") and stripped.count("|") >= 2
def _markdown_blocks(md_text: str) -> List[str]:
lines = md_text.splitlines()
blocks: List[str] = []
current: List[str] = []
table: List[str] = []
def flush_current() -> None:
nonlocal current
if current:
block = "\n".join(current).strip()
if block:
blocks.append(block)
current = []
def flush_table() -> None:
nonlocal table
if table:
blocks.append("\n".join(table).strip())
table = []
for line in lines:
if _is_table_line(line):
flush_current()
table.append(line)
continue
flush_table()
if re.match(r"^\s{0,3}#{1,6}\s+", line):
flush_current()
current.append(line)
continue
if not line.strip():
flush_current()
continue
current.append(line)
flush_table()
flush_current()
return blocks
def _approx_tokens(text: str) -> int:
return max(1, int(len(re.findall(r"\S+", text)) * 1.3))
def _split_long_text(text: str, max_tokens: int) -> List[str]:
sentences = re.split(r"(?<=[.!?])\s+", text)
chunks: List[str] = []
current: List[str] = []
for sentence in sentences:
candidate = " ".join(current + [sentence]).strip()
if current and _approx_tokens(candidate) > max_tokens:
chunks.append(" ".join(current).strip())
current = [sentence]
else:
current.append(sentence)
if current:
chunks.append(" ".join(current).strip())
return [chunk for chunk in chunks if chunk]
def chunk_markdown(md_text: str) -> List[str]:
blocks = _markdown_blocks(md_text)
chunks: List[str] = []
current: List[str] = []
current_tokens = 0
target_min = 500
target_max = 1000
for block in blocks:
block_tokens = _approx_tokens(block)
if block_tokens > target_max and not _is_table_line(block.splitlines()[0]):
for split in _split_long_text(block, target_max):
if current:
chunks.append("\n\n".join(current).strip())
current = []
current_tokens = 0
chunks.append(split)
continue
if current and current_tokens >= target_min and current_tokens + block_tokens > target_max:
chunks.append("\n\n".join(current).strip())
current = [block]
current_tokens = block_tokens
else:
current.append(block)
current_tokens += block_tokens
if current:
chunks.append("\n\n".join(current).strip())
return [chunk for chunk in chunks if chunk]
def _register_bge_m3_if_needed() -> None:
supported = {item["model"].lower() for item in TextEmbedding.list_supported_models()}
if FASTEMBED_MODEL_NAME.lower() in supported:
return
if FASTEMBED_MODEL_NAME.lower() != "baai/bge-m3":
raise RuntimeError(
f"FastEmbed model {FASTEMBED_MODEL_NAME} is not supported. "
"Use a model from TextEmbedding.list_supported_models() or FASTEMBED_MODEL_NAME=BAAI/bge-m3."
)
try:
TextEmbedding.add_custom_model(
model="BAAI/bge-m3",
pooling=PoolingType.CLS,
normalization=True,
sources=ModelSource(hf="BAAI/bge-m3"),
dim=1024,
model_file="onnx/model.onnx",
additional_files=[
"onnx/model.onnx_data",
"onnx/Constant_7_attr__value",
"sentencepiece.bpe.model",
"onnx/sentencepiece.bpe.model",
],
description="BGE-M3 dense embeddings registered for FastEmbed from BAAI/bge-m3 ONNX weights.",
license="mit",
size_in_gb=2.29,
)
except ValueError as exc:
if "already registered" not in str(exc):
raise
def _get_embedding_model() -> TextEmbedding:
global _EMBEDDING_MODEL
if _EMBEDDING_MODEL is None:
_register_bge_m3_if_needed()
_EMBEDDING_MODEL = TextEmbedding(
model_name=FASTEMBED_MODEL_NAME,
cache_dir=FASTEMBED_CACHE_DIR,
lazy_load=True,
)
return _EMBEDDING_MODEL
def embed_texts(chunks: List[str]) -> List[List[float]]:
if not chunks:
return []
try:
model = _get_embedding_model()
vectors = list(model.embed(chunks, batch_size=FASTEMBED_BATCH_SIZE))
except Exception as exc:
raise RuntimeError(
f"Could not generate FastEmbed vectors with {FASTEMBED_MODEL_NAME}. "
f"The first run must download the model into {FASTEMBED_CACHE_DIR}."
) from exc
return [vector.astype("float32").tolist() for vector in vectors]
def init_qdrant():
qdrant_url = os.getenv("QDRANT_URL", "").strip()
qdrant_api_key = os.getenv("QDRANT_API_KEY", "").strip()
if qdrant_url:
client = QdrantClient(
url=qdrant_url,
api_key=qdrant_api_key,
)
else:
QDRANT_PATH.mkdir(parents=True, exist_ok=True)
client = QdrantClient(path=str(QDRANT_PATH))
existing = {collection.name for collection in client.get_collections().collections}
if COLLECTION_NAME not in existing:
client.create_collection(
collection_name=COLLECTION_NAME,
vectors_config={
VECTOR_NAME: models.VectorParams(
size=EMBEDDING_DIM,
distance=models.Distance.COSINE,
)
},
)
return client
return client
def reset_qdrant_collection():
qdrant_url = os.getenv("QDRANT_URL", "").strip()
qdrant_api_key = os.getenv("QDRANT_API_KEY", "").strip()
if qdrant_url:
client = QdrantClient(
url=qdrant_url,
api_key=qdrant_api_key,
)
else:
QDRANT_PATH.mkdir(parents=True, exist_ok=True)
client = QdrantClient(path=str(QDRANT_PATH))
existing = {collection.name for collection in client.get_collections().collections}
if COLLECTION_NAME in existing:
client.delete_collection(collection_name=COLLECTION_NAME)
client.create_collection(
collection_name=COLLECTION_NAME,
vectors_config={
VECTOR_NAME: models.VectorParams(
size=EMBEDDING_DIM,
distance=models.Distance.COSINE,
)
},
)
return client
def _point_id(source: str, chunk_index: int, chunk: str) -> str:
raw = f"{source}:{chunk_index}:{hashlib.sha256(chunk.encode('utf-8')).hexdigest()}"
return hashlib.md5(raw.encode("utf-8")).hexdigest()
def upsert_chunks(chunks, embeddings, metadata_list):
if not (len(chunks) == len(embeddings) == len(metadata_list)):
raise ValueError("chunks, embeddings, and metadata_list must have the same length.")
if not chunks:
return 0
client = init_qdrant()
points = []
for index, (chunk, embedding, metadata) in enumerate(zip(chunks, embeddings, metadata_list)):
payload = {
"page_content": chunk,
"brand": metadata["brand"],
"model": metadata["model"],
"source": metadata["source"],
"source_pdf": metadata.get("source_pdf", ""),
"s3_pdf_path": metadata.get("s3_pdf_path", ""),
"s3_markdown_path": metadata.get("s3_markdown_path", ""),
"section": metadata.get("section", "unknown"),
"chunk_index": metadata.get("chunk_index", index),
"indexed_at": metadata.get("indexed_at", int(time.time())),
}
points.append(
models.PointStruct(
id=_point_id(payload["source"], payload["chunk_index"], chunk),
vector={VECTOR_NAME: embedding},
payload=payload,
)
)
client.upsert(collection_name=COLLECTION_NAME, points=points, wait=True)
return len(points)
def _markdown_output_path(brand: str, model_name: str) -> Path:
return DATA_DIR / brand / f"{model_name}.md"
def index_all_documents():
return index_documents(force_reparse=FORCE_REPARSE_MARKDOWN)
def index_documents(force_reparse: bool = False):
if force_reparse:
logger.warning("force_reparse no regenera Gold existente; elimina el archivo Gold en S3 si necesitas reprocesarlo.")
if not ENABLE_S3_SYNC:
raise RuntimeError("La indexacion productiva ahora usa Gold desde S3. Configura ENABLE_S3_SYNC=true.")
from gold_processor import run_full_gold_pipeline
return run_full_gold_pipeline()
def _build_filter(filters: dict | None):
if not filters:
return None
conditions = []
for key in ("brand", "model", "marca", "modelo", "source"):
value = filters.get(key)
if value:
conditions.append(models.FieldCondition(key=key, match=models.MatchValue(value=value)))
return models.Filter(must=conditions) if conditions else None
def query_qdrant(query: str, filters: dict | None = None):
if not query.strip():
return []
client = init_qdrant()
query_vector = embed_texts([query])[0]
response = client.query_points(
collection_name=COLLECTION_NAME,
query=query_vector,
using=VECTOR_NAME,
query_filter=_build_filter(filters),
limit=5,
with_payload=True,
)
results = response.points
return [
{
"score": result.score,
"page_content": result.payload.get("toon", "") or result.payload.get("page_content", ""),
"toon": result.payload.get("toon", ""),
"brand": result.payload.get("marca", "") or result.payload.get("brand", ""),
"model": result.payload.get("modelo", "") or result.payload.get("model", ""),
"marca": result.payload.get("marca", ""),
"modelo": result.payload.get("modelo", ""),
"variante": result.payload.get("variante", ""),
"combustible": result.payload.get("combustible", ""),
"trac": result.payload.get("trac", ""),
"hp": result.payload.get("hp"),
"precio": result.payload.get("precio_usd"),
"precio_usd": result.payload.get("precio_usd"),
"asientos": result.payload.get("asientos"),
"source": result.payload.get("source", ""),
"s3_gold_path": result.payload.get("s3_gold_path", ""),
"s3_pdf_path": result.payload.get("s3_pdf_path", ""),
"s3_markdown_path": result.payload.get("s3_markdown_path", ""),
"section": result.payload.get("section", "unknown"),
}
for result in results
if result.payload
]
def generate_answer(query: str, context: List[str]) -> str:
api_key = os.getenv("GROQ_API_KEY", "").strip()
if not api_key:
raise RuntimeError("GROQ_API_KEY es requerido para generar respuestas con Groq.")
clean_context = [item.strip() for item in context if item and item.strip()]
if not clean_context:
return "No encontre informacion relevante en los catalogos indexados."
client = Groq(api_key=api_key)
response = client.chat.completions.create(
model=GROQ_MODEL,
temperature=0.2,
max_tokens=2048,
messages=[
{
"role": "system",
"content": (
"Eres Que Auto Comprar, un asesor automotriz digital para usuarios de Peru. "
"Tu objetivo es ayudar a elegir autos con explicaciones claras, utiles y accionables para una persona que no necesariamente conoce terminos tecnicos. "
"Responde siempre en espanol latinoamericano, con tono profesional, cercano y directo. "
"Usa solo la informacion entregada en el contexto de catalogos; no inventes datos, precios, versiones, autonomia, seguridad ni equipamiento. "
"Si un dato no esta en el contexto, dilo claramente como 'no figura en la informacion disponible'. "
"Cuando el usuario compare dos o mas autos, modelos o versiones, responde con una tabla Markdown y columnas relevantes como modelo, version, motor, potencia, transmision, traccion, seguridad, consumo/equipamiento si aparece en el contexto, y una columna de recomendacion. "
"Despues de la tabla, agrega una recomendacion breve segun el uso indicado por el usuario: ciudad, familia, trabajo, carretera, presupuesto, consumo o seguridad. "
"Si no es una comparacion, responde con secciones cortas, bullets concretos y una conclusion final. "
"No menciones JSON, embeddings, Qdrant, S3, RAG, Gold, Silver, Bronze ni detalles tecnicos internos."
),
},
{
"role": "user",
"content": f"Question:\n{query}\n\nContext:\n" + "\n\n---\n\n".join(clean_context),
},
],
)
return (response.choices[0].message.content or "").strip()
def main_app():
import streamlit as st
st.set_page_config(page_title="Asesor de automoviles", layout="centered")
st.title("RAG Cars")
for key in ("GROQ_API_KEY", "LLAMA_CLOUD_API_KEY", "QDRANT_URL", "QDRANT_API_KEY", "QDRANT_COLLECTION"):
try:
secret_value = st.secrets.get(key)
except Exception:
secret_value = None
if secret_value and not os.getenv(key):
os.environ[key] = str(secret_value)
with st.sidebar:
st.subheader("API keys")
for key in ("GROQ_API_KEY", "LLAMA_CLOUD_API_KEY"):
st.caption(f"{key}: {'configured' if os.getenv(key) else 'missing'}")
st.subheader("Index")
st.caption(f"PDF root: {SOURCE_ROOT}")
st.caption(f"Markdown: {DATA_DIR}")
force_reparse = st.checkbox(
"Reparse PDFs with LlamaParse",
value=FORCE_REPARSE_MARKDOWN,
help="El flujo productivo indexa desde Gold en S3. Para reprocesar un archivo Gold existente, elimina su JSONL en S3.",
)
if st.button("Run Gold pipeline", type="primary"):
with st.spinner("Processing Gold from S3 Silver..."):
try:
stats = index_documents(force_reparse=force_reparse)
st.success(
f"Indexed {stats['indexed_variants']} variant(s) from {stats['gold_files']} Gold file(s)."
)
if stats["errors"]:
st.warning("\n".join(stats["errors"]))
except Exception as exc:
st.error(str(exc))
if st.button("Clear chat"):
st.session_state.messages = []
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
prompt = st.chat_input("Ask about a vehicle catalog...")
if not prompt:
return
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
with st.spinner("Searching catalogs..."):
try:
hits = query_qdrant(prompt)
context = [hit["page_content"] for hit in hits]
answer = generate_answer(prompt, context)
except Exception as exc:
answer = f"Error: {exc}"
st.markdown(answer)
if hits:
with st.expander("Sources"):
for hit in hits:
st.write(f"- {hit['brand']}/{hit['model']} ({hit['score']:.3f}) - {hit['source']}")
st.session_state.messages.append({"role": "assistant", "content": answer})
def _run_cli_index() -> None:
stats = index_documents(force_reparse=FORCE_REPARSE_MARKDOWN)
print(json.dumps(stats, ensure_ascii=False, indent=2))
if __name__ == "__main__":
_run_cli_index()