-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Allow markdown or custom formatting for code blocks #1493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e4f4763
Allow markdown formatting for code blocks
aymeric-roucher bd424fa
Fix planning prompts
aymeric-roucher 6ac67a8
Fix failing test
aymeric-roucher e3d81bd
Update src/smolagents/agents.py
aymeric-roucher 9f753c5
Nit: revert naming
aymeric-roucher File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -154,8 +154,8 @@ def parse_json_blob(json_blob: str) -> tuple[dict[str, str], str]: | |
try: | ||
first_accolade_index = json_blob.find("{") | ||
last_accolade_index = [a.start() for a in list(re.finditer("}", json_blob))][-1] | ||
json_data = json_blob[first_accolade_index : last_accolade_index + 1] | ||
json_data = json.loads(json_data, strict=False) | ||
json_str = json_blob[first_accolade_index : last_accolade_index + 1] | ||
json_data = json.loads(json_str, strict=False) | ||
return json_data, json_blob[:first_accolade_index] | ||
except IndexError: | ||
raise ValueError("The model output does not contain any JSON blob.") | ||
|
@@ -172,16 +172,16 @@ def parse_json_blob(json_blob: str) -> tuple[dict[str, str], str]: | |
) | ||
|
||
|
||
def extract_code_from_text(text: str) -> str | None: | ||
def extract_code_from_text(text: str, code_block_tags: tuple[str, str]) -> str | None: | ||
"""Extract code from the LLM's output.""" | ||
pattern = r"<code>(.*?)</code>" | ||
matches = re.findall(pattern, text, re.DOTALL) | ||
initial_pattern = rf"{code_block_tags[0]}(.*?){code_block_tags[1]}" | ||
matches = re.findall(initial_pattern, text, re.DOTALL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why you renamed it to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a residue of implementation change, reverting this |
||
if matches: | ||
return "\n\n".join(match.strip() for match in matches) | ||
return None | ||
|
||
|
||
def parse_code_blobs(text: str) -> str: | ||
def parse_code_blobs(text: str, code_block_tags: tuple[str, str]) -> str: | ||
"""Extract code blocs from the LLM's output. | ||
|
||
If a valid code block is passed, it returns it directly. | ||
|
@@ -195,7 +195,9 @@ def parse_code_blobs(text: str) -> str: | |
Raises: | ||
ValueError: If no valid code block is found in the text. | ||
""" | ||
matches = extract_code_from_text(text) | ||
matches = extract_code_from_text(text, code_block_tags) | ||
if not matches: # Fallback to markdown pattern | ||
matches = extract_code_from_text(text, ("```(?:python|py)", "\n```")) | ||
if matches: | ||
return matches | ||
# Maybe the LLM outputted a code blob directly | ||
|
@@ -209,27 +211,27 @@ def parse_code_blobs(text: str) -> str: | |
raise ValueError( | ||
dedent( | ||
f""" | ||
Your code snippet is invalid, because the regex pattern <code>(.*?)</code> was not found in it. | ||
Your code snippet is invalid, because the regex pattern {code_block_tags[0]}(.*?){code_block_tags[1]} was not found in it. | ||
Here is your code snippet: | ||
{text} | ||
It seems like you're trying to return the final answer, you can do it as follows: | ||
<code> | ||
{code_block_tags[0]} | ||
final_answer("YOUR FINAL ANSWER HERE") | ||
</code> | ||
{code_block_tags[1]} | ||
""" | ||
).strip() | ||
) | ||
raise ValueError( | ||
dedent( | ||
f""" | ||
Your code snippet is invalid, because the regex pattern <code>(.*?)</code> was not found in it. | ||
Your code snippet is invalid, because the regex pattern {code_block_tags[0]}(.*?){code_block_tags[1]} was not found in it. | ||
Here is your code snippet: | ||
{text} | ||
Make sure to include code with the correct pattern, for instance: | ||
Thoughts: Your thoughts | ||
<code> | ||
{code_block_tags[0]} | ||
# Your python code here | ||
</code> | ||
{code_block_tags[1]} | ||
""" | ||
).strip() | ||
) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.