File size: 10,916 Bytes
bc61879
 
 
 
 
 
da1d68a
 
 
7a54e0d
3fbe6fe
da1d68a
3fbe6fe
da1d68a
 
 
 
 
 
 
 
 
 
 
 
bc61879
 
7a54e0d
 
3fbe6fe
7a54e0d
 
da1d68a
7a54e0d
da1d68a
3fbe6fe
7a54e0d
 
 
 
 
bc61879
7a54e0d
 
 
bc61879
 
 
 
1351a87
 
bc61879
1351a87
 
 
 
 
 
 
 
bc61879
 
1351a87
 
bc61879
1351a87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc61879
1351a87
 
bc61879
1351a87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc61879
 
 
da3f47f
bc61879
 
 
 
 
da3f47f
bc61879
 
da3f47f
 
 
 
 
 
bc61879
da3f47f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc61879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da3f47f
da1d68a
 
 
bc61879
 
da1d68a
bc61879
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da3f47f
bc61879
 
 
 
 
 
 
 
1351a87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc61879
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
import os
from datetime import datetime
import json
from huggingface_hub import HfApi
import gradio as gr
import csv
import pandas as pd
import io
from typing import TypedDict, List
from climateqa.constants import DOCUMENT_METADATA_DEFAULT_VALUES
from langchain_core.documents import Document

def serialize_docs(docs:list[Document])->list:
    """Convert document objects to a simplified format compatible with Hugging Face datasets.
    
    This function processes document objects by extracting their page content and metadata,
    normalizing the metadata structure to ensure consistency. It applies default values 
    from DOCUMENT_METADATA_DEFAULT_VALUES for any missing metadata fields.
    
    Args:
        docs (list): List of document objects, each with page_content and metadata attributes
        
    Returns:
        list: List of dictionaries with standardized "page_content" and "metadata" fields
    """
    new_docs = []
    for doc in docs:
        # Make sure we have a clean doc format
        new_doc = {
            "page_content": doc.page_content,
            "metadata": {}
        }
        
        # Ensure all metadata fields exist with defaults if missing
        for field, default_value in DOCUMENT_METADATA_DEFAULT_VALUES.items():
            new_value =  doc.metadata.get(field, default_value)
            try:
                new_doc["metadata"][field] = type(default_value)(new_value)
            except:
                new_doc["metadata"][field] = default_value

        new_docs.append(new_doc)
        
    if new_docs == []:
        new_docs = [{"page_content": "No documents found", "metadata": DOCUMENT_METADATA_DEFAULT_VALUES}]
    return new_docs

## AZURE LOGGING - DEPRECATED

def log_on_azure(file, logs, share_client):
    """Log data to Azure Blob Storage.
    
    Args:
        file (str): Name of the file to store logs
        logs (dict): Log data to store
        share_client: Azure share client instance
    """
    logs = json.dumps(logs)
    file_client = share_client.get_file_client(file)
    file_client.upload_file(logs)


def log_interaction_to_azure(history, output_query, sources, docs, share_client, user_id):
    """Log chat interaction to Azure and Hugging Face.
    
    Args:
        history (list): Chat message history
        output_query (str): Processed query
        sources (list): Knowledge base sources used
        docs (list): Retrieved documents
        share_client: Azure share client instance
        user_id (str): User identifier
    """
    try:
        # Log interaction to Azure if not in local environment
        if os.getenv("GRADIO_ENV") != "local":
            timestamp = str(datetime.now().timestamp())
            prompt = history[1]["content"]
            logs = {
                "user_id": str(user_id),
                "prompt": prompt,
                "query": prompt,
                "question": output_query,
                "sources": sources,
                "docs": serialize_docs(docs),
                "answer": history[-1].content,
                "time": timestamp,
            }
            # Log to Azure
            log_on_azure(f"{timestamp}.json", logs, share_client)
    except Exception as e:
        print(f"Error logging on Azure Blob Storage: {e}")
        error_msg = f"ClimateQ&A Error: {str(e)[:100]} - The error has been noted, try another question and if the error remains, you can contact us :)"
        raise gr.Error(error_msg)
    
def log_drias_interaction_to_azure(query, sql_query, data, share_client, user_id):
    """Log Drias data interaction to Azure and Hugging Face.
    
    Args:
        query (str): User query
        sql_query (str): SQL query used
        data: Retrieved data
        share_client: Azure share client instance
        user_id (str): User identifier
    """
    try:
        # Log interaction to Azure if not in local environment
        if os.getenv("GRADIO_ENV") != "local":
            timestamp = str(datetime.now().timestamp())
            logs = {
                "user_id": str(user_id),
                "query": query,
                "sql_query": sql_query,
                "time": timestamp,
            }
            log_on_azure(f"drias_{timestamp}.json", logs, share_client)
            print(f"Logged Drias interaction to Azure Blob Storage: {logs}")
        else:
            print("share_client or user_id is None, or GRADIO_ENV is local")
    except Exception as e:
        print(f"Error logging Drias interaction on Azure Blob Storage: {e}")
        error_msg = f"Drias Error: {str(e)[:100]} - The error has been noted, try another question and if the error remains, you can contact us :)"
        raise gr.Error(error_msg)    
    
## HUGGING FACE LOGGING

def log_on_huggingface(log_filename, logs, log_type="chat"):
    """Log data to Hugging Face dataset repository.
    
    Args:
        log_filename (str): Name of the file to store logs
        logs (dict): Log data to store
        log_type (str): Type of log to store
    """
    try:
        if log_type =="chat":   
            # Get Hugging Face token from environment
            hf_token = os.getenv("HF_LOGS_TOKEN")
            if not hf_token:
                print("HF_LOGS_TOKEN not found in environment variables")
                return

            # Get repository name from environment or use default
            repo_id = os.getenv("HF_DATASET_REPO", "Ekimetrics/climateqa_logs")
        
        elif log_type =="drias":
            # Get Hugging Face token from environment
            hf_token = os.getenv("HF_LOGS_DRIAS_TOKEN")
            if not hf_token:
                print("HF_LOGS_DRIAS_TOKEN not found in environment variables")
                return

            # Get repository name from environment or use default
            repo_id = os.getenv("HF_DATASET_REPO_DRIAS", "Ekimetrics/climateqa_logs_talk_to_data")

        else:
            raise ValueError(f"Invalid log type: {log_type}")
        
        # Initialize HfApi
        api = HfApi(token=hf_token)
        
        # Add timestamp to the log data
        logs["timestamp"] = datetime.now().strftime("%Y%m%d_%H%M%S_%f")
        
        # Convert logs to JSON string
        logs_json = json.dumps(logs)
        
        # Upload directly from memory
        api.upload_file(
            path_or_fileobj=logs_json.encode('utf-8'),
            path_in_repo=log_filename,
            repo_id=repo_id,
            repo_type="dataset"
        )
            
    except Exception as e:
        print(f"Error logging to Hugging Face: {e}")

    
def log_interaction_to_huggingface(history, output_query, sources, docs, share_client, user_id):
    """Log chat interaction to Hugging Face.
    
    Args:
        history (list): Chat message history
        output_query (str): Processed query
        sources (list): Knowledge base sources used
        docs (list): Retrieved documents
        share_client: Azure share client instance (unused in this function)
        user_id (str): User identifier
    """
    try:
        # Log interaction if not in local environment
        if os.getenv("GRADIO_ENV") != "local":
            timestamp = str(datetime.now().timestamp())
            prompt = history[1]["content"]
            logs = {
                "user_id": str(user_id),
                "prompt": prompt,
                "query": prompt,
                "question": output_query,
                "sources": sources,
                "docs": serialize_docs(docs),
                "answer": history[-1].content,
                "time": timestamp,
            }
            # Log to Hugging Face
            log_on_huggingface(f"chat/{timestamp}.json", logs, log_type="chat")
            print(f"Logged interaction to Hugging Face")
        else:
            print("Did not log to Hugging Face because GRADIO_ENV is local")
    except Exception as e:
        print(f"Error logging to Hugging Face: {e}")
        error_msg = f"ClimateQ&A Error: {str(e)[:100]})"
        raise gr.Error(error_msg)

def log_drias_interaction_to_huggingface(query, sql_query, user_id):
    """Log Drias data interaction to Hugging Face.
    
    Args:
        query (str): User query
        sql_query (str): SQL query used
        data: Retrieved data
        user_id (str): User identifier
    """
    try:
        if os.getenv("GRADIO_ENV") != "local":
            timestamp = str(datetime.now().timestamp())
            logs = {
                "user_id": str(user_id),
                "query": query,
                "sql_query": sql_query,
                "time": timestamp,
            }
            log_on_huggingface(f"drias/drias_{timestamp}.json", logs, log_type="drias")
            print(f"Logged Drias interaction to Hugging Face: {logs}")
        else:
            print("share_client or user_id is None, or GRADIO_ENV is local")
    except Exception as e:
        print(f"Error logging Drias interaction to Hugging Face: {e}")
        error_msg = f"Drias Error: {str(e)[:100]} - The error has been noted, try another question and if the error remains, you can contact us :)"
        raise gr.Error(error_msg)

def log_interaction(history, output_query, sources, docs, share_client, user_id):
    """Log chat interaction to Hugging Face, and fall back to Azure if that fails.
    
    Args:
        history (list): Chat message history
        output_query (str): Processed query
        sources (list): Knowledge base sources used
        docs (list): Retrieved documents
        share_client: Azure share client instance
        user_id (str): User identifier
    """
    try:
        # First try to log to Hugging Face
        log_interaction_to_huggingface(history, output_query, sources, docs, share_client, user_id)
    except Exception as e:
        print(f"Failed to log to Hugging Face, falling back to Azure: {e}")
        try:
            # Fall back to Azure logging
            if os.getenv("GRADIO_ENV") != "local":
                timestamp = str(datetime.now().timestamp())
                prompt = history[1]["content"]
                logs = {
                    "user_id": str(user_id),
                    "prompt": prompt,
                    "query": prompt,
                    "question": output_query,
                    "sources": sources,
                    "docs": serialize_docs(docs),
                    "answer": history[-1].content,
                    "time": timestamp,
                }
                # Log to Azure
                log_on_azure(f"{timestamp}.json", logs, share_client)
                print("Successfully logged to Azure as fallback")
        except Exception as azure_error:
            print(f"Error in Azure fallback logging: {azure_error}")
            error_msg = f"ClimateQ&A Logging Error: {str(azure_error)[:100]})"
            # Don't raise error to avoid disrupting user experience
            print(error_msg)