Skip to content

Instantly share code, notes, and snippets.

@PBIQueryous
Created April 6, 2024 04:38
Show Gist options
  • Save PBIQueryous/08ee4d4ab5361cf8efa3029a68742320 to your computer and use it in GitHub Desktop.
Save PBIQueryous/08ee4d4ab5361cf8efa3029a68742320 to your computer and use it in GitHub Desktop.
#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>";
const string uri = "https://api.openai.com/v1/completions";
const string question = "Please describe this power query code for me:\n\n";
using (var client = new HttpClient()) {
client.DefaultRequestHeaders.Clear();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + apiKey);
foreach (var t in Model.Tables)
{
foreach ( var p in t.Partitions)
{
// Only uncomment the following when running from the command line or the script will
// show a popup after each measure
//
//var body = new requestBody() { prompt = question + m.Expression };
string _type = Convert.ToString(p.SourceType);
string _exp = Convert.ToString(p.Expression);
if ( _type == "M" )
{var body =
"{ \"prompt\": " + JsonConvert.SerializeObject( question + p.Expression ) +
",\"model\": \"text-davinci-003\" " +
",\"temperature\": 1 " +
",\"max_tokens\": 256 " +
",\"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();
//Reference to your calculation group that should hold the calculation Items
var x =(Model.Tables["Documentation"] as CalculationGroupTable).CalculationItems[t.Name];
//deletes the old version
x.Delete();
var calculationItem1 = (Model.Tables["Documentation"] as CalculationGroupTable).AddCalculationItem();
//removes any quotes in the chatGPT description
var s = desc.Replace("\"", "");
calculationItem1.Expression = "\"" + s + "\"";
calculationItem1.Name = t.Name;
//Info("Processing " + t.Name) ;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment