mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Modify prompt so that model may say it cannot help with certain requests.
Summary: This tweaks the prompting so that the user's message is given on its own instead of as a docstring within Python. This is so that the prompt makes sense when: - the user asks a question such as "Can you write me a formula which does ...?" rather than describing their formula as a docstring would, or - the user sends a message that doesn't ask for a formula at all (https://grist.slack.com/archives/C0234CPPXPA/p1687699944315069?thread_ts=1687698078.832209&cid=C0234CPPXPA) Also added wording for the model to refuse when the user asks for something that the model cannot do. Because the code (and maybe in some cases the model) for non-ChatGPT models relies on the prompt consisting entirely of Python code produced by the data engine (which no longer contains the user's message) those code paths have been disabled for now. Updating them now seems like undesirable drag, I think it'd be better to revisit this when iteration/experimentation has slowed down and stabilised. Test Plan: Added entries to the formula dataset where the response shouldn't contain a formula, indicated by the value `1` for the new column `no_formula`. This is somewhat successful, as the model does refuse to help in some of the new test cases, but not all. Performance on existing entries also seems a bit worse, but it's hard to distinguish this from random noise. Hopefully this can be remedied in the future with more work, e.g. automatic followup messages containing example inputs and outputs. Reviewers: paulfitz Reviewed By: paulfitz Subscribers: dsagal Differential Revision: https://phab.getgrist.com/D3936
This commit is contained in:
@@ -46,7 +46,7 @@ export interface AssistanceSchemaPromptV1Context {
|
||||
|
||||
/**
|
||||
* A flavor of assistant for use with the OpenAI API.
|
||||
* Tested primarily with text-davinci-002 and gpt-3.5-turbo.
|
||||
* Tested primarily with gpt-3.5-turbo.
|
||||
*/
|
||||
export class OpenAIAssistant implements Assistant {
|
||||
private _apiKey: string;
|
||||
@@ -60,8 +60,11 @@ export class OpenAIAssistant implements Assistant {
|
||||
throw new Error('OPENAI_API_KEY not set');
|
||||
}
|
||||
this._apiKey = apiKey;
|
||||
this._model = process.env.COMPLETION_MODEL || "text-davinci-002";
|
||||
this._model = process.env.COMPLETION_MODEL || "gpt-3.5-turbo-0613";
|
||||
this._chatMode = this._model.includes('turbo');
|
||||
if (!this._chatMode) {
|
||||
throw new Error('Only turbo models are currently supported');
|
||||
}
|
||||
this._endpoint = `https://api.openai.com/v1/${this._chatMode ? 'chat/' : ''}completions`;
|
||||
}
|
||||
|
||||
@@ -72,25 +75,27 @@ export class OpenAIAssistant implements Assistant {
|
||||
if (messages.length === 0) {
|
||||
messages.push({
|
||||
role: 'system',
|
||||
content: 'The user gives you one or more Python classes, ' +
|
||||
'with one last method that needs completing. Write the ' +
|
||||
'method body as a single code block, ' +
|
||||
'including the docstring the user gave. ' +
|
||||
'Just give the Python code as a markdown block, ' +
|
||||
'do not give any introduction, that will just be ' +
|
||||
'awkward for the user when copying and pasting. ' +
|
||||
'You are working with Grist, an environment very like ' +
|
||||
'regular Python except `rec` (like record) is used ' +
|
||||
'instead of `self`. ' +
|
||||
'Include at least one `return` statement or the method ' +
|
||||
'will fail, disappointing the user. ' +
|
||||
'Your answer should be the body of a single method, ' +
|
||||
'not a class, and should not include `dataclass` or ' +
|
||||
'`class` since the user is counting on you to provide ' +
|
||||
'a single method. Thanks!'
|
||||
content: 'You are a helpful assistant for a user of software called Grist. ' +
|
||||
'Below are one or more Python classes. ' +
|
||||
'The last method needs completing. ' +
|
||||
"The user will probably give a description of what they want the method (a 'formula') to return. " +
|
||||
'If so, your response should include the method body as Python code in a markdown block. ' +
|
||||
'Do not include the class or method signature, just the method body. ' +
|
||||
'If your code starts with `class`, `@dataclass`, or `def` it will fail. Only give the method body. ' +
|
||||
'You can import modules inside the method body if needed. ' +
|
||||
'You cannot define additional functions or methods. ' +
|
||||
'The method should be a pure function that performs some computation and returns a result. ' +
|
||||
'It CANNOT perform any side effects such as adding/removing/modifying rows/columns/cells/tables/etc. ' +
|
||||
'It CANNOT interact with files/databases/networks/etc. ' +
|
||||
'It CANNOT display images/charts/graphs/maps/etc. ' +
|
||||
'If the user asks for these things, tell them that you cannot help. ' +
|
||||
'The method uses `rec` instead of `self` as the first parameter.\n\n' +
|
||||
'```python\n' +
|
||||
await makeSchemaPromptV1(doc, request) +
|
||||
'\n```',
|
||||
});
|
||||
messages.push({
|
||||
role: 'user', content: await makeSchemaPromptV1(doc, request),
|
||||
role: 'user', content: request.text,
|
||||
});
|
||||
} else {
|
||||
if (request.regenerate) {
|
||||
@@ -257,10 +262,11 @@ function getAssistant() {
|
||||
if (process.env.OPENAI_API_KEY) {
|
||||
return new OpenAIAssistant();
|
||||
}
|
||||
if (process.env.HUGGINGFACE_API_KEY) {
|
||||
return new HuggingFaceAssistant();
|
||||
}
|
||||
throw new Error('Please set OPENAI_API_KEY or HUGGINGFACE_API_KEY');
|
||||
// Maintaining this is too much of a burden for now.
|
||||
// if (process.env.HUGGINGFACE_API_KEY) {
|
||||
// return new HuggingFaceAssistant();
|
||||
// }
|
||||
throw new Error('Please set OPENAI_API_KEY');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user