Skip to content

Instantly share code, notes, and snippets.

@si3mshady
Created June 8, 2024 18:05
Show Gist options
  • Save si3mshady/1f4d6a7e0141c5d3f4879333ef985c16 to your computer and use it in GitHub Desktop.
Save si3mshady/1f4d6a7e0141c5d3f4879333ef985c16 to your computer and use it in GitHub Desktop.
Python script that accepts command line input and will generate and execute code
from pathlib import Path
import sys,os
import tempfile
from autogen import ConversableAgent
from autogen.coding import DockerCommandLineCodeExecutor
# python3 -m venv pyautogen
# source pyautogen/bin/activate
# pip install pyautogen
path = "elliottsExampleForLinkedinJune8"
work_dir = Path(path)
work_dir.mkdir(exist_ok=True)
def create_and_execute_code(instructions):
# Create a temporary directory to store the code files.
temp_dir = path
code_writer_system_message = "You are a code generating robot. Generate all code as instructed"
# Create a Docker command line code executor.
executor = DockerCommandLineCodeExecutor(
image="python:3.12-slim", # Execute code using the given docker image name.
timeout=40, # Timeout for each code execution in seconds.
work_dir=temp_dir, # Use the temporary directory to store the code files.
)
# Create an agent with code executor configuration that uses docker.
code_executor_agent_using_docker = ConversableAgent(
"code_executor_agent_docker",
llm_config=False, # Turn off LLM for this agent.
code_execution_config={"executor": executor}, # Use the docker command line code executor.
human_input_mode="NEVER", # Always take human input for this agent for safety.
max_consecutive_auto_reply=2
)
code_writer_agent = ConversableAgent(
"code_writer_agent",
system_message=code_writer_system_message,
llm_config={"config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}]},
code_execution_config=False, # Turn off code execution for this agent.
max_consecutive_auto_reply=2
)
code_writer_agent = ConversableAgent(
"code_writer_agent",
system_message=code_writer_system_message,
llm_config={"config_list": [{"model": "gpt-4", "api_key": os.environ["OPENAI_API_KEY"]}]},
code_execution_config=False, # Turn off code execution for this agent.
)
chat_result = code_executor_agent_using_docker.initiate_chat(
code_writer_agent,
message=instructions,
)
print(chat_result)
if __name__ == "__main__":
# if len(sys.argv) < 2:
# print("Usage: python script.py '<instructions>'")
# sys.exit(1)
# # makefile
# Copy code
# Get the instructions from the command line argument
instructions = sys.argv[1]
print(instructions)
# Pass the instructions to the function.
create_and_execute_code(instructions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment