File size: 1,146 Bytes
69c12a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83d11f8
69c12a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# app/modules/postprocess.py
import json
import threading
from datetime import datetime

from logs.logger import get_logger

log = get_logger(__name__)
_file_lock = threading.Lock()

TUNING_FILE = "interactions.jsonl"

def finalize_answer(user_message, context_docs, model_answer=None, hf_token=None):
    """
    Post-processing the final answer:
    - Push the input/output pairs to the Hugging Face Datasets Hub
    - The HF_TOKEN environment variable is required when running Space
    """
    # 1. Create record
    record = {
        "timestamp": datetime.now().isoformat(),
        "input": user_message,
        "context": [doc.get("snippet", "") for doc in (context_docs or [])],
        "output": model_answer or "",
    }

    # 2. Append to local jsonl file to further model training and save context datas as cache to avoid api cost
    try:
        with _file_lock:
            with open(TUNING_FILE, "a", encoding="utf-8") as f:
                f.write(json.dumps(record, ensure_ascii=False) + "\n")
    except Exception as e:
        log.error("Failed to write to local log file:", e)

def get_file_lock():
    return _file_lock