-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_deepseek.js
More file actions
92 lines (84 loc) · 2.8 KB
/
ai_deepseek.js
File metadata and controls
92 lines (84 loc) · 2.8 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
/**
* Creates a custom menu in the spreadsheet UI to set the API key.
*/
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('🤖 DeepSeek AI')
.addItem('Set API Key', 'setApiKey')
.addToUi();
}
/**
* Prompts the user to enter their DeepSeek API key and stores it securely
* in User Properties, scoped to the current user.
*/
function setApiKey() {
const ui = SpreadsheetApp.getUi();
const prompt = ui.prompt(
'Set DeepSeek API Key',
'Please enter your DeepSeek API key. It will be stored securely for your account only.',
ui.ButtonSet.OK_CANCEL
);
if (prompt.getSelectedButton() == ui.Button.OK) {
const apiKey = prompt.getResponseText();
if (apiKey && apiKey.trim() !== '') {
// Store the key securely in a per-user property.
PropertiesService.getUserProperties().setProperty('DEEPSEEK_API_KEY', apiKey);
ui.alert('Success!', 'Your DeepSeek API key has been saved.', ui.ButtonSet.OK);
} else {
ui.alert('Error', 'API Key cannot be empty.', ui.ButtonSet.OK);
}
}
}
/**
* Queries the DeepSeek API with a given prompt.
*
* @param {string} prompt The text prompt to send to the AI model.
* @return {string} The text response from the DeepSeek API.
* @customfunction
*/
function DEEPSEEK(prompt) {
// 1. Input Validation
if (!prompt || typeof prompt !== 'string' || prompt.trim() === '') {
return "Error: Please provide a valid text prompt.";
}
// 2. Retrieve API Key
const apiKey = PropertiesService.getUserProperties().getProperty('DEEPSEEK_API_KEY');
if (!apiKey) {
return "Error: API key not set. Please use the '🤖 DeepSeek AI > Set API Key' menu.";
}
// 3. Prepare the API Request
const url = 'https://api.deepseek.com/chat/completions';
const payload = {
model: 'deepseek-chat', // Or 'deepseek-coder' for coding tasks
messages: [{
role: 'user',
content: prompt
}],
stream: false // Important for a simple request-response
};
const options = {
method: 'post',
contentType: 'application/json',
headers: {
'Authorization': 'Bearer ' + apiKey
},
payload: JSON.stringify(payload),
muteHttpExceptions: true // Prevents script failure on API errors (e.g., 401, 500)
};
// 4. Execute the API Call and Handle Response
try {
const response = UrlFetchApp.fetch(url, options);
const responseCode = response.getResponseCode();
const responseBody = response.getContentText();
if (responseCode === 200) {
const jsonResponse = JSON.parse(responseBody);
// Extract the text content from the response object
return jsonResponse.choices[0].message.content.trim();
} else {
// Return a descriptive error if the API call fails
return `Error ${responseCode}: ${responseBody}`;
}
} catch (e) {
return "Error: " + e.toString();
}
}