Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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 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 = "";
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.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\": \"text-davinci-003\" " +
",\"temperature\": 1 " +
",\"max_tokens\": 2048 " +
",\"stop\": \".\" }";
var res = client.PostAsync(uri, new StringContent(body, Encoding.UTF8,"application/json"));
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;
Copy link

adamsabourin commented Mar 14, 2023 via email

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