A powerful DSPy-based system for automatically generating optimized prompts based on user queries. This package intelligently analyzes user queries, determines the appropriate expert persona, and creates optimized prompts for improved AI interactions.
- Intelligent Query Classification: Automatically categorizes queries by type, domain, and complexity
- Expert Persona Generation: Creates appropriate expert roles based on query analysis
- Prompt Optimization: Transforms user queries into well-structured, optimized prompts
- Flexible Model Support: Works with various language models through DSPy
- Command Line Interface: Easy-to-use CLI for quick prompt generation
- Python API: Full programmatic access for integration into other projects
# Clone the repository
git clone https://github.com/sabre-code/auto-prompt-generation.git
cd auto-prompt-generation
# Install the package
pip install -e .pip install git+https://github.com/sabre-code/auto-prompt-generation.gitfrom auto_prompt_generation import QueryHandlerSystem
# Initialize the system
system = QueryHandlerSystem()
# Process a query
query = "Generate a case series for patients taking both Rosuvastatin and Clopidogrel"
result = system.process_query(query)
print("Optimized Prompt:")
print(result['optimized_prompt'])# Basic usage
auto-prompt-gen "How do I optimize my Python code for performance?"
# With custom model
auto-prompt-gen "Explain machine learning concepts" --model "gpt-3.5-turbo"
# JSON output with verbose analysis
auto-prompt-gen "Write a business plan" --output-format json --verbosefrom auto_prompt_generation import QueryHandlerSystem
system = QueryHandlerSystem()
query = "Generate a case series for patients taking both Rosuvastatin and Clopidogrel who experienced a non-fatal myocardial infarction."
result = system.process_query(query)
print(result['optimized_prompt'])Output:
You are a clinical research specialist with extensive experience in cardiology and pharmacovigilance. You have a deep understanding of drug interactions, cardiovascular disease management, and clinical case study methodology...
User's question: Generate a case series for patients taking both Rosuvastatin and Clopidogrel who experienced a non-fatal myocardial infarction.
query = "How do I implement a binary search tree in Python?"
result = system.process_query(query)The system will automatically:
- Classify this as a technical, programming query
- Generate a "Senior Software Engineer" expert persona
- Create an optimized prompt for code implementation
You can configure different language models:
# Using OpenAI GPT
system = QueryHandlerSystem(model_name="gpt-3.5-turbo")
# Using Anthropic Claude
system = QueryHandlerSystem(model_name="claude-3-sonnet")
# Using local Ollama model
system = QueryHandlerSystem(model_name="ollama_chat/gemma2:2b")For advanced DSPy configuration:
import dspy
from auto_prompt_generation import QueryHandlerSystem
# Configure DSPy settings
dspy.settings.configure(
lm=dspy.OpenAI(model="gpt-4"),
rm=dspy.ColBERTv2(url='http://20.102.90.50:2017/wiki17_abstracts')
)
system = QueryHandlerSystem()Main class for processing queries and generating optimized prompts.
__init__(model_name: str = "ollama_chat/gemma2:2b"): Initialize the systemprocess_query(user_query: str) -> Dict: Process a query and return results
{
"original_query": str,
"analysis": {
"type": str, # Query type classification
"domain": str, # Domain/field
"complexity": str, # Complexity level
"expert_role": str # Generated expert role
},
"optimized_prompt": str # Final optimized prompt
}QueryClassifier: Classifies query type, domain, and complexityExpertPersonaGenerator: Generates appropriate expert personasPromptOptimizer: Creates optimized promptsDynamicQueryHandler: Main processing pipeline
# Clone the repository
git clone https://github.com/yourusername/auto-prompt-generation.git
cd auto-prompt-generation
# Install in development mode with dev dependencies
pip install -e .[dev]# Run all tests
pytest
# Run with coverage
pytest --cov=auto_prompt_generation# Format code with black
black auto_prompt_generation/
# Check with flake8
flake8 auto_prompt_generation/
# Type checking with mypy
mypy auto_prompt_generation/- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with DSPy - the framework for programming with foundation models
- Inspired by the need for better prompt engineering in AI applications
- Initial release
- Basic query classification and prompt optimization
- Command line interface
- Support for multiple language models through DSPy
If you encounter any issues or have questions:
- Check the Issues page
- Create a new issue with detailed information
- For general questions, start a Discussion