Last active
February 27, 2025 01:21
-
-
Save lyricat/091f9f0ec8582f4ce201da073f3c56b8 to your computer and use it in GitHub Desktop.
AskLLM for Google Sheet Apps Script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function AskLLM(base, token, model, systemPrompt, query) { | |
// support all openai compatible api: openai, deepseek, grok, etc | |
var apiUrl = base + "/chat/completions"; | |
var payload = { | |
"messages": [ | |
{ | |
"role": "system", | |
"content": systemPrompt | |
}, | |
{ | |
"role": "user", | |
"content": query | |
} | |
], | |
"model": model | |
} | |
var options = { | |
"method": "post", | |
"headers": { | |
"Authorization": "Bearer " + token, | |
"Content-Type": "application/json" | |
}, | |
"payload": JSON.stringify(payload) | |
}; | |
var response = UrlFetchApp.fetch(apiUrl, options); | |
var fullText = response.getContentText(); | |
try { | |
const jso = JSON.parse(fullText); | |
if (jso.choices && jso.choices.length > 0) { | |
ret = jso.choices[0].message.content | |
} | |
} catch (e) { | |
ret = e.message | |
} | |
return ret | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script is used by "📊 Use AI in Google Sheets: OpenAI, Deepseek, and Grok simultaneously help you with batch work"