When you deploy a chatbot service on a website, users may enter inappropriate or harmful messages that can lead to unwanted responses from the chatbot. This can pose significant risks, including the potential for revealing sensitive company information or allowing other users' messages to influence the chatbot's responses. That's why it's critical to implement a content filtering mechanism that can inspect messages from users. Azure Content Safety provides a robust solution for inspecting and filtering inappropriate content, ensuring safe and secure interactions. This tutorial is ideal for anyone who wants to build a chatbot service with strong content moderation capabilities. In this tutorial, you will learn how to build a chatbot service that interacts with users using Azure Cosmos DB, Azure Content Safety, and Azure OpenAI. This service provides the following features:
Here is an overview of this tutorial.
This tutorial is related to the following topics:
To store and load your chat history, you need to create an Azure Cosmos DB resource.
In this exercise, you will:
Type cosmos db in the search bar at the top of the portal page and select Azure Cosmos DB from the options that appear.
Select + Create from the navigation menu.
Select Azure Cosmos DB for NoSQL from the navigation menu.
Perform the following tasks:
Select Review + Create.
Select Create.
Navigate to the Azure CosmosDB resource that you created.
Select Data Explorer from the left side tab.
Select New Container from the navigation menu.
Perform the following tasks:
Select OK.
To create a service to detect inappropriate content, you need to create an Azure Content Safety resource.
In this exercise, you will:
Type content safety in the search bar at the top of the portal page and select Content safety from the options that appear.
Select + Create from the navigation menu.
Perform the following tasks:
Select Review + Create.
Select Create.
To enable your chat service to provide answers based on chat history stored in Azure Cosmos DB, you need to create and deploy an Azure OpenAI resource.
In this exercise, you will:
Note
Access to the Azure OpenAI service is currently available by request. To request access, please fill out the form on the Azure OpenAI request page.
Type azure openai in the search bar at the top of the portal page and select Azure OpenAI from the options that appear.
Select + Create from the navigation menu.
Perform the following tasks:
Note
To minimize costs, try to create all the resources in the same region.
Select Next to move to the Network page.
Select a network security Type.
Select Next to move to the Tags page.
Select Next to move to the Review + submit page.
Select Create.
Navigate to the Azure OpenAI resource that you created.
Select Go to Azure OpenAI Studio from the navigation menu.
Inside Azure OpenAI Studio, select Deployments from the left side tab.
Select + Create new deployment from the navigation menu to create a new gpt-35-turbo deployment.
Perform the following tasks:
Select Create.
Now you've learned how to set up Azure resources to implement features that allow the Azure Content Safety resource to analyze conversations and Azure OpenAI to generate responses. In the next exercise, you will develop a Python program that interacts with users to ensure safe conversations.
Now, you will create a folder to work in and set up a virtual environment to develop a program.
In this exercise, you will
Open a terminal window and type the following command to create a folder named safety-chatbot in the default path.
mkdir safety-chatbot
Type the following command inside your terminal to navigate to the safety-chatbot folder you created.
cd safety-chatbot
Type the following command inside your terminal to create a virtual environment named .venv.
python -m venv .venv
Type the following command inside your terminal to activate the virtual environment.
.venv\Scripts\activate.bat
Note
If it worked, you should see (.venv) before the command prompt.
Type the following commands inside your terminal to install the required packages.
pip install azure-cosmos==4.6.0
pip install azure-ai-contentsafety==1.0.0
pip install semantic-kernel==0.9.7b1
To develop a program that uses the Azure resources that you created, you need config.py file to enter Azure information.
In this exercise, you will:
Open Visual Studio Code.
Select File from the menu bar.
Select Open Folder.
Select the safety-chatbot folder that you created, which is located at C:\Users\yourUserName\safety-chatbot.
In the left pane of Visual Studio Code, right-click and select New File to create a new file named example.py.
Add the following code to the example.py file to import the required libraries.
# Library imports
import asyncio
from datetime import datetime, timezone
from concurrent.futures import ThreadPoolExecutor
# Azure imports
from azure.ai.contentsafety.aio import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
from azure.cosmos import CosmosClient
# Semantic Kernel imports
import semantic_kernel as sk
import semantic_kernel.connectors.ai.open_ai as sk_oai
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.prompt_template import PromptTemplateConfig
from semantic_kernel.prompt_template.input_variable import InputVariable
from semantic_kernel.functions.kernel_arguments import KernelArguments
In the left pane of Visual Studio Code, right-click and select New File to create a new file named config.py.
Note
Complete folder structure: └── YourUserName └── safety-chatbot ├── example.py └── config.py
Add the following code to the config.py file to include your Azure information.
# Azure Cosmos DB settings
AZURE_COSMOSDB_ACCOUNT_NAME = 'your-cosmosdb-account-name' # 'safetychatbot-storage'
AZURE_COSMOSDB_DATABASE_NAME = 'your-cosmosdb-database-name' # 'safetychatbot-database'
AZURE_COSMOSDB_CONTAINER_NAME = 'your-cosmosdb-container-name' # 'safetychatbot-container'
AZURE_COSMOSDB_ENDPOINT = f'https://{AZURE_COSMOSDB_ACCOUNT_NAME.lower()}.documents.azure.com:443/'
AZURE_COSMOSDB_KEY = 'your-cosmosdb-key'
# Azure Content Safety settings
AZURE_CONTENT_SAFETY_NAME = 'your-content-safety-name' # 'safetychatbot-contentsafety'
AZURE_CONTENT_SAFETY_ENDPOINT = f'https://{AZURE_CONTENT_SAFETY_NAME.lower()}.cognitiveservices.azure.com/'
AZURE_CONTENT_SAFETY_KEY = 'your-content-safety-key'
# Azure OpenAI settings
AZURE_OPENAI_NAME = 'safetychatbot-openai'
AZURE_OPENAI_ENDPOINT = f'https://{AZURE_OPENAI_NAME.lower()}.openai.azure.com/'
AZURE_OPENAI_KEY = 'your-openai-key'
AZURE_OPENAI_API_VERSION = 'your-API-version' # '2023-08-01-preview'
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME = 'your_chat_deployment_name' # 'gpt-35-turbo'
Perform the following tasks to add the Azure Cosmos DB account name:
Perform the following tasks to add the Azure Cosmos DB database name:
Perform the following tasks to add the Azure Cosmos DB container name:
Perform the following tasks to add the Azure Cosmos DB key:
Perform the following tasks to add the Azure Content Safety name:
Perform the following tasks to add the Azure Content Safety key:
Perform the following tasks to add the Azure OpenAI name:
Perform the following tasks to add the Azure OpenAI key:
Perform the following task to add the Azure OpenAI API versions:
Select the appropriate Azure OpenAI API version. you can refer to the Azure OpenAI Service REST API reference documents.
Note
In this tutorial, you use the 2024-02-15-preview version of the Azure OpenAI API.
Copy and paste your Azure OpenAI API versions into the config.py file.
To implement the ChatbotService for handling chat interactions and Azure services, you need to import the Azure information from the config.py file into the example.py file and implement a class that efficiently orchestrates these functionalities.
In this exercise, you will:
Note
The complete code for this tutorial is provided at the end to make it easy to connect the pieces and understand the overall implementation.
Add the following code to the example.py file to import the values from config.py file.
# Configuration imports
from config import (
AZURE_COSMOSDB_DATABASE_NAME,
AZURE_COSMOSDB_CONTAINER_NAME,
AZURE_COSMOSDB_ENDPOINT,
AZURE_COSMOSDB_KEY,
AZURE_CONTENT_SAFETY_ENDPOINT,
AZURE_CONTENT_SAFETY_KEY,
AZURE_OPENAI_ENDPOINT,
AZURE_OPENAI_KEY,
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME
)
Add the following code to the example.py file to set up the ChatbotService
class. This class initializes Azure Cosmos DB for NoSQL, which is a NoSQL database service that handles database operations synchronously.
class ChatbotService:
def __init__(self, loop, user_id):
"""
Initialize the ChatbotService with event loop, user ID, and necessary Azure CosmosDB clients.
"""
# Set up the event loop and thread pool executor for managing asynchronous tasks,
# allowing Azure Cosmos DB operations to be handled in an asynchronous environment
self.loop = loop
self.executor = ThreadPoolExecutor()
# Store the user ID, which is used as the partition key (/userId) in Azure CosmosDB
self.user_id = user_id
# Initialize the Azure Cosmos DB client
self.cosmos_client = CosmosClient(AZURE_COSMOSDB_ENDPOINT, credential=AZURE_COSMOSDB_KEY)
# Initialize the Azure Cosmos DB database client
self.database = self.cosmos_client.get_database_client(AZURE_COSMOSDB_DATABASE_NAME)
# Initialize the Azure Cosmos DB container client
self.container = self.database.get_container_client(AZURE_COSMOSDB_CONTAINER_NAME)
Add the following code to the example.py file to initialize the Azure Content Safety client and the Semantic Kernel within the ChatbotService
class. This step sets up the asynchronous operations required for Content Safety analysis and chatbot interactions.
async def init(self):
"""
Initialize the Content Safety client and Semantic Kernel.
"""
# Initialize the Azure Content Safety client
self.content_safety_client = ContentSafetyClient(AZURE_CONTENT_SAFETY_ENDPOINT, AzureKeyCredential(AZURE_CONTENT_SAFETY_KEY))
# Initialize the Semantic Kernel
self.kernel = sk.Kernel()
# Initialize the chat service for Azure OpenAI
self.chat_service = AzureChatCompletion(
service_id='chat_service',
deployment_name=AZURE_OPENAI_CHAT_DEPLOYMENT_NAME,
endpoint=AZURE_OPENAI_ENDPOINT,
api_key=AZURE_OPENAI_KEY
)
# Add the chat service to the Semantic Kernel
self.kernel.add_service(self.chat_service)
# Define the prompt template configuration for the chatbot
self.prompt_template_config = PromptTemplateConfig(
template="""ChatBot can have a conversation with you about any topic.
It can give explicit instructions or say 'I don't know' if it does not have an answer.
{{$history}}
User: {{$user_message}}
ChatBot: """,
name='chat_prompt_template',
template_format='semantic-kernel',
input_variables=[
InputVariable(name='user_message', description='The user message.', is_required=True),
InputVariable(name='history', description='The conversation history', is_required=True),
],
execution_settings=sk_oai.OpenAIChatPromptExecutionSettings(
service_id='chat_service',
ai_model_id='gpt-3.5-turbo',
max_tokens=500,
temperature=0.7
)
)
# Add the chat function to the Semantic Kernel
self.chat_function = self.kernel.add_function(
function_name="chat_function",
plugin_name="chat_plugin",
prompt_template_config=self.prompt_template_config,
)
return self
Note
Why is the Constructor Split?
When setting up the ChatbotService, the constructor is split into two parts:
__init__
andasync init
. This separation is critical for effectively managing both synchronous and asynchronous operations required by different Azure services. Azure Cosmos DB for NoSQL operates in a synchronous environment, while Azure Content Safety and the Semantic Kernel benefit from asynchronous operations. This requires a clear distinction between synchronous and asynchronous initialization.
To implement the core functions of the ChatbotService for handling chat interactions and integrating with Azure services, you need to add several methods to the class. These methods enable text analysis, chatbot interactions, and conversation history management.
In this exercise, you will:
Add the following code to the ChatbotService
class to create the analyze_text
function that analyze the input text for safety using Azure Content Safety.
async def analyze_text(self, text):
"""
Analyze the input text for safety using Azure Content Safety.
"""
# Create a request with the input text to be analyzed
request = AnalyzeTextOptions(text=text)
try:
# Send the request to the Azure Content Safety client and await the response
response = await self.content_safety_client.analyze_text(request)
# Get the analysis results for different categories
results = {
'hate': next((item for item in response.categories_analysis if item.category == TextCategory.HATE), None),
'self_harm': next((item for item in response.categories_analysis if item.category == TextCategory.SELF_HARM), None),
'sexual': next((item for item in response.categories_analysis if item.category == TextCategory.SEXUAL), None),
'violence': next((item for item in response.categories_analysis if item.category == TextCategory.VIOLENCE), None),
}
# Print content safety analysis results
print("\n<-- Content Safety Analysis Results -->")
for category, result in results.items():
if result:
print(f"{category.capitalize()} severity: {result.severity}")
print("<-- End of Content Safety Analysis Results -->\n")
# Define a threshold for the text to be considered unsafe
threshold = 2
# Based on the threshold, determine if the text is safe
is_safe = not any(result and result.severity >= threshold for result in results.values())
return is_safe, results
except HttpResponseError as e:
# Handle any HTTP response errors that occur during the request
print(f"Failed to analyze text. Error: {e}")
Add the following code to the ChatbotService
class to create the chat_with_kernel
function that interacts with the chatbot using the semantic kernel and history provided.
async def chat_with_kernel(self, user_message, history):
"""
Interact with the chatbot using the Semantic Kernel and provided history.
"""
# Create arguments for the chat function using the user message and conversation history stored in Azure Cosmos DB
arguments = KernelArguments(user_message=user_message, history=history)
# Invoke the chat function in the Semantic Kernel with the provided arguments and await the response
response = await self.kernel.invoke(self.chat_function, arguments)
# Return the chatbot's response
return response
Add the following code to the ChatbotService
class to create the store_interaction
and _store_interaction_sync
functions that store user interactions with the chatbot in Azure Cosmos DB.
async def store_interaction(self, user_message, chat_response):
"""
Store the user interaction with the chatbot in Azure Cosmos DB.
"""
# Run the _store_interaction_sync method in an asynchronous execution environment
await self.loop.run_in_executor(self.executor, self._store_interaction_sync, user_message, chat_response)
def _store_interaction_sync(self, user_message, chat_response):
"""
Synchronously store the interaction in Azure Cosmos DB.
"""
# Get the current time in UTC
current_time = datetime.now(timezone.utc)
# Upsert (insert or update) the interaction data into the Cosmos DB container
self.container.upsert_item({
'id': str(current_time.timestamp()), # Use the current timestamp as a unique ID
'user_message': user_message, # Store the user message
'bot_response': chat_response, # Store the chatbot response
'timestamp': current_time.isoformat(), # Store the timestamp in ISO format
'userId': self.user_id # Store the user ID for partition key
})
Add the following code to the ChatbotService
class to create the load_historical_context
and _load_historical_context_sync
functions that load the user's chat history from Azure Cosmos DB.
async def load_historical_context(self):
"""
Load the user's chat history from Azure Cosmos DB.
"""
# Run the _load_historical_context_sync method in an asynchronous execution environment
return await self.loop.run_in_executor(self.executor, self._load_historical_context_sync)
def _load_historical_context_sync(self):
"""
Synchronously load the user's chat history from Azure Cosmos DB.
"""
# Define the query to select items for the current user, ordered by timestamp
query = "SELECT * FROM c WHERE c.userId = @userId ORDER BY c.timestamp DESC"
parameters = [{"name": "@userId", "value": self.user_id}]
# Execute the query and retrieve the items
items = list(self.container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
# Include only the last 5 conversations
history_items = items[:5]
# Format the conversion history
return "\n".join([f"User: {item['user_message']}\nChatBot: {item['bot_response']}" for item in history_items])
Add the following code to the ChatbotService
class to create the clear_historical_context
and _clear_historical_context_sync
functions that clear the user's chat history from Azure Cosmos DB.
async def clear_historical_context(self):
"""
Clear the user's chat history from Azure Cosmos DB.
"""
# Run the _clear_historical_context_sync method in an asynchronous execution environment
await self.loop.run_in_executor(self.executor, self._clear_historical_context_sync)
def _clear_historical_context_sync(self):
"""
Synchronously clear the user's chat history from Azure Cosmos DB.
"""
# Define the query to select items for the current user
query = "SELECT * FROM c WHERE c.userId = @userId"
parameters = [{"name": "@userId", "value": self.user_id}]
# Execute the query and retrieve the items
items = list(self.container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
# Clear the chat history by deleting all items for the current user
for item in items:
self.container.delete_item(item, partition_key=self.user_id)
In this exercise, you will implement the main function that manages user interactions, analyzes and stores conversation history using Azure Cosmos DB, Azure Content Safety, Azure OpenAI, and the Semantic Kernel.
In this exercise, you will:
Add the following code to create the main function in the example.py file that handles user inputs, manages chat history, and interacts with the chatbot service.
async def main():
"""
Main function to run the chatbot service.
"""
# Enter the User ID to join the chat.
# A conversation history is stored based on the user ID.
user_id = input("User ID: ")
# Get the event loop to allow synchronous operations in Azure Cosmos DB for NoSQL to run asynchronously.
loop = asyncio.get_running_loop()
# Initialize the ChatbotService with the event loop and user ID
chatbot_service = await ChatbotService(loop, user_id).init()
# Main loop for the interaction with the user
while True:
user_message = input("You: ")
# Exit the chat loop if the user types 'exit'
if user_message.lower() == 'exit':
break
elif user_message.lower() == 'history':
# Load and print the chat history if the user types 'history'
history = await chatbot_service.load_historical_context()
print(f"\n<-- Chat history of user ID: {user_id} -->")
print(history)
print(f"<-- End of chat history of user ID: {user_id} -->\n")
elif user_message.lower() == 'clear':
# Clear the chat history if the user types 'clear'
await chatbot_service.clear_historical_context()
print("Chat history cleared.")
else:
# Analyze the text for safety
is_safe, _ = await chatbot_service.analyze_text(user_message)
if is_safe:
# Load chat history and interact with the chatbot if the message is safe
history = await chatbot_service.load_historical_context()
chat_response = await chatbot_service.chat_with_kernel(user_message, history)
print("Chatbot:", chat_response)
# Store the interaction in Cosmos DB
await chatbot_service.store_interaction(user_message, str(chat_response))
else:
# Inform the user if their message is not safe
print("Chatbot: Your message is not safe to process. Please rephrase and try again.")
if __name__ == "__main__":
asyncio.run(main())
Type the following command inside your terminal to run the program and see if it can answer questions.
python example.py
This will start the chatbot service, prompting you to enter a User ID and interact with the chatbot. You can enter exit to end the session, history to view past interactions, and clear to current user's chat history. Here's an example of the results.
Congratulations! You've successfully learned how to integrate Azure Content Safety with Azure OpenAI. In this tutorial, you have navigated through a practical journey of integrating Azure Cosmos DB, Azure Content Safety, and Azure OpenAI to create a robust chatbot service. By leveraging these Azure services, you now have a chatbot that can safely and effectively interact with users, analyze and store conversations, and ensure content safety.
Cleanup your Azure resources to avoid additional charges to your account. Go to the Azure portal and delete the following resources:
Note
This exercise is designed to provide the complete code used in the tutorial. It is a separate exercise from the rest of the tutorial.
config.py
# Azure Cosmos DB settings
AZURE_COSMOSDB_ACCOUNT_NAME = 'your-cosmosdb-account-name' # 'safetychatbot-storage'
AZURE_COSMOSDB_DATABASE_NAME = 'your-cosmosdb-database-name' # 'safetychatbot-database'
AZURE_COSMOSDB_CONTAINER_NAME = 'your-cosmosdb-container-name' # 'safetychatbot-container'
AZURE_COSMOSDB_ENDPOINT = f'https://{AZURE_COSMOSDB_ACCOUNT_NAME.lower()}.documents.azure.com:443/'
AZURE_COSMOSDB_KEY = 'your-cosmosdb-key'
# Azure Content Safety settings
AZURE_CONTENT_SAFETY_NAME = 'your-content-safety-name' # 'safetychatbot-contentsafety'
AZURE_CONTENT_SAFETY_ENDPOINT = f'https://{AZURE_CONTENT_SAFETY_NAME.lower()}.cognitiveservices.azure.com/'
AZURE_CONTENT_SAFETY_KEY = 'your-content-safety-key'
# Azure OpenAI settings
AZURE_OPENAI_NAME = 'safetychatbot-openai'
AZURE_OPENAI_ENDPOINT = f'https://{AZURE_OPENAI_NAME.lower()}.openai.azure.com/'
AZURE_OPENAI_KEY = 'your-openai-key'
AZURE_OPENAI_API_VERSION = 'your-API-version' # '2024-02-15-preview'
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME = 'your_chat_deployment_name' # 'gpt-35-turbo'
example.py
# Library imports
import asyncio
from datetime import datetime, timezone
from concurrent.futures import ThreadPoolExecutor
# Azure imports
from azure.ai.contentsafety.aio import ContentSafetyClient
from azure.ai.contentsafety.models import AnalyzeTextOptions, TextCategory
from azure.core.credentials import AzureKeyCredential
from azure.core.exceptions import HttpResponseError
from azure.cosmos import CosmosClient
# Semantic Kernel imports
import semantic_kernel as sk
import semantic_kernel.connectors.ai.open_ai as sk_oai
from semantic_kernel.connectors.ai.open_ai import AzureChatCompletion
from semantic_kernel.prompt_template import PromptTemplateConfig
from semantic_kernel.prompt_template.input_variable import InputVariable
from semantic_kernel.functions.kernel_arguments import KernelArguments
# Configuration imports
from config import (
AZURE_COSMOSDB_DATABASE_NAME,
AZURE_COSMOSDB_CONTAINER_NAME,
AZURE_COSMOSDB_ENDPOINT,
AZURE_COSMOSDB_KEY,
AZURE_CONTENT_SAFETY_ENDPOINT,
AZURE_CONTENT_SAFETY_KEY,
AZURE_OPENAI_ENDPOINT,
AZURE_OPENAI_KEY,
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME
)
class ChatbotService:
def __init__(self, loop, user_id):
"""
Initialize the ChatbotService with event loop, user ID, and necessary Azure CosmosDB clients.
"""
# Set up the event loop and thread pool executor for managing asynchronous tasks,
# allowing Azure Cosmos DB operations to be handled in an asynchronous environment
self.loop = loop
self.executor = ThreadPoolExecutor()
# Store the user ID, which is used as the partition key (/userId) in Azure CosmosDB
self.user_id = user_id
# Initialize the Azure Cosmos DB client
self.cosmos_client = CosmosClient(AZURE_COSMOSDB_ENDPOINT, credential=AZURE_COSMOSDB_KEY)
# Initialize the Azure Cosmos DB database client
self.database = self.cosmos_client.get_database_client(AZURE_COSMOSDB_DATABASE_NAME)
# Initialize the Azure Cosmos DB container client
self.container = self.database.get_container_client(AZURE_COSMOSDB_CONTAINER_NAME)
async def init(self):
"""
Initialize the Content Safety client and Semantic Kernel.
"""
# Initialize the Azure Content Safety client
self.content_safety_client = ContentSafetyClient(AZURE_CONTENT_SAFETY_ENDPOINT, AzureKeyCredential(AZURE_CONTENT_SAFETY_KEY))
# Initialize the Semantic Kernel
self.kernel = sk.Kernel()
# Initialize the chat service for Azure OpenAI
self.chat_service = AzureChatCompletion(
service_id='chat_service',
deployment_name=AZURE_OPENAI_CHAT_DEPLOYMENT_NAME,
endpoint=AZURE_OPENAI_ENDPOINT,
api_key=AZURE_OPENAI_KEY
)
# Add the chat service to the Semantic Kernel
self.kernel.add_service(self.chat_service)
# Define the prompt template configuration for the chatbot
self.prompt_template_config = PromptTemplateConfig(
template="""ChatBot can have a conversation with you about any topic.
It can give explicit instructions or say 'I don't know' if it does not have an answer.
{{$history}}
User: {{$user_message}}
ChatBot: """,
name='chat_prompt_template',
template_format='semantic-kernel',
input_variables=[
InputVariable(name='user_message', description='The user message.', is_required=True),
InputVariable(name='history', description='The conversation history', is_required=True),
],
execution_settings=sk_oai.OpenAIChatPromptExecutionSettings(
service_id='chat_service',
ai_model_id='gpt-3.5-turbo',
max_tokens=500,
temperature=0.7
)
)
# Add the chat function to the Semantic Kernel
self.chat_function = self.kernel.add_function(
function_name="chat_function",
plugin_name="chat_plugin",
prompt_template_config=self.prompt_template_config,
)
return self
async def analyze_text(self, text):
"""
Analyze the input text for safety using Azure Content Safety.
"""
# Create a request with the input text to be analyzed
request = AnalyzeTextOptions(text=text)
try:
# Send the request to the Azure Content Safety client and await the response
response = await self.content_safety_client.analyze_text(request)
# Get the analysis results for different categories
results = {
'hate': next((item for item in response.categories_analysis if item.category == TextCategory.HATE), None),
'self_harm': next((item for item in response.categories_analysis if item.category == TextCategory.SELF_HARM), None),
'sexual': next((item for item in response.categories_analysis if item.category == TextCategory.SEXUAL), None),
'violence': next((item for item in response.categories_analysis if item.category == TextCategory.VIOLENCE), None),
}
# Print content safety analysis results
print("\n<-- Content Safety Analysis Results -->")
for category, result in results.items():
if result:
print(f"{category.capitalize()} severity: {result.severity}")
print("<-- End of Content Safety Analysis Results -->\n")
# Define a threshold for the text to be considered unsafe
threshold = 2
# Based on the threshold, determine if the text is safe
is_safe = not any(result and result.severity >= threshold for result in results.values())
return is_safe, results
except HttpResponseError as e:
# Handle any HTTP response errors that occur during the request
print(f"Failed to analyze text. Error: {e}")
async def chat_with_kernel(self, user_message, history):
"""
Interact with the chatbot using the Semantic Kernel and provided history.
"""
# Create arguments for the chat function using the user message and conversation history stored in Azure Cosmos DB
arguments = KernelArguments(user_message=user_message, history=history)
# Invoke the chat function in the Semantic Kernel with the provided arguments and await the response
response = await self.kernel.invoke(self.chat_function, arguments)
# Return the chatbot's response
return response
async def store_interaction(self, user_message, chat_response):
"""
Store the user interaction with the chatbot in Azure Cosmos DB.
"""
# Run the _store_interaction_sync method in an asynchronous execution environment
await self.loop.run_in_executor(self.executor, self._store_interaction_sync, user_message, chat_response)
def _store_interaction_sync(self, user_message, chat_response):
"""
Synchronously store the interaction in Azure Cosmos DB.
"""
# Get the current time in UTC
current_time = datetime.now(timezone.utc)
# Upsert (insert or update) the interaction data into the Cosmos DB container
self.container.upsert_item({
'id': str(current_time.timestamp()), # Use the current timestamp as a unique ID
'user_message': user_message, # Store the user message
'bot_response': chat_response, # Store the chatbot response
'timestamp': current_time.isoformat(), # Store the timestamp in ISO format
'userId': self.user_id # Store the user ID for partition key
})
async def load_historical_context(self):
"""
Load the user's chat history from Azure Cosmos DB.
"""
# Run the _load_historical_context_sync method in an asynchronous execution environment
return await self.loop.run_in_executor(self.executor, self._load_historical_context_sync)
def _load_historical_context_sync(self):
"""
Synchronously load the user's chat history from Azure Cosmos DB.
"""
# Define the query to select items for the current user, ordered by timestamp
query = "SELECT * FROM c WHERE c.userId = @userId ORDER BY c.timestamp DESC"
parameters = [{"name": "@userId", "value": self.user_id}]
# Execute the query and retrieve the items
items = list(self.container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
# Include only the last 5 conversations
history_items = items[:5]
# Format the conversion history
return "\n".join([f"User: {item['user_message']}\nChatBot: {item['bot_response']}" for item in history_items])
async def clear_historical_context(self):
"""
Clear the user's chat history from Azure Cosmos DB.
"""
# Run the _clear_historical_context_sync method in an asynchronous execution environment
await self.loop.run_in_executor(self.executor, self._clear_historical_context_sync)
def _clear_historical_context_sync(self):
"""
Synchronously clear the user's chat history from Azure Cosmos DB.
"""
# Define the query to select items for the current user
query = "SELECT * FROM c WHERE c.userId = @userId"
parameters = [{"name": "@userId", "value": self.user_id}]
# Execute the query and retrieve the items
items = list(self.container.query_items(query=query, parameters=parameters, enable_cross_partition_query=True))
# Clear the chat history by deleting all items for the current user
for item in items:
self.container.delete_item(item, partition_key=self.user_id)
async def main():
"""
Main function to run the chatbot service.
"""
# Enter the User ID to join the chat.
# A conversation history is stored based on the user ID.
user_id = input("User ID: ")
# Get the event loop to allow synchronous operations in Azure Cosmos DB for NoSQL to run asynchronously.
loop = asyncio.get_running_loop()
# Initialize the ChatbotService with the event loop and user ID
chatbot_service = await ChatbotService(loop, user_id).init()
# Main loop for the interaction with the user
while True:
user_message = input("You: ")
# Exit the chat loop if the user types 'exit'
if user_message.lower() == 'exit':
break
elif user_message.lower() == 'history':
# Load and print the chat history if the user types 'history'
history = await chatbot_service.load_historical_context()
print(f"\n<-- Chat history of user ID: {user_id} -->")
print(history)
print(f"<-- End of chat history of user ID: {user_id} -->\n")
elif user_message.lower() == 'clear':
# Clear the chat history if the user types 'clear'
await chatbot_service.clear_historical_context()
print("Chat history cleared.")
else:
# Analyze the text for safety
is_safe, _ = await chatbot_service.analyze_text(user_message)
if is_safe:
# Load chat history and interact with the chatbot if the message is safe
history = await chatbot_service.load_historical_context()
chat_response = await chatbot_service.chat_with_kernel(user_message, history)
print("Chatbot:", chat_response)
# Store the interaction in Cosmos DB
await chatbot_service.store_interaction(user_message, str(chat_response))
else:
# Inform the user if their message is not safe
print("Chatbot: Your message is not safe to process. Please rephrase and try again.")
if __name__ == "__main__":
asyncio.run(main())
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.