How to Build a Python Chatbot That Writes and Runs Code Using Microsoft AutoGen (With Sample Code)

CK CK 3788 
Created at
Updated at  
245 0 0

To build a chatbot using Microsoft's AutoGen framework that can automatically write and execute Python code based on your chat prompt, you can use the autogen library.

 

In AutoGen, the standard setup involves two main agents:

  1. An AssistantAgent that acts as the LLM (writing the code).
  2. A UserProxyAgent that acts as the proxy for the user, which is also capable of automatically running the code blocks inside a secure docker container or a local execution environment.

How to Build a Python Chatbot That Writes and Runs Code Using Microsoft AutoGen (With Sample Code)

 

 

Prerequisites

First, install the AutoGen package:

pip install autogen


 

Python Code Example (using AutoGen and Llama)

Since you want to use Llama as the underlying LLM, you can route it through a local server like Ollama, LM Studio, or LiteLLM that provides an OpenAI-compatible API endpoint.

Here is a complete example setup using AutoGen to create a code-writing and code-executing chatbot loop:

import os
from autogen import AssistantAgent, UserProxyAgent

# 1. Define the LLM configuration pointing to your local Llama model
# Assumes you have Ollama or a similar service running locally on port 11434
config_list = [
    {
        "model": "llama3.1",             # The name of your local Llama model
        "base_url": "http://localhost:11434/v1",  # OpenAI-compatible endpoint
        "api_key": "ollama",             # Placeholder key required by the client
    }
]

llm_config = {
    "config_list": config_list,
    "temperature": 0.1,                  # Lower temperature for accurate coding
}

# 2. Create the Assistant Agent (The Code Writer)
assistant = AssistantAgent(
    name="coder_assistant",
    llm_config=llm_config,
    system_message="You are an expert Python developer. Write clean, complete Python code blocks "
                   "enclosed in ```python ... ``` when asked to solve a problem. Do not provide "
                   "partial snippets. Include print statements so the output can be clearly seen."
)

# 3. Create the User Proxy Agent (The Code Executor)
user_proxy = UserProxyAgent(
    name="user_proxy",
    human_input_mode="NEVER",            # Automatically execute code without asking the user each time
    max_consecutive_auto_reply=3,        # Limit conversation rounds to avoid infinite loops
    is_termination_msg=lambda x: x.get("content", "").rstrip().endswith("TERMINATE"),
    code_execution_config={
        "work_dir": "coding_workspace",  # Directory where files will be saved and executed
        "use_docker": False,             # Set to True if you want to isolate code execution in Docker
    }
)

def ask_chatbot(prompt: str):
    print(f"🚀 User Prompt: {prompt}\n")
    
    # Start the conversation loop
    user_proxy.initiate_chat(
        assistant,
        message=prompt
    )

if __name__ == "__main__":
    # Create the workspace directory if it doesn't exist
    os.makedirs("coding_workspace", exist_ok=True)
    
    # Example task
    sample_prompt = (
        "Write a Python script that fetches the current price of Bitcoin using a public API "
        "and prints it formatted cleanly. Once done, output TERMINATE."
    )
    
    ask_chatbot(sample_prompt)


 

How this works under the hood:

  • AssistantAgent: Receives the text prompt, thinks through the solution, and generates a response containing standard markdown code blocks.
  • UserProxyAgent: Scans the incoming message from the assistant. If it finds a ```python block, it automatically extracts the code, writes it to a file inside the coding_workspace directory, runs it locally, and sends the standard output/error back to the assistant.
  • Termination: The conversation naturally concludes when the agent outputs TERMINATE, or when it reaches the max_consecutive_auto_reply limit.

 

Improving Intelligence by AutoGen Group Chat

To significantly improve intelligence and add structured reasoning, we can transition this setup into an AutoGen Group Chat. By doing this, we keep your two existing core agents (coder_assistant and user_proxy) but inject a third specialized agent—a code_reviewer (or critic)—into the loop.

This introduces a "Review and Refine" architecture where the code isn't just blindly executed: it gets verified, optimized, or fixed before or after the execution phase.

Here is the updated code implementing this multi-agent logic:

import os
from autogen import AssistantAgent, UserProxyAgent, GroupChat, GroupChatManager

# 1. Define the LLM configuration pointing to your local Llama model
config_list = [
    {
        "model": "llama3.1",
        "base_url": "http://localhost:11434/v1",
        "api_key": "ollama",
    }
]

llm_config = {
    "config_list": config_list,
    "temperature": 0.1,
}

# 2. Existing Agent 1: The Code Writer
assistant = AssistantAgent(
    name="coder_assistant",
    llm_config=llm_config,
    system_message="You are an expert Python developer. Write clean, complete Python code blocks "
                   "enclosed in ```python ... ``` when asked to solve a problem or when fixing bugs "
                   "pointed out by the code_reviewer. Do not provide partial snippets."
)

# 3. New Agent: The Code Reviewer / Critic (Improves Intelligence)
# This agent analyzes the written code or execution output for logic gaps, security issues, or bugs.
reviewer = AssistantAgent(
    name="code_reviewer",
    llm_config=llm_config,
    system_message="You are a Senior Quality Assurance Engineer and Code Reviewer. Analyze code written by "
                   "the coder_assistant or execution results from the user_proxy. "
                   "Verify if requirements are fully met, look for edge cases, and ensure clean coding standards. "
                   "If the code looks perfect and execution output matches expectations, reply ONLY with 'TERMINATE'. "
                   "Otherwise, state exactly what needs to be fixed or improved so the coder_assistant can adjust it."
)

# 4. Existing Agent 2: The User Proxy & Code Executor
user_proxy = UserProxyAgent(
    name="user_proxy",
    human_input_mode="NEVER",
    max_consecutive_auto_reply=6,        # Higher limit to accommodate multi-turn planning
    is_termination_msg=lambda x: "TERMINATE" in x.get("content", ""),
    code_execution_config={
        "work_dir": "coding_workspace",
        "use_docker": False,
    }
)

def ask_intelligent_chatbot(prompt: str):
    print(f"🚀 User Prompt: {prompt}\n")
    
    # 5. Define the Group Chat orchestration
    # We bring together all agents into a unified conversation ecosystem
    groupchat = GroupChat(
        agents=[user_proxy, assistant, reviewer], 
        messages=[], 
        max_round=12
    )
    
    manager = GroupChatManager(
        groupchat=groupchat, 
        llm_config=llm_config
    )
    
    # Start the group conversation loop via the manager proxy
    user_proxy.initiate_chat(
        manager,
        message=prompt
    )

if __name__ == "__main__":
    os.makedirs("coding_workspace", exist_ok=True)
    
    sample_prompt = (
        "Write a Python script that fetches the current price of Bitcoin using a public API, "
        "calculates what $500 USD is worth in BTC based on that price, and prints it cleanly."
    )
    
    ask_intelligent_chatbot(sample_prompt)

 

Why this increases intelligence:

  • Iterative Optimization: Instead of a linear write-then-execute flow, coder_assistant writes the code $\rightarrow$ user_proxy runs it $\rightarrow$ code_reviewer validates the code output. If the code throws an error or fails an edge case, code_reviewer explains the bug, and the orchestrator dynamically routes it back to coder_assistant to refactor.
  • Deterministic Termination: Instead of the coder declaring it's done prematurely, the code_reviewer holds the responsibility to issue the final TERMINATE flag only when the script successfully proves its runtime objective.
Tags AssistantAgent AutoGen Chatbot Group Chat Isolated VM LLM LLM Application Llama Microsoft AutoGen UserProxyAgent Facebook X
Comments 0
Similar posts
  1. Conversation Multi-Agent Chatbot with On-the-Fly Code Execition via Microsoft AutoGen
    306
  2. Semantic Kernel: Microsoft's GenAI Orchestration SDK
    416
  3. LLM Application Development with LangGraph: Features and Architectures for Stateful Multi-Actor LLM Applications
    365
  4. Microsoft AutoGen: Next-Generation Multi-Agent Framework
    318
  5. Is a Used M1 or M2 MacBook Pro a Smart Buy Over M4?
    298
  6. Beyond Prompt Engineering: Why Loop Engineering is the Future of Autonomous AI
    310
  7. GPU & LLM Cluster Infrastructure & Task Scheduling Architecture
    255
  8. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    355
  9. Is PyTorch Dead? Deep Learning's Enduring Role Beyond LLMs & Essential Skills
    699
  10. TensorRT for LLMs: Speed, Scale, and Why Startups Choose It Over TensorFlow
    467
  11. Milvus and LLM Integration - Very Smart Approach!
    304
  12. Learn SLM if you have interent in the next after LLM
    4931
  13. VLM vs. LLM: How Vision-Language Models Are Revolutionizing Real-Time Media Processing
    50
  14. Technical Hierarchy of AI Agents
    826
  15. Image Moderation Chatbot - a serverless chatbot on AWS that monitors chat channels and removes images suggestive or explicit image
    10,432
  1. What is RedisVL?
    117
  2. What “GPU Clustering” Really Means (And How to Code for It)
    215
  3. A Python Code to get the Server Instance ID for Remote Management
    366
  4. From Local Python Web to Standalone
    2,056
  5. Demystifying FAISS: Your Guide to ANN(Approximate Nearest Neighbor) Search
    327
  6. Python ML Technologies used for the target Segmentation
    381
  7. Managing WHITE_LIST in FastAPI to Improve the Server Security
    355
  8. A common async example
    990
  9. Automated API Documentation with PyCharm
    896
  10. How to install python 3.9 alongside python 3.8 on CentOS 7?
    719
  11. How to run Python 3.9 on Mac?
    656
  12. Python Quizs
    502
  13. Automated OpenAPI documentation for a Python Flask API
    1,127
  14. Automated API documentation for a Django REST Framework (DRF) project using drf-yasg (Yet Another Swagger Generator)
    1,033
  15. Uploading a file to a Google Drive Folder programmatically with Python
    3,311
  16. Garbage Collection when your system does not have enough memory in your Python App
    1,021
  17. Method to maximize full CPU performance with Python
    325
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    31
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    57
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    51
  4. How do I create animated background for Google Meet?
    23,254
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    273
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    104
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    170
  8. Array of arguments passed to script in PHP
    99
  9. Invalid Auction Storms in Ad Tech
    107
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    209
  11. The Two Clocks of Ad Server Architecture
    78
  12. Frequency Cap Architecture Overview
    527
  13. Collecting Proof-of-Play from 100,000 Screens
    153
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    303
  15. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    247
  16. TPU & GPU Cluster Management Tools
    258
  17. The Concept of Scaffolding in AI Agents
    245
  18. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    598
  19. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    269
  20. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    207
  21. Machine Learning: Training vs. Ranking
    230
  22. The Continuous Learning Machine: Designing a Loop That Actually Turns
    229
  23. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    204
  24. Designing a Data Curation System Around a Vector Database
    230
  25. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  26. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    674
  27. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  28. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    233
  29. Understanding Data Lineage
    272
  30. System Requirements and Architecture: Intelligent Priority-Based Scheduler
    176