In the new unified authoring canvas of Power Virtual Agents – now in preview – it's very easy to set and update variables: Use variables (preview) - Power Virtual Agents | Microsoft Learn. So, keeping the conversation context when interacting with an OpenAPI GPT model is very easy, as demonstrated by @remidyon in this article: Connecting OpenAI to Power Virtual Agent - Part 2: keeping the conversation rolling! | LinkedIn.
Now, how to achieve the same when on the production version of Power Virtual Agents, when variable management is a bit trickier and requires using Bot Framework Composer?
Let me show you how to get to the below result:
Hi @giorgiokatr, thanks!
Have a look at this article: Revolutionize your Enterprise Data with ChatGPT: Next-gen Apps w/ Azure OpenAI and Cognitive Search - Microsoft Community Hub
If you're using a ChatGPT model, I believe you can customize the initial system message prompt with instructions / scope / data, including what your documentation search could have initially returned.
In this other post, I show an example of Azure OpenAI ChatGPT integration with PVA: Solved: Retain conversation context when interacting with ... - Power Platform Community (microsoft.com)
@HenryJammesthis is amazing! i was wondering how to feed the chat with some data from some pdfs so it can answer to specific questions on that data only and no as chat gpt. is there a tutorial for this?
I'm going to use the Fallback topic so that I can use Power Automate to ask OpenAI GPT davinci engine whenever PVA doesn't find a matching topic for a user utterance. To keep track of the full conversation and pass it the OpenAI API – so that it provides answers in context of past questions and answers – I'll initiate and update a global variable.
To manage the global variable keeping track of the conversation with OpenAI, we're going to use Bot Framework Composer to do 2 things:
I start by opening the Bot Framework Composer
If you need help setting things up, refer to this: Getting started with Bot Framework Composer - Power Virtual Agents | Microsoft Learn
I then add a new dialog to my chatbot
I call it "InitializeFullDialog"
I create one new Output for it, that I call FullDialog, of type string:
In BeginDialog, I add a Manage properties > Set a property node, in order set the FullDialog variable to an empty value:
Property: dialog.result.FullDialog
Value: =''
I then add another dialog, SetFullDialog, this time with no outputs.
In the BeginDialog, I add a Manage properties > Set a property node, in order set the Power Virtual Agents ConversationFullDialog variable (the name of FullDialog within my PVA topic) as a concatenation of the previous ConversationFullDialog value as well as new values that Power Automate pass me back (I'm showing this later in this article, but they're OK to set up in advance).
Property: virtualagent.ConversationFullDialog
Value: =concat(virtualagent.ConversationFullDialog,' ',virtualagent.OriginalTriggerPhrase,' ',virtualagent.OpenAIResponseRaw)
Now I simply need to publish my bot in Bot Framework Composer:
As I interact in this example with the OpenAI Text Completion API, I'm using the Power Virtual Agents Fallback topic. This means that whenever a user asks a question that PVA doesn't know how to answer with an existing topic (i.e. there is no match with the topics trigger phrases), it goes to the Fallback topic, and in the Fallback topic we're calling the OpenAI API.
If you haven't enabled the Fallback topic in your chatbot, learn how here: Use a system fallback topic - Power Virtual Agents | Microsoft Learn
In the Fallback topic, I start by removing the existing nodes as we expect to change its behavior.
Just after the 'Trigger Phrases' node, I add a "Redirect to another topic" node, and select the InitializeFullDialog dialog.
I click on the variable name, name it ConversationFullDialog, and change its usage to "Bot (any topic can access)" and also check "External sources can set values":
Next step is to call the OpenAI API using Power Automate.
I add a new node, select Call an action > Create a flow.
I assume you already have an OpenAI account and a developer API key, but if you don't, check: OpenAI for developers and where do I find my Secret API Key?.
In your cloud flow, you'll first want to add 2 text inputs, UnrecognizedTriggerPhrase and ConversationFullDialog:
Then, to call the OpenAI APIs, I'll use the HTTP action, with this configuration
Method: POST
URI: https://api.openai.com/v1/completions
Headers:
Authorization: Bearer {YourAPIKey}
Content-Type: application/json
Body:
{
"model": "text-davinci-003",
"prompt": "",
"max_tokens": 2000,
"temperature": 0,
"top_p": 1,
"n": 1,
"stream": false,
"logprobs": null,
"stop": "None"
}
For the prompt, this is where I need to pass the user question.
To keep context, the trick is to provide past questions and answers, as well as the new question.
So, in order to know if I should pass the initial value that triggered a fallback OR if I should pass the ConversationFullDialog that contains the history, I use an expression to check if the ConversationFullDialog is empty or not. If it is, I just used the UnrecognizedTriggerPhrase. If not, I append the ConversationFullDialog and the new UnrecognizedTriggerPhrase:
if(
empty(triggerBody()['text_1']),
triggerBody()['text'],
concat(
triggerBody()['text_1'],
' ',
triggerBody()['text']
)
)
Now I need to pass back a few things to PVA.
uriComponentToString(
replace(
encodeUriComponent(
body('HTTP:_POST_Request_to_OpenAI_Completions_API')?['choices'][0]?['text']
),
'%0A',
'%0A%0A'
)
)
body('HTTP:_POST_Request_to_OpenAI_Completions_API')?['choices'][0]?['text']
I give my flow a name, and save it.
Back in PVA, still in my Fallback topic, I select my flow, and can map the UnrecognizedTriggerPhrase and ConversationFullDialog to the Power Automate inputs.
I can see that Power Automate has 3 ouputs. I set their Usage to "Bot (any topic can access)":
I now add a new Show a message node, where I select the bot.OpenAIResponse variable:
The final step is to add a "Redirect to another topic" node, and select the SetFullDialog dialog, that will take care of updating the ConversationFullDialog variable with the past questions and answers.
And that's it! 🎉