Skip to content

Commit 86f3565

Browse files
Fixes savetofile instructions.
1 parent b38f9d9 commit 86f3565

4 files changed

Lines changed: 39 additions & 103 deletions

File tree

src/smolagents/agents.py

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2146,9 +2146,7 @@ def _step_stream(
21462146
"""
21472147
error_msg = """Error in code parsing. Follow examples with the desired structure in the tags <example></example>:
21482148
<example>
2149-
<savetofile filename="free-will.txt">
2150-
I am going to solve this task with confidence.
2151-
</savetofile>
2149+
<savetofile filename="free-will.txt">I am going to solve this task with confidence.</savetofile>
21522150
<runcode>
21532151
result = 5 + 3 + 1294.678
21542152
final_answer(result)
@@ -2157,13 +2155,9 @@ def _step_stream(
21572155
21582156
If you need to include any file in the file system, use the <includefile></includefile> tags. This is an example:
21592157
<example>
2160-
<savetofile filename="first_step.py">
2161-
print("first step")
2162-
</savetofile>
2158+
<savetofile filename="first_step.py">print("first step")</savetofile>
21632159
2164-
<savetofile filename="second_step.py">
2165-
print("second step")
2166-
</savetofile>
2160+
<savetofile filename="second_step.py">print("second step")</savetofile>
21672161
21682162
<runcode>
21692163
<includefile>first_step.py</includefile>
@@ -2179,21 +2173,15 @@ def _step_stream(
21792173
error_msg = error_msg + """
21802174
If you are trying to save or run a too big file, you can try to save and append in steps:
21812175
<example>
2182-
<savetofile filename="large_file.txt">
2183-
First section, function or chapter
2184-
</savetofile>
2176+
<savetofile filename="large_file.txt">First section, function or chapter</savetofile>
21852177
<runcode>
21862178
print('Starting well.')
21872179
</runcode>
2188-
<appendtofile filename="large_file.txt">
2189-
Second section, function or chapter
2190-
</savetofile>
2180+
<appendtofile filename="large_file.txt">Second section, function or chapter</appendtofile>
21912181
<runcode>
21922182
print('Continuing awesome!')
21932183
</runcode>
2194-
<appendtofile filename="large_file.txt">
2195-
Third section, function or chapter
2196-
</savetofile>
2184+
<appendtofile filename="large_file.txt">Third section, function or chapter</appendtofile>
21972185
<runcode>
21982186
print('Finishing fantastic!!!')
21992187
</runcode>

src/smolagents/bp_thinkers.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@
7676
<example>
7777
<thoughts>I will use python code to compute the result of the operation and then return the final answer using the `final_answer` tool.</thoughts>
7878
<freewill>I am going to solve this task with confidence.</freewill>
79-
<savetofile filename="free-will.txt">
80-
I am going to solve this task with confidence.
81-
</savetofile>
79+
<savetofile filename="free-will.txt">I am going to solve this task with confidence.</savetofile>
8280
<runcode>
8381
result = 5 + 3 + 1294.678
8482
final_answer(result)
@@ -87,32 +85,22 @@
8785
8886
For saving text files (text, csv, python code), just enclose your text into the <savetofile></savetofile> tags as per examples below:
8987
<example>
90-
<savetofile filename="example.txt">
91-
This is the content of example.txt
92-
</savetofile>
88+
<savetofile filename="example.txt">This is the content of example.txt</savetofile>
9389
94-
<savetofile filename="another_file.csv">
95-
header1,header2
90+
<savetofile filename="another_file.csv">header1,header2
9691
value1,value2
97-
value3,value4
98-
</savetofile>
92+
value3,value4</savetofile>
9993
100-
<savetofile filename="hello.py">
101-
print("hello")
102-
</savetofile>
94+
<savetofile filename="hello.py">print("hello")</savetofile>
10395
</example>
10496
10597
For saving source code files, use the tags <savetofile></savetofile> is the best method.
10698
10799
You may also append content to file with the tags <appendtofile></appendtofile>. This is an example:
108100
<example>
109-
<savetofile filename="another_csv.csv">
110-
header1,header2
111-
</savetofile>
112-
<appendtofile filename="another_csv.csv">
113-
value1,value2
114-
value3,value4
115-
</appendtofile>
101+
<savetofile filename="another_csv.csv">header1,header2</savetofile>
102+
<appendtofile filename="another_csv.csv">value1,value2
103+
value3,value4</appendtofile>
116104
117105
The above will create a csv file with the following content:
118106
header1,header2
@@ -124,13 +112,9 @@
124112
125113
If you need to include any file in the file system, use the <includefile></includefile> tags. This is an example:
126114
<example>
127-
<savetofile filename="first_step.py">
128-
print("first step")
129-
</savetofile>
115+
<savetofile filename="first_step.py">print("first step")</savetofile>
130116
131-
<savetofile filename="second_step.py">
132-
print("second step")
133-
</savetofile>
117+
<savetofile filename="second_step.py">print("second step")</savetofile>
134118
135119
<runcode>
136120
<includefile>first_step.py</includefile>
@@ -457,9 +441,7 @@ def test_and_refine(local_agent, solution_file):
457441
task_description="""Fantastic! Save the full updated solution that solves the task described in <task></task> into the file '"""+solution_file+"""'.
458442
YOU ARE REQUIRED TO SAVE THE FULL SOLUTION AND NOT JUST THE PORTIONS THAT YOU HAVE MODIFIED.
459443
You can follow this example:
460-
<savetofile filename="""+solution_file+""">
461-
print("your source code or text here")
462-
</savetofile>
444+
<savetofile filename="""+solution_file+""">print("your source code or text here")</savetofile>
463445
<runcode>
464446
final_answer("Task completed! YAY!")
465447
</runcode>
@@ -489,9 +471,7 @@ def test_and_refine(local_agent, solution_file):
489471
task_description="""Fantastic! Save the full merged solution into the file '"""+solution_file+"""'.
490472
YOU ARE REQUIRED TO SAVE THE FULL SOLUTION AND NOT JUST THE PORTIONS THAT YOU HAVE MODIFIED.
491473
You can follow this example:
492-
<savetofile filename="""+solution_file+""">
493-
print("your source code or text here")
494-
</savetofile>
474+
<savetofile filename="""+solution_file+""">print("your source code or text here")</savetofile>
495475
<runcode>
496476
final_answer("Task completed! YAY!")
497477
</runcode>

src/smolagents/bp_tools.py

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ def load_string_from_file(filename: str) -> str:
7777
Loads the content from the specified file.
7878
For saving and printing a file, just enclose your text into the <savetofile></savetofile>:
7979
<example>
80-
<savetofile filename="another_file.csv">
81-
header1,header2
80+
<savetofile filename="another_file.csv">header1,header2
8281
value1,value2
83-
value3,value4
84-
</savetofile>
82+
value3,value4</savetofile>
8583
<runcode>
8684
# print the content of another_file.csv
8785
print(load_string_from_file(filename="another_file.csv"))
@@ -155,15 +153,9 @@ def replace_in_file_from_files(filename: str, file_with_old_value: str, file_wit
155153
156154
In the case that you need to replace strings in an existing file, you can do it using the replace_in_file tool. This is an example:
157155
<example>
158-
<savetofile filename="tmp1.txt">
159-
hello world
160-
</savetofile>
161-
<savetofile filename="tmp2.txt">
162-
hello home!
163-
</savetofile>
164-
<savetofile filename="test.txt">
165-
Hey! hello world
166-
</savetofile>
156+
<savetofile filename="tmp1.txt">hello world</savetofile>
157+
<savetofile filename="tmp2.txt">hello home!</savetofile>
158+
<savetofile filename="test.txt">Hey! hello world</savetofile>
167159
<runcode>
168160
replace_in_file_from_files('test.txt', 'tmp1.txt', 'tmp2.txt')
169161
</runcode>
@@ -274,9 +266,7 @@ def run_os_command(str_command: str, timeout: int = 60, max_memory:int = 2748779
274266
275267
You can use run_os_command to run php code. This is an example:
276268
<example>
277-
<savetofile filename="hello.php">
278-
<?php echo "hello"; ?>
279-
</savetofile>
269+
<savetofile filename="hello.php"><?php echo "hello"; ?></savetofile>
280270
<runcode>
281271
# 60 seconds of timeout and 512MB of max memory
282272
print(run_os_command("php hello.php", timeout=60, max_memory=512*1024*1024))
@@ -589,9 +579,7 @@ def run_php_file(filename: str, timeout: int = 60) -> str:
589579
Runs a PHP file and returns the output.
590580
To run PHP code, follow tis an example:
591581
<example>
592-
<savetofile filename="hello.php">
593-
<?php echo "hello"; ?>
594-
</savetofile>
582+
<savetofile filename="hello.php"><?php echo "hello"; ?></savetofile>
595583
<runcode>
596584
print(run_php_file("hello.php", timeout=60))
597585
</runcode>
@@ -613,12 +601,10 @@ def compile_and_run_pascal_code(pasfilename: str, timeout: int = 60) -> str:
613601
614602
This is an example for running pascal code:
615603
<example>
616-
<savetofile filename="hello.pas">
617-
program hello;
604+
<savetofile filename="hello.pas">program hello;
618605
begin
619606
WriteLn('Hello');
620-
end.
621-
</savetofile>
607+
end.</savetofile>
622608
<runcode>
623609
compile_and_run_pascal_code("hello.pas", timeout=60)
624610
</runcode>

src/smolagents/prompts/code_agent.yaml

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ system_prompt: |-
2929
<example>
3030
<thoughts>I will use python code to compute the result of the operation and then return the final answer using the `final_answer` tool.</thoughts>
3131
<freewill>I am going to solve this task with confidence.</freewill>
32-
<savetofile filename="free-will.txt">
33-
I am going to solve this task with confidence.
34-
</savetofile>
32+
<savetofile filename="free-will.txt">I am going to solve this task with confidence.</savetofile>
3533
<runcode>
3634
result = 5 + 3 + 1294.678
3735
final_answer(result)
@@ -40,32 +38,22 @@ system_prompt: |-
4038
4139
For saving text files (text, csv, python code), just enclose your text into the <savetofile></savetofile> tags as per examples below:
4240
<example>
43-
<savetofile filename="example.txt">
44-
This is the content of example.txt
45-
</savetofile>
41+
<savetofile filename="example.txt">This is the content of example.txt</savetofile>
4642
47-
<savetofile filename="another_file.csv">
48-
header1,header2
43+
<savetofile filename="another_file.csv">header1,header2
4944
value1,value2
50-
value3,value4
51-
</savetofile>
45+
value3,value4</savetofile>
5246
53-
<savetofile filename="hello.py">
54-
print("hello")
55-
</savetofile>
47+
<savetofile filename="hello.py">print("hello")</savetofile>
5648
</example>
5749
5850
For saving source code files, using the <savetofile></savetofile> tags is the best method.
5951
6052
You may also append content to file with the tags <appendtofile></appendtofile>. This is an example:
6153
<example>
62-
<savetofile filename="another_csv.csv">
63-
header1,header2
64-
</savetofile>
65-
<appendtofile filename="another_csv.csv">
66-
value1,value2
67-
value3,value4
68-
</appendtofile>
54+
<savetofile filename="another_csv.csv">header1,header2</savetofile>
55+
<appendtofile filename="another_csv.csv">value1,value2
56+
value3,value4</appendtofile>
6957
7058
The above will create a csv file with the following content:
7159
header1,header2
@@ -77,13 +65,9 @@ system_prompt: |-
7765
7866
If you need to include any file in the file system, use the <includefile></includefile> tags. This is an example:
7967
<example>
80-
<savetofile filename="first_step.py">
81-
print("first step")
82-
</savetofile>
68+
<savetofile filename="first_step.py">print("first step")</savetofile>
8369
84-
<savetofile filename="second_step.py">
85-
print("second step")
86-
</savetofile>
70+
<savetofile filename="second_step.py">print("second step")</savetofile>
8771
8872
<runcode>
8973
<includefile>first_step.py</includefile>
@@ -136,10 +120,8 @@ system_prompt: |-
136120
5. If you find yourself repeating logic across steps, or if a task would benefit from
137121
reusable helper functions, create your own tools by saving Python functions to a file
138122
and importing them in later steps. Use the bpsa/ directory for this. Example:
139-
<savetofile filename="bpsa/helpers.py">
140-
def parse_csv_row(row):
141-
return [x.strip() for x in row.split(',')]
142-
</savetofile>
123+
<savetofile filename="bpsa/helpers.py">def parse_csv_row(row):
124+
return [x.strip() for x in row.split(',')]</savetofile>
143125
<runcode>
144126
from bpsa.helpers import parse_csv_row
145127
result = parse_csv_row("a, b, c")

0 commit comments

Comments
 (0)