Skip to content

ServiceNow Model Abstraction

Server Side Script Include

The purpose of this, is to allow for easy calls to the API server via the midserver. This will accept the system message, and array of user and assistant messages. It will then call the API server and return the response.

var modelFormatter = Class.create();
modelFormatter.prototype = {
initialize: function() {},
tinydolphin: function(messages) {
/**
* This builds the prompt for the tinydolphin model, this uses ecma5 syntax
* It's output is like so
* <|im_start|>system
* {systemMessage}
* <|im_end|>
* <|im_start|>{messages[*].role}
* {messages[*].text}
* <|im_end|>
* <|im_start|>{messages[*].role}
* {messages[*].text}
* <|im_end|>
* <|im_start|>assistant
* @param {Array of objects} messages {role: 'user', text: 'message'}
* @returns {string} response
*/
// if first message is system, then it's the system message
var response = "";
var systemMessage = messages[0].role == 'system' ? messages.shift().text : '';
// if there is no then skip it
if (systemMessage) {
response = "<|im_start|>system\n" + systemMessage + "\n<|im_end|>\n";
} else {
response = "";
}
messages.forEach(function(message) {
response += "<|im_start|>" + message.role + "\n" + message.text + "\n<|im_end|>\n";
});
response += "<|im_start|>assistant\n";
return response;
},
mistral: function(messages) {
/**
* This builds the prompt for the mistral model, this uses ecma5 syntax
* It's output is like so
<s>[INST] You are a helpful code assistant. Your task is to generate a valid JSON object based on the given information. So for instance the following:
name: John
lastname: Smith
address: #1 Samuel St.
would be converted to:[/INST]
{
"address": "#1 Samuel St.",
"lastname": "Smith",
"name": "John"
}
</s>
[INST]
name: Ted
lastname: Pot
address: #1 Bisson St.
[/INST]
//this model has no role, it's just a system, and then different INST tags for each message
* @param {Array of objects} messages {role: 'user', text: 'message'}
* @returns {string} response
*
**/
// if first message is system, then it's the system message
var response = "";
var systemMessage = messages[0].role == 'system' ? messages.shift().text : '';
// if there is no then skip it
if (systemMessage) {
response = "<s>[INST]" + systemMessage + "\n";
} else {
response = "";
}
var lastMessage = messages.pop();
// this will build out the most in the <s>[INST]</s>, then add in the last message
messages.forEach(function(message) {
response += message.text + "\n";
});
response += "[/INST]\n[INST]\n";
response += lastMessage.text + "\n[/INST]";
return response;
},
phiQA: function(messages) {
/**
* Instruct: {{prompt}}
Output:
*/
// if first message is system, then it's the system message
var response = "";
var systemMessage = messages[0].role == 'system' ? messages.shift().text : '';
// if there is no then skip it
if (systemMessage) {
response = "Instruct: " + systemMessage + "\n";
} else {
response = "";
}
messages.forEach(function(message) {
response += message.text + "\n";
});
response += "Output: ";
return response;
},
phi2chat: function(messages) {
/**
* Human: Hello, who are you?
* AI: Greetings! I am an AI research assistant. How can I help you today?
* Human: Can you tell me about the creation of black holes?
* AI:
*/
var response = "";
// if first message is system, then it's the system message
var systemMessage = messages[0].role == 'system' ? messages.shift().text : '';
// if there is no then skip it
if (systemMessage) {
response = "AI: " + systemMessage + "\n";
} else {
response = "";
}
messages.forEach(function(message) {
// if message.role == assistant, AI
// if message.role == user, Human
var who = message.role == 'assistant' ? 'AI' : 'Human';
response += who + ": " + message.text + "\n";
});
},
tinyLlama: function(messages){
/*<|system|>
{{ .System }}</s>
<|user|>
{{ .Prompt }}</s>
<|assistant|>*/
var response = "";
// if first message is system, then it's the system message
var systemMessage = messages[0].role == 'system' ? messages.shift().text : '';
// if there is no then skip it
if (systemMessage) {
response = "<|system|>\n" + systemMessage + "</s>\n";
} else {
response = "";
}
messages.forEach(function(message) {
response += "<|" + message.role + "|>\n" + message.text + "</s>\n";
});
return response;
},
llama2: function(messages){
/*[INST] <<SYS>>{{ .System }}<</SYS>>
{{ .Prompt }} [/INST]
*/
var response = "";
// if first message is system, then it's the system message
var systemMessage = messages[0].role == 'system' ? messages.shift().text : '';
// if there is no then skip it
if (systemMessage) {
response = "[INST] <<SYS>>" + systemMessage + "<</SYS>>\n";
} else {
response = "";
}
messages.forEach(function(message) {
response += message.text + "\n";
});
response += " [/INST]";
return response;
},
mistrallite: function(messages){
/**<|prompter|>{{ .System }} {{ .Prompt }}</s><|assistant|>
* */
var response = "";
// if first message is system, then it's the system message
var systemMessage = messages[0].role == 'system' ? messages.shift().text : '';
// if there is no then skip it
if (systemMessage) {
response = "<|prompter|>" + systemMessage + " {{ .Prompt }}</s>\n";
} else {
response = "";
}
messages.forEach(function(message) {
response += "<|" + message.role + "|>\n" + message.text + "</s>\n";
});
return response;
},
notus: function(messages){
/*<|system|>
{{ .System }}
</s>
<|user|>
{{ .Prompt }}
</s>
<|assistant|>
*/
var response = "";
// if first message is system, then it's the system message
var systemMessage = messages[0].role == 'system' ? messages.shift().text : '';
// if there is no then skip it
if (systemMessage) {
response = "<|system|>\n" + systemMessage + "\n</s>\n";
} else {
response = "";
}
messages.forEach(function(message) {
response += "<|" + message.role + "|>\n" + message.text + "\n</s>\n";
});
return response;
},
starcoder: function(prefix, suffix){
/**
*
//"prompt": "<PRE>" + prefix + "<SUF>" + suffix + "<MID>",
*/
return "<PRE>" + prefix + "<SUF>" + suffix + "<MID>";
},
type: 'modelFormatter'
};