Skip to content

Instantly share code, notes, and snippets.

Created February 16, 2024 20:13
Show Gist options
  • Save eskibars/5c080124373abc746b60968b111b3578 to your computer and use it in GitHub Desktop.
Save eskibars/5c080124373abc746b60968b111b3578 to your computer and use it in GitHub Desktop.
Vectara GSheet Integration
* Calculates the sale price of a value at a given discount.
* The sale price is formatted as US dollars.
* @param {string} question The RFI question to answer.
* @param {number} discount The discount to apply, such as .5 or 50%.
* @return The answer according to Vectara.
* @customfunction
function VectaraRFIAnswer(question) {
var vectaraCorpusID = 1; // your corpus ID
var vectaraCustomerID = "YOUR_CUSTOMER_ID";
var vectaraApiKey = 'YOUR_PERSONAL_API_KEY';
var vectaraPrompt = `[
{"role": "system", "content": "You are an RFI answering assistant acting on behalf of the company Vectara. You are provided with search results from previously responded-to RFIs that may help answer the given question. You must summarize these results as a coherent answer. Give slight preference to search results that appear earlier in the chat. Only use information provided in this chat."},
#foreach ($qResult in $vectaraQueryResults)
#if ($foreach.first)
{"role": "user", "content": "Search for \\"$\\\${vectaraQuery})\\", and give me the first search result."},
{"role": "assistant", "content": "$\\\${qResult.getText()})" },
{"role": "user", "content": "Give me the $vectaraIdxWord[$foreach.index] search result."},
{"role": "assistant", "content": "$\\\${qResult.getText()})" },
{"role": "user", "content": "Generate a comprehensive and informative answer (but no more than $vectaraOutChars characters) for the question \\"$\\\${vectaraQuery})\\" solely based on the search results in this chat. You must only use information from the provided results. Combine search results together into a coherent answer. Do not repeat text. Only use the most relevant results that answer the question accurately. If a result does not answer the question, do not use it. If the search results are not valid, respond with \\"The returned results did not contain sufficient information to the question.\\"."}
var headers = {
'x-api-key': vectaraApiKey,
'Content-Type': 'application/json',
"customer-id": vectaraCustomerID
var url = '';
var corpus_key = {
'customer_id': vectaraCustomerID,
'corpus_id': vectaraCorpusID,
'lexicalInterpolationConfig': { 'lambda': 0.025 }
var query = {
'query': question,
'num_results': 10,
'corpus_key': [ corpus_key ],
'summary': [{
'promptText': vectaraPrompt,
'responseLang': 'en',
'maxSummarizedResults': 5
var payload = {
"query": [ query ]
var options = {
'method' : 'post',
'contentType': 'application/json',
'headers': headers,
'payload': JSON.stringify(payload)
var response = JSON.parse(UrlFetchApp.fetch(url, options));
var summary = response['responseSet'][0]['summary'][0]['text'];
return summary;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment