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
Copy file name to clipboardExpand all lines: public/js/modules/local-ai.js
+52-23Lines changed: 52 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -2,12 +2,24 @@
2
2
// This module provides the same interface as the Firebase functions but calls a local server instead.
3
3
constSETTINGS_KEY='localAiSettings';
4
4
5
+
// Default prompt templates. Placeholders use {varName} syntax.
6
+
constdefaultPrompts={
7
+
systemPrompt: 'You are a helpful Chinese teacher for speakers of English who want to learn Chinese. You speak naturally, and you provide helpful sentences that illustrate how to use Chinese vocabulary.',
8
+
explainChinese: 'Explain the Chinese text "{text}".',
9
+
translateEnglish: 'Translate the English text "{text}" into Chinese, and explain the translation.',
10
+
generateSentences: 'Please generate two example Chinese sentences, each with a separate English translation and pinyin, for each of the following definitions of the Chinese word "{word}":\n{definitions}\n\nEach sentence must include "{word}".',
11
+
analyzeCollocation: 'Please generate three example Chinese sentences, each with a separate English translation and pinyin, that uses the phrase "{collocation}".\nEach sentence must include "{collocation}".\n\nPlease also translate "{collocation}" to English and provide a plain-text explanation of how such a phrase would be used.',
12
+
analyzeImage: 'Read the Chinese text in this image, split it into sentences, and then explain it, including an English translation for each sentence and any relevant grammar rules. If the image contains good English translations of the Chinese text, use those verbatim.',
13
+
wordInContext: 'In the sentence "{sentence}", explain how the word "{word}" is used.\n\nProvide:\n1. The meaning of "{word}" as used in this specific sentence (in English).\n2. A plain-text explanation of why "{word}" is used here, including any nuances, grammatical role, or idiomatic usage that would help a learner understand its function in this context.\n\nKeep your explanation focused and practical for a language learner.'
// JSON Schema definitions matching the Firebase function schemas.
42
74
// See `functions/src/schemas.ts` for the backend schema definitions.
43
75
constschemas={
@@ -159,11 +191,6 @@ const schemas = {
159
191
}
160
192
};
161
193
162
-
// System prompts
163
-
constsystemPrompts={
164
-
chineseTeacher: 'You are a helpful Chinese teacher for speakers of English who want to learn Chinese. You speak naturally, and you provide helpful sentences that illustrate how to use Chinese vocabulary.'
content: `Please generate two example Chinese sentences, each with a separate English translation and pinyin, for each of the following definitions of the Chinese word "${word}":\n${definitionsList}\n\nEach sentence must include "${word}".`
content: `Please generate three example Chinese sentences, each with a separate English translation and pinyin, that uses the phrase "${collocation}".\nEach sentence must include "${collocation}".\n\nPlease also translate "${collocation}" to English and provide a plain-text explanation of how such a phrase would be used.`
@@ -296,15 +327,17 @@ async function analyzeCollocation(collocation) {
296
327
// TODO: how common are multi-modal models (which this assumes) in local AI setups?
297
328
// we might need to let the user pick a separate model for images? Not sure yet.
298
329
asyncfunctionanalyzeImage(base64ImageContents){
330
+
constprompts=getActivePrompts();
299
331
// Note: Image analysis requires a vision-capable model
300
332
// The base64 content should be in format: data:image/jpeg;base64,xxxxx
301
333
constmessages=[
334
+
{role: 'system',content: prompts.systemPrompt},
302
335
{
303
336
role: 'user',
304
337
content: [
305
338
{
306
339
type: 'text',
307
-
text: 'Read the Chinese text in this image, split it into sentences, and then explain it, including an English translation for each sentence and any relevant grammar rules. If the image contains good English translations of the Chinese text, use those verbatim.'
340
+
text: prompts.analyzeImage
308
341
},
309
342
{
310
343
type: 'image_url',
@@ -321,17 +354,12 @@ async function analyzeImage(base64ImageContents) {
content: `In the sentence "${sentence}", explain how the word "${word}" is used.
329
-
330
-
Provide:
331
-
1. The meaning of "${word}" as used in this specific sentence (in English).
332
-
2. A plain-text explanation of why "${word}" is used here, including any nuances, grammatical role, or idiomatic usage that would help a learner understand its function in this context.
333
-
334
-
Keep your explanation focused and practical for a language learner.`
362
+
content: applyTemplate(prompts.wordInContext,{ word, sentence })
0 commit comments