You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `CallLLM` function automatically passes the default options set in the `AIExtension` to the LLM request.
259
+
It also handles the LLM response and the AI menu state
260
+
261
+
For advanced use cases, you can also directly use the `doLLMRequest` to issue an LLM request.
262
+
In this case, you will need to manually handle the response.
263
+
264
+
```typescript
265
+
/**
266
+
* Execute an LLM call
267
+
*
268
+
* @parameditor - The BlockNoteEditor the LLM should operate on
269
+
* @paramopts - The options for the LLM call (@link {CallLLMOptions})
270
+
* @returns A `LLMResponse` object containing the LLM response which can be applied to the editor
271
+
*/
272
+
function doLLMRequest(
273
+
editor:BlockNoteEditor<any, any, any>,
274
+
opts:LLMRequestOptions,
275
+
):Promise<LLMResponse>;
276
+
```
277
+
278
+
Call `execute` on the `LLMResponse` object to apply the changes to the editor.
279
+
280
+
## PromptBuilder (advanced)
281
+
282
+
The `PromptBuilder` is a function that takes a BlockNoteEditor and details about the user's prompt
283
+
and turns it into an array of CoreMessage objects (AI SDK) to be passed to the LLM.
284
+
285
+
Providing a custom `PromptBuilder` allows fine-grained control over the instructions sent to the LLM.
286
+
To implement a custom `PromptBuilder`, you can use the `promptHelpers` provided by the LLM format.
287
+
We recommend looking at the [default PromptBuilder](https://github.com/TypeCellOS/BlockNote/blob/main/packages/xl-ai/src/api/formats/html-blocks/defaultHTMLPromptBuilder.ts) implementations as a starting point to implement your own.
288
+
289
+
```typescript
290
+
/**
291
+
* The input passed to a PromptBuilder
292
+
*/
293
+
typePromptBuilderInput= {
294
+
/**
295
+
* The user's prompt
296
+
*/
297
+
userPrompt:string;
298
+
/**
299
+
* The selection of the editor which the LLM should operate on
300
+
*/
301
+
selectedBlocks?:Block<any, any, any>[];
302
+
/**
303
+
* The ids of blocks that should be excluded from the prompt
304
+
* (e.g.: if `deleteEmptyCursorBlock` is true in the LLMRequest,
305
+
* this will be the id of the block that should be ignored)
306
+
*/
307
+
excludeBlockIds?:string[];
308
+
/**
309
+
* When following a multi-step conversation, or repairing a previous error,
310
+
* the previous messages that have been sent to the LLM
311
+
*/
312
+
previousMessages?:Array<CoreMessage>;
313
+
};
314
+
315
+
/**
316
+
* A PromptBuilder is a function that takes a BlockNoteEditor and details about the user's promot
317
+
* and turns it into an array of CoreMessage (AI SDK) to be passed to the LLM.
318
+
*/
319
+
typePromptBuilder= (
320
+
editor:BlockNoteEditor<any, any, any>,
321
+
opts:PromptBuilderInput,
322
+
) =>Promise<Array<CoreMessage>>;
323
+
```
324
+
325
+
## Formats
326
+
327
+
When a LLM is called, the LLM needs to interpret the document, and invoke operations to modify the document.
328
+
Different models might be able to understand different data formats better.
329
+
By default, BlockNote and LLM models interoperate using a HTML based format. We also provide experimental JSON and Markdown based formats.
330
+
331
+
```typescript
332
+
typeLLMFormat= {
333
+
/**
334
+
* Function to get th format specific stream tools that the LLM can choose to invoke
335
+
*/
336
+
getStreamTools: (
337
+
editor:BlockNoteEditor<any, any, any>,
338
+
withDelays:boolean,
339
+
defaultStreamTools?: {
340
+
add?:boolean;
341
+
update?:boolean;
342
+
delete?:boolean;
343
+
},
344
+
selectionInfo?: {
345
+
from:number;
346
+
to:number;
347
+
},
348
+
onBlockUpdate?: (blockId:string) =>void,
349
+
) =>StreamTool<any>[];
350
+
/**
351
+
* The default PromptBuilder that determines how a userPrompt is converted to an array of
352
+
* LLM Messages (CoreMessage[])
353
+
*/
354
+
defaultPromptBuilder:PromptBuilder;
355
+
/**
356
+
* Helper functions which can be used when implementing a custom PromptBuilder.
357
+
* The signature depends on the specific format
358
+
*/
359
+
promptHelpers:any;
360
+
};
361
+
362
+
// The default LLMFormats are exported under `llmFormats`:
0 commit comments