-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgemini_aux_functions.js
97 lines (86 loc) · 5.4 KB
/
gemini_aux_functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
* Sends a text prompt to the Google Gemini API for processing.
*
* This function constructs an API request to the Gemini API, sending the provided
* text as input. It sets the necessary headers and request body for the API call.
*
* Note: This function assumes the API key is stored in the script properties
* under the key 'api_key'.
*
* @param {string} text The text prompt to send to the Gemini API.
* @returns {Object} The options object to be used with UrlFetchApp for making the API call.
*/
function TalkToGemini(text) {
// --- GCP Gemini API Call ---
const apiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash-001:generateContent';
const apiKey = PropertiesService.getScriptProperties().getProperty('api_key'); // retrieve API Key set in "Script Properties" under "Project Settings"
const url = `${apiUrl}?key=${apiKey}`;
var headers = {
"Content-Type": "application/json"
};
var requestBody = {
"contents": [
{
"parts": [
{ "text": `${text}`}
]
}
]
};
var options = {
"method": "POST",
"headers": headers,
"payload": JSON.stringify(requestBody)
}
try {
var response = UrlFetchApp.fetch(url, options);
var data = JSON.parse(response.getContentText());
if (data && data.candidates && data.candidates[0] && data.candidates[0].content && data.candidates[0].content.parts && data.candidates[0].content.parts[0]) {
var output = data.candidates[0].content.parts[0].text;
// Logger.log(output);
return output;
} else {
Logger.log("Unexpected Response Structure. Check logs for details.");
return "Error: Unexpected data from API.";
}
} catch (error) {
Logger.log(`Error encountered: ${error.message}`);
return "An error occurred. Please check the logs.";
}
}
/**
* Builds a complete promptfor an Gemini based on the desired action and user-provided information.
*
* @param {string} actionMethodName - The type of action to be performed (e.g., "planWeekAhead", "summariseLastWeek", "suggestAgenda").
* @param {string} userData - Data about the user, relevant to the action that can include mail, caneldar, tasks and user documents.
* @param {string} userIdentity - A description of the user's role or identity.
* @param {string} agentIdentity - A description of the AI agent's role or identity.
* @param {string} customPrompt - Additional instructions or context provided by the user.
* @returns {string} The full set of instructions for the AI agent.
*/
function buildInstruction (actionMethodName, userData, userIdentity, agentIdentity, customPrompt) {
console.log(`action:${actionMethodName} \n userdata: ${userData} \n userIdentity: ${userIdentity}`)
var agentIdentityConcat = agentIdentity
var agentInstructionsConcat = customPrompt
if (actionMethodName == "planWeekAhead") {
agentIdentityConcat = agentIdentity + " You are a helpful executive assistant who is helping me plan my week and actions I need to take the coming week";
agentInstructionsConcat = " Create a series of action items I need to work on during the next week. Make sure I follow up on the most important items in the emails, document notes, tasks, and meetings I’ve shared with you, and future meetings next week. Also highlight if I need to write to someone or book a meeting with someone to solve an issue. \n" + customPrompt;
} else if (actionMethodName == "summariseLastWeek"){
agentIdentityConcat = agentIdentity + " You are a helpful executive assistant who is helping me report my work to my manager"
agentInstructionsConcat = " Look into the information I have past about my Emails, Meetings, and Note Documents, and write a summary of the work that I've been doing. Organise it into two discting categories: Internal and Customer Facing \n" + customPrompt
} else if (actionMethodName == "suggestAgenda"){
agentIdentityConcat = agentIdentity + " You are a helpful executive assistant who is helping me plan my future meetings next week and write agendas for them"
agentInstructionsConcat = " Look into the information I have past about my Emails, Meetings, and Note Documents, and looking into my future meetings suggest agendas that appropirate to those meetings based on the participants and the outstanding issues mentioned in the emails, documents, tasks, and previous meetings\n" + customPrompt;
} else { // then it's a custom prompt
// in case the user has not provided an identity to the agent
if (!agentIdentity){
agentIdentityConcat = "You are a helpful executive assistant who has access to my calendar, meetings, emails, and tasks and helps me plan my work"
}
// in case the user has not provided custom prompt
if (!customPrompt){
agentInstructionsConcat = "Create a series of action items I need to work on during the next week. Make sure I follow up on the most important items in the emails, document notes, tasks, and meetings I’ve shared with you, and future meetings next week. Also highlight if I need to write to someone or book a meeting with someone to solve an issue."
}
} // end of handling customPrompt
const full_instructions = agentIdentityConcat + "\n " + userIdentity + "\n \n" + userData + "\n \n" + "Here are your instructions: " + agentInstructionsConcat + " \n "
return full_instructions;
}