Files
shell_gpt/sgpt/llm_functions/common/execute_shell.py
T
Farkhod Sadykov 361d1eabf8 Improved tests, REPL stdin, documentation (#452)
* Improved mocked response tests.
* REPL mode can accept stdin as initial prompt.
* Readme and Contributing documentation improvements.
2024-01-19 02:16:46 +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()}"