|
| 1 | +// Business Rule: Smart Incident Categorizer AI |
| 2 | +// Table: incident |
| 3 | +// When: before, insert |
| 4 | +// Filter Conditions: Category is empty |
| 5 | + |
| 6 | +(function executeRule(current, previous) { |
| 7 | + if (current.isNewRecord() && !current.category) { |
| 8 | + var categorizer = new SmartIncidentCategorizer(); |
| 9 | + var suggestedCategory = categorizer.categorizeIncident(current.short_description + ' ' + current.description); |
| 10 | + |
| 11 | + if (suggestedCategory) { |
| 12 | + current.category = suggestedCategory; |
| 13 | + current.work_notes = 'Category auto-assigned by AI: ' + suggestedCategory; |
| 14 | + } |
| 15 | + } |
| 16 | +})(current, previous); |
| 17 | + |
| 18 | +var SmartIncidentCategorizer = Class.create(); |
| 19 | +SmartIncidentCategorizer.prototype = { |
| 20 | + categorizeIncident: function(description) { |
| 21 | + try { |
| 22 | + var openai = new sn_ws.RESTMessageV2(); |
| 23 | + openai.setHttpMethod('POST'); |
| 24 | + openai.setEndpoint('https://api.openai.com/v1/chat/completions'); |
| 25 | + openai.setRequestHeader('Authorization', 'Bearer ' + gs.getProperty('openai.api.key')); |
| 26 | + openai.setRequestHeader('Content-Type', 'application/json'); |
| 27 | + |
| 28 | + var payload = { |
| 29 | + model: "gpt-3.5-turbo", |
| 30 | + messages: [{ |
| 31 | + role: "system", |
| 32 | + content: "You are an IT service desk categorizer. Return only one of these categories: network, hardware, software, database, security, email" |
| 33 | + }, { |
| 34 | + role: "user", |
| 35 | + content: "Categorize this incident: " + description |
| 36 | + }], |
| 37 | + max_tokens: 10, |
| 38 | + temperature: 0.1 |
| 39 | + }; |
| 40 | + |
| 41 | + openai.setRequestBody(JSON.stringify(payload)); |
| 42 | + var response = openai.execute(); |
| 43 | + |
| 44 | + if (response.getStatusCode() == 200) { |
| 45 | + var result = JSON.parse(response.getBody()); |
| 46 | + return result.choices[0].message.content.trim().toLowerCase(); |
| 47 | + } |
| 48 | + } catch (e) { |
| 49 | + gs.error('AI Categorizer Error: ' + e.message); |
| 50 | + } |
| 51 | + return null; |
| 52 | + } |
| 53 | +}; |
0 commit comments