The Power Platform is a suite of tools that allows businesses and individuals to create custom applications and automate workflows without the need for extensive coding. One of the most exciting features of the Power Platform is its ability to connect with external services and data sources. In this blog post, we will walk you through the process of building a custom connector in the Power Platform to connect to OpenAI's ChatGPT, a powerful and growing language model. By the end of this guide, you'll be able to integrate the capabilities of ChatGPT into your Power Platform applications and workflows.
What is OpenAI's ChatGPT?
OpenAI's ChatGPT is a state-of-the-art language model that can generate human-like text based on the input and prompts it receives. It's a versatile tool that can be used for tasks such as natural language understanding, content generation, and more. Integrating ChatGPT with the Power Platform opens up a world of possibilities for automating conversations and text-based processes.
Prerequisites
Before you start building your custom connector, you will need the following:
- A Power Platform account: You can sign up for a Power Platform account if you don't already have one.
- Access to ChatGPT: To use ChatGPT, you'll need an API key from OpenAI. You can obtain this by signing up on the OpenAI platform.
Now, their are costs to these services as expected, and we'll take a look at those below.
Are There Any Costs Involved?
The cost of using custom connectors in the Power Platform depends on the specific licensing and subscription model you have with Microsoft. It's important to mention that these costs could change in the time that this post has been published.
Microsoft
- Power Apps and Power Automate Per User Plan: If you have a Power Apps or Power Automate Per User Plan, you can use custom connectors within your allocated capacity without any additional charges. These plans typically provide a certain number of runs or actions per user per month, and custom connector usage falls within these allocations.
- Power Apps and Power Automate Per App or Per Flow Plan: If you are on a Per App or Per Flow Plan, custom connector usage may be subject to additional costs. These plans have limits on the number of runs or actions you can perform per app or flow, and custom connector calls are included in these counts. If you exceed these limits, you may need to pay for additional capacity.
- Power Apps and Power Automate Premium Plans: Power Apps and Power Automate also offer premium plans that provide increased capacity and features. Custom connectors can be used with these plans, and the cost may vary depending on the specific premium plan you select.
It's essential to check Microsoft's official pricing and licensing documentation, as well as any updates that may have occurred, to get the most accurate and up-to-date information regarding the costs of using custom connectors in the Power Platform. Pricing structures can change, and they often depend on factors such as the number of users, usage limits, and the specific features and capabilities included in your subscription.
OpenAI
The cost of using OpenAI's API depends on the specific API endpoint you are using, the volume of usage, and the pricing plan offered by OpenAI.
- GPT-3 API: OpenAI's GPT-3 API, which includes ChatGPT, is typically priced based on the number of tokens processed. You are billed for both input and output tokens. The price per token may vary depending on the usage level and the plan you choose.
- Data and Model Costs: In addition to token-based pricing, there may be additional costs associated with accessing certain data sources or fine-tuned models that are provided by OpenAI.
Keep in mind that OpenAI may adjust its pricing and offerings over time to reflect market demand and the evolving capabilities of its models. Therefore, it's essential to visit OpenAI's official website and review their pricing and terms of service to get the most accurate and current information regarding the cost of using OpenAI's API.
Step 1: Set Up Your Custom Connector
- Log in to your Power Platform environment.
- In the Power Platform admin center, select the environment where you want to create the custom connector.
- Click on the Data tab and then choose Custom connectors.
- Select New custom connector to begin the setup process.
Step 2: Define Your Connector & Security Details
In this step, you'll need to define the following details:
- General Information: Provide a name and a brief description for your custom connector.
- Host: For ChatGPT, the host will be the base URL for the API endpoint. (api.openai.com)
- Authentication: Depending on the OpenAI authentication method you're using (e.g., API key or OAuth), configure the authentication settings accordingly.
You'll set up the authentication connection later, here we are simply defining how the connector will authenticate.
Step 3: Define Your Actions
Actions represent the operations you can perform with your custom connector. For ChatGPT, you might define actions like GetResponse to send a query to the model and receive a text response.
- Click on New action.
- Select Import from sample
- Define the action request details, including the HTTP method (usually POST for ChatGPT) and the URL (the endpoint provided by OpenAI).
- Set the request and response parameters. In the case of ChatGPT, you will likely define parameters for the input prompt and receive the model's response.
Verb: Post
URL: https://api.openai.com/v1/chat/completions
Headers: Content-Type: application/json
Body: {"model": "gpt-3.5-turbo", "messages": [{"role": "user", "content": "Say this is a test!"}], "temperature": 0.7}
Step 4: Test Your Connector
Before using your custom connector in actual applications or workflows, it's crucial to test it to ensure that it's working correctly. Use the Test operation option to validate your actions and ensure that you're getting the expected results from ChatGPT.
Step 5: Use Your Connector in Power Apps or Power Automate
Once your custom connector is successfully tested and ready, you can start using it in Power Apps or Power Automate. You can now incorporate the capabilities of ChatGPT into your applications and workflows.
To do this, you'll need to access the new custom connector from either Power Apps or Power Automate.
- Power Apps: your connector can be found under the Data tab on the left hand side where you can search or navigate to the name you used when creating the connector.
- Power Automate: you'll find it under the actions alongside a list of requests that you have set up inside the connector itself.
For those familiar with Power Automate, it shouldn't need to much direction on how to use the new connector as input prompts are automatically provided for you when you create a new action. For Power Apps, it's less intuitive. To allow users to interact with ChatGPT from within your app, you'll need to provide a formula to speak directly to the connector.
It will look like this:
Set(varResponse, OpenAIChatGPTPoker.Test1({model: "gpt-3.5-turbo",messages: [{role: "user", content: "What is the capital of Japan"}], temperature: "0.7"}))
Here we are using the OnSelect of a button, setting a variable named varResponse (this is so we can store the response coming back from ChatGPT), then passing in required information as part of this POST request. We are providing it with the ChatGPT model to use, alongside the message (what we want to ask ChatGPT) and the temperature. The temperature can be a value between 0 and 1 where 1, behaves more humanoid in its response.
You'll need to enumerate into the action using the connectors name in formula to retrieve ChatGPT's response.
First(varResponse.choices).message.content
This is retrieving the first response from our ChatGPT request and enumerating into the answer and displaying it in a Text Label.
It's important to note, you can set default values for the expected model, messages and temperature from inside the connector setup pages, under the Request section of the Definition screen.
Conclusion
Building a custom connector in the Power Platform to connect to OpenAI's ChatGPT opens up endless possibilities for automating text-based interactions and content generation. With the flexibility and ease of use provided by the Power Platform, you can create intelligent applications and workflows that leverage the power of ChatGPT to enhance your business processes. Want to see how Collab365 Coach Connor used this to build a Poker Game in Power Apps? Join the Collab365 Platinum Plan to gain access to the full series!
Remember to always follow OpenAI's terms of service and data usage policies when using ChatGPT in your applications. With a custom connector, you're well on your way to harnessing the potential of AI-powered text generation within your Power Platform environment.