Parse the generated content into a dictionary of file paths and contents. Expects the format: file content here
(content: str)
| 5 | from groq import Groq |
| 6 | |
| 7 | def parse_generated_content(content: str) -> Dict[str, str]: |
| 8 | """ |
| 9 | Parse the generated content into a dictionary of file paths and contents. |
| 10 | Expects the format: |
| 11 | <file_path> |
| 12 | <begin_content> |
| 13 | file content here |
| 14 | <end_content> |
| 15 | """ |
| 16 | files = {} |
| 17 | current_file = None |
| 18 | current_content = [] |
| 19 | state = 'expect_file' |
| 20 | |
| 21 | lines = content.split('\n') |
| 22 | for line_num, line in enumerate(lines, 1): |
| 23 | stripped = line.strip() |
| 24 | if state == 'expect_file': |
| 25 | if stripped.startswith('<') and stripped.endswith('>'): |
| 26 | current_file = stripped[1:-1].strip() |
| 27 | if not current_file: |
| 28 | print(f"Warning: Empty file path at line {line_num}") |
| 29 | current_file = None |
| 30 | continue |
| 31 | state = 'expect_begin_content' |
| 32 | elif stripped == '': |
| 33 | continue # Skip empty lines |
| 34 | else: |
| 35 | print(f"Warning: Expected <file_path> at line {line_num}, got: {stripped}") |
| 36 | elif state == 'expect_begin_content': |
| 37 | if stripped == '<begin_content>': |
| 38 | current_content = [] |
| 39 | state = 'collect_content' |
| 40 | else: |
| 41 | print(f"Warning: Expected <begin_content> after <{current_file}> at line {line_num}, got: {stripped}") |
| 42 | current_file = None |
| 43 | state = 'expect_file' |
| 44 | elif state == 'collect_content': |
| 45 | if stripped == '<end_content>': |
| 46 | if current_file in files: |
| 47 | print(f"Warning: Duplicate file path '{current_file}' at line {line_num}. Overwriting previous content.") |
| 48 | files[current_file] = '\n'.join(current_content).strip() |
| 49 | current_file = None |
| 50 | current_content = [] |
| 51 | state = 'expect_file' |
| 52 | else: |
| 53 | current_content.append(line) |
| 54 | # Check if any file was not properly closed |
| 55 | if state != 'expect_file': |
| 56 | print(f"Warning: Incomplete file definition for {current_file}") |
| 57 | return files |
| 58 | |
| 59 | def generate_content_with_groq(client: Groq, model_name: str, prompt: str) -> str: |
| 60 | """Generate content using the specified model via Groq.""" |
no outgoing calls
no test coverage detected