Code Generation with LLMs
Code Generation Capabilities Modern LLMs can generate, complete, explain, and refactor code across dozens of programming languages.
Generation Approaches
Direct Generation
def generate_code(description: str, language: str) -> str:
return llm.generate(f"""
Write {language} code that does the following:
{description}
Only output the code, no explanations.
```{language}
""")
Fill-in-the-Middle (FIM) Complete code with context before and after:
prefix = "def fibonacci(n):
if n <= 1:
return n
"
suffix = "
return fib(n-1) + fib(n-2)"
completion = llm.complete(prefix + "<FILL>" + suffix)
# Returns: " fib = fibonacci"
Function from Docstring
def implement_from_docstring(docstring: str) -> str:
return llm.generate(f"""
Implement this Python function:
{docstring}
Implementation:
""")
Code Assistants
| Tool | Features |
|---|---|
| GitHub Copilot | IDE integration, completions |
| Cursor | AI-first IDE |
| Amazon CodeWhisperer | AWS-focused |
| Cody (Sourcegraph) | Codebase-aware |
| Tabnine | Privacy-focused |
Best Practices
Provide Context
# Better: Include relevant code
context = """
# Existing database module
class Database:
def connect(self): ...
def query(self, sql): ...
"""
prompt = f"{context}
Add a method to insert records in batch:"
Specify Requirements
prompt = """
Write a Python function that:
- Parses CSV files
- Handles missing values
- Returns a pandas DataFrame
- Includes type hints
- Has error handling for file not found
"""
Iterative Refinement
# Generate
code = generate_code(description)
# Review
review = llm.generate(f"Review this code for bugs:
{code}")
# Refactor
improved = llm.generate(f"Improve this code based on:
{review}
{code}")
Use Cases
| Use Case | Approach |
|---|---|
| Boilerplate | Direct generation |
| Algorithm implementation | Detailed specification |
| API integration | Provide API docs as context |
| Bug fixing | Include error message |
| Refactoring | Show before, specify improvements |
Limitations
- May generate plausible but incorrect code
- Security vulnerabilities possible
- May not follow project conventions
- Always review generated code
code generationcopilotcodex
Explore 500+ Semiconductor & AI Topics
From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.