Skip to content

Instantly share code, notes, and snippets.

@St3ph-fr
Last active September 9, 2024 11:30
Show Gist options
  • Save St3ph-fr/f9098d6cd532e80eb49276502de18b0f to your computer and use it in GitHub Desktop.
Save St3ph-fr/f9098d6cd532e80eb49276502de18b0f to your computer and use it in GitHub Desktop.
// Context of this Gist in Medium : https://medium.com/@stephane.giron/discuss-with-your-google-docs-with-google-apps-script-and-gemini-1-5-flash-7f80abef8cb2
// This is implemented in our Google Workspace Add-on onleeAssistant : https://assistant.onlee.xyz/
const API_KEY = '';
const QUESTION = '';
const DOC_ID = '';
function discussWithGoogleDocs() {
let doc_content = DocumentApp.openById(DOC_ID).getBody().getText();
let body ={ contents:[
{"role": "user","parts": [{ "text":QUESTION } ]}
],
systemInstruction :{"role": "user","parts": [{"text": doc_content}]},
generationConfig: {
"temperature": 0.2,
"maxOutputTokens": 4000,
"responseMimeType": "text/plain"
}};
console.log(JSON.stringify(body));
const url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key='+API_KEY;
const params = {
method : "POST",
contentType: 'application/json',
payload : JSON.stringify(body),
muteHttpExceptions :true
};
const req = UrlFetchApp.fetch(url,params);
// console.log(req.getContentText())
if(req.getResponseCode() == 200){
let rep = JSON.parse(req.getContentText());
// let last = rep.contents.pop();
console.log('/* Gemini answer */')
console.log(rep.candidates[0].content.parts[0].text)
}else{
console.log('Error : '+JSON.parse(req.getContentText()).error.message)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment