Last active
June 13, 2023 13:27
-
-
Save cybye/9ed9bd1dc2c53906d7de4fcccfdd5b99 to your computer and use it in GitHub Desktop.
AutoDescribe a view and add to view documentation #jarchi
This file contains 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
/* | |
* AutoDescribe a view and add to view documentation | |
*/ | |
function CallApi(url,api_key, query) { | |
var imports = new JavaImporter(java.net, java.util, java.lang, java.io, java.nio.charset) | |
var result="" | |
with (imports) { | |
var urlObj = new URL(url) | |
var hcon = urlObj.openConnection() | |
hcon.setRequestProperty ("Content-Type", "application/json") | |
hcon.setRequestProperty ("api-key", api_key) | |
hcon.setRequestProperty ("user-agent", "curl/7.81.0") | |
hcon.setRequestProperty ("accept", "*/*") | |
hcon.setDoOutput(true); | |
var writer = new OutputStreamWriter(hcon.getOutputStream()); | |
if(query) { | |
var json = ""+ JSON.stringify(query); | |
writer.write(json); | |
} | |
writer.flush(); | |
writer.close(); | |
var line | |
var res = ""; | |
reader = new BufferedReader(new InputStreamReader(hcon.getInputStream(),StandardCharsets.UTF_8)); | |
while ((line = reader.readLine()) != null) { | |
res += line + "\n"; | |
} | |
reader.close(); | |
return JSON.parse(res); | |
} | |
} | |
var relation_translator_to = { | |
"composition-relationship" : "contains", | |
"association-relationship" : "is associated to", | |
"realization-relationship" : "realises", | |
"influence-relationship" : "influences", | |
"serving-relationship" : "serves", | |
"access-relationship" : "accesses", | |
"specialization-relationship" : "specializes", | |
"flow-relationship" : "flows to", | |
"assignment-relationship" : "is assigned to", | |
"triggering-relationship" : "triggers", | |
"aggregation-relationship" : "aggregates", | |
"diagram-model-connection" : "is connected to" | |
} | |
var access_translator = { | |
"access" : "accesses", | |
"read": "reads", | |
"write": "writes", | |
"readwrite" : "reads and writes" | |
} | |
function describe_relation_to(s,t, sep) { | |
if(!relation_translator_to[t.type]) console.log(t.type) | |
var ttype = relation_translator_to[t.type] | |
if(t.type == "access-relationship") { | |
ttype = access_translator[t.accessType] | |
if(!ttype) { | |
//console.log(t.accessType) | |
ttype = "accesses" | |
} | |
} | |
return (s?format_name(s) + " ":"") + (t.name ? t.name : ttype) + " the " + t.target.type + " " + format_name(t.target.name) + (sep?sep:". ") | |
} | |
function format_name(n) { | |
return n.trim().contains(" ") ? "\"" + n + "\"" : n; | |
} | |
function describe_type(t) { | |
return t; | |
} | |
function format_doc(d) { | |
if(false && d && d.trim().length > 0) { | |
return " and can be described as \"\"\" " + d + "\"\"\"" | |
} else | |
return "" | |
} | |
function describeView(v) { | |
var el = "" | |
el = "This is a description of the view " + $(v).parents().reverse().map(function(x) {return x.name }).join("/") + "/" | |
+ format_name(v.name) | |
+ " in the archimate model " + model.name | |
+ "\n\n" | |
if(false && v.documentation) | |
el += v.documentation + "\n\n" | |
var first = true | |
$(v).find("concept").each(function (x) { | |
if(!x.type.toString().contains('relationship')) { | |
if(first) { | |
first = false | |
el += " The view " + v.name + " contains the concepts: \n" | |
} else { | |
} | |
el += "* the " + x.type + " " + format_name(x.name) | |
var f_first = true | |
var outrel = $(x).outRels() | |
var size = outrel.length | |
var i = 0 | |
outrel.each(function(f) { | |
if(f_first) { | |
f_first = false | |
el += " which " | |
} | |
i ++ | |
el += describe_relation_to("",f, (i==size ? ".":", and ")) | |
}) | |
el += "\n" | |
} | |
}) | |
return el + "\n"; | |
} | |
function appendOrReplaceResult(text, res, model) { | |
const insertionpoint = '\n----- automatically generated' | |
var p = text.indexOf(insertionpoint); | |
if(p < 0) p = text.length | |
return text.substring(0,p) + insertionpoint + " at " + new Date() + " using " + model + " -----\n" + res | |
} | |
var currentView = $(selection).filter("archimate-diagram-model").first();//= selection.first(); | |
if (! currentView) { | |
console.log("> No view identified: exiting."); | |
exit(); | |
} else | |
console.log("> Processing view " + currentView.name) | |
function gpt(url, prompt, key) { | |
const res = CallApi(url, | |
key, | |
{ | |
"prompt": prompt, | |
"max_tokens": 1300, | |
"temperature": 0.1, | |
"frequency_penalty": 0.59, | |
"presence_penalty": 0, | |
"top_p": 0.5, | |
"stop": ["<|im_end|>","<|im_sep|>"] | |
} | |
) | |
return res; | |
} | |
const preprompt = `The facts contain concepts from the Archimate language. Use transitive reasoning, | |
i.e. if a is related to b and b is related to c, a is also related to c. If you find <>, e.g. <var>, treat them as variables. | |
As a technical writer, please use ONLY the below facts in a narrative using plain english. Do not ask questions, do not make things up, do not give examples. | |
Facts: | |
` | |
// then facts | |
const postfacts ="Provide your description of the facts here: " | |
const prompt = preprompt + "\n" + describeView(currentView) + "\n" + postfacts | |
console.log("> PROMPT") | |
console.log(prompt) | |
load(__DIR__ + "myLib/keys.ajs"); | |
const result = gpt(GPT_URL, prompt, GPT_KEY) | |
console.log("> RESULT") | |
console.log(JSON.stringify(result," ",2)) | |
// console.log(result.choices[0].text) | |
currentView.documentation = appendOrReplaceResult(currentView.documentation, result.choices[0].text.trim(), result.model) | |
// console.log(currentView.documentation) |
keys.ajs just contains two variables:
const GPT_KEY = "<your api key goes here>"
const GPT_URL = "<the url of your deployment>"
Sorry, cannot share mine.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you provide file : keys.ajs ???
I want ot try.
Thank in advance.