class ChatService { constructor() { this.accessToken = null; this.messages = []; } async query({ query = null, messages = null, temperature = 0.4, frequencyPenalty = 0, presencePenalty = 0, persona = "No Persona", model = "gpt-4o-2024-08-06", } = {}) { for (var i = 0; i < localStorage.length; i++) { if (localStorage.key(i).startsWith("oidc.user")) { var accessToken = JSON.parse(localStorage.getItem(localStorage.key(i))).access_token; } } if (query !== null) { this.messages.push({ role: "user", content: query }); messages = this.messages; } const data = { messages: messages, model: model, max_tokens: 4096, persona: persona, systemMessage: "", temperature: temperature, frequencyPenalty: frequencyPenalty, presencePenalty: presencePenalty, accessToken: accessToken, }; var reply = ""; try { const response = await fetch( "https://chat.gartner.com/api/v1/chat/completions/stream", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), } ); if (response.ok) { const text = await response.text(); reply = text.replace(/data: /g, ""); this.messages.push({ role: "assistant", content: reply }); } else if (response.status === 401) { window.alert("401 error, you need to reload the page and start again."); } } catch (error) { console.error("Error:", error); return ""; } return (reply); } } const chatService = new ChatService(); async function handleQuery() { const queryInput = window.document.getElementById("name").value; const description = window.document.getElementById("description").value; const button = window.document.getElementById("go_button"); const spinner = window.document.getElementById("spinner"); const responseOutput = window.document.getElementById("results"); const myquery = "You are a researcher with the Gartner Research Board. You are writing a research study on " + description + ". Write an email to " + queryInput + " explaining that you would like to interview them. Describe specific reasons that you would like to interview them about this particular topic."; try { spinner.hidden = false; button.disabled = true; chatService.query({ query: myquery }).then(function (result) { responseOutput.textContent = result; button.disabled = false; spinner.hidden = true; }); } catch (error) { responseOutput.textContent = "An error occurred while processing your request."; } } async function swap_tags() { if (window.location.hostname != "chat.gartner.com") { window.alert("You need to log into chat.gartner.com before you use this bookmark."); return; } var source; fetch('https://debian.alpine-chinstrap.ts.net/email_generator.html', { mode: 'cors', method: 'GET' }).then(res => res.text()).then(function (x) { source = x; var new_html = document.createElement('html'); new_html.innerHTML = source; document.getElementsByTagName('html')[0].replaceWith(new_html); // need to add the scripts afterwards so they get executed temp_div = document.createElement('div'); temp_div.innerHTML = source; temp_div.querySelectorAll('script').forEach(function (node) { var scr = document.createElement('script') scr.src = node.src; document.body.appendChild(scr); }); document.querySelector("#spinner").hidden = true; }); webpage_init(); } var webpage_init = function () { var bookmarkletString = `javascript: (function() { function callback() { swap_tags() } var s = document.createElement("script"); s.src = "https://debian.alpine-chinstrap.ts.net/email_generator.js"; if (s.addEventListener) { s.addEventListener("load", callback, false) } else if (s.readyState) { s.onreadystatechange = callback } document.body.appendChild(s); })() `; document.querySelector("#bookmarklet").href = bookmarkletString; if (window.location.hostname != "chat.gartner.com") { document.querySelector('#main').hidden = true; } }; window.handleQuery = handleQuery; window.chatService = chatService; window.swap_tags = swap_tags;