Files
Farkhod Sadykov 4ea2f834cf Migrate to OpenAI v2 (#744)
* Migrate to OpenAI v2 package

* Remove Click direct package dependency

* Minor handler improvements

* Change README.md demo video

* Version bump, release 1.5.0
2026-01-28 01:52:29 +01:00

43 lines
1.3 KiB
Python

import subprocess
from typing import Any, Dict
from pydantic import BaseModel, Field
class Function(BaseModel):
"""
Executes a shell command and returns the output (result).
"""
shell_command: str = Field(
...,
example="ls -la",
description="Shell command to execute.",
) # type: ignore
@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()}"
@classmethod
def openai_schema(cls) -> Dict[str, Any]:
"""Generate OpenAI function schema from Pydantic model."""
schema = cls.model_json_schema()
return {
"type": "function",
"function": {
"name": "execute_shell_command",
"description": cls.__doc__.strip() if cls.__doc__ else "",
"parameters": {
"type": "object",
"properties": schema.get("properties", {}),
"required": schema.get("required", []),
},
},
}