File size: 1,803 Bytes
0a537e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"log chat messages and feedbacks to a dataset"

from typing import Tuple

import os
import tempfile
import ujson
import uuid

import huggingface_hub
import pandas as pd

LOGS_DATSET_PATH = "logikon/benjamin-logs"


async def log_messages(
        messages: Tuple[str, str],
        conversation_id: str,
        step: int,
        metadata: dict = None
    ):

    data = {
        "conversation_id": conversation_id,
        "step": step,
        "human": messages[0],
        "ai": messages[1],
        "metadata": list(metadata.items()) if metadata else []
    }

    with tempfile.TemporaryFile(mode="w+") as f:
        ujson.dump(data, f)
        f.flush()

        api = huggingface_hub.HfApi()
        api.upload_file(
            path_or_fileobj=f.buffer,
            path_in_repo=os.path.join("data", pd.Timestamp.now().date().isoformat(), conversation_id, f"step_{step}.json"),
            repo_id=LOGS_DATSET_PATH,
            repo_type="dataset",
            token=os.environ["HF_DATASETS_TOKEN"]
        )

async def log_feedback(
        liked: bool,
        conversation_id: str,
        step: int,
        metadata: dict = None
    ):

    data = {
        "conversation_id": conversation_id,
        "step": step,
        "liked": liked,
        "metadata": list(metadata.items()) if metadata else []
    }

    with tempfile.TemporaryFile(mode="w+") as f:
        ujson.dump(data, f)
        f.flush()

        api = huggingface_hub.HfApi()
        api.upload_file(
            path_or_fileobj=f.buffer,
            path_in_repo=os.path.join("data", pd.Timestamp.now().date().isoformat(), conversation_id, f"feedback_{step[0]}_{str(uuid.uuid4())}.json"),
            repo_id=LOGS_DATSET_PATH,
            repo_type="dataset",
            token=os.environ["HF_DATASETS_TOKEN"]
        )