-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppsScript_Main_Code
More file actions
127 lines (106 loc) · 4.3 KB
/
AppsScript_Main_Code
File metadata and controls
127 lines (106 loc) · 4.3 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Function to call OpenAI API from Google Sheets
function callOpenAI(parameterName, userInputText) {
var scriptProperties = PropertiesService.getScriptProperties();
var apiKey = scriptProperties.getProperty('OPENAI_API_KEY');
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SystemPrompts');
var range = sheet.getDataRange();
var values = range.getValues();
var model = '';
var systemPrompt = '';
for (var i = 1; i < values.length; i++) {
var sheetParameterName = values[i][0] ? values[i][0].trim() : '';
if (sheetParameterName === String(parameterName).trim()) {
model = values[i][1] ? values[i][1].trim() : '';
systemPrompt = values[i][2] ? values[i][2].trim() : '';
break;
}
}
if (!model || !systemPrompt) {
throw new Error('Parameter name not found or incomplete in the SystemPrompts sheet. Searched for: ' + parameterName.trim());
}
var apiUrl = 'https://api.openai.com/v1/chat/completions';
var payload = {
model: model,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userInputText }
],
max_tokens: 1000,
temperature: 1
};
var options = {
method: 'post',
contentType: 'application/json',
headers: {
Authorization: 'Bearer ' + apiKey
},
payload: JSON.stringify(payload)
};
var response = UrlFetchApp.fetch(apiUrl, options);
var json = JSON.parse(response.getContentText());
if (!json.choices || json.choices.length === 0 || !json.choices[0].message || !json.choices[0].message.content) {
throw new Error('OpenAI response does not contain the expected message content.');
}
var content = json.choices[0].message.content.trim();
return content;
}
// Function to create custom menu in Google Sheets
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Revise API Response')
.addItem('Re-send request to OpenAI', 'reviseResponse')
.addToUi();
}
// Function to handle revising the response for multiple cells
function reviseResponse() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var ranges = sheet.getActiveRangeList().getRanges();
ranges.forEach(function(range) {
var cells = range.getValues();
var formulas = range.getFormulas();
var startRow = range.getRow();
var startColumn = range.getColumn();
for (var i = 0; i < cells.length; i++) {
for (var j = 0; j < cells[i].length; j++) {
var cell = sheet.getRange(startRow + i, startColumn + j);
var formula = formulas[i][j];
if (formula) {
var parameterName = extractParameterNameFromFormula(formula);
var userInputText = cells[i][j];
var originalPrompt = getSystemPrompt(parameterName);
var originalResponse = userInputText;
var revisedPrompt = `
Here is the original user's system prompt: ${originalPrompt}
Based on this input: ${userInputText}
Here is the response that they got: ${originalResponse}
The user is not satisfied with the response, please revise to make sure it complies with the requirements of the request, including the requested schema.
Please provide only the revised structured response in the specified format without any additional explanation or text.
`;
var newValue = callOpenAI(parameterName, revisedPrompt);
cell.setValue(newValue);
}
}
}
});
}
// Helper function to extract the parameter name from the formula
function extractParameterNameFromFormula(formula) {
var matches = formula.match(/callOpenAI\("([^"]+)",/i);
if (matches) {
return matches[1];
}
throw new Error('Parameter name not found in the formula: ' + formula);
}
// Helper function to get the system prompt based on the parameter name
function getSystemPrompt(parameterName) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SystemPrompts');
var range = sheet.getDataRange();
var values = range.getValues();
for (var i = 1; i < values.length; i++) {
var sheetParameterName = values[i][0] ? values[i][0].trim() : '';
if (sheetParameterName === String(parameterName).trim()) {
return values[i][2] ? values[i][2].trim() : '';
}
}
throw new Error('System prompt not found for parameter name: ' + parameterName.trim());
}