File size: 2,028 Bytes
35c9187 d98ba57 35c9187 dfcff8d cc2ce8c d98ba57 35c9187 6e28a81 35c9187 |
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 |
import datetime
import json
import os
def log(question, history, docs, user_id):
if has_blob_config():
log_in_azure(question, history, docs, user_id)
pass
def has_blob_config():
"""
Checks if the necessary environment variables for Azure Blob Storage are set.
Returns True if they are set, False otherwise.
"""
return all(
key in os.environ
for key in ["BLOB_ACCOUNT_KEY", "BLOB_ACCOUNT_NAME", "BLOB_ACCOUNT_URL"]
)
def log_in_azure(question, history, docs, user_id):
timestamp = str(datetime.now().timestamp())
file_name = timestamp + ".json"
prompt = history[-1][0]
logs = {
"user_id": str(user_id),
"prompt": prompt,
"query": prompt,
"question": question,
"docs": docs,
"answer": history[-1][1],
"time": timestamp,
}
upload_azure(file_name, logs)
def get_azure_blob_client():
account_key = os.environ["BLOB_ACCOUNT_KEY"]
if len(account_key) == 86:
account_key += "=="
credential = {
"account_key": account_key,
"account_name": os.environ["BLOB_ACCOUNT_NAME"],
}
account_url = os.environ["BLOB_ACCOUNT_URL"]
file_share_name = "anything-question-answering"
# I don't know why this is necessary, but it cause an error otherwise when running build_index.py
from azure.storage.fileshare import ShareServiceClient
service = ShareServiceClient(account_url=account_url, credential=credential)
share_client = service.get_share_client(file_share_name)
return share_client
if has_blob_config():
share_client = get_azure_blob_client()
def upload_azure(file, logs):
logs = json.dumps(logs)
print(type(logs))
assert share_client is not None
file_client = share_client.get_file_client(file)
print("Uploading logs to Azure Blob Storage")
print("----------------------------------")
print("")
print(logs)
file_client.upload_file(logs)
print("Logs uploaded to Azure Blob Storage")
|