mirror of
https://github.com/TheR1D/shell_gpt.git
synced 2026-07-03 14:10:18 +02:00
361d1eabf8
* Improved mocked response tests. * REPL mode can accept stdin as initial prompt. * Readme and Contributing documentation improvements.
29 lines
730 B
Python
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()}"
|