Skip to content

Commit

Permalink
Merge pull request #729 from thefazzer/main
Browse files Browse the repository at this point in the history
Fix for failing Powershell code execution on Linux
  • Loading branch information
KillianLucas authored Nov 2, 2023
2 parents ee8556c + 86acf2e commit da787b7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
4 changes: 3 additions & 1 deletion interpreter/code_interpreters/languages/powershell.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import platform
import shutil

from ..subprocess_code_interpreter import SubprocessCodeInterpreter

Expand All @@ -16,7 +17,8 @@ def __init__(self):
self.start_cmd = "powershell.exe"
# self.start_cmd = os.environ.get('SHELL', 'powershell.exe')
else:
self.start_cmd = os.environ.get("SHELL", "bash")
# On non-Windows platforms, prefer pwsh (PowerShell Core) if available, or fall back to bash
self.start_cmd = "pwsh" if shutil.which("pwsh") else "bash"

def preprocess_code(self, code):
return preprocess_powershell(code)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ inquirer = "^3.1.3"
litellm = "0.8.6"
ooba = "^0.0.21"
openai = "^0.28.0"
powershell-core = "^7.0"
python-dotenv = "^1.0.0"
pyyaml = "^6.0.1"
rich = "^13.4.2"
Expand Down
37 changes: 36 additions & 1 deletion tests/test_interpreter.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import time
from random import randint

import re
import pytest

import interpreter
Expand Down Expand Up @@ -147,6 +147,41 @@ def test_nested_loops_and_multiple_newlines():
"""Can you write a nested for loop in python and shell and run them? Don't forget to properly format your shell script and use semicolons where necessary. Also put 1-3 newlines between each line in the code. Only generate and execute the code. No explanations. Thanks!"""
)

@pytest.mark.skip(
reason="Skipping until can verify it runs on the Github build server"
)
@pytest.mark.parametrize("expected_integers", [
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,
233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711,
28657, 46368, 75025, 121393, 196418, 317811, 514229
])
def test_powershell_gen_exec(expected_integers):
TASK_RESULT_PREFIX = "TaskResult:"
num_fibs = str(len(expected_integers)) # Convert to string

result = interpreter.chat(
f"""Write a Powershell script to generate the first {num_fibs} Fibonacci numbers.
Approach this task methodically by planning and implementing it one step at a time.
Only generate and execute code - ie. do not explain, ask questions, seek assistance or offer unsolicited information.
Make sure to always execute the Powershell code and provide the output in the following format: {TASK_RESULT_PREFIX} x0, x1, x2,...x(n)"""
)

is_valid = lambda d: d.get('role') == 'assistant' and d.get('message', '').startswith(TASK_RESULT_PREFIX)
valid_result = next(filter(is_valid, result), None)

if valid_result is not None:
message = valid_result.get('message', '')
pattern = r'{}\s*([\d\s,]+)'.format(re.escape(TASK_RESULT_PREFIX)) # Use re.escape() to handle any special characters
match = re.search(pattern, message)
if match:
integer_series = match.group(1)
extracted_integers = [int(num.strip()) for num in integer_series.split(',')]

assert extracted_integers == expected_integers, "Extracted integers do not match expected integers."

print(f"Extracted message for {num_fibs} Fibonacci numbers:", message)
else:
print(f"No valid message found in the list of results for {num_fibs} Fibonacci numbers.")

def test_markdown():
interpreter.chat(
Expand Down

0 comments on commit da787b7

Please sign in to comment.