This repository contains examples of using Semantic Kernel to execute Python code either locally or in a sandboxed environment. The examples demonstrate how to set up and use ChatCompletionAgent
to process user input, generate Python code, and execute it safely.
- Azure OpenAI Integration: Uses Azure OpenAI services for code generation.
- Safe Code Execution: Executes Python code in a restricted environment to ensure safety.
- Logging: Provides detailed logging for debugging and monitoring.
- Environment Configuration: Uses environment variables for configuration.
- Flexible Execution: Can be executed locally or in the Azure Container Apps Dynamic Sessions Code Interpreter.
- Agent Group Chat: Demonstrates how to create a group chat with multiple agents working together.
- Python 3.12
- Azure OpenAI account
-
Clone the repository:
git clone https://github.com/john-carroll-sw/semantic-kernel-agent-code-execution.git cd semantic-kernel-agent-code-execution
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the required packages:
pip install -r requirements.txt
-
Set up your environment variables in a
.env
file. You can use the provided.env.sample
as a template:AZURE_OPENAI_ENDPOINT=https://your-openai-endpoint.openai.azure.com/ AZURE_OPENAI_API_KEY="your-api-key" AZURE_OPENAI_API_VERSION=2024-08-01-preview AZURE_OPENAI_DEPLOYMENT=your-deployment-name AZURE_CODE_INTERPRETER_POOL_ENDPOINT=your-pool-endpoint
AZURE_OPENAI_ENDPOINT
: The endpoint for your Azure OpenAI service.AZURE_OPENAI_API_KEY
: Your Azure OpenAI API key.AZURE_OPENAI_API_VERSION
: The API version to use for Azure OpenAI.AZURE_OPENAI_DEPLOYMENT
: The deployment name for your Azure OpenAI service.AZURE_CODE_INTERPRETER_POOL_ENDPOINT
: The endpoint for managing the Azure Container Apps Dynamic Sessions Code Interpreter. Only add this if you plan on using the ACA Dynamic Sessions Code Interpreter which uses theSessionsPythonTool
.
By default, the generated code is set to run locally. If you want to run the code in a sandboxed environment, make sure to configure the AZURE_CODE_INTERPRETER_POOL_ENDPOINT
and set USE_CODE_INTERPRETER_SESSIONS_TOOL
to True
in the respective scripts.
Run the script:
python code_execution_example.py
Enter your message when prompted, and the agent will process it, generate Python code, and execute it. This example demonstrates how to execute code either locally or in a sandboxed environment.
Run the script:
python agent_group_code_execution.py
Enter your message when prompted, and the agents will work together to generate and execute Python code. This example also supports executing code either locally or in a sandboxed environment.
Run the script:
python agent_group_writing_example.py
Enter your message when prompted, and the agents will work together to review and rewrite the content.
For examples of good questions or prompts to use with a code interpreter, refer to the code_interpreter_questions.md file.
This project is licensed under the MIT License.