OpenAI Extension

This extension adds the =PROMPT() formula, which sends prompts to the OpenAI (ChatGPT) API from your server, for content generation and data analysis.

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, for example baseURL to use an OpenAI-compatible gateway or a model on your own network. See AI with Jspreadsheet Server for the other AI capabilities.

Example

Generate Marketing Copy

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

Requires Jspreadsheet Server and an 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

The extension registers the PROMPT formula on the server, which makes the OpenAI requests, so the API key never reaches the browser. While a request is pending the cell shows ...; 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 stored response is returned without a new API call.

Installation

Configure the extension and register it with the 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
    },
    beforeConnect: async function(auth) {
        // 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

[
    ['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

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