Skip to content

Commit

Permalink
Remove COT_ANSWER; can use built-in functions instead
Browse files Browse the repository at this point in the history
  • Loading branch information
robatwilliams committed Dec 19, 2023
1 parent c475245 commit 66a46db
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 91 deletions.
31 changes: 0 additions & 31 deletions src/functions/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,36 +115,6 @@ function cost(completionsMatrix, pricesMatrix) {
}, 0);
}

CustomFunctions.associate('COT_ANSWER', cotAnswer);
function cotAnswer(completion, separator) {
validateIsCompletion(completion);

if (separator === null) {
// This default value must be kept in sync with documentation in the function metadata.
separator = '<!-- END CoT -->';
}

const choiceIndex = 0;
const choiceEntity =
completion.properties.response.properties.choices.elements[0][choiceIndex];
const choiceContent =
choiceEntity.properties.message.properties.content.basicValue;
const halves = choiceContent.split(separator, 2);

if (halves.length !== 2) {
throw new CustomFunctions.Error(
CustomFunctions.ErrorCode.invalidValue,
'Completion does not split into two by the separator',
);
}

const answer = halves[1];

// Models have a strong tendency to put a newline after the separator, and
// it's difficult to prompt GPT 3.5T to consistently do anything different.
return answer[0] === '\n' ? answer.substring(1) : answer;
}

function toEntityProperty(value) {
if (value === null) {
// There is no concept of null in Excel's data model.
Expand Down Expand Up @@ -192,6 +162,5 @@ if (typeof module === 'object') {
module.exports = {
chatComplete,
cost,
cotAnswer,
};
}
21 changes: 0 additions & 21 deletions src/functions/functions.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,6 @@
"type": "any"
}
},
{
"description": "Extract the answer part from a Chain of Thought (CoT) completion",
"id": "COT_ANSWER",
"name": "COT_ANSWER",
"parameters": [
{
"name": "completion",
"description": "The completion to extract the answer from",
"type": "any"
},
{
"name": "separator",
"description": "The separator that denotes the end of the CoT reasoning and the beginning of the answer. The default separator is <!-- END CoT -->. Your completion prompt must instruct the model to produce this separator, by using wording such as 'Then state \"<!-- END CoT -->\" followed by your answer.'.",
"type": "string",
"optional": true
}
],
"result": {
"type": "string"
}
},
{
"description": "Calculate the cost of the completions in the given cells",
"id": "COST",
Expand Down
40 changes: 1 addition & 39 deletions src/functions/functions.test.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import assert from 'node:assert';
import { describe, it } from 'node:test';
import { makeCompletionEntity } from '../../testFramework/completionEntityStub.mjs';
import { chatComplete, cost, cotAnswer } from './functions.js';
import { chatComplete, cost } from './functions.js';
import { makeCompletionResponse } from '../../testFramework/completionResponseStub.mjs';

describe('CHAT_COMPLETE', () => {
Expand Down Expand Up @@ -201,44 +201,6 @@ describe('COST', () => {
});
});

describe('COT_ANSWER', () => {
it('extracts the answer', () => {
const completion = makeCompletionEntity({
content:
'The color of the door has been explicitly stated in the provided statement.\n\n<!-- END CoT -->\nBlue',
});

assert.strictEqual(cotAnswer(completion, null), 'Blue');
});

it('uses a custom separator when given', () => {
const completion = makeCompletionEntity({
content:
'The color of the door has been explicitly stated in the provided statement.\n\n*** BEGIN ANSWER ***\nBlue',
});

assert.strictEqual(cotAnswer(completion, '*** BEGIN ANSWER ***'), 'Blue');
});

it('throws an error when the separator is not found', () => {
const completion = makeCompletionEntity({ content: 'Blue' });

assert.throws(() => cotAnswer(completion), {
code: '#VALUE!',
message: 'Completion does not split into two by the separator',
});
});

it('extracts the answer intact when it immediately follows the separator', () => {
const completion = makeCompletionEntity({
content:
'The color of the door has been explicitly stated in the provided statement.\n\n<!-- END CoT -->Blue',
});

assert.strictEqual(cotAnswer(completion, null), 'Blue');
});
});

async function mockResponseOk(body) {
return new Response(JSON.stringify(body), {
status: 200,
Expand Down

0 comments on commit 66a46db

Please sign in to comment.