-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from earlduque/summary
Create Summarize the thread so far.js
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
activation_example:!catchmeup or !summary | ||
regex:!(catchmeup|summary|catchup) | ||
flags:gmi | ||
*/ | ||
|
||
( function( current ){ | ||
var si = new Slacker(); | ||
var thread_ts = current.getValue( 'thread_ts' ); | ||
|
||
if( !thread_ts ){ | ||
message = 'This command is only usable in a thread.'; | ||
si.send_chat( current, message, false ); | ||
return null; | ||
} | ||
|
||
var chats = []; | ||
var chatGr = new GlideRecord('x_snc_pointsthing_chat'); | ||
chatGr.addEncodedQuery('thread_ts=' + thread_ts + '^ORts=' + thread_ts + '^textNOT LIKE!catchmeup^textNOT LIKE!summary^textNOT LIKE!catchup'); | ||
chatGr.orderBy('sys_created_on'); | ||
chatGr.query(); | ||
while (chatGr.next()){ | ||
var chat = chatGr.getDisplayValue('user') + ': ' + chatGr.getValue('text'); | ||
chats.push(chat); | ||
} | ||
|
||
var prompt = current.text.replace(/!chatgpt/gmi, "").trim().substring(0, 1000); | ||
var chatReq = new sn_ws.RESTMessageV2(); | ||
chatReq.setEndpoint('https://api.openai.com/v1/chat/completions'); | ||
chatReq.setHttpMethod("POST"); | ||
chatReq.setRequestHeader("Authorization", "Bearer " + gs.getProperty("openai.key")); | ||
chatReq.setRequestHeader('Content-Type', "application/json"); | ||
chatReq.setRequestHeader('User-Agent', "ServiceNow"); | ||
chatReq.setRequestHeader("Accept", "*/*"); | ||
var body = { | ||
"model": "gpt-3.5-turbo", | ||
"messages": [{"role": "user", "content": "summarize the following conversation. You cannot ask for follow-up responses. Ignore the user named Slackbot.\n\n" + chats.join("\n")}], | ||
// "max_tokens": 250 | ||
}; | ||
chatReq.setRequestBody(JSON.stringify(body)); | ||
var chatResponse = chatReq.execute(); | ||
gs.info(chatResponse.getBody()); | ||
var chatResponseBody = JSON.parse(chatResponse.getBody()); | ||
|
||
var show_tokens = false; | ||
var token_cost = show_tokens ? "> tokens: " + chatResponseBody.usage.total_tokens + " ($" + (parseInt(chatResponseBody.usage.total_tokens) * 0.000002).toFixed(6) + ")\n" : ""; | ||
|
||
new x_snc_slackerbot.Slacker().send_chat(current, "> This thread so far:\n" + token_cost + "\n" + chatResponseBody.choices[0].message.content, false); | ||
} )( current ); |