Back to .md Directory

Create model directory

Here's a complete **end-to-end guide** to create your local AI coding assistant with DeepSeek integration:

May 2, 2026
0 downloads
1 views
ai agent llm prompt
View source

Here's a complete end-to-end guide to create your local AI coding assistant with DeepSeek integration:


Step 1: System Preparation

Hardware Requirements:

  • Minimum: 16GB RAM + NVIDIA GPU (8GB VRAM)
  • Recommended: 32GB RAM + NVIDIA RTX 3090/4090 (24GB VRAM)

Software Requirements:

  • VS Code
  • Docker Desktop
  • NVIDIA Container Toolkit
  • Python 3.10+

Step 2: Model Setup

  1. Download Models:
# Create model directory
mkdir -p ~/ai_models/deepseek && cd ~/ai_models

# Download DeepSeek-Coder (7B quantized GGUF version)
wget https://huggingface.co/TheBloke/deepseek-coder-6.7B-instruct-GGUF/resolve/main/deepseek-coder-6.7b-instruct.Q5_K_M.gguf
  1. Get Access Tokens:

Step 3: Docker Setup

  1. Create Project Structure:
ai-coding-agent/
├── docker/
│   ├── Dockerfile
│   └── requirements.txt
├── app/
│   ├── server.py
│   └── agents/
├── vscode-extension/
│   └── extension.js
└── docker-compose.yml
  1. docker/Dockerfile:
FROM nvidia/cuda:12.2.0-base-ubuntu22.04

RUN apt-get update && apt-get install -y \
    python3.10 \
    python3-pip \
    git \
    wget \
    && rm -rf /var/lib/apt/lists/*

COPY docker/requirements.txt .
RUN pip install -r requirements.txt

WORKDIR /app
COPY . /app

CMD ["python3", "app/server.py"]
  1. docker/requirements.txt:
fastapi>=0.104.0
uvicorn>=0.24.0
llama-cpp-python>=0.2.23
langchain>=0.1.0
transformers>=4.38.0
python-socketio>=5.11.0
watchdog>=3.0.0

Step 4: AI Server Implementation

app/server.py:

from fastapi import FastAPI, WebSocket
from llama_cpp import Llama
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import uvicorn

app = FastAPI()

# Load DeepSeek model
llm = Llama(
    model_path="/app/models/deepseek-coder-6.7b-instruct.Q5_K_M.gguf",
    n_ctx=4096,
    n_gpu_layers=35,
    n_threads=8
)

class CodeChangeHandler(FileSystemEventHandler):
    def on_modified(self, event):
        if not event.is_directory and event.src_path.endswith(".py"):
            with open(event.src_path, "r") as f:
                code = f.read()
                analysis = analyze_code(code)
                # Send to all connected VS Code instances
                manager.broadcast(analysis)

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    while True:
        code = await websocket.receive_text()
        response = llm.create_chat_completion(
            messages=[{"role": "user", "content": f"Analyze and debug:\n{code}"}],
            temperature=0.3
        )
        await websocket.send_text(response['choices'][0]['message']['content'])

def analyze_code(code: str) -> str:
    prompt = f"""As an expert code analyst, perform these tasks:
    1. Static code analysis
    2. Potential bug detection
    3. Security vulnerability check
    4. Performance optimization suggestions
    
    Code:
    {code}
    
    Provide structured response in Markdown:"""
    
    return llm(prompt, max_tokens=512)

if __name__ == "__main__":
    # Start file watcher
    event_handler = CodeChangeHandler()
    observer = Observer()
    observer.schedule(event_handler, path=".", recursive=True)
    observer.start()
    
    # Start server
    uvicorn.run(app, host="0.0.0.0", port=8000)

Step 5: VS Code Extension

vscode-extension/extension.js:

const vscode = require('vscode');
const WebSocket = require('ws');

function activate(context) {
    const ws = new WebSocket('ws://localhost:8000/ws');
    const diagnostics = vscode.languages.createDiagnosticCollection('ai-analysis');

    // Real-time code analysis
    vscode.workspace.onDidChangeTextDocument(e => {
        const code = e.document.getText();
        ws.send(code);
    });

    // Handle AI responses
    ws.on('message', data => {
        const analysis = JSON.parse(data);
        const uri = vscode.window.activeTextEditor.document.uri;
        
        // Show diagnostics
        diagnostics.set(uri, parseDiagnostics(analysis));
        
        // Show suggestions
        vscode.window.showInformationMessage(
            analysis.summary, 
            ...analysis.suggestions
        );
    });

    // Debugger integration
    vscode.debug.registerDebugAdapterTrackerFactory('*', {
        createDebugAdapterTracker(session) {
            return {
                onDidSendMessage: m => {
                    if (m.event === 'stopped') {
                        ws.send(JSON.stringify({
                            type: 'debug',
                            data: m.body
                        }));
                    }
                }
            };
        }
    });
}

function parseDiagnostics(analysis) {
    return analysis.issues.map(issue => {
        const range = new vscode.Range(
            issue.line-1, 0, 
            issue.line-1, Number.MAX_VALUE
        );
        return new vscode.Diagnostic(
            range, 
            issue.message, 
            vscode.DiagnosticSeverity.Warning
        );
    });
}

exports.activate = activate;

Step 6: Build & Run

  1. Start AI Server:
docker-compose up --build
  1. Install VS Code Extension:
  • Package the extension: vsce package
  • Install the .vsix file in VS Code
  1. Configure Workspace:
// .vscode/settings.json
{
    "ai-agent.modelPath": "~/ai_models/deepseek",
    "ai-agent.enableRealTimeAnalysis": true,
    "ai-agent.suggestionLevel": "advanced"
}

Step 7: Usage Workflow

  1. Real-Time Analysis:
  • Open any code file
  • AI automatically analyzes on save/change
  • Suggestions appear as:
    • Inline diagnostics
    • Quick-fix lightbulbs
    • Code completion suggestions
  1. Debug Integration:
  • Start debugging session
  • AI automatically:
    • Analyzes stack traces
    • Suggests potential fixes
    • Recommends test cases
  1. Manual Commands:
  • AI: Full Codebase Analysis (Ctrl+Shift+A)
  • AI: Optimize Current File (Ctrl+Shift+O)
  • AI: Security Audit (Ctrl+Shift+S)

Step 8: Advanced Configuration

  1. Model Switching:
# In server.py
MODELS = {
    'deepseek-7b': 'deepseek-coder-6.7b-instruct.Q5_K_M.gguf',
    'llama2-13b': 'llama-2-13b-chat.Q4_K_M.gguf'
}

def switch_model(model_name):
    global llm
    llm = Llama(
        model_path=f"/app/models/{MODELS[model_name]}",
        n_gpu_layers=35
    )
  1. Custom Prompts:
# Create prompts.yaml
analysis_prompt: |
  You are Senior Software Engineer analyzing {language} code.
  Perform these tasks:
  1. Identify {level} issues
  2. Suggest optimizations
  3. Security audit
  4. Provide code examples
  
  Code:
  {code}

Step 9: Security Considerations

  1. Local Network Only:
# In server.py
app = FastAPI(servers=[{"url": "http://localhost:8000"}])
  1. Model Sandboxing:
# Add to Dockerfile
RUN useradd -m aiuser && chown -R aiuser:aiuser /app
USER aiuser

Final Step: Test Your Setup

  1. Validation Tests:
# Test API endpoint
curl -X POST http://localhost:8000/analyze -d 'print("test")'

# Check GPU utilization
docker exec -it ai-container nvidia-smi

# Test WebSocket connection
wscat -c ws://localhost:8000/ws
  1. VS Code Test Cases:
  • Introduce intentional bug in code
  • Verify AI detection within 2-3 seconds
  • Test debug session breakpoints
  • Verify code completion suggestions

This setup provides a complete local AI development environment with:

  • Real-time code analysis
  • Intelligent debugging assistance
  • Security vulnerability detection
  • Performance optimization suggestions
  • Full codebase understanding
  • Context-aware completions
  • Multi-model support

Let me know if you need any specific component explained in more detail!

Related Documents