Interactive Mode
Master the DSPy Code interactive environment for efficient DSPy development.
What is Interactive Mode?
Interactive Mode is the primary interface for DSPy Code. It provides:
- Natural language code generation
- Conversational workflow
- Context-aware responses
- Session persistence
- Real-time validation
Start interactive mode:
The Welcome Screen
When you start DSPy Code, you see:
â DSPy Version: 3.0.4
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â â
â âââââââ âââââââââââââââ âââ âââ ââââââââââ âââ â
â ââââââââââââââââââââââââââââ ââââ âââââââââââ âââ â
â âââ âââââââââââââââââââ âââââââ âââ âââ âââ â
â âââ ââââââââââââââââââ âââââ âââ âââ âââ â
â âââââââââââââââââââ âââ âââââââââââââââââââ â
â âââââââ âââââââââââ âââ ââââââââââââââââââ â
â â
ââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
⨠Your AI-Powered DSPy Development Assistant: Claude Code for DSPy â¨
⥠Get Started:
/intro - Complete guide & all features
/init - Initialize your project
/demo - See it in action
/help - View all commands
đŦ Or just describe what you want to build in natural language!
đĄ New here? Try /demo or /init to get started
đ Ready! New here? Try /intro for the complete guide!
đ Try: "Create a signature for..." or "Build a module using..." or "/help" for
commands
âââââââââââââââââââââââââââââââ ⨠Your Message ââââââââââââââââââââââââââââââââŽ
â Type your request here... (or /help for commands) â
â°âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¯
â
Key information displayed:
- â Your DSPy version
- â Quick start commands
- â Usage hints
- â Input prompt
Basic Usage
Natural Language Requests
Simply describe what you want:
DSPy Code understands your intent and generates appropriate code!
Slash Commands
Use / prefix for specific commands:
Mixed Interaction
Combine natural language and commands:
â Create a sentiment analyzer
[Code generated]
â /save sentiment.py
â Saved!
â Add error handling to it
[Code updated]
â /validate
â Valid!
â /run
[Execution results]
Session Context
What's in Context?
The session maintains:
1. Generated Code:
2. Generated Data:
3. Conversation History:
context['history'] = [
{'role': 'user', 'content': '...'},
{'role': 'assistant', 'content': '...'}
]
4. Model Connection:
Check Context
Use /status to see what's in context:
Output:
Current Session Status:
â Generated Code: Module (1,234 chars)
Use /save <filename>, /validate, /run
â No Generated Data
Try: "Generate 20 examples for sentiment analysis"
â Model Connected: llama3.1:8b (ollama)
â Project Initialized: my-dspy-project
Use /init to re-index or update
â Codebase Index Loaded: 3,421 elements
Clear Context
Clear conversation history:
What's cleared:
- Conversation history
- Generated code
- Generated data
What's kept:
- Model connection
- Project configuration
- Codebase index
Conversation Flow
Typical Workflow
1. Initialize project:
2. Connect model:
3. Generate code:
4. Save code:
5. Validate:
6. Test:
7. Iterate:
Iterative Development
Refine code through conversation:
â Create a sentiment analyzer
[Initial code]
â Use ChainOfThought instead of Predict
[Updated with ChainOfThought]
â Add a confidence score output field
[Added confidence field]
â Add error handling for empty input
[Added validation]
â /save sentiment_analyzer.py
â Saved!
Multi-Step Tasks
Break complex tasks into steps:
â I want to build a RAG system
Great! Let's break this down:
1. Create retrieval signature
2. Create generation signature
3. Build RAG module
4. Add example usage
Ready to start?
â Yes, start with step 1
[Retrieval signature generated]
â Looks good, next step
[Generation signature generated]
â Continue
[RAG module generated]
â /save rag_system.py
â Complete RAG system saved!
Advanced Features
Code Refinement
Ask for specific improvements:
â Optimize the last generated code for performance
â Add type hints to all functions
â Add docstrings following Google style
â Refactor to use fewer API calls
â Make it more modular
Code Explanation
Ask questions about generated code:
â Explain how the ChainOfThought works in this code
â What does the forward method do?
â Why did you use this signature structure?
DSPy Concepts
Learn while you code:
â What's the difference between Predict and ChainOfThought?
â When should I use ReAct?
â How do I optimize this module?
â What are best practices for signatures?
Project-Specific Questions
Ask about your codebase:
â What modules do I have in my project?
â Show me my existing signatures
â How does my RAG module work?
â What's the structure of my project?
Session Management
Save Sessions
Save your work:
What's saved:
- Generated code
- Training data
- Conversation history
- Model configuration
- Project context
Load Sessions
Resume previous work:
What's restored:
- All generated code
- Training data
- Conversation history
- Model connection
- Project state
List Sessions
See all saved sessions:
Output:
Saved Sessions:
1. my-rag-project
Created: 2025-01-15 14:30
Last modified: 2025-01-15 16:45
Size: 45 KB
2. sentiment-analyzer
Created: 2025-01-14 10:20
Last modified: 2025-01-14 12:30
Size: 23 KB
3. email-classifier
Created: 2025-01-13 09:15
Last modified: 2025-01-13 11:00
Size: 31 KB
Delete Sessions
Remove old sessions:
Tips and Tricks
1. Use Tab Completion
Press Tab to autocomplete:
- Slash commands
- File names
- Model names
- Server names
2. Use History
Press â to cycle through previous commands.
3. Multi-Line Input
For long requests, use natural line breaks:
â Create a module that:
- Takes a question and context
- Uses ChainOfThought for reasoning
- Returns an answer with confidence
- Handles edge cases
4. Quick Edits
Reference previous code:
â Create a classifier
[Code generated]
â Change the predictor to ReAct
[Updated]
â Add another output field called "reasoning"
[Updated]
5. Batch Operations
Do multiple things at once:
â Create a sentiment analyzer, validate it, and save it as sentiment.py
[Generated, validated, saved]
6. Use Examples
Provide examples for better results:
â Create a classifier that categorizes:
- "I love this!" â positive
- "This is terrible" â negative
- "It's okay" â neutral
7. Ask for Alternatives
Get different approaches:
â Show me 3 different ways to implement this
â What's a simpler version?
â What's a more advanced version?
Keyboard Shortcuts
| Shortcut | Action |
|---|---|
Ctrl+C | Cancel current operation |
Ctrl+D | Exit DSPy Code |
â / â | Navigate history |
Tab | Autocomplete |
Ctrl+L | Clear screen |
Visual Feedback
Progress Indicators
Watch for visual feedback:
Thinking:
Success:
Error:
Warning:
Code Display
Generated code is beautifully formatted:
âââââââââââââââââââââââââââ Generated DSPy Module âââââââââââââââââââââââââââŽ
â import dspy â
â â
â class SentimentSignature(dspy.Signature): â
â """Analyze sentiment of text.""" â
â text = dspy.InputField(desc="Text to analyze") â
â sentiment = dspy.OutputField(desc="positive, negative, or neutral") â
â â
â class SentimentAnalyzer(dspy.Module): â
â def __init__(self): â
â super().__init__() â
â self.predictor = dspy.Predict(SentimentSignature) â
â â
â def forward(self, text): â
â return self.predictor(text=text) â
â°âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ¯
Next Steps
After generation, see suggested actions:
Next Steps:
âĸ Type /save <filename> to save this code
âĸ Type /validate to check for errors
âĸ Type /run to test execution
âĸ Type /status to see what's in context
âĸ Ask me to create a complete program
âĸ Request optimizations or improvements
Common Patterns
Pattern 1: Quick Prototype
Pattern 2: Iterative Development
â Create a basic [task] module
â Add [feature]
â Improve [aspect]
â /validate
â /save module.py
Pattern 3: Full Workflow
â /init
â /connect [model]
â Create [program]
â /save program.py
â Generate 50 examples for [task]
â /save-data examples.jsonl
â /optimize program.py examples.jsonl
Pattern 4: Learning Mode
â What is [concept]?
â Show me an example of [feature]
â Create a simple [task] using [technique]
â Explain how this works
Troubleshooting
Command Not Working
Check spelling:
Use /help to see all commands
Code Not Saving
Check context:
Generate code first if context is empty
Model Not Responding
Check connection:
Reconnect if needed:
Unexpected Behavior
Clear context and restart:
Or exit and restart:
Best Practices
1. Start with /init
Always initialize your project:
This builds the codebase index!
2. Check /status Often
Know what's in context:
3. Save Frequently
Don't lose your work:
4. Validate Before Running
Catch errors early:
5. Be Specific
Better requests = better code:
Good: "Create a ChainOfThought module for sentiment analysis with text input and sentiment output"
Bad: "Make sentiment thing"
6. Iterate
Refine through conversation:
7. Use Natural Language
Don't overthink it:
Summary
Interactive Mode provides:
- â Natural language interface
- â Context-aware responses
- â Session persistence
- â Real-time validation
- â Iterative development
- â Learning support
Master these concepts:
- Session context
- Slash commands
- Natural language requests
- Iterative refinement
- Session management