Forked from dgosbell/AutoPopulateMeasureDescriptionsFromOpenAI.csx
Last active
June 25, 2025 09:09
-
-
Save powerbiferrytales/4f4e6c21121dff75b9ce29704efa923d 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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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/chat/completions"; // Correct endpoint for GPT-4 | |
| const string question = "Explain the following calculation in a few sentences in simple business terms without using DAX function names:\n\n"; | |
| const int oneMinute = 60000; // the number of milliseconds in a minute | |
| const int apiLimit = 20; // a free account is limited to 20 calls per minute, change this if you have a paid account | |
| const bool dontOverwrite = true; // this prevents existing descriptions from being overwritten | |
| using (var client = new HttpClient()) { | |
| client.DefaultRequestHeaders.Clear(); | |
| client.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey); | |
| int callCount = 0; | |
| List<Measure> myMeasures = new List<Measure>(); | |
| myMeasures.AddRange(Selected.Measures); | |
| if (myMeasures.Count == 0) { | |
| myMeasures.AddRange(Model.Tables.Where(t => t.Measures.Count() > 0).SelectMany(t => t.Measures)); | |
| } | |
| foreach (var m in myMeasures) { | |
| if (dontOverwrite && !string.IsNullOrEmpty(m.Description)) continue; | |
| // === Construct valid JSON payload for chat/completions === | |
| var messages = new JArray { | |
| new JObject { | |
| { "role", "system" }, | |
| { "content", "You are a helpful assistant that explains DAX measures clearly." } | |
| }, | |
| new JObject { | |
| { "role", "user" }, | |
| { "content", question + m.Expression } | |
| } | |
| }; | |
| var payload = new JObject { | |
| { "model", "gpt-4" }, | |
| { "messages", messages }, | |
| { "temperature", 0.2 }, | |
| { "max_tokens", 800 } | |
| }; | |
| var res = client.PostAsync(uri, new StringContent(payload.ToString(), Encoding.UTF8, "application/json")); | |
| res.Result.EnsureSuccessStatusCode(); | |
| var result = res.Result.Content.ReadAsStringAsync().Result; | |
| var obj = JObject.Parse(result); | |
| var desc = obj["choices"][0]["message"]["content"].ToString().Trim(); | |
| m.Description = desc + "\n=====\n" + m.Expression; | |
| callCount++; | |
| if (callCount % apiLimit == 0) System.Threading.Thread.Sleep(oneMinute); | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The previous model looks to have been deprecated.
New Model:
gpt-3.5-turbo-instruct\