Create OpenAI chat completions from Excel formulas
Developer oriented Excel add-in that provides =OAI.CHAT_COMPLETE()
to call the completions API, and helpers. Use the power of spreadsheets for prompt engineering, prompt decomposition, prototyping systems, and more.
Built with generative AI developers in mind - other products are available which may be more suitable for using generative AI in general spreadsheets. As a rule, we don't include convenience functions for tasks that can be reasonably accomplished using built-in Excel functions.
Read our post Prompt engineering with spreadsheets on the Scott Logic blog, where you'll also find much more about artificial intelligence and all things software.
The add-in is installed via a "sideloading" process, in which you load the manifest file into Excel, which then loads the add-in code directly from GitHub. It's not published to the Office Store.
First, download the manifest.xml
file. You won't need to modify this or download anything else - disregard any such instructions in the following linked instructions.
Follow the sideloading instructions for your platform:
Type =OAI.
in the formula bar or a cell, and you should see the new functions.
OAI.CHAT_COMPLETE()
calls the API to create completions. It is typical to setup a sheet containing parameters for the model call. The first column is used as parameter keys and the second as values. The API_KEY parameter is required and its value should be sourced from the OpenAI console.
OAI.COST()
calculates the billing cost(s) of completion cell(s). Wrap it with SUM()
to calculate a total.
See the function metadata for full documentation of functions and parameters. Excel's presentation of custom function documentation varies in completeness by platform, but is best in the Insert Function dialog and/or the desktop platform.
[!CAUTION] We advise caution to avoid unintentionally incurring excessive costs when working with many completion cells or expensive completions. Changes to dependency cells can cause mass recalculation, and certain actions in Excel can trigger full recalculation. You may wish to switch the calculation mode in Excel from automatic to manual. A maximum of 10 API requests are made in parallel - to cancel waiting or pending requests, undo your change or delete the formula.
Completions populate the cell with a custom data type which includes the complete API request and response. Its text value is the content of the first completion choice.
To obtain the text value for use in formulas, use the VALUETOTEXT()
function. To obtain other request/response data, use dot notation to the property path (e.g. A123.response.model
).
To view the completion data, open the entity card. The lines
property provides a line-by-line view of the completion content.
To extract a final answer that follows a chain of thought, you could use a formula such as:
=TEXTAFTER(VALUETOTEXT(A123), "<!-- END CoT -->" & CHAR(10))