Skip to content

Instantly share code, notes, and snippets.

@lyricat
Last active February 27, 2025 01:21
Show Gist options
  • Save lyricat/091f9f0ec8582f4ce201da073f3c56b8 to your computer and use it in GitHub Desktop.
Save lyricat/091f9f0ec8582f4ce201da073f3c56b8 to your computer and use it in GitHub Desktop.
AskLLM for Google Sheet Apps Script
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
}
@lyricat
Copy link
Author

lyricat commented Feb 24, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment