Skip to content

Instantly share code, notes, and snippets.

@ranfysvalle02
Created January 23, 2025 04:48
Show Gist options
  • Save ranfysvalle02/c6696f648fd74536b311961a52baf0f9 to your computer and use it in GitHub Desktop.
Save ranfysvalle02/c6696f648fd74536b311961a52baf0f9 to your computer and use it in GitHub Desktop.
/**
* Main function to get comments from the active Google Document, send them to the Azure OpenAI API, and log the responses.
*/
function respondToComments() {
try {
// Get the active document and its ID
var doc = DocumentApp.getActiveDocument();
var documentId = doc.getId();
// Get the body text of the document
var documentContent = doc.getBody().getText();
// Fetch comments using your provided method
var comments = getComments(documentId);
if (comments.length > 0) {
Logger.log("Processing comments in the document:");
comments.forEach(function(comment) {
Logger.log("Author: " + comment.author.displayName);
Logger.log("Comment: " + comment.content);
Logger.log("Created Date: " + comment.createdTime);
// Construct the prompt
var prompt = "Given the following document content:\n\n" + documentContent + "\n\nPlease respond to this comment:\n\n" + comment.content;
// Send the prompt to the Azure OpenAI API and get the response
var aiResponse = getAIResponseFromAzureOpenAI(prompt);
// Log the AI response
Logger.log("AI Response: " + aiResponse);
Logger.log("---------------");
});
} else {
Logger.log("No comments found.");
}
} catch (e) {
Logger.log("Error: " + e.toString());
}
}
/**
* Fetches comments from the specified document using your method.
*
* @param {string} documentId - The ID of the Google Document.
* @return {Array} An array of comment objects.
*/
function getComments(documentId) {
var comments = [];
try {
var fields = 'comments(author/displayName,content,createdTime),nextPageToken';
var pageToken = null;
do {
var commentsResponse = Drive.Comments.list(documentId, {
fields: fields,
pageToken: pageToken
});
if (commentsResponse.comments && commentsResponse.comments.length > 0) {
comments = comments.concat(commentsResponse.comments);
}
pageToken = commentsResponse.nextPageToken;
} while (pageToken);
} catch (e) {
Logger.log("Error fetching comments: " + e.toString());
}
return comments;
}
/**
* Sends the prompt to the Azure OpenAI API and returns the response.
*
* @param {string} prompt - The prompt to send to the AI model.
* @return {string} The AI model's response.
*/
function getAIResponseFromAzureOpenAI(prompt) {
// Your Azure OpenAI endpoint and deployment name
var azureEndpoint = 'https://_____________.openai.azure.com';
var deploymentName = 'gpt-4o'; // Use your deployment name
// API version (ensure this version is supported by your Azure OpenAI resource)
var apiVersion = '2023-07-01-preview';
var apiKey = "_______________";
if (!apiKey) {
Logger.log("API key not found. Please set it in the Script Properties.");
return "Error: API key not set.";
}
// Construct the request URL
var url = azureEndpoint + '/openai/deployments/' + deploymentName + '/chat/completions?api-version=' + apiVersion;
var data = {
messages: [
{"role":"system", "content": prompt}
],
};
var options = {
method: 'POST',
contentType: 'application/json',
headers: {
'api-key': apiKey
},
payload: JSON.stringify(data),
muteHttpExceptions: true
};
try {
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() === 200) {
// Get the completion text from the response
var aiResponseText = response.getContentText();
return aiResponseText;
} else {
Logger.log("API call failed with response code: " + response.getResponseCode());
Logger.log("Response body: " + response.getContentText());
return "Error: Unable to get a response from the AI API.";
}
} catch (e) {
Logger.log("Error making API request: " + e.toString());
return "Error: Exception occurred while making API request.";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment