timeki commited on
Commit
c6723cc
·
2 Parent(s): aa3b6c4 aa3e1e8

Merged in dev (pull request #16)

Browse files
Files changed (2) hide show
  1. climateqa/chat.py +2 -2
  2. climateqa/logging.py +108 -68
climateqa/chat.py CHANGED
@@ -14,7 +14,7 @@ from .handle_stream_events import (
14
  handle_retrieved_owid_graphs,
15
  )
16
  from .logging import (
17
- log_interaction_to_huggingface
18
  )
19
 
20
  # Chat functions
@@ -189,6 +189,6 @@ async def chat_stream(
189
  raise gr.Error(str(e))
190
 
191
  # Call the function to log interaction
192
- log_interaction_to_huggingface(history, output_query, sources, docs, share_client, user_id)
193
 
194
  yield history, docs_html, output_query, output_language, related_contents, graphs_html, follow_up_examples#, vanna_data
 
14
  handle_retrieved_owid_graphs,
15
  )
16
  from .logging import (
17
+ log_interaction
18
  )
19
 
20
  # Chat functions
 
189
  raise gr.Error(str(e))
190
 
191
  # Call the function to log interaction
192
+ log_interaction(history, output_query, sources, docs, share_client, user_id)
193
 
194
  yield history, docs_html, output_query, output_language, related_contents, graphs_html, follow_up_examples#, vanna_data
climateqa/logging.py CHANGED
@@ -47,80 +47,80 @@ def serialize_docs(docs:list[Document])->list:
47
 
48
  ## AZURE LOGGING - DEPRECATED
49
 
50
- # def log_on_azure(file, logs, share_client):
51
- # """Log data to Azure Blob Storage.
52
 
53
- # Args:
54
- # file (str): Name of the file to store logs
55
- # logs (dict): Log data to store
56
- # share_client: Azure share client instance
57
- # """
58
- # logs = json.dumps(logs)
59
- # file_client = share_client.get_file_client(file)
60
- # file_client.upload_file(logs)
61
 
62
 
63
- # def log_interaction_to_azure(history, output_query, sources, docs, share_client, user_id):
64
- # """Log chat interaction to Azure and Hugging Face.
65
 
66
- # Args:
67
- # history (list): Chat message history
68
- # output_query (str): Processed query
69
- # sources (list): Knowledge base sources used
70
- # docs (list): Retrieved documents
71
- # share_client: Azure share client instance
72
- # user_id (str): User identifier
73
- # """
74
- # try:
75
- # # Log interaction to Azure if not in local environment
76
- # if os.getenv("GRADIO_ENV") != "local":
77
- # timestamp = str(datetime.now().timestamp())
78
- # prompt = history[1]["content"]
79
- # logs = {
80
- # "user_id": str(user_id),
81
- # "prompt": prompt,
82
- # "query": prompt,
83
- # "question": output_query,
84
- # "sources": sources,
85
- # "docs": serialize_docs(docs),
86
- # "answer": history[-1].content,
87
- # "time": timestamp,
88
- # }
89
- # # Log to Azure
90
- # log_on_azure(f"{timestamp}.json", logs, share_client)
91
- # except Exception as e:
92
- # print(f"Error logging on Azure Blob Storage: {e}")
93
- # 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 :)"
94
- # raise gr.Error(error_msg)
95
 
96
- # def log_drias_interaction_to_azure(query, sql_query, data, share_client, user_id):
97
- # """Log Drias data interaction to Azure and Hugging Face.
98
 
99
- # Args:
100
- # query (str): User query
101
- # sql_query (str): SQL query used
102
- # data: Retrieved data
103
- # share_client: Azure share client instance
104
- # user_id (str): User identifier
105
- # """
106
- # try:
107
- # # Log interaction to Azure if not in local environment
108
- # if os.getenv("GRADIO_ENV") != "local":
109
- # timestamp = str(datetime.now().timestamp())
110
- # logs = {
111
- # "user_id": str(user_id),
112
- # "query": query,
113
- # "sql_query": sql_query,
114
- # "time": timestamp,
115
- # }
116
- # log_on_azure(f"drias_{timestamp}.json", logs, share_client)
117
- # print(f"Logged Drias interaction to Azure Blob Storage: {logs}")
118
- # else:
119
- # print("share_client or user_id is None, or GRADIO_ENV is local")
120
- # except Exception as e:
121
- # print(f"Error logging Drias interaction on Azure Blob Storage: {e}")
122
- # 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 :)"
123
- # raise gr.Error(error_msg)
124
 
125
  ## HUGGING FACE LOGGING
126
 
@@ -240,4 +240,44 @@ def log_drias_interaction_to_huggingface(query, sql_query, user_id):
240
  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 :)"
241
  raise gr.Error(error_msg)
242
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
 
 
47
 
48
  ## AZURE LOGGING - DEPRECATED
49
 
50
+ def log_on_azure(file, logs, share_client):
51
+ """Log data to Azure Blob Storage.
52
 
53
+ Args:
54
+ file (str): Name of the file to store logs
55
+ logs (dict): Log data to store
56
+ share_client: Azure share client instance
57
+ """
58
+ logs = json.dumps(logs)
59
+ file_client = share_client.get_file_client(file)
60
+ file_client.upload_file(logs)
61
 
62
 
63
+ def log_interaction_to_azure(history, output_query, sources, docs, share_client, user_id):
64
+ """Log chat interaction to Azure and Hugging Face.
65
 
66
+ Args:
67
+ history (list): Chat message history
68
+ output_query (str): Processed query
69
+ sources (list): Knowledge base sources used
70
+ docs (list): Retrieved documents
71
+ share_client: Azure share client instance
72
+ user_id (str): User identifier
73
+ """
74
+ try:
75
+ # Log interaction to Azure if not in local environment
76
+ if os.getenv("GRADIO_ENV") != "local":
77
+ timestamp = str(datetime.now().timestamp())
78
+ prompt = history[1]["content"]
79
+ logs = {
80
+ "user_id": str(user_id),
81
+ "prompt": prompt,
82
+ "query": prompt,
83
+ "question": output_query,
84
+ "sources": sources,
85
+ "docs": serialize_docs(docs),
86
+ "answer": history[-1].content,
87
+ "time": timestamp,
88
+ }
89
+ # Log to Azure
90
+ log_on_azure(f"{timestamp}.json", logs, share_client)
91
+ except Exception as e:
92
+ print(f"Error logging on Azure Blob Storage: {e}")
93
+ 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 :)"
94
+ raise gr.Error(error_msg)
95
 
96
+ def log_drias_interaction_to_azure(query, sql_query, data, share_client, user_id):
97
+ """Log Drias data interaction to Azure and Hugging Face.
98
 
99
+ Args:
100
+ query (str): User query
101
+ sql_query (str): SQL query used
102
+ data: Retrieved data
103
+ share_client: Azure share client instance
104
+ user_id (str): User identifier
105
+ """
106
+ try:
107
+ # Log interaction to Azure if not in local environment
108
+ if os.getenv("GRADIO_ENV") != "local":
109
+ timestamp = str(datetime.now().timestamp())
110
+ logs = {
111
+ "user_id": str(user_id),
112
+ "query": query,
113
+ "sql_query": sql_query,
114
+ "time": timestamp,
115
+ }
116
+ log_on_azure(f"drias_{timestamp}.json", logs, share_client)
117
+ print(f"Logged Drias interaction to Azure Blob Storage: {logs}")
118
+ else:
119
+ print("share_client or user_id is None, or GRADIO_ENV is local")
120
+ except Exception as e:
121
+ print(f"Error logging Drias interaction on Azure Blob Storage: {e}")
122
+ 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 :)"
123
+ raise gr.Error(error_msg)
124
 
125
  ## HUGGING FACE LOGGING
126
 
 
240
  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 :)"
241
  raise gr.Error(error_msg)
242
 
243
+ def log_interaction(history, output_query, sources, docs, share_client, user_id):
244
+ """Log chat interaction to Hugging Face, and fall back to Azure if that fails.
245
+
246
+ Args:
247
+ history (list): Chat message history
248
+ output_query (str): Processed query
249
+ sources (list): Knowledge base sources used
250
+ docs (list): Retrieved documents
251
+ share_client: Azure share client instance
252
+ user_id (str): User identifier
253
+ """
254
+ try:
255
+ # First try to log to Hugging Face
256
+ log_interaction_to_huggingface(history, output_query, sources, docs, share_client, user_id)
257
+ except Exception as e:
258
+ print(f"Failed to log to Hugging Face, falling back to Azure: {e}")
259
+ try:
260
+ # Fall back to Azure logging
261
+ if os.getenv("GRADIO_ENV") != "local":
262
+ timestamp = str(datetime.now().timestamp())
263
+ prompt = history[1]["content"]
264
+ logs = {
265
+ "user_id": str(user_id),
266
+ "prompt": prompt,
267
+ "query": prompt,
268
+ "question": output_query,
269
+ "sources": sources,
270
+ "docs": serialize_docs(docs),
271
+ "answer": history[-1].content,
272
+ "time": timestamp,
273
+ }
274
+ # Log to Azure
275
+ log_on_azure(f"{timestamp}.json", logs, share_client)
276
+ print("Successfully logged to Azure as fallback")
277
+ except Exception as azure_error:
278
+ print(f"Error in Azure fallback logging: {azure_error}")
279
+ error_msg = f"ClimateQ&A Logging Error: {str(azure_error)[:100]})"
280
+ # Don't raise error to avoid disrupting user experience
281
+ print(error_msg)
282
+
283