|
| 1 | +const apiKey = "YOUR API KEY"; // Make an API Key with 5 USD from here -> https://openai.com/index/openai-api/ |
| 2 | +const ModelType = "gpt-3.5-turbo"; // You can change the model type. Check here -> https://platform.openai.com/docs/models |
| 3 | + |
| 4 | +// Making another button of options by which the API will work |
| 5 | +function onOpen() { |
| 6 | + DocumentApp.getUi().createMenu("RayeedAI") |
| 7 | + .addItem("Chat AI", "call_the_func") |
| 8 | + .addToUi(); |
| 9 | +} |
| 10 | + |
| 11 | +// Function to save the conversation history to the properties service |
| 12 | +function saveConversationHistory(history) { |
| 13 | + const scriptProperties = PropertiesService.getScriptProperties(); |
| 14 | + scriptProperties.setProperty('conversationHistory', JSON.stringify(history)); |
| 15 | +} |
| 16 | + |
| 17 | +// Function to load the conversation history from the properties service |
| 18 | +function loadConversationHistory() { |
| 19 | + const scriptProperties = PropertiesService.getScriptProperties(); |
| 20 | + const history = scriptProperties.getProperty('conversationHistory'); |
| 21 | + return history ? JSON.parse(history) : []; |
| 22 | +} |
| 23 | + |
| 24 | +function call_the_func() { |
| 25 | + |
| 26 | + // Load the conversation history from the properties service |
| 27 | + let conversationHistory = loadConversationHistory(); |
| 28 | + |
| 29 | + // Activing the doc and Selecting the selectect element ans get the Text as a string |
| 30 | + const doc = DocumentApp.getActiveDocument(); |
| 31 | + const selectedText = doc.getSelection().getRangeElements()[0].getElement().asText().getText(); |
| 32 | + |
| 33 | + // body is the part where the response of the API has to be paste |
| 34 | + const body = doc.getBody(); |
| 35 | + |
| 36 | + // Change promp according to your interest |
| 37 | + const prompt = "This are the conversation history " + conversationHistory + ". Now, only say about " + selectedText + "within 30 words"; |
| 38 | + |
| 39 | + // Update the prompt directly to the conversation history |
| 40 | + conversationHistory.push({ role: "system", content: prompt }); |
| 41 | + |
| 42 | + const temperature = 0.2; // Measure the Randomness of the response |
| 43 | + const maxTokens = 100; // Maximum token will be use to generate response |
| 44 | + |
| 45 | + // Make Resquest to the API |
| 46 | + const requestBody = { |
| 47 | + model: ModelType, |
| 48 | + messages: conversationHistory, |
| 49 | + temperature, |
| 50 | + max_tokens: maxTokens, |
| 51 | + }; |
| 52 | + |
| 53 | + // Initializing request option |
| 54 | + const requestOptions = { |
| 55 | + method: "POST", |
| 56 | + headers: { |
| 57 | + "Content-Type": "application/json", |
| 58 | + Authorization: "Bearer " + apiKey, |
| 59 | + }, |
| 60 | + payload: JSON.stringify(requestBody), |
| 61 | + }; |
| 62 | + |
| 63 | + // Getting response and convering it using JSON parse. |
| 64 | + const response = UrlFetchApp.fetch("https://api.openai.com/v1/chat/completions", requestOptions); |
| 65 | + const responseText = response.getContentText(); |
| 66 | + const json = JSON.parse(responseText); |
| 67 | + const generatedText = "\n " + json['choices'][0]['message']['content']; // Fetching only the generated Text |
| 68 | + |
| 69 | + // Add the generated response to the conversation history as the role of assistant |
| 70 | + conversationHistory.push({ role: "assistant", content: generatedText }); |
| 71 | + |
| 72 | + // Save the updated conversation history back to the properties service |
| 73 | + saveConversationHistory(conversationHistory); |
| 74 | + |
| 75 | + Logger.log(generatedText); |
| 76 | + body.appendParagraph(generatedText.toString()); |
| 77 | +} |
0 commit comments