Tachi67 commited on
Commit
803677d
·
1 Parent(s): 544270e

Upload 6 files

Browse files
Files changed (3) hide show
  1. InterpreterAtomicFlow.py +20 -2
  2. README.md +318 -0
  3. __init__.py +13 -1
InterpreterAtomicFlow.py CHANGED
@@ -2,11 +2,26 @@ import traceback
2
  from copy import deepcopy
3
  from typing import Dict, Any
4
 
5
- from interpreter.code_interpreters.create_code_interpreter import create_code_interpreter
6
- from interpreter.utils.truncate_output import truncate_output
7
 
8
  from aiflows.base_flows import AtomicFlow
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  class InterpreterAtomicFlow(AtomicFlow):
12
  """This flow is used to run the code passed from the caller.
@@ -16,6 +31,9 @@ class InterpreterAtomicFlow(AtomicFlow):
16
 
17
  *Expected Output*:
18
  - `interpreter_output`: output of the code interpreter
 
 
 
19
  """
20
  def __init__(self,
21
  max_output=2000,
 
2
  from copy import deepcopy
3
  from typing import Dict, Any
4
 
5
+ from .code_interpreters.create_code_interpreter import create_code_interpreter
 
6
 
7
  from aiflows.base_flows import AtomicFlow
8
 
9
+ def truncate_output(data, max_output_chars=2000):
10
+ needs_truncation = False
11
+
12
+ message = f'Output truncated. Showing the last {max_output_chars} characters.\n\n'
13
+
14
+ # Remove previous truncation message if it exists
15
+ if data.startswith(message):
16
+ data = data[len(message):]
17
+ needs_truncation = True
18
+
19
+ # If data exceeds max length, truncate it and add message
20
+ if len(data) > max_output_chars or needs_truncation:
21
+ data = message + data[-max_output_chars:]
22
+
23
+ return data
24
+
25
 
26
  class InterpreterAtomicFlow(AtomicFlow):
27
  """This flow is used to run the code passed from the caller.
 
31
 
32
  *Expected Output*:
33
  - `interpreter_output`: output of the code interpreter
34
+
35
+ Full credits to open-interpreter (https://github.com/KillianLucas/open-interpreter)
36
+ for the usage of code interpreters (package `code_interpreters`) and the function truncate_output()
37
  """
38
  def __init__(self,
39
  max_output=2000,
README.md CHANGED
@@ -4,6 +4,41 @@
4
  * [InterpreterAtomicFlow](#InterpreterAtomicFlow.InterpreterAtomicFlow)
5
  * [set\_up\_flow\_state](#InterpreterAtomicFlow.InterpreterAtomicFlow.set_up_flow_state)
6
  * [\_\_init\_\_](#__init__)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
  <a id="InterpreterAtomicFlow"></a>
9
 
@@ -25,6 +60,9 @@ This flow is used to run the code passed from the caller.
25
  *Expected Output*:
26
  - `interpreter_output`: output of the code interpreter
27
 
 
 
 
28
  <a id="InterpreterAtomicFlow.InterpreterAtomicFlow.set_up_flow_state"></a>
29
 
30
  #### set\_up\_flow\_state
@@ -40,3 +78,283 @@ which describes the programming language and the code to run.
40
 
41
  # \_\_init\_\_
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  * [InterpreterAtomicFlow](#InterpreterAtomicFlow.InterpreterAtomicFlow)
5
  * [set\_up\_flow\_state](#InterpreterAtomicFlow.InterpreterAtomicFlow.set_up_flow_state)
6
  * [\_\_init\_\_](#__init__)
7
+ * [code\_interpreters](#code_interpreters)
8
+ * [code\_interpreters.language\_map](#code_interpreters.language_map)
9
+ * [code\_interpreters.create\_code\_interpreter](#code_interpreters.create_code_interpreter)
10
+ * [code\_interpreters.languages.javascript](#code_interpreters.languages.javascript)
11
+ * [preprocess\_javascript](#code_interpreters.languages.javascript.preprocess_javascript)
12
+ * [code\_interpreters.languages.shell](#code_interpreters.languages.shell)
13
+ * [preprocess\_shell](#code_interpreters.languages.shell.preprocess_shell)
14
+ * [add\_active\_line\_prints](#code_interpreters.languages.shell.add_active_line_prints)
15
+ * [code\_interpreters.languages.python](#code_interpreters.languages.python)
16
+ * [preprocess\_python](#code_interpreters.languages.python.preprocess_python)
17
+ * [add\_active\_line\_prints](#code_interpreters.languages.python.add_active_line_prints)
18
+ * [AddLinePrints](#code_interpreters.languages.python.AddLinePrints)
19
+ * [insert\_print\_statement](#code_interpreters.languages.python.AddLinePrints.insert_print_statement)
20
+ * [process\_body](#code_interpreters.languages.python.AddLinePrints.process_body)
21
+ * [visit](#code_interpreters.languages.python.AddLinePrints.visit)
22
+ * [code\_interpreters.languages.html](#code_interpreters.languages.html)
23
+ * [code\_interpreters.languages.r](#code_interpreters.languages.r)
24
+ * [R](#code_interpreters.languages.r.R)
25
+ * [preprocess\_code](#code_interpreters.languages.r.R.preprocess_code)
26
+ * [code\_interpreters.languages.powershell](#code_interpreters.languages.powershell)
27
+ * [preprocess\_powershell](#code_interpreters.languages.powershell.preprocess_powershell)
28
+ * [add\_active\_line\_prints](#code_interpreters.languages.powershell.add_active_line_prints)
29
+ * [wrap\_in\_try\_catch](#code_interpreters.languages.powershell.wrap_in_try_catch)
30
+ * [code\_interpreters.languages](#code_interpreters.languages)
31
+ * [code\_interpreters.languages.applescript](#code_interpreters.languages.applescript)
32
+ * [AppleScript](#code_interpreters.languages.applescript.AppleScript)
33
+ * [preprocess\_code](#code_interpreters.languages.applescript.AppleScript.preprocess_code)
34
+ * [add\_active\_line\_indicators](#code_interpreters.languages.applescript.AppleScript.add_active_line_indicators)
35
+ * [detect\_active\_line](#code_interpreters.languages.applescript.AppleScript.detect_active_line)
36
+ * [detect\_end\_of\_execution](#code_interpreters.languages.applescript.AppleScript.detect_end_of_execution)
37
+ * [code\_interpreters.subprocess\_code\_interpreter](#code_interpreters.subprocess_code_interpreter)
38
+ * [SubprocessCodeInterpreter](#code_interpreters.subprocess_code_interpreter.SubprocessCodeInterpreter)
39
+ * [preprocess\_code](#code_interpreters.subprocess_code_interpreter.SubprocessCodeInterpreter.preprocess_code)
40
+ * [code\_interpreters.base\_code\_interpreter](#code_interpreters.base_code_interpreter)
41
+ * [BaseCodeInterpreter](#code_interpreters.base_code_interpreter.BaseCodeInterpreter)
42
 
43
  <a id="InterpreterAtomicFlow"></a>
44
 
 
60
  *Expected Output*:
61
  - `interpreter_output`: output of the code interpreter
62
 
63
+ Full credits to open-interpreter (https://github.com/KillianLucas/open-interpreter)
64
+ for the usage of code interpreters (package `code_interpreters`) and the function truncate_output()
65
+
66
  <a id="InterpreterAtomicFlow.InterpreterAtomicFlow.set_up_flow_state"></a>
67
 
68
  #### set\_up\_flow\_state
 
78
 
79
  # \_\_init\_\_
80
 
81
+ <a id="code_interpreters"></a>
82
+
83
+ # code\_interpreters
84
+
85
+ <a id="code_interpreters.language_map"></a>
86
+
87
+ # code\_interpreters.language\_map
88
+
89
+ <a id="code_interpreters.create_code_interpreter"></a>
90
+
91
+ # code\_interpreters.create\_code\_interpreter
92
+
93
+ <a id="code_interpreters.languages.javascript"></a>
94
+
95
+ # code\_interpreters.languages.javascript
96
+
97
+ <a id="code_interpreters.languages.javascript.preprocess_javascript"></a>
98
+
99
+ #### preprocess\_javascript
100
+
101
+ ```python
102
+ def preprocess_javascript(code)
103
+ ```
104
+
105
+ Add active line markers
106
+ Wrap in a try catch
107
+ Add end of execution marker
108
+
109
+ <a id="code_interpreters.languages.shell"></a>
110
+
111
+ # code\_interpreters.languages.shell
112
+
113
+ <a id="code_interpreters.languages.shell.preprocess_shell"></a>
114
+
115
+ #### preprocess\_shell
116
+
117
+ ```python
118
+ def preprocess_shell(code)
119
+ ```
120
+
121
+ Add active line markers
122
+ Wrap in a try except (trap in shell)
123
+ Add end of execution marker
124
+
125
+ <a id="code_interpreters.languages.shell.add_active_line_prints"></a>
126
+
127
+ #### add\_active\_line\_prints
128
+
129
+ ```python
130
+ def add_active_line_prints(code)
131
+ ```
132
+
133
+ Add echo statements indicating line numbers to a shell string.
134
+
135
+ <a id="code_interpreters.languages.python"></a>
136
+
137
+ # code\_interpreters.languages.python
138
+
139
+ <a id="code_interpreters.languages.python.preprocess_python"></a>
140
+
141
+ #### preprocess\_python
142
+
143
+ ```python
144
+ def preprocess_python(code)
145
+ ```
146
+
147
+ Add active line markers
148
+ Wrap in a try except
149
+ Add end of execution marker
150
+
151
+ <a id="code_interpreters.languages.python.add_active_line_prints"></a>
152
+
153
+ #### add\_active\_line\_prints
154
+
155
+ ```python
156
+ def add_active_line_prints(code)
157
+ ```
158
+
159
+ Add print statements indicating line numbers to a python string.
160
+
161
+ <a id="code_interpreters.languages.python.AddLinePrints"></a>
162
+
163
+ ## AddLinePrints Objects
164
+
165
+ ```python
166
+ class AddLinePrints(ast.NodeTransformer)
167
+ ```
168
+
169
+ Transformer to insert print statements indicating the line number
170
+ before every executable line in the AST.
171
+
172
+ <a id="code_interpreters.languages.python.AddLinePrints.insert_print_statement"></a>
173
+
174
+ #### insert\_print\_statement
175
+
176
+ ```python
177
+ def insert_print_statement(line_number)
178
+ ```
179
+
180
+ Inserts a print statement for a given line number.
181
+
182
+ <a id="code_interpreters.languages.python.AddLinePrints.process_body"></a>
183
+
184
+ #### process\_body
185
+
186
+ ```python
187
+ def process_body(body)
188
+ ```
189
+
190
+ Processes a block of statements, adding print calls.
191
+
192
+ <a id="code_interpreters.languages.python.AddLinePrints.visit"></a>
193
+
194
+ #### visit
195
+
196
+ ```python
197
+ def visit(node)
198
+ ```
199
+
200
+ Overridden visit to transform nodes.
201
+
202
+ <a id="code_interpreters.languages.html"></a>
203
+
204
+ # code\_interpreters.languages.html
205
+
206
+ <a id="code_interpreters.languages.r"></a>
207
+
208
+ # code\_interpreters.languages.r
209
+
210
+ <a id="code_interpreters.languages.r.R"></a>
211
+
212
+ ## R Objects
213
+
214
+ ```python
215
+ class R(SubprocessCodeInterpreter)
216
+ ```
217
+
218
+ <a id="code_interpreters.languages.r.R.preprocess_code"></a>
219
+
220
+ #### preprocess\_code
221
+
222
+ ```python
223
+ def preprocess_code(code)
224
+ ```
225
+
226
+ Add active line markers
227
+ Wrap in a tryCatch for better error handling in R
228
+ Add end of execution marker
229
+
230
+ <a id="code_interpreters.languages.powershell"></a>
231
+
232
+ # code\_interpreters.languages.powershell
233
+
234
+ <a id="code_interpreters.languages.powershell.preprocess_powershell"></a>
235
+
236
+ #### preprocess\_powershell
237
+
238
+ ```python
239
+ def preprocess_powershell(code)
240
+ ```
241
+
242
+ Add active line markers
243
+ Wrap in try-catch block
244
+ Add end of execution marker
245
+
246
+ <a id="code_interpreters.languages.powershell.add_active_line_prints"></a>
247
+
248
+ #### add\_active\_line\_prints
249
+
250
+ ```python
251
+ def add_active_line_prints(code)
252
+ ```
253
+
254
+ Add Write-Output statements indicating line numbers to a PowerShell script.
255
+
256
+ <a id="code_interpreters.languages.powershell.wrap_in_try_catch"></a>
257
+
258
+ #### wrap\_in\_try\_catch
259
+
260
+ ```python
261
+ def wrap_in_try_catch(code)
262
+ ```
263
+
264
+ Wrap PowerShell code in a try-catch block to catch errors and display them.
265
+
266
+ <a id="code_interpreters.languages"></a>
267
+
268
+ # code\_interpreters.languages
269
+
270
+ <a id="code_interpreters.languages.applescript"></a>
271
+
272
+ # code\_interpreters.languages.applescript
273
+
274
+ <a id="code_interpreters.languages.applescript.AppleScript"></a>
275
+
276
+ ## AppleScript Objects
277
+
278
+ ```python
279
+ class AppleScript(SubprocessCodeInterpreter)
280
+ ```
281
+
282
+ <a id="code_interpreters.languages.applescript.AppleScript.preprocess_code"></a>
283
+
284
+ #### preprocess\_code
285
+
286
+ ```python
287
+ def preprocess_code(code)
288
+ ```
289
+
290
+ Inserts an end_of_execution marker and adds active line indicators.
291
+
292
+ <a id="code_interpreters.languages.applescript.AppleScript.add_active_line_indicators"></a>
293
+
294
+ #### add\_active\_line\_indicators
295
+
296
+ ```python
297
+ def add_active_line_indicators(code)
298
+ ```
299
+
300
+ Adds log commands to indicate the active line of execution in the AppleScript.
301
+
302
+ <a id="code_interpreters.languages.applescript.AppleScript.detect_active_line"></a>
303
+
304
+ #### detect\_active\_line
305
+
306
+ ```python
307
+ def detect_active_line(line)
308
+ ```
309
+
310
+ Detects active line indicator in the output.
311
+
312
+ <a id="code_interpreters.languages.applescript.AppleScript.detect_end_of_execution"></a>
313
+
314
+ #### detect\_end\_of\_execution
315
+
316
+ ```python
317
+ def detect_end_of_execution(line)
318
+ ```
319
+
320
+ Detects end of execution marker in the output.
321
+
322
+ <a id="code_interpreters.subprocess_code_interpreter"></a>
323
+
324
+ # code\_interpreters.subprocess\_code\_interpreter
325
+
326
+ <a id="code_interpreters.subprocess_code_interpreter.SubprocessCodeInterpreter"></a>
327
+
328
+ ## SubprocessCodeInterpreter Objects
329
+
330
+ ```python
331
+ class SubprocessCodeInterpreter(BaseCodeInterpreter)
332
+ ```
333
+
334
+ <a id="code_interpreters.subprocess_code_interpreter.SubprocessCodeInterpreter.preprocess_code"></a>
335
+
336
+ #### preprocess\_code
337
+
338
+ ```python
339
+ def preprocess_code(code)
340
+ ```
341
+
342
+ This needs to insert an end_of_execution marker of some kind,
343
+ which can be detected by detect_end_of_execution.
344
+
345
+ Optionally, add active line markers for detect_active_line.
346
+
347
+ <a id="code_interpreters.base_code_interpreter"></a>
348
+
349
+ # code\_interpreters.base\_code\_interpreter
350
+
351
+ <a id="code_interpreters.base_code_interpreter.BaseCodeInterpreter"></a>
352
+
353
+ ## BaseCodeInterpreter Objects
354
+
355
+ ```python
356
+ class BaseCodeInterpreter()
357
+ ```
358
+
359
+ .run is a generator that yields a dict with attributes: active_line, output
360
+
__init__.py CHANGED
@@ -1 +1,13 @@
1
- from .InterpreterAtomicFlow import InterpreterAtomicFlow
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from .InterpreterAtomicFlow import InterpreterAtomicFlow
2
+ from .code_interpreters import base_code_interpreter
3
+ from .code_interpreters import languages
4
+ from .code_interpreters.languages import python
5
+ from .code_interpreters.languages import r
6
+ from .code_interpreters.languages import applescript
7
+ from .code_interpreters.languages import html
8
+ from .code_interpreters.languages import javascript
9
+ from .code_interpreters.languages import powershell
10
+ from .code_interpreters.languages import shell
11
+ from .code_interpreters import create_code_interpreter
12
+ from .code_interpreters import language_map
13
+ from .code_interpreters import subprocess_code_interpreter