Skip to content

Commit 1b039ab

Browse files
committed
Tune LLM prompt and misc. LLM UI improvements
1 parent cbff63a commit 1b039ab

File tree

3 files changed

+40
-15
lines changed

3 files changed

+40
-15
lines changed

TODO.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,14 @@
3434
- CSP to restrict `worker-src` (except that we don't want to block web
3535
workers, only service workers)
3636
- LLM
37-
- Make LLM conversational -- send messages back and forth to iterate
37+
- Button to clear conversation
38+
- Button to export conversation
39+
- Button to import conversation
40+
- Link to where to get API key
41+
- API support
42+
- ChatGPT
43+
- Huggingface
44+
- Ollama
3845
- Clipboard integration
3946
- Cut
4047
- Fix "put" behavior to copy formulas around to selection

src/Llm.svelte

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,34 @@
6666
let responsePromise = $state();
6767
let modelName = $state("Gemini");
6868
let template =
69-
$state(`You modify a spreadsheet by executing JavaScript code. You output JavaScript code in Markdown blocks. You do not output any explanation or comments. You are concise and succinct. You are a technical expert with extensive experience with JavaScript and data science.
69+
$state(`You modify a spreadsheet by executing JavaScript code. You output JavaScript code in Markdown blocks. You do not output any explanation or comments. You are concise and succinct. You are a technical expert with extensive experience with JavaScript and data science. You query for more information if it would improve your response.
7070
71-
You have access to the following functions:
72-
- \${Object.entries(llmToolFunctions).map(([name, f]) => {
73-
const args = f.toString().replaceAll("\\n", " ").replaceAll(/ */g, " ").match(/\\([^)]*\\)/)?.[0] ?? "";
74-
return \`llmToolFunctions.\${name}\${args} \${f.description ?? ""}\`;
75-
}).join("\\n- ")}
76-
77-
Spreadsheet formulas use R0C0 notation. Row and column indices start at 0. Formulas support double-quoted strings, integers, floats, booleans, function calls, and arithmetic. Formulas begin with \\\`=\\\` unless they only contain a single number. Anything that is not a nubmer or formula is a string. Formulas can call custom functions defined in JavaScript. Formula functions receive parsed arguments. Cell formatting is handled by formulas (for example the \\\`BOLD\\\` formula will make the cell bold by editing this.style).
71+
Formulas begin with an equals sign (\\\`=\\\`), and can contain:
72+
- Numbers such as \\\`123\\\` and \\\`-3.21\\\`
73+
- Strings such as \\\`"asdf"\\\` and \\\`"multi\\\\nline"\\\`
74+
- Singleton references in R1C1 notation such as \\\`R10C3\\\` (zero-indexed) for absolute references, \\\`R[-1]c[2]\\\` for relative references, and \\\`RC\\\` for self-references
75+
- Negative absolute references start from the end of a row or column, such as \\\`R-1C-1\\\` to select the cell in the bottom right corner of the sheet, and \\\`R1C0:R1C-1\\\` to select all of row 1
76+
- Ranges such as \\\`R[-3]C:R[-1]C\\\`
77+
- References and ranges across sheets like \\\`S1!R1C1\\\` and \\\`S[1]!R2C2:R2C-1\\\` and \\\`S-1R2C3\\\` (the exclamation point is optional)
78+
- Function calls (case insensitive) containing expressions as arguments such as \\\`sum(RC0:RC[-1])\\\`, \\\`sLiDeR(0, 10, 1)\\\`, and \\\`DOLLARS(PRODUCT(1 * 2 + 3, 4, 3, R[-1]C))\\\`
79+
- Optionally parenthesized binary operations combining any of the expressions above such as \\\`(RC[-2] + RC[-3]) * 100\\\` and \\\`1 + -2 + 3 ** 5\\\`
7880
79-
Formula functions have access to a \\\`this\\\` object with:
81+
Formula function definitions have access to a \\\`this\\\` object with:
8082
- this.row and this.col - readonly
8183
- this.set(value)
8284
- this.element - writable value with the HTML element that will be displayed in the cell (e.g., buttons, checkboxes, canvas, SVG, etc.)
8385
- this.style - writable value with the CSS style string for the containing \\\`<td>\\\`
8486
85-
To register formula functions, they must be assigned to the functions object like: "functions.formula_name = function() {}". Create any formulas necessary.
87+
You define any formula functions you use that do not already exist. To define formula functions, they must be assigned like: "functions.formula_name = function() {}" in a call to \\\`addFunction\\\`.
8688
8789
The currently available formula functions are all of the JavaScript Math.* functions and: \${Object.keys(formulaFunctions).filter(k => !(k in Math)).join(", ")}.
8890
91+
You can run the following functions:
92+
- \${Object.entries(llmToolFunctions).map(([name, f]) => {
93+
const args = f.toString().replaceAll("\\n", " ").replaceAll(/ */g, " ").match(/\\([^)]*\\)/)?.[0] ?? "";
94+
return \`llmToolFunctions.\${name}\${args} \${f.description ?? ""}\`;
95+
}).join("\\n- ")}
96+
8997
Available spreadsheets:
9098
\${
9199
globals.sheets.map(
@@ -144,7 +152,7 @@ Available spreadsheets:
144152
});
145153
}
146154
147-
function execute(llmCode) {
155+
function execute(llmCode, i) {
148156
llmToolFunctions.globals = globals;
149157
eval(
150158
llmCode +
@@ -153,6 +161,10 @@ Available spreadsheets:
153161
);
154162
delete llmToolFunctions.globals;
155163
}
164+
165+
function scrollIntoView(e) {
166+
e.scrollIntoView();
167+
}
156168
</script>
157169
158170
<h1>Edit Spreadsheets with Large Language Models ("AI")</h1>
@@ -219,7 +231,7 @@ Available spreadsheets:
219231
</Details>
220232
</div>
221233
{:else if part.role == "model"}
222-
<div style="margin-right: 10%;">
234+
<div use:scrollIntoView style="margin-right: 10%;">
223235
{#if part.code}
224236
<Details open>
225237
{#snippet summary()}Code{/snippet}
@@ -228,7 +240,7 @@ Available spreadsheets:
228240
style="min-height: 10em; resize: vertical;"
229241
></CodeEditor>
230242
<div class="buttons">
231-
<Button onclick={() => execute(part.code)}>Execute</Button>
243+
<Button onclick={() => execute(part.code, i)}>Execute</Button>
232244
</div>
233245
</Details>
234246
{:else}
@@ -242,7 +254,7 @@ Available spreadsheets:
242254
{/each}
243255
244256
{#await responsePromise}
245-
<p>Loading...</p>
257+
<p use:scrollIntoView>Loading...</p>
246258
{:catch e}
247259
<p>Error: {e?.message ?? e}</p>
248260
{/await}

src/llm.svelte.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { functions } from "./formula-functions.svelte";
2+
13
export const llmToolFunctions = $state({});
24

35
llmToolFunctions.newSheet = function (name, cells) {
@@ -27,6 +29,10 @@ llmToolFunctions.setCellFormula = function (sheetIndex, row, col, formula) {
2729
this.globals.sheets[sheetIndex].cells[row][col].formula = formula;
2830
};
2931

32+
llmToolFunctions.getFormulaFunction = function (name) {
33+
return functions[name].toString();
34+
};
35+
3036
// TODO: Save models to IndexDB (or local storage)
3137
export const llmModels = $state({});
3238

0 commit comments

Comments
 (0)