-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_ai.js
More file actions
565 lines (493 loc) · 17 KB
/
Copy pathutils_ai.js
File metadata and controls
565 lines (493 loc) · 17 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
* File: utils_ai.js
* Project: google_apps_scripts
* Created: 2025-12-09
* Author: Victor Cheng
* Email: hi@victor42.work
* Description: AI服务工具函数库,提供多模型AI服务调用功能(Gemini、Deepseek、Groq)。
*/
/**
* A collection of AI service utility functions.
* This acts as a namespace to prevent global scope pollution.
* @namespace UtilsAI
*/
const UtilsAI = {
/**
* 校验并规范化调用参数
* @param {Object} options - 调用参数
* @param {string} providerName - provider名称
* @param {Object} defaults - 默认参数
* @returns {Object} 规范化后的参数
*/
normalizeOptions: function(options, providerName, defaults) {
if (!options || typeof options !== 'object' || Array.isArray(options)) {
throw new Error('AI调用参数必须是对象');
}
const normalized = Object.assign({}, defaults || {}, options);
normalized.delaySeconds = normalized.delaySeconds ?? 0;
normalized[providerName] = Object.assign(
{},
(defaults && defaults[providerName]) || {},
options[providerName] || {}
);
return normalized;
},
/**
* 如果配置了延迟,则等待指定秒数
* @param {number} delaySeconds - 延迟秒数
*/
sleepIfNeeded: function(delaySeconds) {
if (delaySeconds > 0) {
Utilities.sleep(delaySeconds * 1000);
}
},
/**
* 对可恢复的AI调用执行有限重试,最终失败时抛出最后一次错误。
* @param {Function} operation - 要执行的函数
* @param {Object} options - 重试配置
* @param {number} options.maxAttempts - 最大尝试次数(默认3)
* @param {number} options.retryDelaySeconds - 重试间隔秒数(默认5)
* @param {string} options.context - 日志上下文
* @param {Function} options.shouldRetry - 可选,返回false时立即停止当前重试链
* @returns {*} operation的返回值
*/
withRetry: function(operation, options = {}) {
if (typeof operation !== 'function') {
throw new Error('withRetry需要传入函数');
}
const maxAttempts = Math.max(1, options.maxAttempts || 3);
const retryDelaySeconds = options.retryDelaySeconds ?? 5;
const context = options.context || 'AI调用';
const shouldRetry = options.shouldRetry;
let lastError = null;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
return operation(attempt);
} catch (error) {
lastError = error;
if (attempt < maxAttempts) {
if (typeof shouldRetry === 'function' && shouldRetry(error, attempt, maxAttempts) === false) {
if (typeof Utils !== 'undefined' && typeof Utils.logError === 'function') {
Utils.logError(error, `${context}失败,按策略停止当前模型重试`);
} else {
Logger.log(`${context}失败,按策略停止当前模型重试: ${error.message || error.toString()}`);
}
throw error;
}
if (typeof Utils !== 'undefined' && typeof Utils.logError === 'function') {
Utils.logError(error, `${context}失败,第${attempt}次尝试失败,${retryDelaySeconds}秒后重试`);
} else {
Logger.log(`${context}失败,第${attempt}次尝试失败,${retryDelaySeconds}秒后重试: ${error.message || error.toString()}`);
}
if (retryDelaySeconds > 0) {
Utilities.sleep(retryDelaySeconds * 1000);
}
}
}
}
throw lastError || new Error(`${context}失败`);
},
/**
* 过滤掉值为undefined的字段
* @param {Object} source - 原始对象
* @returns {Object} 过滤后的对象
*/
pickDefined: function(source) {
const result = {};
Object.keys(source || {}).forEach(key => {
if (source[key] !== undefined) {
result[key] = source[key];
}
});
return result;
},
/**
* 规范化消息内容为字符串或原始数组
* @param {*} content - 消息内容
* @returns {*}
*/
normalizeMessageContent: function(content) {
if (content === undefined || content === null) {
return '';
}
return content;
},
/**
* 将内容规范化为Gemini所需的parts数组
* @param {*} content - 消息内容
* @returns {Array<Object>}
*/
normalizeGeminiParts: function(content) {
if (content === undefined || content === null) {
return [{ text: '' }];
}
if (typeof content === 'string') {
return [{ text: content }];
}
if (Array.isArray(content)) {
return content.map(part => {
if (typeof part === 'string') {
return { text: part };
}
if (part && typeof part === 'object') {
if (part.text !== undefined) {
return { text: part.text };
}
if (part.type === 'text' && part.text !== undefined) {
return { text: part.text };
}
return part;
}
return { text: String(part) };
});
}
if (typeof content === 'object') {
if (content.text !== undefined) {
return [{ text: content.text }];
}
if (content.type === 'text' && content.text !== undefined) {
return [{ text: content.text }];
}
return [content];
}
return [{ text: String(content) }];
},
/**
* 构建OpenAI兼容接口的messages数组
* @param {Object} options - 调用参数
* @returns {Array<Object>}
*/
buildOpenAIMessages: function(options) {
const messages = [];
if (options.systemPrompt) {
messages.push({
role: 'system',
content: options.systemPrompt
});
}
if (options.messages && options.messages.length > 0) {
options.messages.forEach(message => {
if (!message || !message.role) {
throw new Error('messages中的每一项都必须包含role');
}
messages.push({
role: message.role,
content: this.normalizeMessageContent(message.content),
name: message.name
});
});
} else if (options.prompt !== undefined) {
messages.push({
role: 'user',
content: options.prompt
});
} else {
throw new Error('必须提供prompt或messages');
}
return messages;
},
/**
* 构建Gemini接口的contents与systemInstruction
* @param {Object} options - 调用参数
* @returns {{contents: Array<Object>, systemInstruction: (Object|undefined)}}
*/
buildGeminiContents: function(options) {
const contents = [];
const systemPrompts = [];
if (options.systemPrompt) {
systemPrompts.push(options.systemPrompt);
}
if (options.messages && options.messages.length > 0) {
options.messages.forEach(message => {
if (!message || !message.role) {
throw new Error('messages中的每一项都必须包含role');
}
if (message.role === 'system') {
if (message.content !== undefined && message.content !== null) {
if (typeof message.content === 'string') {
systemPrompts.push(message.content);
} else {
systemPrompts.push(JSON.stringify(message.content));
}
}
return;
}
const role = message.role === 'assistant' ? 'model' : (message.role === 'model' ? 'model' : 'user');
contents.push({
role: role,
parts: this.normalizeGeminiParts(message.content)
});
});
} else if (options.prompt !== undefined) {
contents.push({
role: 'user',
parts: [{ text: options.prompt }]
});
} else {
throw new Error('必须提供prompt或messages');
}
if (contents.length === 0) {
throw new Error('至少需要一条非system消息');
}
const systemInstruction = systemPrompts.length > 0
? { parts: [{ text: systemPrompts.join('\n\n') }] }
: undefined;
return { contents, systemInstruction };
},
/**
* 执行HTTP请求并解析JSON响应
* @param {string} url - 请求URL
* @param {Object} fetchOptions - UrlFetchApp选项
* @returns {{response: GoogleAppsScript.URL_Fetch.HTTPResponse, responseData: Object, responseText: string}}
*/
fetchJson: function(url, fetchOptions) {
const response = UrlFetchApp.fetch(url, fetchOptions);
const responseText = response.getContentText();
let responseData = null;
try {
responseData = JSON.parse(responseText);
} catch (error) {
responseData = null;
}
return { response, responseData, responseText };
},
/**
* 构造统一的API失败错误
* @param {string} providerName - provider名称
* @param {GoogleAppsScript.URL_Fetch.HTTPResponse} response - HTTP响应
* @param {string} responseText - 响应文本
* @returns {Error}
*/
buildApiError: function(providerName, response, responseText) {
return new Error(`${providerName} API请求失败,状态码: ${response.getResponseCode()}, 响应: ${responseText}`);
},
/**
* 将通用responseFormat映射为Gemini generationConfig字段
* @param {*} responseFormat - 通用responseFormat配置
* @returns {Object}
*/
buildGeminiResponseFormatConfig: function(responseFormat) {
if (!responseFormat) {
return {};
}
const formatType = typeof responseFormat === 'string'
? responseFormat
: responseFormat.type;
if (formatType === 'json_object') {
return { responseMimeType: 'application/json' };
}
return {};
},
/**
* ==================== AI服务调用工具 ====================
*/
/**
* 向Google Gemini API发送请求并获取AI回复
* @param {Object} options - 调用参数
* @returns {string} AI的回复内容
*/
askGemini: function(options) {
const scriptProperties = PropertiesService.getScriptProperties();
const apiKey = scriptProperties.getProperty('GEMINI_API_KEY');
const config = this.normalizeOptions(options, 'gemini', {
model: 'gemini-flash-lite-latest',
temperature: 0.7,
maxTokens: 8192,
topP: 0.95,
gemini: {
topK: 20,
candidateCount: 1
}
});
if (!apiKey) {
throw new Error('未配置 GEMINI_API_KEY');
}
this.sleepIfNeeded(config.delaySeconds);
const url = `https://generativelanguage.googleapis.com/v1beta/models/${config.model}:generateContent?key=${apiKey}`;
const contentPayload = this.buildGeminiContents(config);
const generationConfig = this.pickDefined({
temperature: config.temperature,
maxOutputTokens: config.maxTokens,
topP: config.topP,
topK: config.gemini.topK,
candidateCount: config.gemini.candidateCount,
stopSequences: config.stop,
seed: config.seed,
presencePenalty: config.presencePenalty,
frequencyPenalty: config.frequencyPenalty,
responseMimeType: config.gemini.responseMimeType,
responseSchema: config.gemini.responseSchema,
responseJsonSchema: config.gemini.responseJsonSchema,
responseLogprobs: config.gemini.responseLogprobs,
logprobs: config.gemini.logprobs,
thinkingConfig: config.gemini.thinkingConfig
});
Object.assign(generationConfig, this.buildGeminiResponseFormatConfig(config.responseFormat));
const requestData = this.pickDefined({
contents: contentPayload.contents,
systemInstruction: contentPayload.systemInstruction,
generationConfig: generationConfig,
safetySettings: config.gemini.safetySettings,
tools: config.gemini.tools,
toolConfig: config.gemini.toolConfig,
serviceTier: config.gemini.serviceTier,
store: config.gemini.store
});
const fetchOptions = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(requestData),
'muteHttpExceptions': true
};
try {
const { response, responseData, responseText } = this.fetchJson(url, fetchOptions);
if (response.getResponseCode() === 200) {
const reply = responseData.candidates &&
responseData.candidates[0] &&
responseData.candidates[0].content &&
responseData.candidates[0].content.parts &&
responseData.candidates[0].content.parts[0] &&
responseData.candidates[0].content.parts[0].text;
if (reply) {
return reply;
} else {
throw new Error('无法从API响应中提取回复内容');
}
} else {
throw this.buildApiError('Gemini', response, responseText);
}
} catch (error) {
throw new Error(`请求失败: ${error.message}`);
}
},
/**
* 向Deepseek API发送请求并获取AI回复
* @param {Object} options - 调用参数
* @returns {string} AI的回复内容
*/
askDeepseek: function(options) {
const scriptProperties = PropertiesService.getScriptProperties();
const apiKey = scriptProperties.getProperty('DEEPSEEK_API_KEY');
const config = this.normalizeOptions(options, 'deepseek', {
model: 'deepseek-v4-flash',
temperature: 0.7,
maxTokens: 8192,
topP: 1
});
if (!apiKey) {
throw new Error('未配置 DEEPSEEK_API_KEY');
}
this.sleepIfNeeded(config.delaySeconds);
const url = 'https://api.deepseek.com/chat/completions';
const messages = this.buildOpenAIMessages(config);
const requestData = this.pickDefined({
model: config.model,
messages: messages,
max_tokens: config.maxTokens,
temperature: config.temperature,
top_p: config.topP,
stop: config.stop,
frequency_penalty: config.frequencyPenalty,
presence_penalty: config.presencePenalty,
response_format: typeof config.responseFormat === 'string'
? { type: config.responseFormat }
: config.responseFormat,
thinking: config.deepseek.thinking,
stream_options: config.deepseek.streamOptions,
logprobs: config.deepseek.logprobs,
top_logprobs: config.deepseek.topLogprobs
});
const fetchOptions = {
'method': 'post',
'contentType': 'application/json',
'headers': {
'Authorization': 'Bearer ' + apiKey
},
'payload': JSON.stringify(requestData),
'muteHttpExceptions': true
};
try {
const { response, responseData, responseText } = this.fetchJson(url, fetchOptions);
if (response.getResponseCode() === 200) {
const reply = responseData.choices &&
responseData.choices[0] &&
responseData.choices[0].message &&
responseData.choices[0].message.content;
if (reply) {
return reply;
} else {
throw new Error('无法从API响应中提取回复内容');
}
} else {
throw this.buildApiError('DeepSeek', response, responseText);
}
} catch (error) {
throw new Error(`请求失败: ${error.message}`);
}
},
/**
* 向Groq API发送请求并获取AI回复
* @param {Object} options - 调用参数
* @returns {string} AI的回复内容
*/
askGroq: function(options) {
const scriptProperties = PropertiesService.getScriptProperties();
const apiKey = scriptProperties.getProperty('GROQ_API_KEY');
const config = this.normalizeOptions(options, 'groq', {
model: 'openai/gpt-oss-120b',
temperature: 0.7,
maxTokens: 8192,
topP: 1
});
if (!apiKey) {
throw new Error('未配置 GROQ_API_KEY');
}
this.sleepIfNeeded(config.delaySeconds);
const url = 'https://api.groq.com/openai/v1/chat/completions';
const messages = this.buildOpenAIMessages(config);
const requestData = this.pickDefined({
model: config.model,
messages: messages,
max_completion_tokens: config.maxTokens,
temperature: config.temperature,
top_p: config.topP,
stop: config.stop,
response_format: typeof config.responseFormat === 'string'
? { type: config.responseFormat }
: config.responseFormat,
seed: config.seed,
reasoning_effort: config.groq.reasoningEffort,
reasoning_format: config.groq.reasoningFormat,
include_reasoning: config.groq.includeReasoning,
service_tier: config.groq.serviceTier,
truncation: config.groq.truncation
});
const fetchOptions = {
'method': 'post',
'contentType': 'application/json',
'headers': {
'Authorization': 'Bearer ' + apiKey
},
'payload': JSON.stringify(requestData),
'muteHttpExceptions': true
};
try {
const { response, responseData, responseText } = this.fetchJson(url, fetchOptions);
if (response.getResponseCode() === 200) {
const reply = responseData.choices &&
responseData.choices[0] &&
responseData.choices[0].message &&
responseData.choices[0].message.content;
if (reply) {
return reply;
} else {
throw new Error('无法从API响应中提取回复内容');
}
} else {
throw this.buildApiError('Groq', response, responseText);
}
} catch (error) {
throw new Error(`请求失败: ${error.message}`);
}
}
};