Files
shell_gpt/sgpt/default_functions/common/execute_shell.py
T
2024-01-09 02:52:41 +01:00

29 lines
730 B
Python

import subprocess
from instructor import OpenAISchema
from pydantic import Field
class Function(OpenAISchema):
"""
Executes a shell command and returns the output (result).
"""
shell_command: str = Field(
...,
example="ls -la",
descriptions="Shell command to execute.",
)
class Config:
title = "execute_shell_command"
@classmethod
def execute(cls, shell_command: str) -> str:
process = subprocess.Popen(
shell_command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT
)
output, _ = process.communicate()
exit_code = process.returncode
return f"Exit code: {exit_code}, Output:\n{output.decode()}"