OpenAI Extension

This extension integrates the ChatGPT API into a Jspreadsheet Server application for content generation and data analysis. Frontend queries are sent to the OpenAI API through your server, so the API key stays on the backend.

Documentation

Methods

Method Description
=PROMPT() Combines values and text fragments to create dynamic prompts for AI model interactions. The arguments are joined and sent to the OpenAI chat completions API (model gpt-4).

Settings

Property Description
apiKey: string Your OpenAI API key. Required, and without it the extension does not connect.
onbeforesend: function onbeforesend(client, request) is called before each request with the OpenAI client and the request payload; modify the payload here, for example to change the model.
onsuccess: function Called with the model's text response and the raw API response before the value is applied to the cell. Return false to discard the response, or return a value to replace the text.

Any other properties are passed to the OpenAI client constructor.

Example

Generate Marketing Copy

Generate marketing copy for products using their attributes.

A1: "Product Name"
B1: "Product Description"
C1: "Marketing Copy"
A2: "Smartwatch"
B2: "Fitness tracking, sleep monitoring, 48-hour battery life"

In C2, you would enter:

=PROMPT("Write an engaging marketing copy for a product named ", A2, " with these features: ", B2)

Installation

This extension requires Jspreadsheet Server and an active OpenAI API account.

Using NPM

npm install @jspreadsheet/openai

Using a CDN

<script src="https://cdn.jsdelivr.net/npm/@jspreadsheet/openai/dist/index.min.js"></script>

Configuration

Requests to the OpenAI API are made by the server: the extension registers the PROMPT formula on the backend, so the API key never reaches the browser. When a user enters the formula, the cell shows ... while the request is pending; the response is then saved to the cell's cache and broadcast to all connected users of the document. With the cache option enabled on the spreadsheet, a previously stored response is returned without a new API call.

Installation

Load the OpenAI extension and register it with Jspreadsheet Server:

const server = require('@jspreadsheet/server');
const openai = require('@jspreadsheet/openai');

// Connect to the Open AI API
openai({ apiKey: 'your-openai-key' });

// You can get this information on your Jspreadsheet Account
const license = {
    clientId: 'your-client-id',
    licenseKey: 'your-license'
}

// Server that runs on your own infrastructure
server({
    config: {
        cors: {
            origin: "*"
        },
    },
    port: 3000,
    error: function(e) {
        // Your implementation
    },
    auth: async function(socket) {
        // Your implementation
    },
    load: async function(guid) {
        // Your implementation
    },
    create: async function(guid, config) {
        // Your implementation
    },
    destroy: async function(guid) {
        // Your implementation
    },
    change: async function(guid, changes) {
        // Your implementation
    },
    license: license,
    extensions: { openai },
});

Examples

Translating Column to French

Use the PROMPT function to translate the values of a column.

[
    ['Hello', '=PROMPT("Translate ",A1," to French")'],
    ['Bye', '=PROMPT("Translate ",A2," to French")'],
    ['Thanks', '=PROMPT("Translate ",A3," to French")'],
    ['Sorry', '=PROMPT("Translate ",A4," to French")'],
]

Combining Word Semantics

Use PROMPT to combine the meaning of two words.

['Flower', 'Bee', `=PROMPT(A1," and ",B1," combined result in this word:")`]