mirror of
https://github.com/Aider-AI/aider.git
synced 2025-05-31 01:35:00 +00:00
aider: Refactored format_messages
to use a new function format_content
for line-by-line formatting.
# Aider chat conversation: USER: Now refactor format_messages: extract a function that takes a role and content (where content is a string), and returns the line by line formatted string. ASSISTANT: To refactor `format_messages`, we will: 1. Create a new function `format_content` that takes a role and content (where content is a string) and returns the line-by-line formatted string. 2. Update `format_messages` to use `format_content`. Here are the *SEARCH/REPLACE* blocks: aider/utils.py ```python <<<<<<< SEARCH def format_messages(messages, title=None): ======= def format_content(role, content): formatted_lines = [] for line in content.splitlines(): formatted_lines.append(f"{role} {line}") return "\n".join(formatted_lines) >>>>>>> REPLACE ``` aider/utils.py ```python <<<<<<< SEARCH elif isinstance(content, str): # Handle string content for line in content.splitlines(): output.append(f"{role} {line}") ======= elif isinstance(content, str): # Handle string content output.append(format_content(role, content)) >>>>>>> REPLACE ``` USER: # Fix any errors below, if possible. ## Running: flake8 --select=E9,F821,F823,F831,F406,F407,F701,F702,F704,F706 --show-source --isolated aider/utils.py aider/utils.py:94:8: F821 undefined name 'title' if title: ^ aider/utils.py:95:26: F821 undefined name 'title' output.append(f"{title.upper()} {'*' * 50}") ^ aider/utils.py:97:16: F821 undefined name 'messages' for msg in messages: ^ aider/utils.py:115:24: F821 undefined name 'format_messages' formatted_output = format_messages(messages, title) ^ ## See relevant lines below marked with █. aider/utils.py: ...⋮... 87│def format_content(role, content): 88│ formatted_lines = [] ...⋮... 91│ return "\n".join(formatted_lines) 92│ 93│ output = [] 94█ if title: 95█ output.append(f"{title.upper()} {'*' * 50}") 96│ 97█ for msg in messages: 98│ output.append("") 99│ role = msg["role"].upper() 100│ content = msg.get("content") ...⋮... 112│ 113│ 114│def show_messages(messages, title=None, functions=None): 115█ formatted_output = format_messages(messages, title) 116│ print(formatted_output) 117│ 118│ if functions: ...⋮... ASSISTANT: To fix the errors, we need to ensure that the `format_messages` function is properly defined and that the misplaced code is correctly organized. Here are the corrected *SEARCH/REPLACE* blocks: aider/utils.py ```python <<<<<<< SEARCH return "\n".join(formatted_lines) output = [] if title: output.append(f"{title.upper()} {'*' * 50}") for msg in messages: output.append("") role = msg["role"].upper() content = msg.get("content") if isinstance(content, list): # Handle list content (e.g., image messages) for item in content: if isinstance(item, dict) and "image_url" in item: output.append(f"{role} Image URL: {item['image_url']['url']}") elif isinstance(content, str): # Handle string content output.append(format_content(role, content)) content = msg.get("function_call") if content: output.append(f"{role} {content}") return "\n".join(output) ======= return "\n".join(formatted_lines) def format_messages(messages, title=None): output = [] if title: output.append(f"{title.upper()} {'*' * 50}") for msg in messages: output.append("") role = msg["role"].upper() content = msg.get("content") if isinstance(content, list): # Handle list content (e.g., image messages) for item in content: if isinstance(item, dict) and "image_url" in item: output.append(f"{role} Image URL: {item['image_url']['url']}") elif isinstance(content, str): # Handle string content output.append(format_content(role, content)) content = msg.get("function_call") if content: output.append(f"{role} {content}") return "\n".join(output) >>>>>>> REPLACE ```
This commit is contained in:
parent
a0b279f105
commit
abf8d5e60d
1 changed files with 2 additions and 0 deletions
|
@ -90,6 +90,8 @@ def format_content(role, content):
|
|||
formatted_lines.append(f"{role} {line}")
|
||||
return "\n".join(formatted_lines)
|
||||
|
||||
|
||||
def format_messages(messages, title=None):
|
||||
output = []
|
||||
if title:
|
||||
output.append(f"{title.upper()} {'*' * 50}")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue