-
-
Notifications
You must be signed in to change notification settings - Fork 735
Expand file tree
/
Copy pathApp.tsx
More file actions
166 lines (153 loc) · 5.85 KB
/
App.tsx
File metadata and controls
166 lines (153 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { createGroq } from "@ai-sdk/groq";
import { BlockNoteEditor, filterSuggestionItems } from "@blocknote/core";
import "@blocknote/core/fonts/inter.css";
import { en } from "@blocknote/core/locales";
import { BlockNoteView } from "@blocknote/mantine";
import "@blocknote/mantine/style.css";
import {
FormattingToolbar,
FormattingToolbarController,
getDefaultReactSlashMenuItems,
getFormattingToolbarItems,
SuggestionMenuController,
useCreateBlockNote,
} from "@blocknote/react";
import {
AIMenuController,
AIToolbarButton,
createAIExtension,
createBlockNoteAIClient,
getAISlashMenuItems,
} from "@blocknote/xl-ai";
import { en as aiEn } from "@blocknote/xl-ai/locales";
import "@blocknote/xl-ai/style.css";
import { getEnv } from "./getEnv.js";
// Optional: proxy requests through the `@blocknote/xl-ai-server` proxy server
// so that we don't have to expose our API keys to the client
const client = createBlockNoteAIClient({
apiKey: getEnv("BLOCKNOTE_AI_SERVER_API_KEY") || "PLACEHOLDER",
baseURL:
getEnv("BLOCKNOTE_AI_SERVER_BASE_URL") || "https://localhost:3000/ai",
});
// Use an "open" model such as llama, in this case via groq.com
const model = createGroq({
// call via our proxy client
...client.getProviderSettings("groq"),
})("llama-3.3-70b-versatile");
/*
ALTERNATIVES:
Call a model directly (without the proxy):
const model = createGroq({
apiKey: "<YOUR_GROQ_API_KEY>",
})("llama-3.3-70b-versatile");
Or, use a different provider like OpenAI:
const model = createOpenAI({
...client.getProviderSettings("openai"),
})("gpt-4", {});
*/
export default function App() {
// Creates a new editor instance.
const editor = useCreateBlockNote({
dictionary: {
...en,
ai: aiEn, // add default translations for the AI extension
},
// Register the AI extension
extensions: [
createAIExtension({
model,
/*
executor: (opts) => {
// fetch data
const resp = await fetch(opts)
// process to stream tool calls
const streamToolCalls = await yourLogicToConvertRespToStreamToolCalls(opts);
return LLMResponse.fromArray(opts.messages, opts.streamTools, streamToolCalls);
},*/
}),
],
// We set some initial content for demo purposes
initialContent: [
{
type: "heading",
props: {
level: 1,
},
content: "Open source software",
},
{
type: "paragraph",
content:
"Open source software refers to computer programs whose source code is made available to the public, allowing anyone to view, modify, and distribute the code. This model stands in contrast to proprietary software, where the source code is kept secret and only the original creators have the right to make changes. Open projects are developed collaboratively, often by communities of developers from around the world, and are typically distributed under licenses that promote sharing and openness.",
},
{
type: "paragraph",
content:
"One of the primary benefits of open source is the promotion of digital autonomy. By providing access to the source code, these programs empower users to control their own technology, customize software to fit their needs, and avoid vendor lock-in. This level of transparency also allows for greater security, as anyone can inspect the code for vulnerabilities or malicious elements. As a result, users are not solely dependent on a single company for updates, bug fixes, or continued support.",
},
{
type: "paragraph",
content:
"Additionally, open development fosters innovation and collaboration. Developers can build upon existing projects, share improvements, and learn from each other, accelerating the pace of technological advancement. The open nature of these projects often leads to higher quality software, as bugs are identified and fixed more quickly by a diverse group of contributors. Furthermore, using open source can reduce costs for individuals, businesses, and governments, as it is often available for free and can be tailored to specific requirements without expensive licensing fees.",
},
],
});
// Renders the editor instance using a React component.
return (
<div>
<BlockNoteView
editor={editor}
// We're disabling some default UI elements
formattingToolbar={false}
slashMenu={false}
>
{/* Add the AI Command menu to the editor */}
<AIMenuController />
{/* We disabled the default formatting toolbar with `formattingToolbar=false`
and replace it for one with an "AI button" (defined below).
(See "Formatting Toolbar" in docs)
*/}
<FormattingToolbarWithAI />
{/* We disabled the default SlashMenu with `slashMenu=false`
and replace it for one with an AI option (defined below).
(See "Suggestion Menus" in docs)
*/}
<SuggestionMenuWithAI editor={editor} />
</BlockNoteView>
</div>
);
}
// Formatting toolbar with the `AIToolbarButton` added
function FormattingToolbarWithAI() {
return (
<FormattingToolbarController
formattingToolbar={() => (
<FormattingToolbar>
{...getFormattingToolbarItems()}
{/* Add the AI button */}
<AIToolbarButton />
</FormattingToolbar>
)}
/>
);
}
// Slash menu with the AI option added
function SuggestionMenuWithAI(props: {
editor: BlockNoteEditor<any, any, any>;
}) {
return (
<SuggestionMenuController
triggerCharacter="/"
getItems={async (query) =>
filterSuggestionItems(
[
...getDefaultReactSlashMenuItems(props.editor),
// add the default AI slash menu items, or define your own
...getAISlashMenuItems(props.editor),
],
query,
)
}
/>
);
}