Skip to content

Instantly share code, notes, and snippets.

@dgosbell
Last active February 17, 2024 03:53
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dgosbell/f3253c7ec52efe441b80596ffddea07c to your computer and use it in GitHub Desktop.
Save dgosbell/f3253c7ec52efe441b80596ffddea07c to your computer and use it in GitHub Desktop.
This is a script for tabular editor that will loop through all the measures in a model and get ChatGPT to write a description of the calculation.
#r "System.Net.Http"
using System.Net.Http;
using System.Text;
using Newtonsoft.Json.Linq;
// You need to signin to https://platform.openai.com/ and create an API key for your profile then paste that key
// into the apiKey constant below
const string apiKey = "<YOUR API KEY HERE>";
const string uri = "https://api.openai.com/v1/completions";
const string question = "Explain the following calculation in a few sentences in simple business terms without using DAX function names:\n\n";
using (var client = new HttpClient()) {
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);
foreach (var t in Model.Tables)
{
foreach ( var m in t.Measures)
{
// Only uncomment the following when running from the command line or the script will
// show a popup after each measure
//Info("Processing " + m.DaxObjectFullName)
//var body = new requestBody() { prompt = question + m.Expression };
var body =
"{ \"prompt\": " + JsonConvert.SerializeObject( question + m.Expression ) +
",\"model\": \"gpt-3.5-turbo-instruct\" " +
",\"temperature\": 1 " +
",\"max_tokens\": 2048 " +
",\"stop\": \".\" }";
var res = client.PostAsync(uri, new StringContent(body, Encoding.UTF8,"application/json"));
res.Result.EnsureSuccessStatusCode();
var result = res.Result.Content.ReadAsStringAsync().Result;
var obj = JObject.Parse(result);
var desc = obj["choices"][0]["text"].ToString().Trim();
m.Description = desc + "\n=====\n" + m.Expression;
}
}
}
@adamsabourin
Copy link

Screenshot 2023-03-13 114843

@adamsabourin
Copy link

Any ideas here Darren?

@adamsabourin
Copy link

Only 2 measures in the testing model...

@dgosbell
Copy link
Author

No sorry I have no idea. That error is coming from the OpenAI servers. You'd have to ask them. A free account should be able to make 20 requests per minute.

I do have an updated version of the script here https://gist.github.com/dgosbell/4cc2c459214961a8d308ae4a8cab7822 which I posted in the follow up to my original blog post. It adds a delay to account for the API throttling, but that would only help when you have more measures in your model than the number of requests your account is allowed to issue per minute.

@adamsabourin
Copy link

adamsabourin commented Mar 13, 2023 via email

@dgosbell
Copy link
Author

If you have some measures selected the updated script will use those, otherwise it goes over all the measures in the model.

@adamsabourin
Copy link

adamsabourin commented Mar 14, 2023 via email

@Power-BI-guy
Copy link

Hi Darren,
This was giving me 404 errors until I changed the model to the following:

","model": "gpt-3.5-turbo-instruct" " +

@dgosbell
Copy link
Author

@Power-BI-guy Thanks. it looks like they deprecated the previous model https://platform.openai.com/docs/deprecations/2023-07-06-gpt-and-embeddings I've updated the script to use "gpt-3.5-turbo-instruct" too. I've also updated this more complicated sample which has some code to deal with the rate limiting https://gist.github.com/dgosbell/4cc2c459214961a8d308ae4a8cab7822

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment