mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Prevent the AI assistant response from including class definitions
Summary: Sometimes the model repeats the classes given in the prompt which would mess up extracting the actual formula. This diff solves this by: 1. Changes the generated Python schema so that (a) the thing that needs completing is a plain top level function instead of a property/method inside the class and (2) the classes are fully valid syntax, which makes it easier to 2. Remove classes from the parsed Python code when converting the completion to a formula. 3. Tweak the prompt wording to discourage including classes in general, especially because sometimes the model tries to solve the problem by defining extra methods/attributes/classes. While I was at it, I changed type hints to use builtins (e.g. `list` instead of `List`) to prevent `from typing import List` which was happening sometimes and would look weird in a formula. Similarly I removed `@dataclass` since that also implies an import, and this also fits with the tweaked wording that the classes are fake. Test Plan: Added a new test case to the formula dataset which triggers the unwanted behaviour. The factors that seem to trigger the problem are (1) a small schema so the classes are easier to repeat and (2) the need to import modules, which the model wants to place before all other code. The case failed before this diff and succeeded after. The tweaked wording reduces the chances of repeating the classes but didn't eliminate it, so forcibly removing the classes in Python was needed. There were also a couple of other existing cases where repeating the classes was observed before but not after. Overall the score increased from 49 to 51 out of 69 (including the new case). At one point the score was 53, but changes in whitespace were enough to make it drop again. Reviewers: georgegevoian Reviewed By: georgegevoian Differential Revision: https://phab.getgrist.com/D4000
This commit is contained in:
@@ -144,20 +144,18 @@ export class OpenAIAssistant implements Assistant {
|
||||
newMessages.push({
|
||||
role: 'system',
|
||||
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. ' +
|
||||
"Below are one or more fake Python classes representing the structure of the user's data. " +
|
||||
'The function at the end needs completing. ' +
|
||||
"The user will probably give a description of what they want the function (a 'formula') to return. " +
|
||||
'If so, your response should include the function BODY as Python code in a markdown block. ' +
|
||||
"Your response will be automatically concatenated to the code below, so you mustn't repeat any of it. " +
|
||||
'You cannot change the function signature or define additional functions or classes. ' +
|
||||
'It 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' +
|
||||
"\n\n" +
|
||||
'```python\n' +
|
||||
await makeSchemaPromptV1(optSession, doc, request) +
|
||||
'\n```',
|
||||
@@ -198,6 +196,10 @@ export class OpenAIAssistant implements Assistant {
|
||||
|
||||
const userIdHash = getUserHash(optSession);
|
||||
const completion: string = await this._getCompletion(messages, userIdHash);
|
||||
|
||||
// It's nice to have this ready to uncomment for debugging.
|
||||
// console.log(completion);
|
||||
|
||||
const response = await completionToResponse(doc, request, completion);
|
||||
if (response.suggestedFormula) {
|
||||
// Show the tweaked version of the suggested formula to the user (i.e. the one that's
|
||||
|
||||
Reference in New Issue
Block a user