Skip to content

Commit

Permalink
Introduce const for the zero value that denotes an empty cell
Browse files Browse the repository at this point in the history
  • Loading branch information
robatwilliams committed Dec 18, 2023
1 parent 982437f commit 9b97e8a
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/functions/functions.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
const COMPLETION_ENTITY_KIND = 'openai-excel-formulas:chat-completion';
const EMPTY_OR_ZERO = 0;

CustomFunctions.associate('CHAT_COMPLETE', chatComplete);
async function chatComplete(messages, params) {
const {
API_KEY: apiKey,
API_BASE: apiBase = 'https://api.openai.com/',
messages: _,
0: __, // Empty key cell in range
[EMPTY_OR_ZERO]: __,
...userParams
} = Object.fromEntries(params);

if (apiKey == null || apiKey === 0) {
if (apiKey == null || apiKey === EMPTY_OR_ZERO) {
throw new CustomFunctions.Error(
CustomFunctions.ErrorCode.invalidValue,
'API_KEY is required',
Expand All @@ -28,7 +29,7 @@ async function chatComplete(messages, params) {
const requestBody = {
...userParams,
messages: messages
.filter(([role]) => role !== 0)
.filter(([role]) => role !== EMPTY_OR_ZERO)
.map(([role, content]) => ({ role, content })),
};

Expand Down Expand Up @@ -86,9 +87,9 @@ async function chatComplete(messages, params) {
// Terminology note: our _cost_ is driven by usage and OpenAI's _prices_.
CustomFunctions.associate('COST', cost);
function cost(completionsMatrix, pricesMatrix) {
const completions = completionsMatrix.flat().filter(
(value) => value !== 0, // Empty value in range
);
const completions = completionsMatrix
.flat()
.filter((value) => value !== EMPTY_OR_ZERO);
completions.forEach(validateIsCompletion);
const allPrices = Object.fromEntries(
pricesMatrix.map((row) => [row[0], { input: row[1], output: row[2] }]),
Expand Down

0 comments on commit 9b97e8a

Please sign in to comment.