Yep. Here’s a simple formula (inference) that calls the PaLM 2 API.
/*
***********************************************************
PALM2 EXAMPLE
Copyright (c) 2023 by Global Technologies Corporation
ALL RIGHTS RESERVED ...
***********************************************************
*/
// import the packs sdk
import * as coda from "@codahq/packs-sdk";
// create the pack
export const pack = coda.newPack();
pack.setUserAuthentication({
// type: coda.AuthenticationType.HeaderBearerToken,
type: coda.AuthenticationType.QueryParamToken,
paramName: "key",
instructionsUrl: 'https://makersuite.google.com/app/apikey',
});
// set the network domain
pack.addNetworkDomain('generativelanguage.googleapis.com');
var cPaLMEndpoint = "https://generativelanguage.googleapis.com/v1beta2/models/";
var oResponse;
// ################################################################
//
// inference
//
// ################################################################
pack.addFormula({
resultType : coda.ValueType.String,
// codaType: coda.ValueHintType.Markdown,
name: "inference",
description: "Text completion...",
cacheTtlSecs: 0,
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
name: "xPrompt",
description: "The text to verify for factuality.",
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "xLearnerShot",
description: "An optional learner shot.",
optional: true
}),
],
// execute the formula
execute: async function ([xPrompt, xLearnerShot, xDateTime], context) {
console.log("xPrompt: " + xPrompt);
var examples = (xLearnerShot) ? "Examples:\n" + xLearnerShot : "";
var prompt = `
${xPrompt}
${examples}
[output]
`;
var tokens = 32;
let thisPromise = await palmCreateTextCompletion(prompt, "", "text-bison-001", 1.0, tokens, 3, context)
.then(json => {
oResponse = json;
});
var response = "";
if (oResponse.body.candidates) {
try {
response = await oResponse.body.candidates;
console.log(JSON.stringify(response));
} catch (e) {
console.log("Failure!");
console.log(e.message);
response = await JSON.stringify(oResponse);
}
}
return(response);
}
});
//
// TEXT COMPLETION
//
async function palmCreateTextCompletion(textPrompt, textContext, textModel, textTemp, textTokens, candidateCount, context)
{
var url = cPaLMEndpoint + textModel + ":generateText";
console.log('url: ' + url);
var body = {
"prompt": {
"text" : textPrompt
},
"temperature": textTemp,
"candidate_count": candidateCount,
"max_output_tokens" : textTokens,
"top_k" : 4,
'safety_settings' : [
{
"category": "HARM_CATEGORY_DEROGATORY",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_TOXICITY",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_VIOLENCE",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_SEXUAL",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_MEDICAL",
"threshold": "BLOCK_NONE"
},
{
"category": "HARM_CATEGORY_DANGEROUS",
"threshold": "BLOCK_NONE"
}
]
}
console.log(JSON.stringify(body));
const response = await context.fetcher.fetch( {
url : url,
method : 'POST',
headers: {
'Content-Type' : 'application/json'
},
'body' : JSON.stringify(body)
});
return(response);
}