Transform your ideas and prompts into DSPy signatures using DSPy Signature!
This is an experimental approach for DSPy framework.
- Natural Language to DSPy Signature: Convert ideas like "Translate English to German" or "Count people on the image" into a full
dspy.Signature. - Dynamic Class Factory: Generate and use
dspy.Signatureclasses at runtime. - Interactive Refinement: Iteratively improve generated signatures by providing feedback in a loop.
- CLI Interface: command-line interface built with
TyperandRich.
- Clone the repository:
git clone https://github.com/Archelunch/vibe-dspy cd vibe-dspy - Install the required dependencies:
pip install -r requirements.txt
Before using the generator, you need to configure your API key for the language model.
Set it as an environment variable:
export GEMINI_API_KEY="your-api-key-here"The following examples are taken from the Examples.ipynb notebook.
You can generate the Python source code for a signature directly from a prompt.
import dspy
from src.signature_generator import SignatureGenerator
# Configure your LM (e.g., Gemini)
# dspy.settings.configure(lm=...)
generator = SignatureGenerator()
result = generator(prompt="Generate jokes by prompt")
# Print the generated Python code
print(generator.generate_code(result))Output:
import dspy
class JokeGenerator(dspy.Signature):
"""Generates a joke based on a given prompt or topic."""
topic: str = dspy.InputField(desc="The topic or theme for the joke.")
joke: str = dspy.OutputField(desc="The generated joke.")We can also create a dspy.Signature class dynamically at runtime.
# Use the result from the previous step
JokeGeneratorSignature = SignatureGenerator.create_signature_class(result)
# You now have a real, usable class
print(JokeGeneratorSignature)
print(JokeGeneratorSignature.__doc__)Output:
JokeGenerator(topic -> joke
instructions='Generates a joke based on a given prompt or topic.'
topic = Field(annotation=str required=True ... )
joke = Field(annotation=str required=True ... )
)
Generates a joke based on a given prompt or topic.
This generated class can be used immediately.
joker = dspy.Predict(JokeGeneratorSignature)
# Get a prediction
response = joker(topic="clowns")
print(response.joke)You can also refine a generated signature by providing feedback in a loop until you're satisfied with the result.
def user_feedback_validator(args, pred):
"""A simple validation function that asks for user feedback."""
print(SignatureGenerator.generate_code(pred))
is_ok = input("Is the signature correct? (Y/n): ")
if is_ok.lower() == "n":
feedback = input("What should be changed? ")
return dspy.Prediction(score=0.0, feedback=feedback)
return 1.0
# Set up the Refine module to try up to 3 times
refiner = dspy.Refine(
module=generator,
N=3,
reward_fn=user_feedback_validator,
threshold=1.0
)
# Run the refinement process
final_result = refiner(prompt="Count birds in the image")
print("\\n--- Final Approved Signature ---")
print(SignatureGenerator.generate_code(final_result))This tool also includes a full command-line interface.
Generate a single signature:
python -m src.main generate "Count birds in an image" --output-file bird_counter.pyInteractive mode:
python -m src.main interactiveRefine a signature with feedback:
python -m src.main refine "Analyze customer reviews" --attempts 5.
├── src/
│ ├── signature_generator.py # Core logic and dynamic class factory
│ ├── cli.py # CLI interface
│ ├── utils.py # Utility functions
│ └── main.py # Entry point for the CLI
├── Examples.ipynb # Jupyter notebook with usage examples
├── requirements.txt
└── README.md
I'm not sure about future of this project. It was small and fun idea to do. But let me know if you are interested in this project and want to see more.
- Tests?
- Web-interface?
- MCP-server ???
- VS Code extension???
- Complex data schema with set of pydantic classes
- Optimisation experiments with MIPRO or GEPA
This project is licensed under the BSD 3-Clause License. See the LICENSE file for details.
- Author: Michael Pavlukhin
- Email: [email protected]
- X/Twitter: @mike_pavlukhin