Last active
August 15, 2023 17:41
-
-
Save dewitt4/f04228726daca8e1c0c0b347b7e6407f to your computer and use it in GitHub Desktop.
an integration between OpenAI and Google Docs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define your OpenAI API key | |
const OPENAI_API_KEY = 'YOUR_OPENAI_API_KEY'; | |
// This function makes a request to the OpenAI API | |
function generateOpenAIResponse(prompt) { | |
const apiUrl = 'https://api.openai.com/v1/engines/davinci-codex/completions'; | |
const headers = { | |
Authorization: `Bearer ${OPENAI_API_KEY}`, | |
'Content-Type': 'application/json', | |
}; | |
const payload = { | |
prompt: prompt, | |
max_tokens: 50, // Adjust this as needed | |
}; | |
const options = { | |
method: 'post', | |
headers: headers, | |
payload: JSON.stringify(payload), | |
}; | |
const response = UrlFetchApp.fetch(apiUrl, options); | |
const responseData = JSON.parse(response.getContentText()); | |
return responseData.choices[0].text; | |
} | |
// This function adds OpenAI-generated content to the active Google Doc | |
function addOpenAIContentToDoc() { | |
const doc = DocumentApp.getActiveDocument(); | |
const cursor = doc.getCursor(); | |
if (cursor) { | |
const prompt = cursor.getSurroundingText().getText(); | |
const generatedText = generateOpenAIResponse(prompt); | |
cursor.insertText('\n\nGenerated by OpenAI:\n' + generatedText); | |
} else { | |
Logger.log("Cursor not found."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment