Skip to content

Instantly share code, notes, and snippets.

@cybye
Last active September 20, 2024 04:11
Show Gist options
  • Save cybye/9ed9bd1dc2c53906d7de4fcccfdd5b99 to your computer and use it in GitHub Desktop.
Save cybye/9ed9bd1dc2c53906d7de4fcccfdd5b99 to your computer and use it in GitHub Desktop.
AutoDescribe a view and add to view documentation #jarchi
/*
* 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)
@cybye
Copy link
Author

cybye commented Mar 29, 2023

image

becomes

----- automatically generated at Wed Mar 29 2023 19:31:12 GMT+0200 (CEST) using gpt-35-turbo -----
The Default View is a view in the GPT archimate model. It contains several concepts that are related to each other. The User is a business-actor that asks the Question data-object and receives the Answer data-object. The Question & Answer business-interaction triggers the Simulated Intelligence business-actor and accesses several data-objects, including Final Query template, Documents indexed by Vectors, Question, Answer, History Question Template, and Dialog History. The Formulate Query application-process accesses Final Query template and triggers Return Result application-process while getting Completion from LLM application-service. Compute Embedding application-process gets Vector from LLM (embeddings) application-service and triggers Find top k neareast documents application-process. Receive Question application-process accesses Question data-object and triggers Rephrase to standalone Question application-process. Find top k neareast documents application-process accesses Documents indexed by Vectors data-object and triggers Formulate Query application-process. Return Result application-process accesses Answer data-object. Rephrase to standalone Question application-process triggers Compute Embedding application-process while accessing History Question Template data-object and rephrasing to standalone LLM service while accessing Dialog History data-object which contains both Answer and Question data-objects. Simulated Intelligence is another business-actor in this view while LLM (embeddings), LLM are two different types of Application Services.

@pararsma
Copy link

pararsma commented May 9, 2023

Can you provide file : keys.ajs ???

I want ot try.

Thank in advance.

@cybye
Copy link
Author

cybye commented May 9, 2023

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