An intelligent code generation system that uses DSPy and Ollama to automatically generate, test, and refine Python code based on natural language descriptions.
- Intelligent Code Generation: Converts natural language task descriptions into working Python code
- Automatic Test Generation: Creates comprehensive unit tests for generated code
- Self-Correcting Loop: Automatically refines code based on test failures until tests pass
- DSPy Integration: Leverages DSPy's optimization capabilities for improved code generation
- Local LLM Support: Uses Ollama with Llama3 for privacy-focused, offline code generation
Before running this project, ensure you have the following installed:
- Python 3.8+
- Ollama with Llama3 model
- pytest for test execution
- Install Ollama from https://ollama.ai
- Pull the Llama3 model:
ollama pull llama3
- Verify Ollama is running:
ollama serve
-
Clone the repository:
git clone https://github.com/evalops/dspy-code-generator.git cd dspy-code-generator -
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
- Ensure Ollama is running with Llama3 model available
- Run the code generator:
python main.py
The default example will generate a function to filter even numbers from a list.
The self-correcting loop follows these steps:
- Initial Generation: Takes a natural language task description and generates Python code
- Test Generation: Creates comprehensive unit tests for the generated code
- Test Execution: Runs the tests using pytest in a temporary environment
- Refinement Loop: If tests fail, uses the error output to refine the code (up to 3 iterations)
- Success: Returns the final working code and tests when all tests pass
- CodeGeneratorSignature: Converts task descriptions to Python code
- TestGeneratorSignature: Generates pytest unit tests for given code
- CodeRefinerSignature: Refines failing code based on test output
- CodeGenProgram: Orchestrates the entire self-correcting pipeline
Edit the model configuration in main.py:
llm = dspy.LM(
"ollama_chat/llama3", # Change model here
)Modify the max_refinements parameter:
prediction = compiled_program(task_description=new_task, max_refinements=5) # Default: 3from main import CodeGenProgram
# Initialize the program
code_gen_program = CodeGenProgram()
# Define your task
task = "Write a Python function that calculates the factorial of a number recursively"
# Generate code
result = code_gen_program(task_description=task, max_refinements=3)
print("Generated Code:")
print(result.python_code)
print("\nGenerated Tests:")
print(result.unit_test_code)
print(f"\nTests Passed: {result.test_passed}")# Add your own training examples
custom_training_data = [
dspy.Example(
task_description="Your task description here",
python_code="def your_function():\n pass",
unit_test_code="def test_your_function():\n assert True"
).with_inputs("task_description")
]
# Compile with custom data
teleprompter = dspy.teleprompt.BootstrapFewShot(metric=test_execution_metric)
compiled_program = teleprompter.compile(code_gen_program, trainset=custom_training_data)Ollama Connection Error:
- Ensure Ollama service is running:
ollama serve - Verify Llama3 model is available:
ollama list
Pytest Not Found:
- Install pytest:
pip install pytest - Ensure pytest is in your PATH
Code Generation Fails:
- Check if the task description is clear and specific
- Try increasing
max_refinementsfor complex tasks - Verify internet connection for Ollama model downloads
Tests Never Pass:
- The task might be too complex or ambiguous
- Try breaking down complex tasks into smaller subtasks
- Check the generated test logic for correctness
dspy-code-generator/
├── main.py # Main application with DSPy pipeline
├── requirements.txt # Python dependencies
├── README.md # This file
└── venv/ # Virtual environment (created after setup)
The system generates its own tests, but you can also test the framework:
# Test with a simple task
python -c "
from main import CodeGenProgram
program = CodeGenProgram()
result = program('Write a function that adds two numbers')
print('Success!' if result.test_passed else 'Failed')
"- Fork the repository
- Create a feature branch:
git checkout -b feature-name - Make your changes and test thoroughly
- Commit with clear messages:
git commit -m "Add feature description" - Push to your fork:
git push origin feature-name - Submit a pull request
This project is open source. Please check the license file for details.
- dspy-ai: DSPy framework for language model programming
- pytest: Testing framework for generated code
- ollama: Local LLM inference engine