Here’s how you can seamlessly integrate OpenAI’s GPT-4 Turbo with Google Docs to enhance your workflow.
At Swatle, we frequently publish blogs and rely on an AI-powered proofreading system to ensure their quality. This system uses the same underlying code, enhanced by system instructions provided to our AI assistants. You can easily set up a similar system to proofread your own documents.
Simply use the code below to integrate OpenAI GPT-4 Turbo with Google Docs. Replace the placeholder API key with your own and input your assistant ID in the Google Docs app script. Once configured, this integration will allow you to proofread your content effortlessly and efficiently.
function onOpen() {
DocumentApp.getUi().createMenu('AI Analysis')
.addItem('Analyze Document', 'analyzeDocument')
.addToUi();
}
function analyzeDocument() {
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
var text = body.getText();
// Add a log to check the length of the extracted text
Logger.log("Extracted text length: " + text.length);
var analysis = callOpenAIAssistant(text);
displayAnalysis(analysis);
}
function callOpenAIAssistant(text) {
var apiKey = 'APIKEY';
var assistantId = 'AssistantID';
var baseUrl = 'https://api.openai.com/v1';
// Create a thread
var threadResponse = UrlFetchApp.fetch(baseUrl + '/threads', {
'method': 'post',
'headers': {
'Authorization': 'Bearer ' + apiKey,
'Content-Type': 'application/json',
'OpenAI-Beta': 'assistants=v1'
},
'payload': JSON.stringify({})
});
var thread = JSON.parse(threadResponse.getContentText());
// Add a message to the thread
// Check if the text is too long and truncate if necessary
var maxLength = 128000; // Adjust this value based on OpenAI's limits
var truncatedText = text.length > maxLength ? text.substring(0, maxLength) + "..." : text;
UrlFetchApp.fetch(baseUrl + '/threads/' + thread.id + '/messages', {
'method': 'post',
'headers': {
'Authorization': 'Bearer ' + apiKey,
'Content-Type': 'application/json',
'OpenAI-Beta': 'assistants=v1'
},
'payload': JSON.stringify({
'role': 'user',
'content': 'Analyze the following text:nn' + truncatedText
})
});
// Run the assistant
var runResponse = UrlFetchApp.fetch(baseUrl + '/threads/' + thread.id + '/runs', {
'method': 'post',
'headers': {
'Authorization': 'Bearer ' + apiKey,
'Content-Type': 'application/json',
'OpenAI-Beta': 'assistants=v1'
},
'payload': JSON.stringify({
'assistant_id': assistantId
})
});
var run = JSON.parse(runResponse.getContentText());
// Wait for the run to complete
while (run.status !== 'completed') {
Utilities.sleep(1000);
var checkResponse = UrlFetchApp.fetch(baseUrl + '/threads/' + thread.id + '/runs/' + run.id, {
'method': 'get',
'headers': {
'Authorization': 'Bearer ' + apiKey,
'OpenAI-Beta': 'assistants=v1'
}
});
run = JSON.parse(checkResponse.getContentText());
}
// Retrieve the assistant's response
var messagesResponse = UrlFetchApp.fetch(baseUrl + '/threads/' + thread.id + '/messages', {
'method': 'get',
'headers': {
'Authorization': 'Bearer ' + apiKey,
'OpenAI-Beta': 'assistants=v1'
}
});
var messages = JSON.parse(messagesResponse.getContentText());
// Return the assistant's last message
return messages.data[0].content[0].text.value;
}
function displayAnalysis(analysis) {
var ui = DocumentApp.getUi();
ui.alert('Document Analysis', analysis, ui.ButtonSet.OK);
}
// ... (rest of the code remains the same)
Simply add your API key and assistant ID to your Google Docs app script.
Jerryton surya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.