in vision/use-cases/hey_llm/src/main.ts [243:312]
function HEY_LLM(
instruction: string,
input: string,
context: string[][] | string = [],
model = DEFAULT_GEMINI_MODEL,
) {
const formattedContext =
typeof context === 'string'
? context
: context.map(row => row.join(', ')).join('\n');
const prompt = `
## Instruction
${instruction}
${formattedContext ? '## Context (CSV formatted)\n' + formattedContext : ''}
## Task
Input: ${input}
Output:`;
const cache = CacheService.getDocumentCache();
const cacheKey = `hey_llm:${generateHashValue(prompt)}:${model}`;
const cached = cache?.get(cacheKey);
if (cached) return cached;
const clientID = PropService.clientID;
const clientSecret = PropService.clientSecret;
if (!clientID || !clientSecret) {
throw new Error('OAuth client ID / Secret not set.');
}
const oauthService = getGoogleService_(clientID, clientSecret);
const url = `https://${LOCATION}-aiplatform.googleapis.com/v1/projects/${getProjectNumber_()}/locations/${LOCATION}/publishers/google/models/${model}:generateContent`;
const payload = JSON.stringify({
contents: [
{
role: 'user',
parts: [
{
text: prompt,
},
],
},
],
systemInstruction: {
parts: [
{
text: 'The response is to be used in side a spreadsheet cell and needs to be concise. Just show the answer only.',
},
],
},
});
const res = UrlFetchApp.fetch(url, {
method: 'post',
headers: {
Authorization: 'Bearer ' + oauthService.getAccessToken(),
},
contentType: 'application/json',
payload: payload,
muteHttpExceptions: true,
});
const result: GenerateContentResponse = JSON.parse(res.getContentText());
if (!(result.candidates && result.candidates[0].content.parts[0].text)) {
Logger.log(payload, result);
throw new Error('Request to Gemini failed. ' + res.getContentText());
}
const out = result.candidates[0].content.parts[0].text.trim();
cache?.put(cacheKey, out, CACHE_EXPIRATION_IN_SECONDS);
return out;
}