Chat GPT Powered Teams Private Virtual Agent
Tags: pva, chatbot, chatgpt, powerautomate
Published: 2023-07-19 08:04:57

![chatbot demo](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ulkjwwzq9h1tjl90m7pe.gif)
Demo based on Amazon Delivery Info [here](https://www.amazon.co.uk/gp/help/customer/display.html?ref_=hp_left_v4_sib&nodeId=GNLLY9HNDPETDHYV)

Full Private Virtual Agents are great, the lates update to its NLP and auto topic creation make it incredibly powerful. But unlike Power Automate and Power Apps its locked behind expensive license and usage costs.

So if we wanted to generate multiple knowledge article chatbots bots we really need a different option.

That leaves 2 options, Power Apps and Power Virtual Agents for Teams. Surprisingly Power Apps has more power to create intelligent chatbots then PVA for teams, as the Teams Dataverse environments don't allow AI Builder or Custom Connectors. Fortunately, there is a work around, and now an easy way to leverage Chat GPT functionality.

![chose pva](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/trqo1s2jquu7uqsv9jqc.png)

There are a couple of caveats:

- The solution isn't great from a security side
- Although free at the moment there is a usage cost
- Its an Azure GPT model not OpenAi (but little difference)

There are the following key sections to the solution
1. Front End
2. GPT integration
3. Data Modelling
4. End to End Solution

---

## 1. Front End
As stated we are going with Teams PVA, but this could easily be swapped out with a Canvas Power App (if wanted to embed into SharePoint site etc).

![canvas app chat gpt](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wo9h0wykngyf9t03rwzp.png)

## 2. GPT integration
There are lots of ways to add GPT integrations (Premium connectors, custom connectors, http connector), but the easiest is the new `Create text with GPT` AI Builder connector.

![create text with GPT](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hy70u91j7k48q3u0g24q.png)

We can call the connector in a flow and pass the data back to the chatbot (or Power App if you go that way).
I've written a full blog about how to use it and how cool it is [here](https://dev.to/wyattdave/new-power-automate-gpt-connector-58g7)

## 3. Data Modelling

The only issue with the connector (and ChatGPT for that matter) is the token (character limit), there is no documentation yet on the connectors limit (OpenAI limit is 2048), but it means we need to be creative when we are analysing large documents of data.

This is where a little work is required, we need to transfer knowledge article data into a database/list (SharePoint works great). It's a pain as most business guides etc are in pdf or site pages, but as an overall strategy it's better in the long run for data accuracy and maintenance.

Now we have our data indexed with can filter the list to get the context, the challenge is how do we know what to query for. Luckly we already have the answer, the GPT model can summarize the key words from the question (so we use the questions as the context). We then filter the text to see if it contains the key words. During testing I found that the sweet spot was 3 words but ignore the third (as sometimes this would be a more related word then required).

![key words](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgagcv84s2kd9paiwpke.png)

We then loop over all the filtered items and append to a string variable.
I also had a fall back that would return any of the key words, so the flow would filter by both key words, else if non returned then by either key word. 

![question](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yg8g5um8nywvx54yj57s.png)
_note I found the 'Fail threshold and action' parameter had unexpected impacts so I removed it_


## 4. End to End Solution
Our end to end solution looks like this:

![chatbot process](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8frqkhdm8mm0b2mjkmkc.png)

But we have now one more issue to handle, and this is where it gets a little messy (from a security side), how do we use the AI Builder in Dataverse for Teams when it's not allowed.

The trick is we will need to pass our request to a different environment flow, to do this we need the HTTP actions:

- HTTP - to call and receive the data from the different environment flow
- When a HTTP request is received - to trigger the different environment flow

Security issue one is the When a `HTTP request is received` end point is open, so anyone could call it.

There are a couple of ways to secure it, but to keep it simple I went with an API key in the header.

![http request](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/motqwyfet131dz8c1n5x.png)

In this case the API key is called 'secretkey' and its value is 'secretValueToRunThisFlow'.

In the `When a HTTP request is received` trigger we then have a trigger condition to check the key

![trigger conditions](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mdh0p9yv8461dy175uj9.png)

The second security issue is the secretkey value, it's not stored in an Azure key vault, so anyone with access to the flows could see it in plain text. The logs would also show it unless we secure inputs for both.

![chatbot process 2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kwrgt96x7vfcxjionoex.png)

If security isn't happy then we can always fall back to replacing PVA with a Canvas App (no need for the HTTP hops anymore).

**PVA Flow**
![PVA Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v98b8akh5n3qppcez2yc.png)

**GPT Flow**
![GPT Flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2aate2uefhodb5sokdqq.png)

**PVA**

![pva](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4f3u4myn89d9qlqjr9zu.png)
The PVA topic should be the default or called by the greeting

And there you have it, a fully-fledged chatbot in teams, for free(ish). Whats cool is you could store all your knowledge data in one list with an additional chatbot field. Then different chatbots could call the same parent flow which just filters the data for it.

---

**Additional Requirements**
As I didn't want to go into too much detail I didn't include exception handling for:

- No topics found
- Escalation for incorrect answer