Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.
Register now!To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.
I have been using ChatGPT API in Power BI as a function to get response to prompts.
I am using the below code -
let
GetOpenAIResponse = (message as text) =>
let
apiKey = "YOUR_OPENAI_API_KEY",
modelId = "text-davinci-003",
openaiEndpoint = "https://api.openai.com/v1/engines/" & modelId & "/completions",
pre = "Analyze the following review : ",
postmsg = "prompt",
requestBody = "{ ""prompt"": """ & pre & message & postmsg & """, ""max_tokens"": 1024 }",
options = [
Headers = [
#"Authorization" = "Bearer " & apiKey,
#"Content-Type" = "application/json"
],
Content = Text.ToBinary(requestBody)
],
response = Web.Contents(openaiEndpoint, options),
jsonResponse = Json.Document(response),
choices = jsonResponse[choices],
text = choices{0}[text]
in
text,
ChatWithOpenAI = (message as text) =>
let
response = GetOpenAIResponse(message)
in
response
in
ChatWithOpenAI
and this works perfectly fine..
However, when i try to use the same code to access Azure OpenAI.. its throwing an authentication issue -
Adding that code too for reference..
let
GetOpenAIResponse = (message as text) =>
let
apiKey = "YOUR_OPENAI_API_KEY",
modelId = "my model id",
//modelId = "operational-excellence-model",
openaiEndpoint = "https://<deployment name>-openai-dev.openai.azure.com/openai/deployments/" & modelId & "/completions?api-version=2023-03-15-preview",
pre = "Analyze the following review : ",
postmsg = "prompt",
requestBody = "{ ""prompt"": """ & pre & message & postmsg & """, ""max_tokens"": 500}",
options = [
Headers = [
#"Authorization" = "Bearer" & apiKey,
#"Content-Type" = "application/json"
],
Content = Text.ToBinary(requestBody)
],
response = Web.Contents(openaiEndpoint, options),
jsonResponse = Json.Document(response),
choices = jsonResponse[choices],
text = choices{1}[text]
in
text,
ChatWithOpenAI = (message as text) =>
let
response = GetOpenAIResponse(message)
in
response
in
ChatWithOpenAI
Could soneone please help me with it?
Hi,
another way simpler way that works well for us is a ChatGPT window via powerapps. Querying the API via Power Automate and/or Azure functions involves far fewer hurdles and the flow is very easy to integrate into PowerApps.
You can take a look at the Power Bi Visual we are developing here. It essentially accopmplishes what you want. In the data source area of the visual it allows you to hook up the data sources you want OpenAI to have access to and then gives you a chatgpt like interface to talk about your data. We interact with OpenAI's API for a secure and confident visual. Let us know if you have any questions!
You can download it for free here: https://www.powerbiailens.com/