-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
1085 lines (973 loc) ยท 49 KB
/
Copy pathchatbot.py
File metadata and controls
1085 lines (973 loc) ยท 49 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import streamlit as st
import time
from datetime import datetime
import re
import base64
from langchain_openai import ChatOpenAI
from utils import get_level_appropriate_content, get_level_color, format_level_badge
# Generic system prompt with language-specific adaptation
SYSTEM_PROMPT = """
You are an adaptive language tutor designed for learners. Your goal is to create an immersive, personalized learning experience strictly adapted to the learner's current CEFR level (A1, A2, B1, B2, or C1) and their target language.
## CORE CAPABILITIES
โข Translate text between the target language and English (format: "T: text")
โข Analyze and provide feedback on learner-submitted text
โข Create personalized exercises incorporating previously learned material
โข Track vocabulary and grammar concepts the learner has encountered
โข Adapt difficulty based on learner's level and performance
โข Create exercises only when the user asks for them
โข ALWAYS consider the learner's current level (A1, A2, B1, B2, C1) and target language in ALL interactions
## LEVEL ADAPTATION
You MUST follow these strict guidelines based on the learner's CEFR level:
โข A1 (Beginner): Use only very basic vocabulary and simple present tense. Focus on everyday words, simple sentence structures, basic questions, and simple grammar. Limit instructions to short, clear sentences. Use only present tense and avoid complex grammar.
โข A2 (Elementary): Build on A1 with past tense, more grammar structures, and expanded everyday vocabulary. Keep explanations simple.
โข B1 (Intermediate): Introduce perfect and imperfect tenses, conditional mood, passive voice, and more complex grammar rules. Expand to more abstract vocabulary but avoid specialized terminology.
โข B2 (Upper Intermediate): Include more complex moods, complex sentence structures, and more specialized vocabulary. Allow for complex topics but structure them clearly.
โข C1 (Advanced): Use all grammatical structures, literary language, complex constructions, and specialized vocabulary. Challenge the learner with authentic language use.
## TRANSLATION & EXPLANATIONS
- The medium of instruction and explanation will be English.
- When translating, provide:
1. The direct translation
2. Concise explanations of relevant grammar features, BUT ONLY features appropriate for the current level
3. Example sentences using the same words/structures in different contexts, ensuring all examples use LEVEL-APPROPRIATE vocabulary and grammar
4. Cultural notes when relevant
5. Simplified pronunciation guidance when appropriate
- If the word is incorrect due to a spelling mistake, provide the correct word and its translation.
## EXERCISE TYPES
ALWAYS adapt exercises strictly to the learner's current CEFR level. The user may ask for the following exercises.
1. Reading exercises: Create at least 3 paragraphs in the form of a story or situation, using the grammar and vocabulary ONLY from the current level. Do not create any sections for the paragraphs.
2. Vocabulary exercises: These exercises must introduce words appropriate for the current level. Also introduce the words previous used or asked by the user.
3. Writing exercises: Create at least 3 paragraphs in the form of a story or situation **in English**, using the grammar and vocabulary ONLY from the current level. Do not create any sections for the paragraphs. **Do not create anything else just the 3 paragraphs of English text.** AND **DO NOT give the user the writing tasks or the writing instructions. Give him the exact 3 paragraphs which he can translate in the selected language**. After generating the text, offer user to check their answer.
4. Quizzes: Quizzes should test concepts appropriate for the current level. These could be multiple choice questions.
The reading and writing exercises should be related to a given context or to the daily life matters such as housing, household, healthcare, worklife, education, family life, social life, travels, phone calls to different services (police, hospital, school, offices, etc.), daily life problems and activities, asking help, and others.
Reading exercise should be followed by its translation for reference.
## FEEDBACK APPROACH
โข After creating a vocabulary or quizz exercise, tell the user how to answer the questions and how you'll evaluate them
โข Mark answers as correct or incorrect with appropriate symbols in the target language
โข For incorrect answers, explain the specific error and provide the correct form, using explanations matching their level
โข Highlight patterns in mistakes to address underlying misconceptions
โข Offer encouraging feedback that acknowledges progress
โข After each exercise, provide a summary and assessment of the answers
โข ALWAYS consider the learner's level when giving feedback - be gentler and more basic with beginners
## PERSONALIZATION
โข Adjust complexity based on demonstrated proficiency and current level
โข Use spaced repetition to reintroduce challenging concepts
โข Respond to emotional cues in learner messages with appropriate encouragement
## FILE HANDLING
โข For uploaded files, analyze the content and extract text in the target language when possible
โข For all content from files, STRICTLY adapt your analysis, translations, and exercises to the learner's current level
โข If content is significantly above the learner's level, simplify it or select portions appropriate to their level
## FORMATTING AND INTERACTION
โข Begin each new session by greeting the learner in the target language with an appropriate time-of-day greeting
โข Format your responses with clear headings, examples, and explanations
โข Use emoji sparingly for emphasis
โข Structure translations in a clear, readable format that distinguishes the target language and English text
โข For grammar explanations, use tables when useful to show patterns (like verb conjugations or noun cases)
โข ALWAYS include the learner's current level (e.g., A1, B2) visually in your responses
Remember: It is ESSENTIAL that you never introduce vocabulary or grammar that is beyond the learner's current level, as this will confuse and discourage them. Always check if content is appropriate for their specified CEFR level before presenting it.
"""
# Function to get detailed CEFR level guidelines for each level and language
def get_cefr_level_guidelines(level_code, language_code):
"""
Returns detailed guidelines for a specific CEFR level and language
"""
# Base guidelines applicable to most languages
base_guidelines = {
"A1": """
A1 LEVEL GUIDELINES:
- Vocabulary: Only basic words (around 500-800 words) related to immediate needs
- Grammar: Present tense only, basic question formation, simple negation
- Sentence structure: Simple, short sentences with basic connectors (and, but)
- Topics: Personal information, basic everyday activities, simple needs
- Numbers 1-100, basic time expressions
- Avoid any advanced structures, past tenses, conditional forms
""",
"A2": """
A2 LEVEL GUIDELINES:
- Vocabulary: Expanded everyday vocabulary (around 1500 words)
- Grammar: Simple past tense, basic verb conjugation patterns
- Sentence structure: Simple sentences with some coordination
- Topics: Daily routines, shopping, local environment, simple past experiences
- Simple descriptive adjectives
- Avoid complex tenses, conditionals, complex clauses
""",
"B1": """
B1 LEVEL GUIDELINES:
- Vocabulary: More varied vocabulary (around 3000 words), some abstract terms
- Grammar: Perfect and imperfect tenses, conditional mood, passive forms
- Sentence structure: Compound sentences, simple subordination
- Topics: Work, school, leisure, travel, current events, feelings
- Comparative forms
- Avoid literary language, complex constructions
""",
"B2": """
B2 LEVEL GUIDELINES:
- Vocabulary: Broader vocabulary (around 5000 words), some specialized terms
- Grammar: All tense forms, more complex verbal constructions
- Sentence structure: Complex sentences with various clause types
- Topics: Social issues, professional topics, abstract concepts, hypothetical situations
- Imperative forms, indirect speech
- Avoid highly specialized terminology, dialectal expressions
""",
"C1": """
C1 LEVEL GUIDELINES:
- Vocabulary: Rich vocabulary (8000+ words), specialized terms, colloquial expressions
- Grammar: All grammatical structures, including rare forms
- Sentence structure: Sophisticated, complex sentences with embedded clauses
- Topics: Any academic, professional or abstract topic, cultural references
- Complex constructions, literary expressions
- Nuanced differences in word meanings and connotations
- Full range of language features
"""
}
# Language-specific guidelines
language_specific = {
"fin": {
"A1": """
FINNISH A1 SPECIFIC GUIDELINES:
- Focus on nominative/partitive/genitive cases only
- Simple consonant gradation patterns (kk-k, pp-p, tt-t)
- Basic subject-verb agreement
- Simple question particles and words
- Personal pronouns (minรค, sinรค, hรคn, etc.)
""",
"A2": """
FINNISH A2 SPECIFIC GUIDELINES:
- Include inessive, elative, illative cases
- All basic verb types
- Standard consonant gradation patterns
- Simple possessive suffixes
- More extensive use of partitive case
""",
"B1": """
FINNISH B1 SPECIFIC GUIDELINES:
- All locative cases (also adessive, ablative, allative)
- Object cases and their rules
- Perfect and pluperfect tenses
- Passive voice (present and past)
- More complex uses of pronouns and demonstratives
""",
"B2": """
FINNISH B2 SPECIFIC GUIDELINES:
- Potential mood
- Complex object rules
- Multiple infinitive forms
- Participles
- Complex sentence structures
""",
"C1": """
FINNISH C1 SPECIFIC GUIDELINES:
- Complex participle constructions
- Literary expressions and rare forms
- Dialectal variations
- Subtle case usage differences
- Advanced idiomatic expressions
"""
},
"spa": {
"A1": """
SPANISH A1 SPECIFIC GUIDELINES:
- Regular verb conjugations in present tense
- Basic gender and number agreement
- Simple prepositions
- Question formation with intonation
- Basic adjective placement
- Common irregular verbs (ser, estar, ir, tener)
""",
"A2": """
SPANISH A2 SPECIFIC GUIDELINES:
- Preterite vs. imperfect tenses
- Reflexive verbs
- Direct and indirect object pronouns
- Common irregular verbs
- Comparative forms
- Simple commands
""",
"B1": """
SPANISH B1 SPECIFIC GUIDELINES:
- Present subjunctive
- Future and conditional tenses
- Perfect tenses
- Por vs. para distinctions
- Relative pronouns
- Formal commands
""",
"B2": """
SPANISH B2 SPECIFIC GUIDELINES:
- All subjunctive uses (present and imperfect)
- Compound tenses
- Passive structures
- Advanced connecting phrases
- Idiomatic expressions
- Reported speech
""",
"C1": """
SPANISH C1 SPECIFIC GUIDELINES:
- Regional variations and dialectal features
- Literary language
- Complex hypothetical structures
- Subtle tense distinctions
- Cultural and historical references
- Specialized vocabulary in various domains
"""
},
"fra": {
"A1": """
FRENCH A1 SPECIFIC GUIDELINES:
- Regular -er verb conjugations
- Basic irregular verbs (รชtre, avoir, aller, faire)
- Gender of nouns
- Definite and indefinite articles
- Basic prepositions
- Question formation with est-ce que
- Negation with ne...pas
""",
"A2": """
FRENCH A2 SPECIFIC GUIDELINES:
- Passรฉ composรฉ with avoir and รชtre
- Reflexive verbs
- Direct and indirect object pronouns
- Imperative mood
- Futur proche (aller + infinitive)
- Introduction to imparfait
- Comparative forms
""",
"B1": """
FRENCH B1 SPECIFIC GUIDELINES:
- All past tenses (passรฉ composรฉ, imparfait, plus-que-parfait)
- Future simple
- Conditional present
- Introduction to subjunctive
- Relative pronouns (qui, que, oรน, dont)
- Passive voice
- Reported speech
""",
"B2": """
FRENCH B2 SPECIFIC GUIDELINES:
- Advanced subjunctive uses
- Conditional past
- Literary tenses (recognition)
- Complex pronouns (y, en, lequel)
- Advanced connecting expressions
- Nominalizations
- Idiomatic expressions
""",
"C1": """
FRENCH C1 SPECIFIC GUIDELINES:
- Literary tenses (passรฉ simple, subjonctif imparfait)
- Complex hypothetical structures
- Advanced nominalizations
- Stylistic variations
- Regional expressions
- Linguistic nuances and connotations
- Specialized terminology
"""
},
"deu": {
"A1": """
GERMAN A1 SPECIFIC GUIDELINES:
- Present tense regular and irregular verbs
- Word order in main clauses
- Nominative and accusative cases
- Modal verbs
- Definite and indefinite articles
- Question formation
- Negation with nicht and kein
""",
"A2": """
GERMAN A2 SPECIFIC GUIDELINES:
- Perfect tense
- Dative case
- Prepositions with fixed case
- Imperative forms
- Possessive articles
- Comparative and superlative
- Subordinate clauses with weil, dass, wenn
""",
"B1": """
GERMAN B1 SPECIFIC GUIDELINES:
- Genitive case
- Adjective declension
- Simple passive voice
- Subjunctive II (wรผrde + infinitive, hรคtte, wรคre)
- Relative clauses
- Temporal prepositions and conjunctions
- Future tense
""",
"B2": """
GERMAN B2 SPECIFIC GUIDELINES:
- Konjunktiv I (reported speech)
- Extended passive voice (with modal verbs)
- N-declension nouns
- Participle constructions
- Advanced connecting phrases
- Extended attributes
- Advanced subordinate clauses
""",
"C1": """
GERMAN C1 SPECIFIC GUIDELINES:
- Advanced verbal constructions
- All passive and subjunctive forms
- Nominalized verbs
- Literary and formal expressions
- Idiomatic expressions and collocations
- Regional variations
- Complex sentence structures
"""
},
"ita": {
"A1": """
ITALIAN A1 SPECIFIC GUIDELINES:
- Present tense of regular verbs (-are, -ere, -ire)
- Common irregular verbs (essere, avere, fare, andare)
- Definite and indefinite articles
- Noun gender and number
- Basic adjective agreement
- Simple prepositions
- Question formation
""",
"A2": """
ITALIAN A2 SPECIFIC GUIDELINES:
- Passato prossimo with avere and essere
- Introduction to imperfetto
- Reflexive verbs
- Direct object pronouns
- Comparative forms
- Future tense (introduction)
- Modal verbs (dovere, potere, volere)
""",
"B1": """
ITALIAN B1 SPECIFIC GUIDELINES:
- Contrasting imperfetto and passato prossimo
- Future tense
- Conditional present
- Introduction to congiuntivo
- Combined pronouns
- Relative pronouns (che, cui, quale)
- Introduction to passive voice
""",
"B2": """
ITALIAN B2 SPECIFIC GUIDELINES:
- All subjunctive tenses
- Passato remoto (recognition)
- Conditional past
- Advanced pronoun usage
- Passive constructions
- Gerund and participle forms
- Complex connecting expressions
""",
"C1": """
ITALIAN C1 SPECIFIC GUIDELINES:
- Literary tenses
- Complex sentence structures
- Stylistic variations
- Idiomatic expressions
- Regional language variations
- Advanced formal registers
- Specialized terminology
"""
},
"rus": {
"A1": """
RUSSIAN A1 SPECIFIC GUIDELINES:
- Cyrillic alphabet and pronunciation
- Present tense of common verbs
- Gender of nouns
- Personal and possessive pronouns
- Nominative case
- Simple questions
- Numerals 1-100
""",
"A2": """
RUSSIAN A2 SPECIFIC GUIDELINES:
- Past tense
- Future tense (imperfective and simple perfective)
- Introduction to cases (accusative, prepositional)
- Basic aspects of verbs
- Possessive pronouns
- Adjective agreement
- More question types
""",
"B1": """
RUSSIAN B1 SPECIFIC GUIDELINES:
- All cases (nominative, accusative, genitive, dative, instrumental, prepositional)
- Verbal aspects (perfective and imperfective)
- Basic verbs of motion
- Imperatives
- Conditional expressions
- Complex sentence structures
- Time expressions
""",
"B2": """
RUSSIAN B2 SPECIFIC GUIDELINES:
- Verbs of motion with prefixes
- Participles and verbal adverbs (introduction)
- Passive constructions
- Complex sentences
- Advanced use of cases
- Numerals and counting
- Formal and informal registers
""",
"C1": """
RUSSIAN C1 SPECIFIC GUIDELINES:
- Complex verbal constructions
- Advanced participles and verbal adverbs
- Nuanced aspects usage
- Idiomatic expressions
- Stylistic variations
- Language of literature and media
- Dialectal features
"""
},
"swe": {
"A1": """
SWEDISH A1 SPECIFIC GUIDELINES:
- Present tense of regular verbs
- Common irregular verbs (รคr, har, gรถr)
- En/ett gender system
- Indefinite and definite forms of nouns
- Personal pronouns
- Word order in main clauses
- Simple questions
""",
"A2": """
SWEDISH A2 SPECIFIC GUIDELINES:
- Past tense (preteritum)
- Present perfect (perfekt)
- Adjective agreement
- Modal verbs
- Adverbs and word order
- Possessive pronouns
- Comparative forms
""",
"B1": """
SWEDISH B1 SPECIFIC GUIDELINES:
- Future constructions
- Conditional forms
- Subordinate clauses and word order
- Relative clauses
- Passive voice
- Reflexive verbs
- Particles and phrasal verbs
""",
"B2": """
SWEDISH B2 SPECIFIC GUIDELINES:
- Past perfect (pluskvamperfekt)
- Subjunctive in fixed expressions
- Advanced subordinate clauses
- Extended attributes
- S-passiv vs. bli-passiv
- Advanced connecting expressions
- Formal language
""",
"C1": """
SWEDISH C1 SPECIFIC GUIDELINES:
- Complex verbal constructions
- Advanced word order variations
- Stylistic nuances
- Idiomatic expressions
- Regional language variations
- Literary language
- Specialized terminology
"""
}
}
# Start with base guidelines
guidelines = base_guidelines.get(level_code, "")
# Add language-specific guidelines if available
if language_code in language_specific and level_code in language_specific[language_code]:
guidelines += language_specific[language_code][level_code]
return guidelines
# Function to convert chat history to markdown format
def get_chat_history_markdown(session_state):
"""
Convert the chat history to a markdown string format with level indicators
"""
# Get language info if available
lang_code = session_state.selected_language if hasattr(session_state, 'selected_language') else "fin"
lang_name = "Finnish" # Default
# Try to get language name from app.py's SUPPORTED_LANGUAGES
try:
import app
if hasattr(app, 'SUPPORTED_LANGUAGES') and lang_code in app.SUPPORTED_LANGUAGES:
lang_name = app.SUPPORTED_LANGUAGES[lang_code]["name"]
except:
# If can't import, use a basic mapping
lang_mapping = {
"fin": "Finnish",
"spa": "Spanish",
"fra": "French",
"deu": "German",
"ita": "Italian",
"jpn": "Japanese",
"zho": "Chinese",
"rus": "Russian"
}
lang_name = lang_mapping.get(lang_code, "Unknown")
markdown_text = f"# Polyglot {lang_name} Language Tutor - Chat History\n\n"
markdown_text += f"Session ID: {session_state.session_id}\n"
markdown_text += f"Date: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
markdown_text += f"Language: {lang_name}\n"
markdown_text += f"Current Proficiency Level: {session_state.selected_level}\n\n"
# Add level history if available
if hasattr(session_state, 'level_history') and session_state.level_history:
markdown_text += "## Level Progression\n\n"
for change in session_state.level_history:
lang = change.get('language', lang_code)
lang_display = ""
try:
import app
if hasattr(app, 'SUPPORTED_LANGUAGES') and lang in app.SUPPORTED_LANGUAGES:
lang_display = f" ({app.SUPPORTED_LANGUAGES[lang]['name']})"
except:
pass
markdown_text += f"- Changed from {change['from']} to {change['to']} on {change['timestamp']}{lang_display}\n"
markdown_text += "\n"
markdown_text += "---\n\n"
for message in session_state.chat_history:
timestamp = message.get("timestamp", "")
if message["role"] == "user":
markdown_text += f"## User ({timestamp})\n\n"
markdown_text += f"{message['content']}\n\n"
else:
# Try to determine what level this message was at
level = "Unknown"
if hasattr(message, 'level'):
level = message.get('level', session_state.selected_level)
markdown_text += f"## Tutor - {level} ({timestamp})\n\n"
markdown_text += f"{message['content']}\n\n"
markdown_text += "---\n\n"
return markdown_text
# Extract and track topics from user messages
def extract_topics(message, current_topics):
"""
Extract potential learning topics from user messages using LLM to personalize future exercises
Parameters:
- message: User's message text
- current_topics: List of previously identified topics
Returns:
- Updated list of topics
"""
# If message is too short, return current topics unchanged
if not message or len(message.strip()) < 10:
return current_topics
try:
# Try to use LLM for topic extraction
new_topics = extract_topics_llm(message)
# Add new topics to current list, avoiding duplicates
updated_topics = current_topics.copy()
for topic in new_topics:
if topic and topic not in updated_topics:
updated_topics.append(topic)
# Keep list at reasonable size
if len(updated_topics) > 30:
updated_topics = updated_topics[-30:]
return updated_topics
except Exception as e:
# Fallback to simplified rule-based method if LLM fails
import logging
logging.warning(f"LLM topic extraction failed: {str(e)}. Using rule-based method.")
# Simplified rule-based fallback (similar to original implementation)
# Look for potential words excluding common stopwords
import re
words = re.findall(r'\b[a-zA-Z\u00C0-\u00FF\u0100-\u017F\u0180-\u024F\u0370-\u03FF\u0400-\u04FF]{4,}\b',
message.lower())
# Basic stopwords to filter out
stopwords = {"about", "after", "all", "also", "and", "any", "because", "but", "can", "come", "could",
"day", "even", "first", "from", "get", "give", "have", "here", "him", "his", "how",
"into", "its", "just", "know", "like", "look", "make", "many", "more", "most", "much",
"must", "new", "now", "one", "only", "other", "our", "out", "over", "people", "say",
"see", "she", "some", "take", "than", "that", "the", "their", "them", "then", "there",
"these", "they", "think", "this", "time", "two", "use", "very", "want", "way", "well",
"what", "when", "which", "who", "will", "with", "would", "your"}
# Filter out stopwords
filtered_words = [word for word in words if word not in stopwords]
# Grammar terms to specifically look for
grammar_terms = re.findall(r'\b(verb|noun|case|tense|plural|singular|adjective|adverb|conjugation|particle|preposition|article|gender|declension)\b',
message.lower())
# Learning-related terms to specifically look for
learning_terms = re.findall(r'\b(exercise|translate|vocabulary|grammar|pronunciation|reading|writing|speaking|listening)\b',
message.lower())
# Combine filtered words with grammar and learning terms
all_topics = filtered_words + grammar_terms + learning_terms
# Add new topics to current list, avoiding duplicates
updated_topics = current_topics.copy()
for topic in all_topics:
if topic not in updated_topics:
updated_topics.append(topic)
# Keep list at reasonable size
if len(updated_topics) > 30:
updated_topics = updated_topics[-30:]
return updated_topics
# Use caching for LLM-based topic extraction to improve performance
import functools
@functools.lru_cache(maxsize=100)
def extract_topics_llm(message):
"""
Extract learning topics from message using LLM with caching
"""
# Import required libraries
from langchain_openai import ChatOpenAI
import streamlit as st
import json
import re
# Get API key from Streamlit secrets
api_key = st.secrets.get("OPENAI_API_KEY", "")
if not api_key:
raise ValueError("OpenAI API key not configured in Streamlit secrets")
# Initialize the LLM
model_name = st.secrets.get("MODEL_NAME", "gpt-4.1-mini-2025-04-14")
chat = ChatOpenAI(
openai_api_key=api_key,
model=model_name,
max_tokens=150 # Small context for topic extraction
)
# Get current language and level if available
current_language = "unknown"
current_level = "unknown"
try:
if hasattr(st.session_state, 'selected_language'):
language_code = st.session_state.selected_language
import app
if hasattr(app, 'SUPPORTED_LANGUAGES') and language_code in app.SUPPORTED_LANGUAGES:
current_language = app.SUPPORTED_LANGUAGES[language_code]["name"]
if hasattr(st.session_state, 'selected_level'):
current_level = st.session_state.selected_level
except:
pass
# Prepare the prompt
prompt = [
{
"role": "system",
"content": f"""You are a topic extraction system for a language learning application.
Extract meaningful learning topics from the user's message that can be used to personalize future exercises.
The user is learning {current_language} at {current_level} level.
Focus on extracting:
1. Grammar concepts mentioned (e.g., verb tenses, cases, pronouns)
2. Vocabulary themes (e.g., food, travel, work)
3. Language skills (e.g., reading, writing, pronunciation)
4. Specific linguistic features of {current_language}
5. Learning goals or interests
Return a JSON array of 2-8 topics, with each topic being a single word or short phrase.
Example: ["past tense", "food vocabulary", "pronunciation", "travel"]
Only extract topics that are actually present in the message. If no clear topics are found, return an empty array.
"""
},
{
"role": "user",
"content": f"Extract learning topics from this message: \"{message}\""
}
]
# Get the response from the LLM
response = chat.invoke(prompt)
response_content = response.content.strip()
# Try to parse the response as JSON
try:
# Extract JSON array if embedded in text
json_match = re.search(r'\[.*\]', response_content, re.DOTALL)
if json_match:
topics = json.loads(json_match.group(0))
else:
# Try parsing the whole response as JSON
topics = json.loads(response_content)
# Ensure it's a list
if not isinstance(topics, list):
topics = []
except:
# If parsing fails, extract topics using simple regex
topics = re.findall(r'"([^"]+)"', response_content)
if not topics:
# Try without quotes
topics = [t.strip() for t in response_content.split(",") if t.strip()]
# Filter out any non-string items and convert to lowercase
topics = [str(topic).lower() for topic in topics if topic]
return topics
# Function to process user messages
def process_question(question, session_state):
"""
Process a user message, update topic tracking, and generate assistant response
"""
# Get current level code
current_level = session_state.selected_level
level_code = current_level.split()[0] # A1, A2, etc.
# Get current language
lang_code = session_state.selected_language if hasattr(session_state, 'selected_language') else "fin"
# Add user question to the chat
session_state.messages.append({"role": "user", "content": question})
session_state.chat_history.append({
"role": "user",
"content": question,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"level": current_level, # Track level at time of message
"language": lang_code # Track language at time of message
})
# Extract topics from the user message
session_state.user_topics = extract_topics(question, session_state.user_topics)
# Set chat as started
session_state.chat_started = True
# Get AI response
response = call_openai_api(session_state)
# Add assistant response to chat
session_state.messages.append({"role": "assistant", "content": response})
session_state.chat_history.append({
"role": "assistant",
"content": response,
"timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"level": current_level, # Track level at time of message
"language": lang_code # Track language at time of message
})
# Reset level and language change flags if they were set
if hasattr(session_state, 'current_level_changed') and session_state.current_level_changed:
session_state.current_level_changed = False
if hasattr(session_state, 'language_changed') and session_state.language_changed:
session_state.language_changed = False
# Function to get MIME type description
def get_file_type_description(mime_type):
"""
Get a user-friendly description of a file based on its MIME type
"""
mime_descriptions = {
"text/plain": "text file",
"text/csv": "CSV spreadsheet",
"text/html": "HTML document",
"text/markdown": "Markdown document",
"application/pdf": "PDF document",
"application/json": "JSON file",
"application/xml": "XML document",
"application/msword": "Word document",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "Word document",
"application/vnd.ms-excel": "Excel spreadsheet",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "Excel spreadsheet",
"application/vnd.ms-powerpoint": "PowerPoint presentation",
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "PowerPoint presentation",
"image/jpeg": "JPEG image",
"image/png": "PNG image",
"image/gif": "GIF image",
"image/svg+xml": "SVG image",
"audio/mpeg": "MP3 audio file",
"audio/wav": "WAV audio file",
"video/mp4": "MP4 video file"
}
# Check if we have a specific description, otherwise use the generic MIME type
if mime_type in mime_descriptions:
return mime_descriptions[mime_type]
elif mime_type.split('/')[0] in ["text", "application", "image", "audio", "video"]:
return f"{mime_type.split('/')[0]} file"
else:
return "file"
# Function to get language display name
def get_language_display_name(lang_code):
"""
Get the display name for a language code
"""
try:
# Try to import app.py to get language info
import app
if hasattr(app, 'SUPPORTED_LANGUAGES') and lang_code in app.SUPPORTED_LANGUAGES:
return app.SUPPORTED_LANGUAGES[lang_code]["name"]
except:
# Fallback if import fails
language_names = {
"fin": "Finnish",
"spa": "Spanish",
"fra": "French",
"deu": "German",
"ita": "Italian",
"jpn": "Japanese",
"zho": "Chinese",
"rus": "Russian",
"ara": "Arabic",
"por": "Portuguese",
"hin": "Hindi",
"swe": "Swedish",
"nld": "Dutch",
"tur": "Turkish"
}
return language_names.get(lang_code, "Unknown")
# Function to get language flag emoji
def get_language_flag(lang_code):
"""
Get the flag emoji for a language code
"""
try:
# Try to import app.py to get language info
import app
if hasattr(app, 'SUPPORTED_LANGUAGES') and lang_code in app.SUPPORTED_LANGUAGES:
return app.SUPPORTED_LANGUAGES[lang_code]["flag"]
except:
# Fallback if import fails
language_flags = {
"fin": "๐ซ๐ฎ",
"spa": "๐ช๐ธ",
"fra": "๐ซ๐ท",
"deu": "๐ฉ๐ช",
"ita": "๐ฎ๐น",
"jpn": "๐ฏ๐ต",
"zho": "๐จ๐ณ",
"rus": "๐ท๐บ",
"ara": "๐ธ๐ฆ",
"por": "๐ต๐น",
"hin": "๐ฎ๐ณ",
"swe": "๐ธ๐ช",
"nld": "๐ณ๐ฑ",
"tur": "๐น๐ท"
}
return language_flags.get(lang_code, "๐")
# Function to call OpenAI API using LangChain's ChatOpenAI
def call_openai_api(session_state):
try:
# Get API key and model from Streamlit secrets
api_key = st.secrets.get("OPENAI_API_KEY", "")
model_name = st.secrets.get("MODEL_NAME", "gpt-4.1-mini-2025-04-14")
max_tokens = st.secrets.get("MAX_TOKENS", 8000)
if not api_key:
return "Error: OpenAI API key not configured. Please set up your API key in the .streamlit/secrets.toml file."
# Initialize the LangChain OpenAI client
chat = ChatOpenAI(
openai_api_key=api_key,
model=model_name,
max_tokens=max_tokens,
streaming=True
)
# Get current level and code
level = session_state.selected_level
level_code = level.split()[0] # Extract just the level code (A1, A2, etc.)
# Get current language
lang_code = session_state.selected_language if hasattr(session_state, 'selected_language') else "fin"
lang_name = get_language_display_name(lang_code)
lang_flag = get_language_flag(lang_code)
# Get level-appropriate content guidelines
level_content = get_level_appropriate_content(level_code, lang_code)
# Get CEFR level guidelines
cefr_guidelines = get_cefr_level_guidelines(level_code, lang_code)
# Create a detailed level-specific and language-specific prompt
specific_prompt = SYSTEM_PROMPT + f"""
## CURRENT LEARNER LANGUAGE: {lang_flag} {lang_name}
## CURRENT LEARNER LEVEL: {level}
{cefr_guidelines}
VOCABULARY GUIDELINES FOR {lang_name} {level_code}:
{', '.join(level_content.get('vocabulary', ['No specific vocabulary guidelines available']))}
GRAMMAR GUIDELINES FOR {lang_name} {level_code}:
{', '.join(level_content.get('grammar', ['No specific grammar guidelines available']))}
EXAMPLE SENTENCES FOR {lang_name} {level_code}:
{' '.join(level_content.get('example_sentences', ['No example sentences available']))}
YOU MUST STRICTLY ADHERE TO THESE GUIDELINES FOR {lang_name} {level_code} LEVEL:
1. ONLY use vocabulary appropriate for {level_code} level
2. ONLY use grammar structures appropriate for {level_code} level
3. Keep explanations appropriate for {level_code} level complexity
4. Format your responses clearly with the level indicator
Remember: Always visually include the {level_code} level indicator in your responses using a badge or highlight.
"""
# Add personalization based on user topics if available
if session_state.user_topics and len(session_state.user_topics) > 0:
topics_str = ", ".join(session_state.user_topics[-10:]) # Use last 10 topics for relevance
specific_prompt += f"\n\nThe learner has shown interest in these topics: {topics_str}. Try to incorporate these topics into examples and exercises when appropriate to personalize the learning experience. Remember to ONLY use vocabulary and grammar structures appropriate for {level_code} level when incorporating these topics."
# Add level history information if available
if hasattr(session_state, 'level_history') and session_state.level_history:
# If user has changed levels, provide context
specific_prompt += "\n\nLevel progression history:"
for change in session_state.level_history[-3:]: # Last 3 changes
change_lang = change.get('language', lang_code)
change_lang_name = get_language_display_name(change_lang)
specific_prompt += f"\n- Changed from {change['from']} to {change['to']} on {change['timestamp']} for {change_lang_name}"
# If user recently moved up, note potential need for review
if session_state.level_history and len(session_state.level_history) > 0:
last_change = session_state.level_history[-1]
prev_level_code = last_change['from'].split()[0]
curr_level_code = last_change['to'].split()[0]
# Check if this is a move up the CEFR scale
cefr_progression = {"A1": 1, "A2": 2, "B1": 3, "B2": 4, "C1": 5}
if cefr_progression.get(prev_level_code, 0) < cefr_progression.get(curr_level_code, 0):
specific_prompt += f"""
\n\nIMPORTANT: The learner recently progressed from {prev_level_code} to {curr_level_code}.
This means:
1. Occasionally include review material from {prev_level_code} level
2. Focus primarily on {curr_level_code} level content
3. Build bridges between what they already know and new concepts
4. Give extra encouragement when they master new {curr_level_code} level structures
"""
# Check if level was recently changed
if hasattr(session_state, 'current_level_changed') and session_state.current_level_changed:
specific_prompt += f"""
\n\nALERT: The learner JUST changed their level to {level_code}. In your next response:
1. Acknowledge this level change explicitly
2. Briefly explain what {level_code} level means for {lang_name} learning
3. Give a short example of appropriate content for this level
4. Be encouraging about their language learning journey
"""
# Check if language was recently changed
if hasattr(session_state, 'language_changed') and session_state.language_changed:
specific_prompt += f"""
\n\nALERT: The learner JUST changed their language to {lang_name}. In your next response:
1. Acknowledge this language change explicitly
2. Include a brief, appropriate greeting in {lang_name}
3. Briefly explain how you'll adapt to teaching {lang_name} at their {level_code} level