The Chitchat of the other kind - ChatGPT in Power Virtual Agents
Published: 2023-01-14 11:35:15

---
title: The Chitchat of the other kind - ChatGPT in Power Virtual Agents
series: 101 - Power Virtual Agents
published: true
date: 2023-01-14 11:11:21 UTC
tags: 
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qas8lhz3u5kt26loxzth.jpg
canonical_url: https://the.cognitiveservices.ninja/the-chitchat-of-the-other-kind-chatgpt-in-power-virtual-agents
---
Picture of <a href="https://unsplash.com/@wocintechchat?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Christina @ wocintechchat.com</a> on <a href="https://unsplash.com/de/fotos/OW5KP_Pj85Q?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>

> Note: a lot has changed since the release of the article in Jan 2023  
> Everything described below is still working (2023.05.22), but I would suggest using [OpenAI Services on Azure](https://the.cognitiveservices.ninja/series/openai-on-azure) instead of OpenAI directly. as with Azure OpenAI Services you have control regarding your Data.

## Motivation

While working with bots, you must consider two points,  
_how to present the answer_ and  
_where does the answer come from?_  
With all articles you might have read in the last few days about openais ChatGPT, you could think - why not integrate this in my installation as a last line of defense to answer when my system is incapable of doing so - at least for chitchat.

This article demonstrates the "how". But do not forget to read the entire article and find some thoughts about the "why" in the conclusions.

## OpenAi

To use openai, you need to create an account with them at [www.openai.com](https://www.openai.com) and generate an API token.

After creating an account and adding some funds as pocket money,  
Click top right on your account and the on "View API Keys"


![Figure 1: Account Settings](https://cdn.hashnode.com/res/hashnode/image/upload/v1673031157730/4664512c-840a-435f-ab83-0732928e4538.png)

<figcaption>Figure 1: Account Settings</figcaption>

Generate a new API Key

---
![Figure 2: Create new secret key](https://cdn.hashnode.com/res/hashnode/image/upload/v1673031253555/dbc8f7fd-39a2-4d29-bf3b-ed431bc339f8.png)

<figcaption>Figure 2: Create new secret key</figcaption>

## Power Virtual Agent

### Activate the Fallback topic in Power Virtual Agent

Power Virtual Agent has a built-in topic that can be a hook to the outside world, the Fallback system topic.

To activate the Fallback system topic, go to Settings within the editing canvas (click on the cog symbol), then System fallback, and then +Add.

![Figure 3: activated fallback topic](https://cdn.hashnode.com/res/hashnode/image/upload/v1673020588423/06a5384d-111f-4ce0-bd65-66f283725526.png)

<figcaption>Figure 3: activated fallback topic</figcaption>

### Edit the fallback topic

Create a message box with a meaningful message and a new action.
![Figure 4: Fallback topic with message and action node](https://cdn.hashnode.com/res/hashnode/image/upload/v1673020748142/13c47fb9-fa50-4740-9884-aa51c3b1d875.png)

<figcaption>Figure 4: Fallback topic with message and action node</figcaption>

In Power Automate, create a new text-based variable, "UnrecognizedUserInput"

![Figure 5: text variable as input](https://cdn.hashnode.com/res/hashnode/image/upload/v1673020772610/fb5dac90-8575-4757-900d-e3c03938f5e8.png)

<figcaption>Figure 5: text variable as input</figcaption>

and an HTTP Node.

> The syntax for openai is straightforward. We need to authenticate ourselves within the header and give the model, the question, the temperature, and max\_tokens as the body. [[documentation](https://beta.openai.com/docs/api-reference/completions/create)].  
> temperature: Higher values mean the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.

![Figure 6: syntax sample](https://cdn.hashnode.com/res/hashnode/image/upload/v1673021199732/d6d5e346-8c55-4adf-8595-a5aad0e05fa8.png)

<figcaption>Figure 6: syntax sample</figcaption>
I translated this to Power Automate; it will look like the following in the HTTP node.  
The token you created above goes directly behind Bearer in the header.

![Figure 7: complete HTTP Request](https://cdn.hashnode.com/res/hashnode/image/upload/v1673030809705/806f894e-f4f6-4ae4-a7ff-7e67808a012d.png)

<figcaption>Figure 7: complete HTTP Request</figcaption>

As a next step, we create a String variable.

![Figure 8: Initialize a Variable](https://cdn.hashnode.com/res/hashnode/image/upload/v1673030841539/d2165238-f8d9-4a39-9794-54987a13e3b8.png)

<figcaption>Figure 8: Initialize a Variable</figcaption>  

Create a "Parse JSON node" to analyze the Body of the answer we get from openai.

![Figure 9: Parse JSON](https://cdn.hashnode.com/res/hashnode/image/upload/v1673030865692/7e4eca1c-ee81-4113-859f-122160a3994c.png)

<figcaption>Figure 9: Parse JSON</figcaption>  

You can use the following as a schema or generate it with the Body of the outcome of the previous step.

```
{
    "type": "object",
    "properties": {
        "id": {
            "type": "string"
        },
        "object": {
            "type": "string"
        },
        "created": {
            "type": "integer"
        },
        "model": {
            "type": "string"
        },
        "choices": {
            "type": "array",
            "items": {
                "type": "object",
                "properties": {
                    "text": {
                        "type": "string"
                    },
                    "index": {
                        "type": "integer"
                    },
                    "logprobs": {},
                    "finish_reason": {
                        "type": "string"
                    }
                },
                "required": [
                    "text",
                    "index",
                    "logprobs",
                    "finish_reason"
                ]
            }
        }
    }
}

```

After this, we create an "Apply to each" node with a "Set variable"

and we close by creating an output value.

![Figure 10: Last step](https://cdn.hashnode.com/res/hashnode/image/upload/v1673030950550/3c79006b-2a35-4681-aefd-7230e0d84ca6.png)

<figcaption>Figure 10: Last step</figcaption>

Back to Power Virtual Agent, we adjust the created action to the correct input and output values and create a new message box to speak/display the answer on openai.

![Figure 11: Completing Action Node](https://cdn.hashnode.com/res/hashnode/image/upload/v1673031884498/4105f3df-e4a8-407c-a5b6-38e0e9843a62.png)

<figcaption>Figure 11: Completing Action Node</figcaption>
## Test the result

Lets test the result within our configured channels as text chat or voice with Dynamics Customer Service 365 or AudioCodes VoiceAI.

![Figure 12: Output](https://cdn.hashnode.com/res/hashnode/image/upload/v1673032118952/f6a4b736-6565-4b37-9073-305e505186c2.png)

<figcaption>Figure 12: Output</figcaption>

## Conclusion

ChatGPT gives us answers, most of the time correct answers, and the models are getting better and better.

Is there a downside? Why should we integrate it or why should we not to do so?

Do the user or us will "like" the answer or is it appropriate? Here we have no control; we do not own the model.

**If you check the logs of your bots, you will see questions asked by users you would not even expect in your darkest dreams.**

There are situations where a technically correct answer is not the answer you will give to your users when they are, e.g., in highly emotional situations.