File size: 1,672 Bytes
6e09785
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ============================================================
# FILE: src/logging_utils.py
# ============================================================
# PURPOSE:
# Save structured logs for RAG runs.
#
# WHY LOGGING MATTERS:
# Production AI systems need observability.
#
# Useful fields:
# - user question
# - generated answer
# - retrieved sources
# - model name
# - prompt version
# - latency
# - errors
#
# AI ENGINEER PRODUCTION TIP:
# Be careful with sensitive data.
# Do not log private user data unless necessary and protected.
# ============================================================

import json
import time
from pathlib import Path
from typing import Any, Dict


def write_jsonl_event(logs_folder: Path, event: Dict[str, Any]) -> Path:
    """
    Append one JSON event to a JSONL file.

    JSONL format:
    - one JSON object per line
    - easy to append
    - easy to load later
    """

    logs_folder.mkdir(parents=True, exist_ok=True)

    log_path = logs_folder / "rag_events.jsonl"

    event_with_timestamp = {
        "timestamp": time.strftime("%Y-%m-%d %H:%M:%S"),
        **event,
    }

    with log_path.open("a", encoding="utf-8") as file:
        file.write(json.dumps(event_with_timestamp, ensure_ascii=False) + "\n")

    return log_path


def save_json_output(outputs_folder: Path, data: Dict[str, Any], file_name: str) -> Path:
    """
    Save a full RAG result as a JSON file.
    """

    outputs_folder.mkdir(parents=True, exist_ok=True)

    output_path = outputs_folder / file_name

    with output_path.open("w", encoding="utf-8") as file:
        json.dump(data, file, indent=2, ensure_ascii=False)

    return output_path