Coda AI Live

icon picker
Coda AI Live Pack Code

This is the Coda AI Live Pack I created and used in all the examples and tests. It is an interface with . You’ll need an and an API key. The free tier provides 100 searches per month. The next rung on the ladder is $50/month for 5,000 searches per month. There also are many search services available that do this. If you’ve never created a pack before, this is the perfect time to learn. Simply copy this source into a new pack created from scratch. Overwrite the few lines of code with this copied code, and build it. It’s ready to be used in any document.
The Pack contains only one formula - serpAPI() which requires two parameters. The first is your API credentials from SerpAPI and the second is the query.
One of the challenges is ensuring the search results are prepared in a form that can be reliably injected into Coda AI prompts. The Pack formula does this just before passing the search results back to Coda. This transformation is dependent on the JSON structure returned from SerpAPI. If you remix this Pack with a different search interface, you will need to perform a similar transformation to avoid overrunning the current limitations of the Coda AI prompt window.

/*

***********************************************************
SerpAPI Interface
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.QueryParamToken,
paramName: "api_key",
instructionsUrl: 'https://serpapi.com/dashboard',
});

// set the network domain
pack.addNetworkDomain('serpapi.com');

// define the static vars
var oResponse;

// ################################################################
//
// SERPAPI
//
// ################################################################
pack.addFormula({

resultType : coda.ValueType.String,

name: "serpAPI",
description: "Search the web.",
cacheTtlSecs: 0,

parameters: [

coda.makeParameter({
type: coda.ParameterType.String,
name: "q",
description: "Query string.",
}),

],

// execute the formula
execute: async function ([q], context) {

console.log("q: " + q);

if (q) {

let thisPromise = await serpAPI(q, context)
.then(json => {
oResponse = json;
});

console.log(JSON.stringify(oResponse));

let oOrganicResults = oResponse.body.organic_results;
var oResults = {};
for (let i in oOrganicResults) {
oResults[i] = {
"title" : oOrganicResults[i].title,
"link" : oOrganicResults[i].link,
"thumbnail" : oOrganicResults[i].thumbnail,
"date" : oOrganicResults[i].date,
"snippet" : oOrganicResults[i].snippet
}
}

// add the answer box
oResults["answerBox"] = oResponse.body["answer_box"]

} else {
oResponse = {};
}

console.log(JSON.stringify(oResults));

return(JSON.stringify(oResults));

}

});

//
// TEXT COMPLETION
//
async function serpAPI(q, context)
{

let url = "https://serpapi.com/search.json?engine=google&q=" + encodeURIComponent(q);

const response = await context.fetcher.fetch( {
url : url,
method : 'GET',
headers: {
'Content-Type' : 'application/json'
}
});

return(response);
}

Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.