(core) Add AI Assistant retry with shorter prompt

Summary:
If the longer OpenAI model exceeds the OpenAPI context length, we now perform another retry with a
shorter variant of the formula prompt. The shorter prompt excludes non-referenced tables and lookup
method definitions, which should help reduce token usage in documents with larger schemas.

Test Plan: Server test.

Reviewers: JakubSerafin

Reviewed By: JakubSerafin

Subscribers: JakubSerafin

Differential Revision: https://phab.getgrist.com/D4184
This commit is contained in:
George Gevoian
2024-02-11 22:11:06 -05:00
parent 43a76235c7
commit 94eec5e906
5 changed files with 175 additions and 65 deletions

View File

@@ -160,7 +160,7 @@ def get_formula_prompt(engine, table_id, col_id, _description,
other_tables = (all_other_tables(engine, table_id)
if include_all_tables else referenced_tables(engine, table_id))
for other_table_id in sorted(other_tables):
result += class_schema(engine, other_table_id, lookups)
result += class_schema(engine, other_table_id, None, lookups)
result += class_schema(engine, table_id, col_id, lookups)

View File

@@ -146,8 +146,9 @@ def run(sandbox):
return objtypes.encode_object(eng.get_formula_error(table_id, col_id, row_id))
@export
def get_formula_prompt(table_id, col_id, description):
return formula_prompt.get_formula_prompt(eng, table_id, col_id, description)
def get_formula_prompt(table_id, col_id, description, include_all_tables=True, lookups=True):
return formula_prompt.get_formula_prompt(eng, table_id, col_id, description,
include_all_tables, lookups)
@export
def convert_formula_completion(completion):