This is an example project that helps external agents, such as LLMs, execute code and answer queries in a restricted environment by securely wrapping Python's built-in functions.
- Secure Wrapping: The
wrapped_builtins.pyfile wraps standard Python built-in functions likeprint()andlen()into functions with aw_prefix (e.g.,w_print,w_len). Decorators are used to preserve the original function signatures, making it easy for the agent to understand and use the functions. - Restricted Environment: The
ChatAgentinpa3_initial.pycan only use the functions passed as thetoolsparameter. This allows for selective exposure of functions, excluding potentially dangerous ones likeevalorexec.
pa3_initial.py: The main script that initializes and runs theChatAgent.wrapped_builtins.py: Wraps built-in functions and manages the list of functions (wrapped_builtins) provided to the agent.README.md: The project's documentation in Korean.README_EN.md: The project's documentation in English..gitignore: A list of files to be ignored by Git.
- Python 3.8+
python-dotenv: Used to load environment variables from a.envfile.openai: Interacts with the OpenAI API.agent-framework: An AI agent framework from Microsoft. For more details, see the official GitHub repository.
Dependency Installation (Virtual Environment Recommended):
# Create and activate a Python virtual environment
python3 -m venv .venv
source .venv/bin/activate
# Install dependencies from requirements.txt
# Note: agent-framework may require a pre-release version.
pip install -r requirements.txt --preOpenAI API information is required to run the project. Create a .env file in the root directory and set the environment variables as shown below.
# .env file example
OPENAI_MODEL_NAME="gpt-4o"
OPENAI_API_KEY="sk-..."
OPENAI_ENDPOINT="https://api.openai.com/v1"Once the environment variables are set, you can run the agent from the terminal in the following ways.
1. Pass a query directly as an argument
python pa3_initial.py "1 + 1"2. Run in interactive mode
python pa3_initial.py
# Query: 0.043 - 0.0013. Pass input via a pipe (|)
echo "2 * 3" | python pa3_initial.pypa3_initial.py creates a ChatAgent and calls the run() function to process the query. The result is displayed on the standard output with a Response: prefix.
To add a new function to wrapped_builtins:
- Define a new
w_<name>function in thewrapped_builtins.pyfile using the@wrap_builtindecorator. - Add the created function to the
wrapped_builtinslist. - Modify
pa3_initial.pyto pass the function name or the entire list to thetoolsparameter.
⚠️ Security Warning Exposing functions that can execute arbitrary code, such asevalorexec, to the agent can pose a serious security risk. The current code includesw_eval, so it must be disabled or used with caution in a real production environment.
Byeongki Jeong
This project is licensed under the MIT License.
MIT License
Copyright (c) 2025 Byeongki Jeong
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.