Skip to main content

headless_lms_chatbot/
cms_ai_suggestion.rs

1use std::collections::HashMap;
2
3use crate::{
4    azure_chatbot::{
5        ArrayItem, ArrayProperty, InputItem, JSONType, LLMRequest, LLMRequestParams,
6        LLMRequestResponseFormatParam, NonThinkingParams, RequestTextOptions, Schema,
7        ThinkingParams,
8    },
9    chatbot_error::chatbot_err,
10    content_cleaner::calculate_safe_token_limit,
11    llm_utils::{
12        APIInputMessage, MessageContent, estimate_tokens, make_blocking_llm_request,
13        model_is_thinking, parse_text_completion,
14    },
15    prelude::{ChatbotError, ChatbotErrorType, ChatbotResult},
16};
17use headless_lms_base::config::ApplicationConfiguration;
18use headless_lms_base::error::backend_error::BackendError;
19use headless_lms_models::{
20    application_task_default_language_models::TaskLMSpec,
21    chatbot_conversation_message_messages::MessageRole, cms_ai::ParagraphSuggestionAction,
22};
23
24/// Structured LLM response for CMS paragraph suggestions.
25#[derive(serde::Deserialize)]
26struct CmsParagraphSuggestionResponse {
27    suggestions: Vec<String>,
28}
29
30/// Returns a short human-readable instruction for the given action id. Used so the model
31/// sees a precise operation description instead of an opaque id, and so we can stress
32/// "only this, nothing else" per action.
33fn action_instruction(action: ParagraphSuggestionAction) -> String {
34    let s = match action {
35        ParagraphSuggestionAction::GenerateDraftFromNotes => {
36            "Write a coherent draft paragraph using only the ideas present in the notes. Fill in connective wording as needed, but do not invent facts, examples, or claims not supported by the notes."
37        }
38        ParagraphSuggestionAction::GenerateContinueParagraph => {
39            "Append 1-3 new sentences that continue the paragraph's current idea and tone. Keep the existing paragraph text verbatim, and do not rewrite, reorder, or delete any existing words."
40        }
41        ParagraphSuggestionAction::GenerateAddExample => {
42            "Append one concise example that directly supports the paragraph's main point. Keep the existing text unchanged, and do not introduce unrelated facts or a new topic."
43        }
44        ParagraphSuggestionAction::GenerateAddCounterpoint => {
45            "Append one concise, relevant counterpoint or limitation that stays in scope with the paragraph's topic. Keep the existing text unchanged, and do not drift into a different topic."
46        }
47        ParagraphSuggestionAction::GenerateAddConcludingSentence => {
48            "Append exactly one concluding sentence that reinforces the paragraph's main point. Do not introduce new claims, and keep the existing text unchanged."
49        }
50        ParagraphSuggestionAction::FixSpelling => {
51            "Correct spelling, grammar, punctuation, and obvious typos only. Do not change meaning, tone, structure, or phrasing beyond what is required for correctness."
52        }
53        ParagraphSuggestionAction::ImproveClarity => {
54            "Improve clarity and readability with minimal rewrites. Preserve meaning and tone, and do not add new ideas, remove important details, or change the overall message."
55        }
56        ParagraphSuggestionAction::ImproveFlow => {
57            "Improve flow by refining transitions and, if needed, lightly reordering sentences for smoother reading. Preserve all original claims and details, and do not add new content or change the tone."
58        }
59        ParagraphSuggestionAction::ImproveConcise => {
60            "Make the paragraph more concise by removing redundancy and tightening phrasing. Preserve meaning and tone, and do not remove important details or add new ideas."
61        }
62        ParagraphSuggestionAction::ImproveExpandDetail => {
63            "Expand the paragraph with specific, on-topic supporting detail that deepens existing ideas. Preserve the original claims and tone, and do not add unrelated content or change the topic."
64        }
65        ParagraphSuggestionAction::ImproveAcademicStyle => {
66            "Strengthen the academic style by making the wording more precise, formal, and discipline-appropriate. Preserve meaning and evidence level, and do not add or remove content beyond stylistic refinement."
67        }
68        ParagraphSuggestionAction::StructureCreateTopicSentence => {
69            "Add or refine a topic sentence that clearly introduces the paragraph's main idea. Do not rewrite unrelated parts of the paragraph beyond minimal adjustments needed to fit the topic sentence."
70        }
71        ParagraphSuggestionAction::StructureReorderSentences => {
72            "Reorder the existing sentences to improve logical progression. Do not add, remove, split, combine, or materially rewrite sentence content."
73        }
74        ParagraphSuggestionAction::StructureSplitIntoParagraphs => {
75            "Split the content into multiple paragraphs at appropriate break points. Preserve wording and order, and do not add, remove, or rewrite content beyond paragraph breaks."
76        }
77        ParagraphSuggestionAction::StructureCombineIntoOne => {
78            "Combine the content into a single well-formed paragraph. Preserve wording and order, and do not add, remove, or rewrite content beyond joining the paragraphs."
79        }
80        ParagraphSuggestionAction::StructureToBullets => {
81            "Convert the content into bullet points while preserving the original information and order. Do not add, remove, or materially rewrite content beyond the format change."
82        }
83        ParagraphSuggestionAction::StructureFromBullets => {
84            "Convert the bullet points into prose while preserving the original information and order. Do not add, remove, or materially rewrite content beyond the format change."
85        }
86        ParagraphSuggestionAction::LearningSimplifyBeginners => {
87            "Simplify the explanation for beginners using clearer language and less assumed prior knowledge. Preserve factual accuracy, and do not add new concepts, tangents, or unrelated examples."
88        }
89        ParagraphSuggestionAction::LearningAddDefinitions => {
90            "Add brief inline definitions for technical or unfamiliar terms at their first relevant mention. Keep the existing explanation intact, and do not rewrite unrelated parts of the paragraph."
91        }
92        ParagraphSuggestionAction::LearningAddAnalogy => {
93            "Append one short, relevant analogy that clarifies the core concept. Keep the existing text unchanged, and do not introduce unrelated ideas or a separate topic."
94        }
95        ParagraphSuggestionAction::LearningAddPracticeQuestion => {
96            "Append exactly one practice question that tests the paragraph's key idea. Do not include an answer, and keep the existing text unchanged."
97        }
98        ParagraphSuggestionAction::LearningAddCheckUnderstanding => {
99            "Append exactly one quick check-for-understanding question followed by a one-sentence model answer. Keep the existing text unchanged, and do not add extra explanation beyond that."
100        }
101        ParagraphSuggestionAction::SummariesOneSentence => {
102            "Summarize the paragraph in exactly one sentence. Preserve the central meaning, and do not add interpretation, advice, or extra detail."
103        }
104        ParagraphSuggestionAction::SummariesTwoThreeSentences => {
105            "Summarize the paragraph in exactly two or three sentences. Preserve the central meaning, and do not add interpretation, advice, or extra detail."
106        }
107        ParagraphSuggestionAction::SummariesKeyTakeaway => {
108            "State exactly one sentence describing the single most important takeaway for students. Do not add extra interpretation, advice, or supporting detail."
109        }
110        ParagraphSuggestionAction::ToneAcademicFormal
111        | ParagraphSuggestionAction::ToneFriendlyConversational
112        | ParagraphSuggestionAction::ToneEncouragingSupportive
113        | ParagraphSuggestionAction::ToneNeutralObjective
114        | ParagraphSuggestionAction::ToneConfident
115        | ParagraphSuggestionAction::ToneSerious => {
116            "Adjust wording only as needed to match the target tone. Preserve all facts, claims, detail level, and overall structure, and do not add, remove, or materially reframe content."
117        }
118        ParagraphSuggestionAction::TranslateEnglish
119        | ParagraphSuggestionAction::TranslateFinnish
120        | ParagraphSuggestionAction::TranslateNorwegian
121        | ParagraphSuggestionAction::TranslateSwedish => {
122            "Translate the paragraph into the target language while preserving meaning, domain terminology, and inline formatting. Do not add, omit, simplify, paraphrase, or reinterpret the content."
123        }
124    };
125    s.to_string()
126}
127
128/// System prompt for generating multiple alternative paragraph suggestions for CMS content.
129const SYSTEM_PROMPT: &str = r#"You are helping course staff improve a single paragraph of course material.
130
131Your task is to generate several alternative versions of the given paragraph based on the requested action.
132
133Critical: Perform only the requested action. Do not make any additional edits beyond what is strictly necessary to complete that action. Preserve wording, structure, tone, detail level, and sentence order unless the requested action explicitly requires changing them. For example: if the action is spelling/grammar, only fix spelling and grammar; if it is translation, only translate; if it is tone, only change tone; if it is clarity, only improve clarity. Do not also summarize, expand, simplify, reorder, or otherwise rewrite unless the requested action requires it.
134
135General rules:
136- Always preserve the original meaning and important details unless the action explicitly asks to add or remove content.
137- Maintain a clear, pedagogical tone appropriate for course materials.
138- Do not invent facts that contradict the original paragraph.
139
140About the suggestions:
141- Produce multiple alternative rewrites of the same paragraph.
142- Do not output duplicate or near-duplicate suggestions.
143- Keep each suggestion self-contained and suitable for direct insertion into the material.
144
145You will receive:
146- The original paragraph text.
147- The requested action (a precise instruction; follow it and do nothing else).
148- Optional metadata such as target tone and target language.
149
150Your output must follow the JSON schema exactly:
151{
152  "suggestions": ["...", "...", "..."]
153}"#;
154
155/// User prompt prefix; the concrete action instruction and paragraph will be appended.
156pub const USER_PROMPT_PREFIX: &str = "Apply only the requested action to the paragraph below. Do not make any other changes. The paragraph may contain inline HTML markup valid inside a Gutenberg paragraph; preserve existing inline tags (links, emphasis, code, sub/superscripts) where possible, do not introduce block-level elements, and do not add new formatting to spans of text that were previously unformatted. Return JSON only.";
157
158/// Input payload for CMS paragraph suggestions.
159pub struct CmsParagraphSuggestionInput {
160    pub action: ParagraphSuggestionAction,
161    pub content: String,
162    pub is_html: bool,
163    pub meta_tone: Option<String>,
164    pub meta_language: Option<String>,
165    pub meta_setting_type: Option<String>,
166}
167
168/// Generate multiple paragraph suggestions for CMS using an LLM with structured JSON output.
169pub async fn generate_paragraph_suggestions(
170    app_config: &ApplicationConfiguration,
171    task_lm: TaskLMSpec,
172    input: &CmsParagraphSuggestionInput,
173) -> ChatbotResult<Vec<String>> {
174    let CmsParagraphSuggestionInput {
175        action,
176        content,
177        is_html: _,
178        meta_tone,
179        meta_language,
180        meta_setting_type,
181    } = input;
182
183    let action_instruction = action_instruction(*action);
184
185    let mut system_instructions = SYSTEM_PROMPT.to_owned();
186    system_instructions.push_str("\n\nRequested action: ");
187    system_instructions.push_str(&action_instruction);
188    if let Some(tone) = meta_tone {
189        system_instructions.push_str("\nTarget tone: ");
190        system_instructions.push_str(tone);
191    }
192    if let Some(lang) = meta_language {
193        system_instructions.push_str("\nTarget language: ");
194        system_instructions.push_str(lang);
195    }
196    if let Some(setting_type) = meta_setting_type {
197        system_instructions.push_str("\nSetting type: ");
198        system_instructions.push_str(setting_type);
199    }
200
201    let paragraph_source = content.as_str();
202
203    let user_message_content = format!(
204        "{prefix}\n\nRequested action: {action_instruction}\n\nOriginal paragraph (may include inline HTML):\n{paragraph}",
205        prefix = USER_PROMPT_PREFIX,
206        action_instruction = action_instruction,
207        paragraph = paragraph_source
208    );
209
210    let used_tokens =
211        estimate_tokens(&system_instructions) + estimate_tokens(&user_message_content);
212    let token_budget =
213        calculate_safe_token_limit(task_lm.context_size, task_lm.context_utilization);
214
215    if used_tokens > token_budget {
216        return Err(chatbot_err!(
217            ChatbotMessageSuggestError,
218            "Input paragraph is too long for the CMS AI suggestion context window.".to_string()
219        ));
220    }
221
222    let system_message = APIInputMessage {
223        message_type: InputItem::Message {
224            role: MessageRole::System,
225            content: MessageContent::Text(system_instructions),
226        },
227    };
228
229    let user_message = APIInputMessage {
230        message_type: InputItem::Message {
231            role: MessageRole::User,
232            content: MessageContent::Text(user_message_content),
233        },
234    };
235
236    let (params, max_output_tokens) = if model_is_thinking(task_lm.model_type) {
237        (
238            LLMRequestParams::GPTThinking(ThinkingParams { reasoning: None }),
239            Some(4000),
240        )
241    } else {
242        (
243            LLMRequestParams::GPTNonThinking(NonThinkingParams {
244                temperature: None,
245                top_p: None,
246                frequency_penalty: None,
247                presence_penalty: None,
248            }),
249            Some(2000),
250        )
251    };
252
253    let chat_request = LLMRequest {
254        input: vec![system_message, user_message],
255        model: task_lm.model.to_owned(),
256        max_output_tokens,
257        tools: vec![],
258        tool_choice: None,
259        params,
260        text: Some(RequestTextOptions {
261            verbosity: None,
262            format: Some(LLMRequestResponseFormatParam {
263                format_type: JSONType::JsonSchema,
264                name: "CmsParagraphSuggestionResponse".to_string(),
265                schema: Schema {
266                    type_field: JSONType::Object,
267                    properties: HashMap::from([(
268                        "suggestions".to_string(),
269                        ArrayProperty {
270                            type_field: JSONType::Array,
271                            items: ArrayItem {
272                                type_field: JSONType::String,
273                            },
274                        },
275                    )]),
276                    required: vec!["suggestions".to_string()],
277                    additional_properties: false,
278                },
279                strict: true,
280            }),
281        }),
282    };
283
284    let completion = make_blocking_llm_request(chat_request, app_config).await?;
285
286    let completion_content: &String = &parse_text_completion(completion)?;
287    let response: CmsParagraphSuggestionResponse = serde_json::from_str(completion_content)
288        .map_err(|_| {
289            chatbot_err!(
290                ChatbotMessageSuggestError,
291                "The CMS paragraph suggestion LLM returned an incorrectly formatted response."
292                    .to_string()
293            )
294        })?;
295
296    if response.suggestions.is_empty() {
297        return Err(chatbot_err!(
298            ChatbotMessageSuggestError,
299            "The CMS paragraph suggestion LLM returned an empty suggestions list.".to_string()
300        ));
301    }
302
303    Ok(response.suggestions)
304}