mirror of
https://github.com/Aider-AI/aider.git
synced 2025-06-06 12:45:00 +00:00
refactor: Add helper function to replace reasoning tags in base_coder.py
This commit is contained in:
parent
01454674c8
commit
f3b9831a0c
1 changed files with 29 additions and 6 deletions
|
@ -1692,6 +1692,28 @@ class Coder:
|
||||||
if args:
|
if args:
|
||||||
self.io.ai_output(json.dumps(args, indent=4))
|
self.io.ai_output(json.dumps(args, indent=4))
|
||||||
|
|
||||||
|
def replace_reasoning_tags(self, text, tag_name):
|
||||||
|
"""
|
||||||
|
Replace opening and closing reasoning tags with standard formatting.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
text (str): The text containing the tags
|
||||||
|
tag_name (str): The name of the tag to replace
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: Text with reasoning tags replaced with standard format
|
||||||
|
"""
|
||||||
|
if not text:
|
||||||
|
return text
|
||||||
|
|
||||||
|
# Replace opening tag
|
||||||
|
text = re.sub(f"<{tag_name}>", REASONING_START, text)
|
||||||
|
|
||||||
|
# Replace closing tag
|
||||||
|
text = re.sub(f"</{tag_name}>", REASONING_END, text)
|
||||||
|
|
||||||
|
return text
|
||||||
|
|
||||||
def show_send_output(self, completion):
|
def show_send_output(self, completion):
|
||||||
if self.verbose:
|
if self.verbose:
|
||||||
print(completion)
|
print(completion)
|
||||||
|
@ -1735,12 +1757,13 @@ class Coder:
|
||||||
show_resp = self.render_incremental_response(True)
|
show_resp = self.render_incremental_response(True)
|
||||||
|
|
||||||
if reasoning_content:
|
if reasoning_content:
|
||||||
show_resp = (
|
formatted_reasoning = self.replace_reasoning_tags(
|
||||||
f"<{REASONING_TAG}>\n\n"
|
f"<{REASONING_TAG}>\n\n"
|
||||||
+ reasoning_content
|
+ reasoning_content
|
||||||
+ f"\n\n</{REASONING_TAG}>\n\n"
|
+ f"\n\n</{REASONING_TAG}>",
|
||||||
+ show_resp
|
REASONING_TAG
|
||||||
)
|
) + "\n\n"
|
||||||
|
show_resp = formatted_reasoning + show_resp
|
||||||
|
|
||||||
self.io.assistant_output(show_resp, pretty=self.show_pretty())
|
self.io.assistant_output(show_resp, pretty=self.show_pretty())
|
||||||
|
|
||||||
|
@ -1780,7 +1803,7 @@ class Coder:
|
||||||
reasoning_content = chunk.choices[0].delta.reasoning_content
|
reasoning_content = chunk.choices[0].delta.reasoning_content
|
||||||
if reasoning_content:
|
if reasoning_content:
|
||||||
if not self.got_reasoning_content:
|
if not self.got_reasoning_content:
|
||||||
text += f"<{REASONING_TAG}>\n\n"
|
text += REASONING_START
|
||||||
text += reasoning_content
|
text += reasoning_content
|
||||||
self.got_reasoning_content = True
|
self.got_reasoning_content = True
|
||||||
received_content = True
|
received_content = True
|
||||||
|
@ -1791,7 +1814,7 @@ class Coder:
|
||||||
content = chunk.choices[0].delta.content
|
content = chunk.choices[0].delta.content
|
||||||
if content:
|
if content:
|
||||||
if self.got_reasoning_content and not self.ended_reasoning_content:
|
if self.got_reasoning_content and not self.ended_reasoning_content:
|
||||||
text += f"\n\n</{REASONING_TAG}>\n\n"
|
text += REASONING_END
|
||||||
self.ended_reasoning_content = True
|
self.ended_reasoning_content = True
|
||||||
|
|
||||||
text += content
|
text += content
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue