SoFunction
Updated on 2025-04-04

Guide to Integrating DeepSeek Mockup in VSCode

This article will provide two access solutions (direct debugging and API services) and include VSCode-specific configuration techniques.

1. Environmental preparation

1. Project structure configuration

deepseek-vscode/
├── models/            # Model file directory│   └── deepseek-7b-chat/
├── src/
│   ├──         # API service file│   └──      # Client Test Script├── .env              # Environment variables└──   # Dependency list

2. VSCode necessary extensions

  • Python extension (ID: )
  • Jupyter Notebook Support (ID: )
  • Docker support (ID: -docker)
  • Remote - SSH (Remote Development Scenario)

2. Basic access plan

Solution 1: Direct debugging (interactive development)

Createsrc/deepseek_demo.py

from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
 
MODEL_PATH = "./models/deepseek-7b-chat"
 
def load_model():
    tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
    model = AutoModelForCausalLM.from_pretrained(
        MODEL_PATH,
        device_map="auto",
        torch_dtype=torch.float16
    )
    return model, tokenizer
 
def generate_response(prompt):
    model, tokenizer = load_model()
    inputs = tokenizer.apply_chat_template(
        [{"role": "user", "content": prompt}],
        return_tensors="pt"
    ).to()
    
    outputs = (inputs, max_new_tokens=200)
    return (outputs[0], skip_special_tokens=True)
 
# Press F5 in VSCode to start debuggingif __name__ == "__main__":
    while True:
        query = input("User input:")
        print("DeepSeek:", generate_response(query))

Scenario 2: Create API Service

Createsrc/

from fastapi import FastAPI
from  import CORSMiddleware
from src.deepseek_demo import generate_response
import uvicorn
 
app = FastAPI()
 
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)
 
@("/chat")
async def chat(q: str):
    try:
        response = generate_response(q)
        return {"response": response}
    except Exception as e:
        return {"error": str(e)}
 
if __name__ == "__main__":
    (app, host="0.0.0.0", port=8000)

3. VSCode special configuration

1. Debug configuration file (.vscode/)

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Start API Service",
            "type": "python",
            "request": "launch",
            "program": "src/",
            "args": [],
            "env": {"PYTHONPATH": "${workspaceFolder}"}
        },
        {
            "name": "Interactive debugging",
            "type": "python",
            "request": "launch",
            "program": "src/deepseek_demo.py",
            "console": "integratedTerminal",
            "env": {"PYTHONPATH": "${workspaceFolder}"}
        }
    ]
}

2. Jupyter Notebook integration

  • New.ipynbdocument

  • Insert code block:

# %%
from src.deepseek_demo import generate_response
 
# Real-time test model responsedef test_model(prompt):
    response = generate_response(prompt)
    print(f"enter:{prompt}\nOutput:{response}")
 
test_model("Explanation of the basic principles of quantum computing")

4. Advanced debugging skills

1. GPU video memory monitoring

  • InstallNVIDIA GPU StatusExtension (ID: -gpu-status)

  • The bottom status bar is displayed in real time:

    • GPU utilization

    • Video memory usage

    • Temperature monitoring

2. Tensor visualization

Used during debuggingPython Debugger

  • Setting breakpoints in the generated code line

  • View the tensor structure in the Variables panel

  • Right-click Tensor and select "View Value in Data Viewer"

5. Optimized configuration guide

1. Workspace settings (.vscode/)

{
    "": ["./src"],
    "": "Pylance",
    "": true,
    "": true,
    "": true
}

2. Docker container development

CreateDockerfile

FROM nvidia/cuda:12.2.0-base
 
WORKDIR /app
COPY . .
 
RUN apt-get update && \
    apt-get install -y python3.10 python3-pip && \
    pip install -r 
 
CMD ["python3", "src/"]

useDev ContainersExtend to realize one-click containerized development.

6. Frequently Asked Questions

Problem phenomenon Solution
Module import error exist.envFile AddPYTHONPATH=/path/to/project-root
CUDA version mismatch Create an isolated environment using VSCode's Dev Container feature
Long text generation stutter InstallTransformer TokensExtended real-time monitoring of token consumption
Chinese display garbled code Set terminal encoding:"": "Command Prompt"

7. Recommended workflow

  1. Development stage: Quickly verify prompt with Jupyter Notebook

  2. Debugging stage: Analyze tensor data through Python Debugger

  3. Testing phase: Send API requests using REST Client extension

  4. Deployment phase: Build production images through Docker extension

Performance test example (VSCode terminal):

# Start the stress testpython -m src.stress_test --threads 4 --requests 100

Recommended extension combination:

  1. Code Runner- Quickly execute code snippets

  2. GitLens- Version control integration

  3. Remote Explorer- Manage remote development servers

  4. Tabnine- AI code completion assistance

Through the above configuration, it can be implemented in VSCode:

  • Start the model service with one click

  • Real-time GPU resource monitoring

  • Interactive Prompt Test

  • Production-level API deployment

This is the article about the practical guide to integrating DeepSeek models in VSCode. For more related content related to VSCode integration DeepSeek, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!