Category: News
My DDPG agent model is generating same output from every simulation.
I trained a rlDDPGagent(a biped walking robot) and simulated, but it always generates a same walking from every simulation.
However, I need a different gaits to acquire several sensor data. The code below is the training and agent option code. I used the msra-walking-robot-master code from matlab github.
% Create DDPG agent and training options for walking robot example
%
% Copyright 2019 The MathWorks, Inc.
%% DDPG Agent Options
agentOptions = rlDDPGAgentOptions;
agentOptions.SampleTime = Ts;
agentOptions.DiscountFactor = 0.99;
agentOptions.MiniBatchSize = 128;
agentOptions.ExperienceBufferLength = 1e6;
agentOptions.TargetSmoothFactor = 1e-3;
agentOptions.NoiseOptions.MeanAttractionConstant = 5;
agentOptions.NoiseOptions.Variance = 0.4;
agentOptions.NoiseOptions.VarianceDecayRate = 1e-5;
%% Training Options
trainingOptions = rlTrainingOptions;
trainingOptions.MaxEpisodes = 5000;
trainingOptions.MaxStepsPerEpisode = Tf/Ts;
trainingOptions.ScoreAveragingWindowLength = 100;
trainingOptions.StopTrainingCriteria = ‘AverageReward’;
trainingOptions.StopTrainingValue = 110;
trainingOptions.SaveAgentCriteria = ‘EpisodeReward’;
trainingOptions.SaveAgentValue = 150;
trainingOptions.Plots = ‘training-progress’;
trainingOptions.Verbose = true;
if useParallel
trainingOptions.Parallelization = ‘async’;
trainingOptions.ParallelizationOptions.StepsUntilDataIsSent = 32;
end
The code below is training code:
% Walking Robot — DDPG Agent Training Script (2D)
% Copyright 2019 The MathWorks, Inc.
warning off parallel:gpu:device:DeviceLibsNeedsRecompiling %don’t show the warning
%% SET UP ENVIRONMENT
% Speedup options
useFastRestart = true;
useGPU = false;
useParallel = true;
% Create the observation info
numObs = 31;
observationInfo = rlNumericSpec([numObs 1]);
observationInfo.Name = ‘observations’;
% create the action info
numAct = 6;
actionInfo = rlNumericSpec([numAct 1],’LowerLimit’,-1,’UpperLimit’, 1);
actionInfo.Name = ‘foot_torque’;
% Environment
mdl = ‘walkingRobotRL2D’;
load_system(mdl);
blk = [mdl,’/RL Agent’];
env = rlSimulinkEnv(mdl,blk,observationInfo,actionInfo);
env.ResetFcn = @(in)walkerResetFcn(in,upper_leg_length/100,lower_leg_length/100,h/100,’2D’);
if ~useFastRestart
env.UseFastRestart = ‘off’;
end
%% CREATE NEURAL NETWORKS
createDDPGNetworks;
%% CREATE AND TRAIN AGENT
createDDPGOptions;
agent = rlDDPGAgent(actor,critic,agentOptions);
trainingResults = train(agent,env,trainingOptions)
%% SAVE AGENT
reset(agent); % Clears the experience buffer
curDir = pwd;
saveDir = ‘savedAgents’;
cd(saveDir)
save([‘trainedAgent_2D_’ datestr(now,’mm_DD_YYYY_HHMM’)],’agent’,’trainingResults’,’trainingOptions.MaxEpisodes’);
cd(curDir)
The code below is the simulation code:
% Simulates the walking robot model
%% Setup
clc; close all;
robotParametersRL
% Create the observation info
numObs = 31;
observationInfo = rlNumericSpec([numObs 1]);
observationInfo.Name = ‘observations’;
% create the action info
numAct = 6;
actionInfo = rlNumericSpec([numAct 1],’LowerLimit’,-1,’UpperLimit’, 1);
actionInfo.Name = ‘foot_torque’;
% Environment
mdl = ‘walkingRobotRL2D’;
load_system(mdl);
blk = [mdl,’/RL Agent’];
env = rlSimulinkEnv(mdl,blk,observationInfo,actionInfo);
load trainedAgent_2D_04_25_2024_1541_5000 %load agent
%action = getAction(agent);
simOpts = rlSimulationOptions;
simOpts.MaxSteps = 1000;
simOpts.NumSimulations = 3;
%plot(env);
reset(env);
experience = sim(env,agent,simOpts);
The result of the code always show the same gait. Is there any method to get different output from every simulation?
Thank you so much!I trained a rlDDPGagent(a biped walking robot) and simulated, but it always generates a same walking from every simulation.
However, I need a different gaits to acquire several sensor data. The code below is the training and agent option code. I used the msra-walking-robot-master code from matlab github.
% Create DDPG agent and training options for walking robot example
%
% Copyright 2019 The MathWorks, Inc.
%% DDPG Agent Options
agentOptions = rlDDPGAgentOptions;
agentOptions.SampleTime = Ts;
agentOptions.DiscountFactor = 0.99;
agentOptions.MiniBatchSize = 128;
agentOptions.ExperienceBufferLength = 1e6;
agentOptions.TargetSmoothFactor = 1e-3;
agentOptions.NoiseOptions.MeanAttractionConstant = 5;
agentOptions.NoiseOptions.Variance = 0.4;
agentOptions.NoiseOptions.VarianceDecayRate = 1e-5;
%% Training Options
trainingOptions = rlTrainingOptions;
trainingOptions.MaxEpisodes = 5000;
trainingOptions.MaxStepsPerEpisode = Tf/Ts;
trainingOptions.ScoreAveragingWindowLength = 100;
trainingOptions.StopTrainingCriteria = ‘AverageReward’;
trainingOptions.StopTrainingValue = 110;
trainingOptions.SaveAgentCriteria = ‘EpisodeReward’;
trainingOptions.SaveAgentValue = 150;
trainingOptions.Plots = ‘training-progress’;
trainingOptions.Verbose = true;
if useParallel
trainingOptions.Parallelization = ‘async’;
trainingOptions.ParallelizationOptions.StepsUntilDataIsSent = 32;
end
The code below is training code:
% Walking Robot — DDPG Agent Training Script (2D)
% Copyright 2019 The MathWorks, Inc.
warning off parallel:gpu:device:DeviceLibsNeedsRecompiling %don’t show the warning
%% SET UP ENVIRONMENT
% Speedup options
useFastRestart = true;
useGPU = false;
useParallel = true;
% Create the observation info
numObs = 31;
observationInfo = rlNumericSpec([numObs 1]);
observationInfo.Name = ‘observations’;
% create the action info
numAct = 6;
actionInfo = rlNumericSpec([numAct 1],’LowerLimit’,-1,’UpperLimit’, 1);
actionInfo.Name = ‘foot_torque’;
% Environment
mdl = ‘walkingRobotRL2D’;
load_system(mdl);
blk = [mdl,’/RL Agent’];
env = rlSimulinkEnv(mdl,blk,observationInfo,actionInfo);
env.ResetFcn = @(in)walkerResetFcn(in,upper_leg_length/100,lower_leg_length/100,h/100,’2D’);
if ~useFastRestart
env.UseFastRestart = ‘off’;
end
%% CREATE NEURAL NETWORKS
createDDPGNetworks;
%% CREATE AND TRAIN AGENT
createDDPGOptions;
agent = rlDDPGAgent(actor,critic,agentOptions);
trainingResults = train(agent,env,trainingOptions)
%% SAVE AGENT
reset(agent); % Clears the experience buffer
curDir = pwd;
saveDir = ‘savedAgents’;
cd(saveDir)
save([‘trainedAgent_2D_’ datestr(now,’mm_DD_YYYY_HHMM’)],’agent’,’trainingResults’,’trainingOptions.MaxEpisodes’);
cd(curDir)
The code below is the simulation code:
% Simulates the walking robot model
%% Setup
clc; close all;
robotParametersRL
% Create the observation info
numObs = 31;
observationInfo = rlNumericSpec([numObs 1]);
observationInfo.Name = ‘observations’;
% create the action info
numAct = 6;
actionInfo = rlNumericSpec([numAct 1],’LowerLimit’,-1,’UpperLimit’, 1);
actionInfo.Name = ‘foot_torque’;
% Environment
mdl = ‘walkingRobotRL2D’;
load_system(mdl);
blk = [mdl,’/RL Agent’];
env = rlSimulinkEnv(mdl,blk,observationInfo,actionInfo);
load trainedAgent_2D_04_25_2024_1541_5000 %load agent
%action = getAction(agent);
simOpts = rlSimulationOptions;
simOpts.MaxSteps = 1000;
simOpts.NumSimulations = 3;
%plot(env);
reset(env);
experience = sim(env,agent,simOpts);
The result of the code always show the same gait. Is there any method to get different output from every simulation?
Thank you so much! I trained a rlDDPGagent(a biped walking robot) and simulated, but it always generates a same walking from every simulation.
However, I need a different gaits to acquire several sensor data. The code below is the training and agent option code. I used the msra-walking-robot-master code from matlab github.
% Create DDPG agent and training options for walking robot example
%
% Copyright 2019 The MathWorks, Inc.
%% DDPG Agent Options
agentOptions = rlDDPGAgentOptions;
agentOptions.SampleTime = Ts;
agentOptions.DiscountFactor = 0.99;
agentOptions.MiniBatchSize = 128;
agentOptions.ExperienceBufferLength = 1e6;
agentOptions.TargetSmoothFactor = 1e-3;
agentOptions.NoiseOptions.MeanAttractionConstant = 5;
agentOptions.NoiseOptions.Variance = 0.4;
agentOptions.NoiseOptions.VarianceDecayRate = 1e-5;
%% Training Options
trainingOptions = rlTrainingOptions;
trainingOptions.MaxEpisodes = 5000;
trainingOptions.MaxStepsPerEpisode = Tf/Ts;
trainingOptions.ScoreAveragingWindowLength = 100;
trainingOptions.StopTrainingCriteria = ‘AverageReward’;
trainingOptions.StopTrainingValue = 110;
trainingOptions.SaveAgentCriteria = ‘EpisodeReward’;
trainingOptions.SaveAgentValue = 150;
trainingOptions.Plots = ‘training-progress’;
trainingOptions.Verbose = true;
if useParallel
trainingOptions.Parallelization = ‘async’;
trainingOptions.ParallelizationOptions.StepsUntilDataIsSent = 32;
end
The code below is training code:
% Walking Robot — DDPG Agent Training Script (2D)
% Copyright 2019 The MathWorks, Inc.
warning off parallel:gpu:device:DeviceLibsNeedsRecompiling %don’t show the warning
%% SET UP ENVIRONMENT
% Speedup options
useFastRestart = true;
useGPU = false;
useParallel = true;
% Create the observation info
numObs = 31;
observationInfo = rlNumericSpec([numObs 1]);
observationInfo.Name = ‘observations’;
% create the action info
numAct = 6;
actionInfo = rlNumericSpec([numAct 1],’LowerLimit’,-1,’UpperLimit’, 1);
actionInfo.Name = ‘foot_torque’;
% Environment
mdl = ‘walkingRobotRL2D’;
load_system(mdl);
blk = [mdl,’/RL Agent’];
env = rlSimulinkEnv(mdl,blk,observationInfo,actionInfo);
env.ResetFcn = @(in)walkerResetFcn(in,upper_leg_length/100,lower_leg_length/100,h/100,’2D’);
if ~useFastRestart
env.UseFastRestart = ‘off’;
end
%% CREATE NEURAL NETWORKS
createDDPGNetworks;
%% CREATE AND TRAIN AGENT
createDDPGOptions;
agent = rlDDPGAgent(actor,critic,agentOptions);
trainingResults = train(agent,env,trainingOptions)
%% SAVE AGENT
reset(agent); % Clears the experience buffer
curDir = pwd;
saveDir = ‘savedAgents’;
cd(saveDir)
save([‘trainedAgent_2D_’ datestr(now,’mm_DD_YYYY_HHMM’)],’agent’,’trainingResults’,’trainingOptions.MaxEpisodes’);
cd(curDir)
The code below is the simulation code:
% Simulates the walking robot model
%% Setup
clc; close all;
robotParametersRL
% Create the observation info
numObs = 31;
observationInfo = rlNumericSpec([numObs 1]);
observationInfo.Name = ‘observations’;
% create the action info
numAct = 6;
actionInfo = rlNumericSpec([numAct 1],’LowerLimit’,-1,’UpperLimit’, 1);
actionInfo.Name = ‘foot_torque’;
% Environment
mdl = ‘walkingRobotRL2D’;
load_system(mdl);
blk = [mdl,’/RL Agent’];
env = rlSimulinkEnv(mdl,blk,observationInfo,actionInfo);
load trainedAgent_2D_04_25_2024_1541_5000 %load agent
%action = getAction(agent);
simOpts = rlSimulationOptions;
simOpts.MaxSteps = 1000;
simOpts.NumSimulations = 3;
%plot(env);
reset(env);
experience = sim(env,agent,simOpts);
The result of the code always show the same gait. Is there any method to get different output from every simulation?
Thank you so much! simulink, ddpg, rl, reinforcement learning, biped robot, simulation MATLAB Answers — New Questions
How to use PMSM from SimPowerSystems block with Simscape Model
Hello together,
I’m trying modelling of actuator which is powered by PMSM from SimPowerSystems block. I have connected the motor ‘S’ port with other mechanical input such as gear and spring and ideal torque source but when I vary papameters of spring there is no change in output position or speed. Attached the similar work insted of stepper i wanted to use PMSM block with ‘S’ port.
How can I connect the PSMS with the mechanical model, so that the robot is I can get accurate readings?
Thank you for your help.
Kind regards,
DhananjayHello together,
I’m trying modelling of actuator which is powered by PMSM from SimPowerSystems block. I have connected the motor ‘S’ port with other mechanical input such as gear and spring and ideal torque source but when I vary papameters of spring there is no change in output position or speed. Attached the similar work insted of stepper i wanted to use PMSM block with ‘S’ port.
How can I connect the PSMS with the mechanical model, so that the robot is I can get accurate readings?
Thank you for your help.
Kind regards,
Dhananjay Hello together,
I’m trying modelling of actuator which is powered by PMSM from SimPowerSystems block. I have connected the motor ‘S’ port with other mechanical input such as gear and spring and ideal torque source but when I vary papameters of spring there is no change in output position or speed. Attached the similar work insted of stepper i wanted to use PMSM block with ‘S’ port.
How can I connect the PSMS with the mechanical model, so that the robot is I can get accurate readings?
Thank you for your help.
Kind regards,
Dhananjay pmsm, simscape, simpowersystems, bldc MATLAB Answers — New Questions
[Simscape] Having two thermal liquid loops with different thermal liquid settings?
I’m modeling a two-loop system on both sides of a heat exchanger and clearly each loop uses different heat transfer fluid.
So I though I should be able to connect two different thermal liquid setting blocks on each side of the heat exchanger, but I’m getting the following error.
"Failed to propagate domain parameters. Domain parameters are propagated to the same set of nodes from sources: ‘Thermal_Liquid_Settings_TL.A’ and ‘SecondaryLoop.Thermal_Liquid_Settings_TL2.A’. Each node may only have at most one source of propagation."
Doesn’t this feel like a bug in the current heat exchanger model? Surely the properties could be different. The version I’m on is 2021b. I’ll try to look for ways to bypass it (maybe first go to a newer version), but if anyone happens to know about this issue, please share your ideas.I’m modeling a two-loop system on both sides of a heat exchanger and clearly each loop uses different heat transfer fluid.
So I though I should be able to connect two different thermal liquid setting blocks on each side of the heat exchanger, but I’m getting the following error.
"Failed to propagate domain parameters. Domain parameters are propagated to the same set of nodes from sources: ‘Thermal_Liquid_Settings_TL.A’ and ‘SecondaryLoop.Thermal_Liquid_Settings_TL2.A’. Each node may only have at most one source of propagation."
Doesn’t this feel like a bug in the current heat exchanger model? Surely the properties could be different. The version I’m on is 2021b. I’ll try to look for ways to bypass it (maybe first go to a newer version), but if anyone happens to know about this issue, please share your ideas. I’m modeling a two-loop system on both sides of a heat exchanger and clearly each loop uses different heat transfer fluid.
So I though I should be able to connect two different thermal liquid setting blocks on each side of the heat exchanger, but I’m getting the following error.
"Failed to propagate domain parameters. Domain parameters are propagated to the same set of nodes from sources: ‘Thermal_Liquid_Settings_TL.A’ and ‘SecondaryLoop.Thermal_Liquid_Settings_TL2.A’. Each node may only have at most one source of propagation."
Doesn’t this feel like a bug in the current heat exchanger model? Surely the properties could be different. The version I’m on is 2021b. I’ll try to look for ways to bypass it (maybe first go to a newer version), but if anyone happens to know about this issue, please share your ideas. simscape MATLAB Answers — New Questions
How do I call a function from the command window
myEquation(2)
function myEquation (x)
a = 0.4361836;
b = 0.1201676;
c = 0.937298;
r = exp(-0.5*(x^2))/(2*pi) ;
t = 1/(1+(0.3326*x)) ;
phi = 0.5 – r*((a*t)-(b*(t^2))+(c*(t^3))) ;
fprintf(‘The value of Φ(x) is: %i’, phi)
fprintf(‘n’)
end
I have this code, and it works properly, however, I need a way to be able to call it from the command window. The line myEquation(2) auto inputs the value as 2, but I need to be able to enter other values without editing the code. Should I use an input prompt to prompt the user for a value of x to run my equation on?myEquation(2)
function myEquation (x)
a = 0.4361836;
b = 0.1201676;
c = 0.937298;
r = exp(-0.5*(x^2))/(2*pi) ;
t = 1/(1+(0.3326*x)) ;
phi = 0.5 – r*((a*t)-(b*(t^2))+(c*(t^3))) ;
fprintf(‘The value of Φ(x) is: %i’, phi)
fprintf(‘n’)
end
I have this code, and it works properly, however, I need a way to be able to call it from the command window. The line myEquation(2) auto inputs the value as 2, but I need to be able to enter other values without editing the code. Should I use an input prompt to prompt the user for a value of x to run my equation on? myEquation(2)
function myEquation (x)
a = 0.4361836;
b = 0.1201676;
c = 0.937298;
r = exp(-0.5*(x^2))/(2*pi) ;
t = 1/(1+(0.3326*x)) ;
phi = 0.5 – r*((a*t)-(b*(t^2))+(c*(t^3))) ;
fprintf(‘The value of Φ(x) is: %i’, phi)
fprintf(‘n’)
end
I have this code, and it works properly, however, I need a way to be able to call it from the command window. The line myEquation(2) auto inputs the value as 2, but I need to be able to enter other values without editing the code. Should I use an input prompt to prompt the user for a value of x to run my equation on? function, duplicate post MATLAB Answers — New Questions
Time incorrect on my Win 11
Hi, I am running Windows 11 23H2 Build 22631.3593
My time zone is set to Dublin, Edinburgh, Lisbon, London which is correct.
Region is United Kingdom
Set time automatically is set to – on
Set time automatically is set to – on
Show time & date in system tray is set to – on
I have synced the the time – Time server – time.windows.com
HOWEVER the time is an hour behind what it should be.
Any ideas on how to get this rectified?
Many thanks
Hi, I am running Windows 11 23H2 Build 22631.3593My time zone is set to Dublin, Edinburgh, Lisbon, London which is correct.Region is United KingdomSet time automatically is set to – onSet time automatically is set to – onShow time & date in system tray is set to – onI have synced the the time – Time server – time.windows.comHOWEVER the time is an hour behind what it should be.Any ideas on how to get this rectified? Many thanks Read More
Lost access to form and responses
Recently, I conducted a survey and had the responses transfer into an excel file so the extended team could track the results. As of yesterday, I tried to check in, as well as someone else with access, and we have both lost access.
I believe it might be due to me deleting a Teams environment that the Form was created in, not knowing that that would lock me out of the form. It is not in my deleted forms, so I believe it still exists, but as I was the sole owner and have no ability to get back into the Teams environment that was removed, I’m wondering if there is any solution to this!
Recently, I conducted a survey and had the responses transfer into an excel file so the extended team could track the results. As of yesterday, I tried to check in, as well as someone else with access, and we have both lost access. I believe it might be due to me deleting a Teams environment that the Form was created in, not knowing that that would lock me out of the form. It is not in my deleted forms, so I believe it still exists, but as I was the sole owner and have no ability to get back into the Teams environment that was removed, I’m wondering if there is any solution to this! Read More
How to set up staging slots in WordPress on App Service
What is a staging site?
A staging site, also called as a development site, testing site, or sandbox – is a clone of your actual WordPress website which cannot be accessed by your visitors. This can only be accessed by people who have the staging URL.
Why do you need a staging site for your WordPress website?
A staging site is extremely useful when you are working on a WordPress website. It provides a safe area for development, testing, and experimentation. When you are trying out a new plugin, theme or custom code, there are chances that the website might break leading to downtime for your users, or even cause irreparable damage to your website like loss of content and data.
Users would not be able to see your work in progress. You can make the changes live when you are ready. A staging site helps you experiment with new plugins without being locked to the changes and troubleshoot in case there are any issues. You do not have to rush making changes since your users are safely using the original version.
Although the staging site is a clone of your original website, the changes do not reflect in the original site until you publish the changes, also known as ‘swapping’. In case a new deployment causes issues, you can easily swap the slots back to previous versions, effectively rolling back the changes.
Staging sites can be immensely useful for scenarios such as A/B testing, allowing you to gather feedback from users and make informed decisions.
How to create a staging site in WordPress on Azure App Service?
Note: We will make use of Deployment slots feature of App Service to create staging sites for our WordPress application. However, this scenario is not completely same as WordPress on App Service also uses an Azure database for MySQL instance.
Scenario 1: Creating a staging site while creating WordPress sites on App Service
Step 1: Go to https://ms.portal.azure.com/#create/WordPress.WordPress . Go to Deployment tab. Check ‘Add staging slot’.
Scenario 2: Creating a staging site for an already created WordPress site on App Service.
There are five parts to this:
Part 1: Create the App Service deployment slot.
Step 1: Once you have a WordPress website on Azure App Service (See how to create one here), go to the App Service Overview. Then go to Deployment Slots.
Step 2: Click on Add Slot
Step 3: Enter your preferred name for the staging slot. In the ‘Clone settings from:’ select the deployment slot from there you wish to copy the settings. In this case, the name of the staging slot is ‘staging’ and deployment slot is ‘wpsite01’. Notice that the staging site can be accessed using the link wpsite01-staging.azurewebsites.net
Click on Add.
Step 4: Now you can see that the staging slot has been added. Click on Close.
Part 2: Connect the staging slot to VNET.
Step 1: Go to App service staging slot.
Step 2: Go to networking.
Step 3: Check outbound networking. If not configured, click on link.
Step 4: Click on connect.
Part 3: Create a new Database in your Azure database for MySQL instance
Step 1: Go to phpMyAdmin by using the URL: https://<yourappname>.azurewebsites.net/phpmyadmin
Step 2: Login using your database credentials. Tip: you will find your database credentials in App Settings.
Step 3: Go to your database
Step 4: Go to Operations. You will see the option “Copy database to”. Add database name. Click on Go.
Part 4: If you are using CDN/AFD with your WordPress site, create a new CDN/AFD endpoint and Blob storage container.
Part 5: Map App Service staging slot to the staging database, CDN, Blob storage.
Step 1: Go to App Service staging slot. Go to Environment Variables.
Step 2: Edit ‘Database name’ to new database name
Step 3: Edit CDN endpoint
Step 4: Edit Blob storage container name
Step 5: Apply. Confirm.
Swapping
Step 1: Now visit the staging site by visiting the website by clicking on the staging slot.
Step 2: This will open the overview page for the staging slot. Notice that this is similar to the overview page of the original web app, only the name of the staging slot is different as set in step 3. Click on the website URL to visit the website. In this case, https://wpsite01-staging.azurewebsites.net/
Step 3: As you can see, the default website appears as the original slot. Since we have made no changes to the original slot, the default website appears.
Step 4: Go to the admin panel of WordPress by adding ‘/wp-admin’ after the address and logging in using your credentials that are same as the original website. Make the desired changes on the website. In this case, the changes look like this:
Step 5: Now go to the original website. In this case, https://wpsite01.azurewebsites.net/
We can see that the original website is present in the original state.
Step 6: Go to the Deployment slots page in Azure Portal. Click on Swap. This will swap the staging and production sites.
Step 7: Select the staging slot as the source and the production slot as the Target. Click on Swap.
Step 8: You can see that the swap operation has been completed. Click on Close.
Step 9: Now visit the original website. In this case, https://wpsite01.azurewebsites.net/
We see that the changes have successfully been published to the production slot. Now your website visitors can see the changes.
Further reading
Deployment slots (Staging sites) can be used for more advanced use cases. Please go through Set up staging environments – Azure App Service | Microsoft Learn to understand more about deployment slots.
Support and Feedback
In case you need any support, you can open a support request at New support request – Microsoft Azure.
For more details about the offering, please visit Announcing the General Availability of WordPress on Azure App Service – Microsoft Tech Community.
If you have any ideas about how we can make WordPress on Azure App Service better, please post your ideas at Post idea · Community (azure.com)
or you could email us at wordpressonazure@microsoft.com to start a conversation.
Microsoft Tech Community – Latest Blogs –Read More
how to aviod the two loops
I have a code:
x=linspace(-10,10,10001);
y=linspace(-5,5,20001);
A=[];
for j=1:length(x)
for k=1:length(y)
A=[A;x(j),y(k)];
end
end
The computation is very slow due to the two loops, so How can I aviod the two loops in this code?
Thanks very much!I have a code:
x=linspace(-10,10,10001);
y=linspace(-5,5,20001);
A=[];
for j=1:length(x)
for k=1:length(y)
A=[A;x(j),y(k)];
end
end
The computation is very slow due to the two loops, so How can I aviod the two loops in this code?
Thanks very much! I have a code:
x=linspace(-10,10,10001);
y=linspace(-5,5,20001);
A=[];
for j=1:length(x)
for k=1:length(y)
A=[A;x(j),y(k)];
end
end
The computation is very slow due to the two loops, so How can I aviod the two loops in this code?
Thanks very much! loops MATLAB Answers — New Questions
What is the correct use of the “RGB2Lab” function in this situation?
Hi, what I am looking to do is an analysis on the color change of a sample over time.
What I thought of doing is the following:
1- A mask of the first photo that will be used in all the following ones. In this way observe only the sample.
2- Using the mask to obtain the average value of the RGB channels of the sample in each of the photos.
3- Use the "RGB2Lab" function to obtain the values in Lab mode for each photo.
4-From these last values calculate the color difference "Delta E" for each of the photos with respect to the first one.
My doubts are then the following (if you have any recommendation for any of the above steps I will also be grateful, I am new to image analysis and I learned about Lab mode last week):
Does it make sense to occupy Lab mode as I am doing?
From what I understand the "RGB2Lab" function has other inputs that are related to the light used. If I used a white fluorescent light I should use the light standard "F" or am I wrong?
However this standard is not available for the function, any advice?
This is the sample:
EDIT: I was quick to ask the question myself. But I found some links to other answered questions that, I think, encapsulate everything I asked.
https://la.mathworks.com/matlabcentral/answers/23013-makecform-srgb2lab?#comment_443644
https://la.mathworks.com/matlabcentral/answers/797742-how-to-separate-the-rgb-channels-from-an-image-and-get-the-average-of-each-rgb-channel-separetly?s_tid=sug_suHi, what I am looking to do is an analysis on the color change of a sample over time.
What I thought of doing is the following:
1- A mask of the first photo that will be used in all the following ones. In this way observe only the sample.
2- Using the mask to obtain the average value of the RGB channels of the sample in each of the photos.
3- Use the "RGB2Lab" function to obtain the values in Lab mode for each photo.
4-From these last values calculate the color difference "Delta E" for each of the photos with respect to the first one.
My doubts are then the following (if you have any recommendation for any of the above steps I will also be grateful, I am new to image analysis and I learned about Lab mode last week):
Does it make sense to occupy Lab mode as I am doing?
From what I understand the "RGB2Lab" function has other inputs that are related to the light used. If I used a white fluorescent light I should use the light standard "F" or am I wrong?
However this standard is not available for the function, any advice?
This is the sample:
EDIT: I was quick to ask the question myself. But I found some links to other answered questions that, I think, encapsulate everything I asked.
https://la.mathworks.com/matlabcentral/answers/23013-makecform-srgb2lab?#comment_443644
https://la.mathworks.com/matlabcentral/answers/797742-how-to-separate-the-rgb-channels-from-an-image-and-get-the-average-of-each-rgb-channel-separetly?s_tid=sug_su Hi, what I am looking to do is an analysis on the color change of a sample over time.
What I thought of doing is the following:
1- A mask of the first photo that will be used in all the following ones. In this way observe only the sample.
2- Using the mask to obtain the average value of the RGB channels of the sample in each of the photos.
3- Use the "RGB2Lab" function to obtain the values in Lab mode for each photo.
4-From these last values calculate the color difference "Delta E" for each of the photos with respect to the first one.
My doubts are then the following (if you have any recommendation for any of the above steps I will also be grateful, I am new to image analysis and I learned about Lab mode last week):
Does it make sense to occupy Lab mode as I am doing?
From what I understand the "RGB2Lab" function has other inputs that are related to the light used. If I used a white fluorescent light I should use the light standard "F" or am I wrong?
However this standard is not available for the function, any advice?
This is the sample:
EDIT: I was quick to ask the question myself. But I found some links to other answered questions that, I think, encapsulate everything I asked.
https://la.mathworks.com/matlabcentral/answers/23013-makecform-srgb2lab?#comment_443644
https://la.mathworks.com/matlabcentral/answers/797742-how-to-separate-the-rgb-channels-from-an-image-and-get-the-average-of-each-rgb-channel-separetly?s_tid=sug_su image analysis, rgb MATLAB Answers — New Questions
Webcam cannot be initialized properly. Please check if the device USB Camera: USB Camera (usb-0000:01:00.0-1.3) is available and free to use.
matlab–
1.r=raspi;–Successfully established connection
Raspberry Pi–
2. lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 0bda:3035 Realtek Semiconductor Corp.
Bus 001 Device 005: ID 25a7:fa67
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
3. fswebcam -r 640*480 a.png
— Opening /dev/video0…
Trying source module v4l2…
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 640x-1 to 640×480.
— Capturing frame…
Captured frame in 0.00 seconds.
— Processing captured image…Writing JPEG image to ‘a.png’matlab–
1.r=raspi;–Successfully established connection
Raspberry Pi–
2. lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 0bda:3035 Realtek Semiconductor Corp.
Bus 001 Device 005: ID 25a7:fa67
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
3. fswebcam -r 640*480 a.png
— Opening /dev/video0…
Trying source module v4l2…
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 640x-1 to 640×480.
— Capturing frame…
Captured frame in 0.00 seconds.
— Processing captured image…Writing JPEG image to ‘a.png’ matlab–
1.r=raspi;–Successfully established connection
Raspberry Pi–
2. lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 004: ID 0bda:3035 Realtek Semiconductor Corp.
Bus 001 Device 005: ID 25a7:fa67
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
3. fswebcam -r 640*480 a.png
— Opening /dev/video0…
Trying source module v4l2…
/dev/video0 opened.
No input was specified, using the first.
Adjusting resolution from 640x-1 to 640×480.
— Capturing frame…
Captured frame in 0.00 seconds.
— Processing captured image…Writing JPEG image to ‘a.png’ matlab,raspberry pi MATLAB Answers — New Questions
How to extract a slider value?
%Slider
fig = uifigure(‘Position’,[100 100 350 275]);
pnl1 = uipanel(fig);
sld1 = uislider(pnl1,’Position’,[100 40 120 3],… ‘ValueChangingFcn’,@(sld,event1) slider1(event1));
sld1.Limits = [-100 100];
sld2 = uislider(pnl1,’Position’,[100 100 120 3],… ‘ValueChangingFcn’,@(sld,event2) slider2(event2));
sld2.Limits = [-100 100];
sld3 = uislider(pnl1,’Position’,[100 180 120 3],… ‘ValueChangingFcn’,@(sld,event3) slider3(event3));
sld3.Limits = [-100 100];
I’m trying to make a program to extract the power of my engine from the sliders I declared(each one corresponds to one engine). So I need to extract the value of each slider when i dragg it as an integer variable, if anyone can help it will be great.
Thanks!%Slider
fig = uifigure(‘Position’,[100 100 350 275]);
pnl1 = uipanel(fig);
sld1 = uislider(pnl1,’Position’,[100 40 120 3],… ‘ValueChangingFcn’,@(sld,event1) slider1(event1));
sld1.Limits = [-100 100];
sld2 = uislider(pnl1,’Position’,[100 100 120 3],… ‘ValueChangingFcn’,@(sld,event2) slider2(event2));
sld2.Limits = [-100 100];
sld3 = uislider(pnl1,’Position’,[100 180 120 3],… ‘ValueChangingFcn’,@(sld,event3) slider3(event3));
sld3.Limits = [-100 100];
I’m trying to make a program to extract the power of my engine from the sliders I declared(each one corresponds to one engine). So I need to extract the value of each slider when i dragg it as an integer variable, if anyone can help it will be great.
Thanks! %Slider
fig = uifigure(‘Position’,[100 100 350 275]);
pnl1 = uipanel(fig);
sld1 = uislider(pnl1,’Position’,[100 40 120 3],… ‘ValueChangingFcn’,@(sld,event1) slider1(event1));
sld1.Limits = [-100 100];
sld2 = uislider(pnl1,’Position’,[100 100 120 3],… ‘ValueChangingFcn’,@(sld,event2) slider2(event2));
sld2.Limits = [-100 100];
sld3 = uislider(pnl1,’Position’,[100 180 120 3],… ‘ValueChangingFcn’,@(sld,event3) slider3(event3));
sld3.Limits = [-100 100];
I’m trying to make a program to extract the power of my engine from the sliders I declared(each one corresponds to one engine). So I need to extract the value of each slider when i dragg it as an integer variable, if anyone can help it will be great.
Thanks! sliders MATLAB Answers — New Questions
3D styling for indoor maps
Is it possible to extrude walls for an Azure indoor map so rooms give a 3D appearance in a pitch view?
Is it possible to extrude walls for an Azure indoor map so rooms give a 3D appearance in a pitch view? Read More
FWT_PO Formula
Hello,
I need the mathematical formula of FWT_PO (forward wavelet transform periodized orthogonal).
ThanksHello,
I need the mathematical formula of FWT_PO (forward wavelet transform periodized orthogonal).
Thanks Hello,
I need the mathematical formula of FWT_PO (forward wavelet transform periodized orthogonal).
Thanks wavelet, mathematics MATLAB Answers — New Questions
creating a function for linear interpolation based on two changing states
So I’m trying to add in linear interpolation into a fitness equation that has two information states. I was talking about it with my professor and he said I could make a function that combines the probability of the information states being between two integers multiplied by the fitness of being at those information states. So looking something like this:
j=2.3
k=4.5
%the function would be combining all possible combinations
%((there is also a physical state (x) and a time state for the fitness function))
p(j=2,k=4)*Ft(x,2,4,t)+
p(j=3,k=4)*Ft(x,3,4,t)+
p(j=2,k=5)*Ft(x,2,5,t)+
p(j=3,k=5)*Ft(x,3,5,t)
How would I create a function that would do this, but for any combination of numbers?So I’m trying to add in linear interpolation into a fitness equation that has two information states. I was talking about it with my professor and he said I could make a function that combines the probability of the information states being between two integers multiplied by the fitness of being at those information states. So looking something like this:
j=2.3
k=4.5
%the function would be combining all possible combinations
%((there is also a physical state (x) and a time state for the fitness function))
p(j=2,k=4)*Ft(x,2,4,t)+
p(j=3,k=4)*Ft(x,3,4,t)+
p(j=2,k=5)*Ft(x,2,5,t)+
p(j=3,k=5)*Ft(x,3,5,t)
How would I create a function that would do this, but for any combination of numbers? So I’m trying to add in linear interpolation into a fitness equation that has two information states. I was talking about it with my professor and he said I could make a function that combines the probability of the information states being between two integers multiplied by the fitness of being at those information states. So looking something like this:
j=2.3
k=4.5
%the function would be combining all possible combinations
%((there is also a physical state (x) and a time state for the fitness function))
p(j=2,k=4)*Ft(x,2,4,t)+
p(j=3,k=4)*Ft(x,3,4,t)+
p(j=2,k=5)*Ft(x,2,5,t)+
p(j=3,k=5)*Ft(x,3,5,t)
How would I create a function that would do this, but for any combination of numbers? linear, interpolation MATLAB Answers — New Questions
Issue with document Synchronization in OneDrive and SharePoint
Great day,
I am encountering an issue with the synchronization of photos between the OneDrive app on my mobile device and the SharePoint server. Photos that I upload via my mobile device to a specific folder on SharePoint are visible to other users, but I am unable to see them on my device. Reinstalling the OneDrive app temporarily resolves the issue, but I am seeking a more permanent solution. Could you please assist me in identifying and resolving the cause of this problem?
Thank you for your assistance.
Great day,I am encountering an issue with the synchronization of photos between the OneDrive app on my mobile device and the SharePoint server. Photos that I upload via my mobile device to a specific folder on SharePoint are visible to other users, but I am unable to see them on my device. Reinstalling the OneDrive app temporarily resolves the issue, but I am seeking a more permanent solution. Could you please assist me in identifying and resolving the cause of this problem? Thank you for your assistance. Read More
Change excel file in batch
Hi all,
I have a lot of identical files in the same folder, all born from the same template: the only difference between them is their file name and the values inserted manually in the unlocked cells of each sheet. I’m guessing if there is any way to change the formulas of each file by just changing the original one and get it all reflected at once (let’s say I find out I did a mistake in some cells and want to change for all the files).
I’m up to any solution, even using power automate or some crazy script.
Really hope someone can help,
Many thanks
Hi all, I have a lot of identical files in the same folder, all born from the same template: the only difference between them is their file name and the values inserted manually in the unlocked cells of each sheet. I’m guessing if there is any way to change the formulas of each file by just changing the original one and get it all reflected at once (let’s say I find out I did a mistake in some cells and want to change for all the files). I’m up to any solution, even using power automate or some crazy script. Really hope someone can help,Many thanks Read More
Is it possible to create a fully functional database in excel or should I use Access?
With all the changes happening with Access, Excel, the new Dataverse, etc., I wonder which direction to take.
I am a sole trader and have Microsoft Business Standard. I am willing to justify the cost of this subscription for my business, but I can’t justify any additional expenses.
So, my question concerns business records and the best application for these.
For example, I would like to design a fully functional database that includes a chart of accounts, various client accounts, invoicing capabilities, profit and loss, balance sheets, etc, amongst other things. It will need to have linked categories and subcategories, the ability to auto-complete, and the capability to be complex.
So, should I build it in Access or Excel? Access is now only available on desktops, and Excel is becoming more capable. I seem to be jumping between the two, unable to decide my future direction. Your opinion and expertise would be much appreciated regarding my conundrum.
With all the changes happening with Access, Excel, the new Dataverse, etc., I wonder which direction to take.I am a sole trader and have Microsoft Business Standard. I am willing to justify the cost of this subscription for my business, but I can’t justify any additional expenses.So, my question concerns business records and the best application for these.For example, I would like to design a fully functional database that includes a chart of accounts, various client accounts, invoicing capabilities, profit and loss, balance sheets, etc, amongst other things. It will need to have linked categories and subcategories, the ability to auto-complete, and the capability to be complex.So, should I build it in Access or Excel? Access is now only available on desktops, and Excel is becoming more capable. I seem to be jumping between the two, unable to decide my future direction. Your opinion and expertise would be much appreciated regarding my conundrum. Read More
Project operations with nonprofit accelerator
Is there any native integration between Microsoft project operations with nonprofit accelerator?
Is there any native integration between Microsoft project operations with nonprofit accelerator? Read More
Windows 11 Insider Preview 10.0.22635.3640 (ni_release) Download error – 0x80248007
I have been trying to update Windows 11 Insider Preview 10.0.22635.3640 (ni_release) all day the update keeps on failing have been getting Download error all day I am now up to Download error – 0x80248007 if you can help email me email address removed for privacy reasons.
I have been trying to update Windows 11 Insider Preview 10.0.22635.3640 (ni_release) all day the update keeps on failing have been getting Download error all day I am now up to Download error – 0x80248007 if you can help email me email address removed for privacy reasons. Read More
Build a chatbot service to ensure safe conversations: Using Azure OpenAI & Azure Content Safety
Build a chatbot service to ensure safe conversations: Using Azure OpenAI & Azure Content Safety
Why should we care about the safety of our chat service?
When you deploy a chat 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 chat 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:
Analyze user messages for safety: Analyze messages entered by users using Azure Content Safety to evaluate them for hate, self-harm, sexual content, and violence.
Conversations with chatbot: Conduct conversations about safe messages using Azure OpenAI.
Manage conversation history: Store a user’s conversation history in Azure Cosmos DB and load or clear the history as needed.
Here is an overview of this tutorial.
This tutorial is related to the following topics:
AI Engineer
Developer
Azure CosmosDB
Azure Content Safety
Azure OpenAI
Semantic Kernel
Prerequisites
Python
Azure subscription
Visual Studio Code
Microsoft Cloud Technologies used in this Tutorial
Azure CosmosDB
Azure Content Safety
Azure OpenAI Service
Table of Contents
Create an Azure CosmosDB
Create an Azure Content Safety
Create an Azure OpenAI
Set up the project and install the libraries
Set up the project in Visual Studio Code
Set up and initialize the ChatbotService class with Azure Services
Implement core functions in ChatbotService
Run the main function and test the ChatbotService
Congratulations!
Full code for tutorial
Create an Azure CosmosDB
To store and load your chat history, you need to create an Azure Cosmos DB resource.
In this exercise, you will:
Create an Azure Cosmos DB account to store your chat history.
Create a container within Azure Cosmos DB to store and manage chat messages.
Create an Azure CosmosDB account
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 your Azure Subscription.
Select the Resource group to use (create a new one if needed).
Enter Account Name. It must be a unique value.
Select the Availability Zones to Disable.
Select the Location you’d like to use.
Select the Capacity mode to Provisioned throughput.
Select the Apply Free Tier Discount to Apply.
Select the Limit total account throughput to prevent unexpected charges.
Select Review + Create.
Select Create.
Create an Azure CosmosDB database and container
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 the Database id to Create new.
Enter database id. It must be a unique value.
Select the Database throughtput (autoscale) to Autoscale.
Enter Database Max RU/s as 1000.
Enter Container id. It must be a unique value.
Select the Indexing to Automatic.
Enter Partition key as /userId.
Select OK.
Create an Azure Content Safety
To create a service to detect inappropriate content, you need to create an Azure Content Safety resource.
In this exercise, you will:
Create an Azure Content Safety to detect inappropriate content.
Create an Azure Content Safety resource
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 your Azure Subscription.
Select the Resource group to use (create a new one if needed).
Select the Region you’d like to use.
Enter Content safety name. It must be a unique value.
Select the Free F0 pricing tier.
Select Review + Create.
Select Create.
Create an Azure OpenAI
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:
Create an Azure OpenAI resource.
Deploy Azure OpenAI models.
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.
Create an Azure OpenAI resource
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:
Select your Azure Subscription.
Select the Resource group to use (create a new one if needed).
Select the Region you’d like to use.
Enter Azure OpenAI Name. It must be a unique value.
Select the Standard S0 pricing tier.
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.
Deploy Azure OpenAI models
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:
For the model, select gpt-35-turbo.
For the Model version, select Default.
For the Deployment name, add a name that’s unique to this cloud instance. For example, gpt-35-turbo.
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.
Set up the project and install the libraries
Now, you will create a folder to work in and set up a virtual environment to develop a program.
In this exercise, you will
Create a folder to work inside it.
Create a virtual environment.
Install the required packages.
Create a folder to work inside it
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
Create a virtual environment
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.
.venvScriptsactivate.bat
Note
If it worked, you should see (.venv) before the command prompt.
Install the required packages
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
Set up the project in Visual Studio Code
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:
Create an example.py file.
Import the required packages.
Create a config.py file to enter Azure information.
Set up example.py file
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:UsersyourUserNamesafety-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
Set up config.py file
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’
Add Azure Environment Variables
Perform the following tasks to add the Azure Cosmos DB account name:
Navigate to the Azure CosmosDB resource that you created.
Copy and paste your account name into the config.py file.
Perform the following tasks to add the Azure Cosmos DB database name:
Navigate to the Azure Cosmos DB resource that you created.
Select Data Explorer from the left side tab.
Copy and paste your database name into the config.py file.
Perform the following tasks to add the Azure Cosmos DB container name:
Navigate to the Azure Cosmos DB resource that you created.
Select Data Explorer from the left side tab.
Copy and paste your container name into the config.py file.
Perform the following tasks to add the Azure Cosmos DB key:
Navigate to the Azure CosmosDB resource that you created.
Select Keys from the left side tab.
Copy and paste your key into the config.py file.
Perform the following tasks to add the Azure Content Safety name:
Navigate to the Azure Content Safety resource that you created.
Copy and paste your resource name into the config.py file.
Perform the following tasks to add the Azure Content Safety key:
Navigate to the Azure Content Safety resource that you created.
Select Keys and Endpoint from the left side tab.
Copy and paste your key into the config.py file.
Perform the following tasks to add the Azure OpenAI name:
Navigate to the Azure OpenAI resource that you created.
Copy and paste your resource name into the config.py file.
Perform the following tasks to add the Azure OpenAI key:
Navigate to the Azure OpenAI resource that you created.
Select Keys and Endpoint from the left side tab.
Copy and paste your key into the config.py file.
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.
Set up and initialize the ChatbotService class with Azure Services
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:
Import Azure information from the config.py file into the example.py file.
Create the ChatbotService class to efficiently manage chat interactions and integrate Azure service capabilities.
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.
Set up and initialize the ChatbotService class with Azure Services
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:
Import Azure information from the config.py file into the example.py file.
Create the ChatbotService class to efficiently manage chat interactions and integrate Azure service capabilities.
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.
Import Azure information from the config.py file
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
)
Create the ChatbotService Class
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__ and async 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.
Implement core functions in ChatbotService
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:
Implement a method to analyze text for safety using Azure Content Safety.
Implement a method to interact with the chatbot using the Semantic Kernel.
Implement methods to store, load, and clear the conversation history in Azure Cosmos DB.
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)
Run the main function and test the ChatbotService
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:
Create the main function to manage user interactions and perform safety checks.
Run the program to see if it works well.
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 completed this tutorial
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.
Clean Up Azure Resources
Cleanup your Azure resources to avoid additional charges to your account. Go to the Azure portal and delete the following resources:
The Azure Cosmos DB resource
The Azure Content Safety resource
The Azure OpenAI resource
Next Steps
Documentation
Azure AI Content Safety documentation
Azure OpenAI Service documentation
Training Content
Get started with Azure Cosmos DB for NoSQL
Moderate content and detect harm with Azure AI Content Safety Studio
Develop Generative AI solutions with Azure OpenAI Service
Full code for tutorial
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())
Microsoft Tech Community – Latest Blogs –Read More