| | |
| |
|
| | from typing import List, Tuple, Dict, Optional |
| | import re |
| |
|
| | History = List[Tuple[str, str]] |
| | Messages = List[Dict[str, str]] |
| |
|
| | def history_to_messages(history: History, system: str) -> Messages: |
| | messages = [{'role': 'system', 'content': system}] |
| | for user_msg, assistant_msg in history: |
| | messages.extend([ |
| | {'role': 'user', 'content': user_msg}, |
| | {'role': 'assistant', 'content': assistant_msg} |
| | ]) |
| | return messages |
| |
|
| | def history_to_chatbot_messages(history: History) -> List[Dict[str, str]]: |
| | return [{'role': role, 'content': content} |
| | for user_msg, assistant_msg in history |
| | for role, content in [('user', user_msg), ('assistant', assistant_msg)]] |
| |
|
| | def remove_code_block(text: str) -> str: |
| | match = re.search(r'```(?:\w+)?\n(.+?)```', text, re.DOTALL) |
| | return match.group(1).strip() if match else text |
| |
|
| | def parse_transformers_js_output(text: str) -> Dict[str, str]: |
| | files = {'index.html': '', 'index.js': '', 'style.css': ''} |
| | for ext in files.keys(): |
| | pattern = rf'```{ext.split(".")[1]}\s*\n(.+?)\n```' |
| | match = re.search(pattern, text, re.DOTALL | re.IGNORECASE) |
| | if match: |
| | files[ext] = match.group(1).strip() |
| | return files |
| |
|
| | def format_transformers_js_output(files: Dict[str, str]) -> str: |
| | return '\n\n'.join(f'=== {name} ===\n{content}' for name, content in files.items()) |
| |
|
| | def apply_search_replace_changes(original: str, changes: str) -> str: |
| | search_pattern = r'<<<<<< SEARCH\n(.+?)\n=======\n(.+?)\n>>>>>>> REPLACE' |
| | for search, replace in re.findall(search_pattern, changes, re.DOTALL): |
| | original = original.replace(search.strip(), replace.strip()) |
| | return original |
| |
|
| | def extract_text_from_file(filepath: str) -> str: |
| | with open(filepath, 'r', encoding='utf-8') as f: |
| | return f.read() |
| |
|
| | def extract_website_content(url: str) -> str: |
| | |
| | return "Website content from URL: " + url |
| |
|