Ollama AI - VS Code Extension

AI-powered coding assistant with built-in Ollama server support. Generate code, explain code, refactor, and chat with AI models like Mistral, Llama 2, and more.
🚀 Features
- 🤖 AI Code Generation: Generate code from natural language descriptions
- 💬 Interactive Chat: Chat with AI models directly in VS Code
- 🔧 Code Explanation: Get detailed explanations of your code
- 🔄 Code Refactoring: Automatically refactor and improve your code
- 🐛 Code Fixing: Identify and fix issues in your code
- ⚡ Built-in Server: No need to install Ollama separately
- 🎯 Multiple Models: Support for Mistral, Llama 2, Code Llama, and more
- 📁 File Creation: Generate complete files with AI assistance
🎯 Quick Start
Install the Extension
- Search for "Ollama AI" in VS Code Extensions
- Click Install
Enable Built-in Server (Optional)
{
"ollama.useBuiltInServer": true
}
Start Chatting
- Press
Ctrl+Shift+P
and run "Ollama: Open Chat Panel"
- Ask the AI to help with your code!
📋 Commands
Command |
Description |
Shortcut |
ollama.startModel |
Start an Ollama model |
- |
ollama.stopModel |
Stop the current model |
- |
ollama.chat |
Quick chat with the model |
- |
ollama.generateCode |
Generate code from description |
- |
ollama.explainCode |
Explain selected code |
- |
ollama.refactorCode |
Refactor selected code |
- |
ollama.fixCode |
Fix issues in selected code |
- |
ollama.openChatPanel |
Open the chat panel (default: right panel) |
- |
ollama.openChatPanelRight |
Open the chat panel in the rightmost panel |
- |
ollama.openChatPanelBeside |
Open the chat panel beside the current editor |
- |
ollama.pullModel |
Pull a model from Ollama |
- |
ollama.listModels |
List available models |
- |
ollama.checkModelStatus |
Check if a model is currently running |
- |
ollama.startBuiltInServer |
Start the built-in Ollama server |
- |
ollama.stopBuiltInServer |
Stop the built-in Ollama server |
- |
ollama.checkServerStatus |
Check if any Ollama server is running |
- |
⚙️ Configuration
Add these settings to your VS Code settings:
{
"ollama.serverUrl": "http://localhost:11434",
"ollama.defaultModel": "mistral",
"ollama.maxTokens": 2048,
"ollama.temperature": 0.7,
"ollama.autoStart": true,
"ollama.autoOpenChat": true,
"ollama.keepModelRunningOnBlur": true,
"ollama.stopModelOnPanelClose": false,
"ollama.defaultChatPanelLocation": "right",
"ollama.openFileInNewTab": true,
"ollama.openFileAfterCreation": true,
"ollama.refocusChatAfterFileCreation": false,
"ollama.useBuiltInServer": false
}
Settings Explained
ollama.serverUrl
: URL of your Ollama server (default: http://localhost:11434
)
ollama.defaultModel
: Default model to use (default: mistral
)
ollama.maxTokens
: Maximum tokens for responses (default: 2048
)
ollama.temperature
: Temperature for model responses, 0.0-1.0 (default: 0.7
)
ollama.autoStart
: Automatically start the default model when VS Code starts (default: true
)
ollama.autoOpenChat
: Automatically open the chat panel in the right panel when VS Code starts (default: true
)
ollama.keepModelRunningOnBlur
: Keep the model running when the chat panel loses focus (default: true
)
ollama.stopModelOnPanelClose
: Stop the model when the chat panel is closed (default: false
)
ollama.defaultChatPanelLocation
: Default location for the chat panel (default: "right"
, options: "left"
, "center"
, "right"
, "beside"
)
ollama.openFileInNewTab
: Open created files in a new tab instead of replacing the current editor (default: true
)
ollama.openFileAfterCreation
: Automatically open files after they are created by the AI (default: true
)
ollama.refocusChatAfterFileCreation
: Refocus the chat panel after creating a file (useful to maintain chat history) (default: false
)
ollama.useBuiltInServer
: Use built-in Ollama server instead of external server (default: false
)
Note: The chat panel now opens in the right panel by default. You can change this behavior using the ollama.defaultChatPanelLocation
setting.
🤖 Supported Models
The extension works with any model available in Ollama. Popular models include:
- Mistral: High-performance open model (default)
- GPT-OSS: Open-source GPT model
- Llama 2: Meta's large language model
- Code Llama: Specialized for code generation
- Phi-2: Microsoft's small but powerful model
- Gemma: Google's open model
🔧 Built-in Ollama Server
The extension now supports a built-in Ollama server, eliminating the need to install and run Ollama separately.
Features
- ✅ Automatic Server Management: Starts and stops the server automatically
- ✅ Cross-Platform Support: Works on Windows, macOS, and Linux
- ✅ Fallback Support: Falls back to external server if built-in fails
- ✅ Easy Configuration: Simple toggle in settings
Setup
Enable Built-in Server:
{
"ollama.useBuiltInServer": true
}
Install Ollama Binary (if not included in extension):
- Download Ollama from ollama.ai
- Place the binary in the extension's
bin
directory
- Or use the external server option
Start the Server:
- Use command:
Ollama: Start Built-in Server
- Or let the extension start it automatically
Commands
ollama.startBuiltInServer
: Start the built-in server
ollama.stopBuiltInServer
: Stop the built-in server
ollama.checkServerStatus
: Check server status
Benefits
- No Manual Installation: No need to install Ollama separately
- Automatic Management: Server starts/stops with VS Code
- Isolated Environment: Doesn't interfere with system Ollama
- Easy Setup: One-click server management
🎯 Usage Examples
Code Generation
- Right-click in the editor
- Select "Ollama: Generate Code"
- Describe what you want to create
- AI generates the code for you
Code Explanation
- Select code in the editor
- Right-click and select "Ollama: Explain Code"
- Get detailed explanations
Interactive Chat
- Open the chat panel
- Ask questions about your code
- Get AI assistance in real-time
File Creation
In the chat panel, try:
- "Write a hello world in typescript in main.ts"
- "Create a React component in App.jsx"
- "Generate a Python function in utils.py"
🛠️ Troubleshooting
Model Not Found
If you get a "Model not found" error:
- Make sure Ollama is running:
ollama serve
- Pull the model:
ollama pull <model-name>
- Check available models:
ollama list
Connection Issues
If you can't connect to Ollama:
- Verify Ollama is running:
ollama serve
- Check the server URL in settings
- Ensure no firewall is blocking port 11434
- Use smaller models for faster responses
- Reduce
maxTokens
setting for shorter responses
- Lower
temperature
for more focused outputs
Model Stops When Panel Loses Focus
If the model stops running when you switch to another tab or panel:
- Check the
ollama.keepModelRunningOnBlur
setting (should be true
by default)
- The model will now stay running when the chat panel loses focus
- You can manually stop the model using the "Stop Model" button or command
- If you want the model to stop when the panel is closed, set
ollama.stopModelOnPanelClose
to true
Chat History Lost When Creating Files
If the chat history is lost when the AI creates files:
- Set
ollama.openFileInNewTab
to true
(default) to open files in separate tabs
- Set
ollama.refocusChatAfterFileCreation
to true
to automatically return to the chat panel
- The chat panel will maintain its history and position
- Files will open in new editor tabs without affecting the chat interface
🚀 Development
Building the Extension
# Install dependencies
npm install
# Compile TypeScript
npm run compile
# Watch for changes
npm run watch
# Run tests
npm test
Project Structure
src/
├── extension.ts # Main extension entry point
├── ollamaClient.ts # Ollama API client
├── ollamaServer.ts # Built-in server management
├── chatPanel.ts # Chat interface
└── codeGenerator.ts # Code generation utilities
🤝 Contributing
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
📄 License
MIT License - see LICENSE file for details
🆘 Support
- Issues: Report bugs and request features on GitHub
- Documentation: Check the README and inline comments
- Community: Join the VS Code extension community
🙏 Acknowledgments
- Ollama for providing the AI models
- VS Code for the excellent extension platform
- The open-source community for inspiration and contributions
Made with ❤️ for the developer community