| """ |
| Text Explanation utilities using Mistral AI. |
| Splits text by markdown headings and generates contextual explanations for each section. |
| Maintains chat history to provide coherent explanations that build upon previous sections. |
| """ |
|
|
| import os |
| import re |
| from typing import List, Dict, Tuple, Optional |
| from mistralai import Mistral |
|
|
|
|
| class TextExplainer: |
| """Generate explanations for text sections using Mistral AI.""" |
| |
| def __init__(self): |
| """Initialize the text explainer with Mistral AI client.""" |
| self.api_key = os.environ.get("MISTRAL_API_KEY") |
| if not self.api_key: |
| raise ValueError("MISTRAL_API_KEY environment variable is required") |
| self.client = Mistral(api_key=self.api_key) |
| self.chat_history = [] |
|
|
| def get_topic(self, text: str) -> Optional[str]: |
| """ |
| Extract the main topic from the text using Mistral AI with structured output. |
| |
| Args: |
| text: Input text to analyze |
| |
| Returns: |
| Main topic as a string or None if not found |
| """ |
| try: |
| |
| topic_schema = { |
| "type": "json_schema", |
| "json_schema": { |
| "schema": { |
| "type": "object", |
| "properties": { |
| "main_topic": { |
| "type": "string", |
| "title": "Main Topic", |
| "description": "The primary / general topic or subject of the text" |
| }, |
| }, |
| "required": ["main_topic"], |
| "additionalProperties": False |
| }, |
| "name": "topic_extraction", |
| "strict": True |
| } |
| } |
| |
| response = self.client.chat.complete( |
| model="ministral-8b-2410", |
| messages=[ |
| { |
| "role": "system", |
| "content": "You are an expert in summarizing texts. Extract the main topic from the provided text." |
| }, |
| { |
| "role": "user", |
| "content": f"Analyze this text and extract the main topic:\n\n{text[:2000]}..." |
| } |
| ], |
| temperature=0.3, |
| max_tokens=200, |
| response_format=topic_schema |
| ) |
| |
| if hasattr(response, 'choices') and response.choices: |
| |
| import json |
| try: |
| topic_data = json.loads(response.choices[0].message.content) |
| main_topic = topic_data.get("main_topic", "").strip() |
| confidence = topic_data.get("confidence", 0.0) |
| secondary_topics = topic_data.get("secondary_topics", []) |
| |
| |
| print(f"π Topic extraction - Main: '{main_topic}', Confidence: {confidence:.2f}") |
| if secondary_topics: |
| print(f"π Secondary topics: {', '.join(secondary_topics)}") |
| |
| return main_topic if main_topic else None |
| except json.JSONDecodeError as json_err: |
| print(f"Error parsing JSON response: {json_err}") |
| |
| return response.choices[0].message.content.strip() |
| return None |
| except Exception as e: |
| print(f"Error extracting topic: {str(e)}") |
| return None |
|
|
| def split_text_by_headings(self, text: str) -> List[Dict[str, str]]: |
| """ |
| Split text into sections based on markdown headings. |
| |
| Args: |
| text: Input text with markdown headings |
| |
| Returns: |
| List of dictionaries with 'heading' and 'content' keys |
| """ |
| if not text: |
| return [] |
| |
| |
| sections = [] |
| |
| |
| |
| heading_pattern = r'^(#{1,6})\s+(.+?)$' |
| |
| lines = text.split('\n') |
| current_heading = None |
| current_content = [] |
| current_level = 0 |
| |
| for line in lines: |
| heading_match = re.match(heading_pattern, line.strip()) |
| |
| if heading_match: |
| |
| if current_heading and current_content: |
| content_text = '\n'.join(current_content).strip() |
| if content_text: |
| sections.append({ |
| 'heading': current_heading, |
| 'content': content_text, |
| 'level': current_level |
| }) |
| |
| |
| level = len(heading_match.group(1)) |
| current_heading = heading_match.group(2).strip() |
| current_level = level |
| current_content = [] |
| else: |
| |
| if current_heading is not None: |
| current_content.append(line) |
| |
| |
| if current_heading and current_content: |
| content_text = '\n'.join(current_content).strip() |
| if content_text: |
| sections.append({ |
| 'heading': current_heading, |
| 'content': content_text, |
| 'level': current_level |
| }) |
| |
| |
| if not sections and text.strip(): |
| sections.append({ |
| 'heading': 'Document Content', |
| 'content': text.strip(), |
| 'level': 1 |
| }) |
| return sections |
|
|
| def generate_explanation(self, topic: str, heading: str, content: str, section_number: int = 1, total_sections: int = 1) -> str: |
| """ |
| Generate an explanation for a text section using Mistral AI with chat history context. |
| |
| Args: |
| topic: General topic of the document |
| heading: Section heading |
| content: Section content |
| section_number: Current section number (for context) |
| total_sections: Total number of sections (for context) |
| |
| Returns: |
| Generated explanation in simple terms |
| """ |
| try: |
| |
| prompt = f""" |
| **Section {section_number} of {total_sections}** |
| **Section Heading:** {heading} |
| |
| **Section Content:** |
| {content} |
| |
| **Your Explanation:**""" |
|
|
| |
| if section_number == 1: |
| system_prompt = f"""You are an expert teacher who explains complex topics in simple, easy-to-understand terms. |
| |
| I will give you sections of text with their headings on the topic of "{topic}", and I want you to explain what each section is about in simple language, by breaking down any complex concepts or terminology. You should also explain why this information might be important or useful, use examples or analogies when helpful, and keep the explanation engaging and educational. |
| |
| Make your explanation clear enough for someone without prior knowledge of the topic to understand. As you explain each section, consider how it relates to the previous sections you've already explained to provide coherent, contextual explanations throughout the document. |
| |
| Do not mention anything far irrelevant from the topic of "{topic}". Do not repeat information unnecessarily, but build on previous explanations to create a comprehensive understanding of the topic. Avoid using the term 'section' and use the actual section heading instead. No need to mention the section number in your explanation. |
| """ |
| |
| |
| self.chat_history = [ |
| { |
| "role": "system", |
| "content": system_prompt |
| } |
| ] |
|
|
| |
| if len(content) < 200: |
| print(f"π Skipping API call for short content in '{heading}' ({len(content)} chars < 200)") |
| |
| self.chat_history.append({ |
| "role": "user", |
| "content": prompt |
| }) |
| |
| return f"This section contains minimal content ({len(content)} characters). The information has been noted for context in subsequent explanations." |
|
|
| |
| self.chat_history.append({ |
| "role": "user", |
| "content": prompt |
| }) |
|
|
| |
| response = self.client.chat.complete( |
| model="mistral-small-2503", |
| messages=self.chat_history, |
| temperature=0.7, |
| |
| ) |
| |
| |
| if hasattr(response, 'choices') and response.choices: |
| explanation = response.choices[0].message.content |
| |
| |
| self.chat_history.append({ |
| "role": "assistant", |
| "content": explanation |
| }) |
| |
| return explanation.strip() |
| else: |
| return f"Could not generate explanation for section: {heading}" |
| |
| except Exception as e: |
| print(f"Error generating explanation for '{heading}': {str(e)}") |
| return f"Error generating explanation for this section: {str(e)}" |
| |
| def explain_all_sections(self, text: str) -> List[Dict[str, str]]: |
| """ |
| Split text by headings and generate explanations for all sections with chat history context. |
| |
| Args: |
| text: Input text with markdown headings |
| |
| Returns: |
| List of dictionaries with 'heading', 'content', 'explanation', and 'level' keys |
| """ |
| sections = self.split_text_by_headings(text) |
| |
| if not sections: |
| return [] |
| |
| print(f"π Found {len(sections)} sections to explain...") |
| |
| |
| print("π― Extracting main topic...") |
| topic = self.get_topic(text) |
| if topic: |
| print(f"π Main topic identified: {topic}") |
| else: |
| topic = "General Content" |
| print("β οΈ Could not identify main topic, using fallback") |
| |
| |
| self.chat_history = [] |
| |
| explained_sections = [] |
| |
| for i, section in enumerate(sections, 1): |
| print(f"π Generating explanation for section {i}/{len(sections)}: {section['heading'][:50]}...") |
| |
| |
| explanation = self.generate_explanation( |
| topic, |
| section['heading'], |
| section['content'], |
| section_number=i, |
| total_sections=len(sections) |
| ) |
| |
| explained_sections.append({ |
| 'heading': section['heading'], |
| 'content': section['content'], |
| 'explanation': explanation, |
| 'level': section['level'] |
| }) |
| |
| print(f"β
Generated explanations for all {len(explained_sections)} sections") |
| return explained_sections |
| |
| def reset_chat_history(self): |
| """Reset the chat history for a new document or conversation.""" |
| self.chat_history = [] |
| |
| def get_chat_history(self) -> List[Dict[str, str]]: |
| """Get the current chat history for debugging purposes.""" |
| return self.chat_history.copy() |
| |
| def get_chat_history_summary(self) -> str: |
| """Get a summary of the current chat history.""" |
| if not self.chat_history: |
| return "No chat history available." |
| |
| summary = f"Chat history contains {len(self.chat_history)} messages:\n" |
| for i, message in enumerate(self.chat_history, 1): |
| role = message['role'] |
| content_preview = message['content'][:100] + "..." if len(message['content']) > 100 else message['content'] |
| summary += f"{i}. {role.upper()}: {content_preview}\n" |
| |
| return summary |
|
|
| def format_explanations_for_display(self, explained_sections: List[Dict[str, str]]) -> str: |
| """ |
| Concatenate only the explanations from all sections for display, filtering out placeholder explanations for minimal content. |
| Args: |
| explained_sections: List of sections with explanations |
| Returns: |
| Concatenated explanations as a single string |
| """ |
| if not explained_sections: |
| return "No sections found to explain." |
| skip_phrase = "This section contains minimal content" |
| return "\n\n".join( |
| section['explanation'] |
| for section in explained_sections |
| if section.get('explanation') and not section['explanation'].strip().startswith(skip_phrase) |
| ) |
|
|