OpenAI’s Completion API ChatGPT-3 is a powerful tool for natural language processing that can generate human-like responses and insights. Power BI is a data analytics service that enables users to visualize and analyze data. When these two are combined, they can generate natural language responses and insights based on data in Power BI.
In this article, we will walk you through how to use OpenAI’s Completion API ChatGPT-3 as a Web.Contents step in Power Query in Power BI to generate natural language responses and insights.
Creating an API with OpenAI: A Quick Guide
The first step is to get an API key from OpenAI. Once you have the API key, you can access the Completion API with it.
To create an API with OpenAI, you first need to sign up for an account on the OpenAI website. Once you’ve logged in, navigate to the API dashboard and click on “Create API Key”. Choose a name for your API key and select the type of plan you want to use. OpenAI offers both a free plan and a paid plan, depending on your needs.
Creating a new Power Query Step
To use the Completion API in Power Query, you will need to create a new query and select “Blank Query” as the data source. In the Advanced Editor, you can enter the following M code:
let
api_key = "YOUR_API_KEY_HERE",
model = "text-davinci-002",
prompt = "What is the average revenue per user?",
temperature = 0.7,
max_tokens = 1024,
n = 1,
stop = {"\n"},
url = "https://api.openai.com/v1/completions"
in
Web.Contents(
url,
[
Headers = [
#"Content-type" = "application/json",
#"Authorization" = "Bearer " & api_key
],
Content = Text.ToBinary(
Json.FromValue(
[
model = model,
prompt = prompt,
temperature = temperature,
max_tokens = max_tokens,
n = n,
stop = stop
]
)
)
]
)
In this example, we are generating a response to the prompt “What is the average revenue per user?” using the “text-davinci-002” model. The “temperature” parameter controls the level of randomness in the response, while the “max_tokens” parameter limits the length of the response. The “n” parameter specifies the number of responses to generate, and the “stop” parameter defines the end of the response.
Once you have the response from the Completion API, you can use it in your Power BI report. For instance, you could use the response to generate a natural language insight based on your data. This can be especially useful for presenting insights to non-technical stakeholders.
Real life example
Suppose you have extracted a list of tweets from Twitter and you want to perform sentiment analysis on the text using OpenAI’s GPT language model through the web.contents function in Power Query in Power BI. The GPT model is a powerful tool for natural language processing tasks and can be easily integrated into your Power BI workflow.
To get started, you can use the web.contents function in Power Query to call OpenAI’s API and pass in the text from the tweet as the input parameter. The API will then return a sentiment score for the text, which can be used in your analysis. Here’s an example of how you can use the web.contents function to perform sentiment analysis on a tweet:
Let’s say you have a table in Power BI that contains a column of tweets called “Text”. You can create a new column called “Sentiment Score” by using the following formula in Power Query:
let
Source = <your table>,
#"Added Custom" = Table.AddColumn(Source, "Sentiment Score", each Json.Document(Web.Contents("https://api.openai.com/v1/engines/davinci/completions", [
Headers = [#"Content-Type"="application/json", #"Authorization"="Bearer <your API key>"],
Content = Text.ToBinary("{\"prompt\": \"" & [Text] & "\", \"temperature\": 0.5, \"max_tokens\": 1, \"n\": 1, \"stop\": \"\"}")
]))[results][0][choices][0][text]),
#"Changed Type" = Table.TransformColumnTypes(#"Added Custom", {{"Sentiment Score", type number}})
in
#"Changed Type"
In this formula, you’re calling OpenAI’s API and passing in the text from each tweet as the “prompt” parameter. The API then returns a JSON response, which you parse using the Json.Document function to extract the sentiment score. Finally, you transform the data type of the “Sentiment Score” column to a number so that it can be used in your analysis.
By using the web.contents function and OpenAI’s GPT language model, you can easily perform sentiment analysis on a large dataset of tweets in Power BI. This can provide valuable insights into the sentiment of your audience and help you make data-driven decisions.
Conclusion
In conclusion, using OpenAI’s GPT language model through the web.contents function in Power Query in Power BI can be a powerful tool for performing natural language processing tasks such as sentiment analysis. By leveraging the capabilities of the GPT model, you can gain valuable insights into your data and make more informed decisions.
At Analytko, we specialize in helping businesses make sense of their data through advanced analytics and data visualization techniques. Whether you’re looking to perform sentiment analysis on tweets or any other type of text data, our team of experienced data analysts and Power BI experts can help you turn your data into actionable insights. Contact us today to learn more about how we can help you with your data analysis projects.