Spaces:
Running
Running
File size: 8,535 Bytes
6560820 |
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 |
# This file will be used to store the interim conversational state of the doctor and patient and then store the various diagnostic results and the final diagnosis and next best action.
# To store the data we will use Azure blob store and each file will be stored as a json file with the conversation id as the file name.
# Steps
# 1. Create a class to store the conversation state
# 2. Create a function to check if this is a new conversation or an existing conversation
# 3. Create a function to count interactions in the conversation if existing exists
# 4. Create a function to store the conversation in Azure blob storage
# 5. Create a function to retrieve the conversation from Azure blob storage
# 6. Create a function to update the conversation in Azure blob storage
# 7. Create a function to delete the conversation from Azure blob storage
# Import the required libraries
import os
import json
from azure.storage.blob import BlobServiceClient, BlobClient, ContainerClient
# Azure connection string
connect_str = "DefaultEndpointsProtocol=https;AccountName=chatlogs;AccountKey=7tiHxmMbbdEp3/yrydWOEoAc7PPDkVHXV5QXZWR1jTH0hX8/O2gIrSHwMNqXwEnEwWWAD8pqIwuV+AStWqHVNA==;EndpointSuffix=core.windows.net"
container_name = "vme25"
# Create a class to store the conversation state
class Conversation:
def __init__(self, conversation_id, patient, conversation):
self.conversation_id = conversation_id
self.patient = patient
self.conversation = conversation
# Create a function to check if this is a new conversation or an existing conversation
def check_conversation(conversation_id):
# Create a blob service client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(conversation_id)
if blob_client.exists():
return True
else:
return False
# Create a function to count interactions in the conversation if existing exists
def count_interactions(conversation_id):
# Create a blob service client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(conversation_id)
# Retrieve the existing conversation
conversation_json = blob_client.download_blob().readall()
conversation_obj = json.loads(conversation_json)
# Count the number of user interactions
user_interactions = sum(1 for message in conversation_obj['conversation'] if message['role'] == 'user')
return user_interactions
# Create a function to store the conversation in Azure blob storage
def initail_conversation(conversation_id, patient, from_user, response_from_ai):
# Create a blob service client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(conversation_id)
# Initialize the conversation with the first user and assistant messages
conversation = [
{'role': 'user', 'content': from_user},
{'role': 'assistant', 'content': response_from_ai}
]
conversation_obj = Conversation(conversation_id, patient, conversation)
conversation_json = json.dumps(conversation_obj.__dict__)
blob_client.upload_blob(conversation_json)
# Create a function to retrieve the conversation from Azure blob storage
def retrieve_conversation(conversation_id):
print("Retrieving conversation")
# Create a blob service client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(conversation_id)
conversation_json = blob_client.download_blob().readall()
conversation_obj = json.loads(conversation_json)
return conversation_obj
# Create a function to update the conversation in Azure blob storage
def update_conversation(conversation_id, from_user, response_from_ai):
paitent = "patient"
print("Updating conversation")
# Create a blob service client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(conversation_id)
# Retrieve the existing conversation
conversation_obj = retrieve_conversation(conversation_id)
# Update the conversation with new messages
conversation_obj['conversation'].append({'role': 'user', 'content': from_user})
conversation_obj['conversation'].append({'role': 'assistant', 'content': response_from_ai})
# Convert the updated conversation to JSON
conversation_json = json.dumps(conversation_obj)
# Upload the updated conversation, overwriting the existing blob
blob_client.upload_blob(conversation_json, overwrite=True)
return conversation_json
def write_diagnosis(conversation_id, diagnosis):
# Create a blob service client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(conversation_id)
# Retrieve the existing conversation
conversation_obj = retrieve_conversation(conversation_id)
# Update the conversation with new messages
conversation_obj['conversation'].append({'role': 'diagnosis', 'content': diagnosis})
# Convert the updated conversation to JSON
conversation_json = json.dumps(conversation_obj)
# Upload the updated conversation, overwriting the existing blob
blob_client.upload_blob(conversation_json, overwrite=True)
# Create a function to delete the conversation from Azure blob storage
def delete_conversation(conversation_id):
# Create a blob service client
blob_service_client = BlobServiceClient.from_connection_string(connect_str)
container_client = blob_service_client.get_container_client(container_name)
blob_client = container_client.get_blob_client(conversation_id)
blob_client.delete_blob()
def store_conversation(conversation_id, from_user, response_from_ai):
patient = "patient"
# Create a blob service client
try:
print("Storing conversation")
res_check = check_conversation(conversation_id)
print(res_check)
if res_check == False:
print("Conversation doesn't exists, starting new conversation")
initail_conversation(conversation_id, patient, from_user, response_from_ai)
print("Conversation stored")
else:
update_conversation(conversation_id, patient, from_user, response_from_ai)
print("Conversation updated")
except Exception as e:
return "FAIL", str(e)
def get_conversation(conversation_id):
# Create a blob service client
try:
res_check = check_conversation(conversation_id)
if res_check == True:
print("Conversation exists")
count = count_interactions(conversation_id)
conversation_obj = retrieve_conversation(conversation_id)
return "PASS", count, conversation_obj
else:
print("Conversation doesn't exists")
return "FAIL", 0, "Conversation does not exist"
except Exception as e:
print("an error occured")
return "FAIL", str(e)
# # Test the functions
# conversation_id = "12345623"
# patient = "John Doe"
# from_user = "I do feel a bit sick"
# response_from_ai = "thank you for this information"
# # print(get_conversation(conversation_id))
# store_conversation(conversation_id, patient, from_user, response_from_ai)
#print(check_conversation(conversation_id))
# print(count_interactions(conversation_id))
# conversation_obj = retrieve_conversation(conversation_id)
# print(conversation_obj)
# update_conversation(conversation_id, patient, "What is your name?", "I am an AI assistant.")
# conversation_obj = retrieve_conversation(conversation_id)
# print(conversation_obj)
# delete_conversation(conversation_id) |