Systems Library / AI Model Setup / How to Set Up DeepSeek API for Code Generation
AI Model Setup foundations

How to Set Up DeepSeek API for Code Generation

Connect DeepSeek code-specialized models for automated programming tasks.

Jay Banlasan

Jay Banlasan

The AI Systems Guy

DeepSeek api code generation setup gets you a model trained specifically on code, at a fraction of the cost of GPT-4. I route code generation tasks to DeepSeek in production systems because it is cheaper for tasks like writing boilerplate, generating unit tests, and converting data formats. DeepSeek-Coder scores near the top on HumanEval benchmarks and costs significantly less per token than comparable general-purpose models.

The API is OpenAI-compatible, which means if you already have code calling OpenAI, switching to DeepSeek is changing one line. That compatibility also means all the same patterns for streaming, function calling, and multi-turn conversations work without modification.

What You Need Before Starting

Step 1: Make Your First DeepSeek API Call

DeepSeek uses an OpenAI-compatible endpoint. Point the client at their base URL.

import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["DEEPSEEK_API_KEY"],
    base_url="https://api.deepseek.com"
)

def generate_code(prompt: str, model: str = "deepseek-coder") -> str:
    response = client.chat.completions.create(
        model=model,
        messages=[
            {
                "role": "system",
                "content": "You are an expert programmer. Write clean, well-commented code. Return only the code, no explanation unless asked."
            },
            {
                "role": "user",
                "content": prompt
            }
        ],
        max_tokens=2000,
        temperature=0.1  # Low temperature for code generation - less creative variation
    )
    return response.choices[0].message.content

# Test
code = generate_code("Write a Python function that takes a list of dicts and returns them sorted by a specified key, with null values last.")
print(code)

Use temperature=0.1 for code tasks. Higher temperatures introduce unnecessary variation and can change logic in ways that break tests.

Step 2: Build a Code-Specific System Prompt

General instructions produce inconsistent results. A precise system prompt gets you consistent output format every time.

CODE_SYSTEM_PROMPT = """You are a senior Python developer producing production-ready code.

Rules:
- Return ONLY code unless asked for explanation
- Include type hints on all function signatures
- Add a docstring to every function
- Handle edge cases (empty input, None values, type mismatches)
- Prefer standard library over third-party when equivalent
- Write code that passes flake8 linting
- Use snake_case for variables and functions, PascalCase for classes

When generating tests:
- Use pytest
- Test happy path, edge cases, and error cases
- Use descriptive test names that read as requirements"""

def generate_production_code(task: str) -> str:
    response = client.chat.completions.create(
        model="deepseek-coder",
        messages=[
            {"role": "system", "content": CODE_SYSTEM_PROMPT},
            {"role": "user", "content": task}
        ],
        max_tokens=3000,
        temperature=0.1
    )
    return response.choices[0].message.content

Step 3: Build a Code Extraction Utility

DeepSeek sometimes returns code wrapped in markdown code blocks. Strip them reliably.

import re

def extract_code_blocks(response: str) -> list[str]:
    """Extract all code blocks from a markdown response."""
    pattern = r'```(?:\w+)?\n(.*?)```'
    blocks = re.findall(pattern, response, re.DOTALL)
    return [block.strip() for block in blocks]

def extract_primary_code(response: str) -> str:
    """Get the main code block, or the full response if no blocks."""
    blocks = extract_code_blocks(response)
    if blocks:
        return blocks[0]  # Return first block
    # If no markdown blocks, return as-is (clean code output)
    return response.strip()

# Usage
raw_response = generate_production_code("Write a function to validate email addresses")
code = extract_primary_code(raw_response)
print(code)

Step 4: Build Common Code Generation Tasks

Real-world code generation follows repeatable patterns. Build functions for each task type.

def generate_unit_tests(code: str, test_framework: str = "pytest") -> str:
    task = f"""Write comprehensive {test_framework} unit tests for this code:

```python
{code}

Include:

def convert_code_language(code: str, from_lang: str, to_lang: str) -> str: task = f"""Convert this {from_lang} code to {to_lang}. Keep the same logic and structure.

{code}
```"""

    response = client.chat.completions.create(
        model="deepseek-coder",
        messages=[
            {"role": "system", "content": f"Convert code from {from_lang} to {to_lang}. Return only the converted code."},
            {"role": "user", "content": task}
        ],
        max_tokens=3000,
        temperature=0.1
    )
    return extract_primary_code(response.choices[0].message.content)

def add_docstrings(code: str) -> str:
    task = f"""Add comprehensive docstrings to all functions and classes in this code.
Keep all existing logic unchanged.

```python
{code}
```"""
    return generate_production_code(task)

def generate_sql_from_description(description: str, schema: str = "") -> str:
    schema_context = f"\nDatabase schema:\n{schema}" if schema else ""
    task = f"""Write SQL for this requirement: {description}{schema_context}

Return only the SQL query, no explanation."""

    response = client.chat.completions.create(
        model="deepseek-coder",
        messages=[
            {"role": "system", "content": "You write clean, optimized SQL. Return only the SQL query."},
            {"role": "user", "content": task}
        ],
        max_tokens=1000,
        temperature=0.0
    )
    return response.choices[0].message.content.strip()

Step 5: Add Code Review Functionality

DeepSeek can review code for bugs, security issues, and style problems.

def review_code(code: str, review_type: str = "all") -> dict:
    review_prompts = {
        "security": "Review this code for security vulnerabilities. List each issue with severity (critical/high/medium/low) and how to fix it.",
        "performance": "Review this code for performance issues. List bottlenecks and how to optimize them.",
        "bugs": "Find bugs and logic errors in this code. For each one, explain what is wrong and the correct fix.",
        "all": "Review this code for: (1) security vulnerabilities, (2) bugs and logic errors, (3) performance issues, (4) code quality. Format as JSON with keys: security, bugs, performance, quality. Each key holds a list of {issue, severity, fix}."
    }

    prompt = review_prompts.get(review_type, review_prompts["all"])

    response = client.chat.completions.create(
        model="deepseek-coder",
        messages=[
            {"role": "system", "content": "You are a senior code reviewer. Be specific and actionable."},
            {"role": "user", "content": f"{prompt}\n\n```python\n{code}\n```"}
        ],
        max_tokens=2000,
        temperature=0.1
    )

    raw_review = response.choices[0].message.content

    if review_type == "all":
        try:
            import json
            # Extract JSON if wrapped in code block
            json_text = extract_primary_code(raw_review)
            return json.loads(json_text)
        except (json.JSONDecodeError, Exception):
            return {"raw_review": raw_review}

    return {"review": raw_review}

Step 6: Build a Simple Code Generation Pipeline

Wire the pieces into a complete pipeline: generate, test, review.

def code_generation_pipeline(task_description: str) -> dict:
    print(f"Generating code for: {task_description[:60]}...")
    code = generate_production_code(task_description)
    clean_code = extract_primary_code(code)

    print("Generating tests...")
    tests = generate_unit_tests(clean_code)
    clean_tests = extract_primary_code(tests)

    print("Running code review...")
    review = review_code(clean_code, review_type="bugs")

    return {
        "code": clean_code,
        "tests": clean_tests,
        "review": review
    }

# Example
result = code_generation_pipeline(
    "Write a function that parses CSV data from a URL, validates required columns exist, and returns a list of dicts"
)

print("\n=== GENERATED CODE ===")
print(result["code"])
print("\n=== TESTS ===")
print(result["tests"])
print("\n=== REVIEW ===")
print(result["review"])

What to Build Next

Related Reading

Want this system built for your business?

Get a free assessment. We will map every system your business needs and show you the ROI.

Get Your Free Assessment

Related Systems