# 1. Download OpenCode# Visit https://github.com/opencode-org/opencode/releases# Download the installer for your platform# Linux/macOSwget https://github.com/opencode-org/opencode/releases/latest/download/opencv-linux-x64.tar.gz
tar -xzf opencv-linux-x64.tar.gz
cd opencv
./opencode
# macOS (using Homebrew Cask)brew install --cask opencode
# Windows# Download opencv-setup.exe and run the installer# 2. Launch OpenCode# The welcome page will appear on first launch
# Single-line completion
def calculate_discount(price, discount_rate):
return price * (1 - discount_
# OpenCode auto-completes:
# rate)
# Multi-line completion
# Input comment:
# Create a function to calculate the nth term of the Fibonacci sequence
# OpenCode generates complete function:
def fibonacci(n):
"""
Calculate the nth term of the Fibonacci sequence
Args:
n (int): The term number to calculate
Returns:
int: The value of the nth term
"""
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci(n-1) + fibonacci(n-2)
2. Code Explanation
# Select complex code, OpenCode provides explanation
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
# OpenCode explanation:
# This is an implementation of the merge sort algorithm.
# 1. Base case: If array length <= 1, return directly
# 2. Divide and conquer: Split the array in half, sort recursively
# 3. Merge: Merge two sorted arrays
# Time Complexity: O(n log n)
# Space Complexity: O(n)
3. Code Refactoring
# Original code
def process_user(user):
if user is not None:
if user.get("name") is not None:
if user.get("age") is not None and user["age"] >= 18:
return True
return False
# OpenCode refactoring suggestion:
def process_user(user):
"""
Check if user is a valid adult
Args:
user (dict): User info dictionary
Returns:
bool: Whether the user is valid
"""
if not user:
return False
name = user.get("name")
age = user.get("age")
return bool(name and age >= 18)
# Buggy codedefdivide_numbers(a, b):
result = a / b
return result
# Callprint(divide_numbers(10, 0)) # ZeroDivisionError# OpenCode detects and fixes:defdivide_numbers(a, b):
"""
Safely divide two numbers
Args:
a (float): Dividend
b (float): Divisor
Returns:
float: Division result
Raises:
ValueError: When divisor is 0
"""if b ==0:
raiseValueError("Divisor cannot be zero")
return a / b
# Create model configuration files~/.opencode/models/
├── glm-4.json
├── minimax-m2.5.json
├── kimi-k2.5.json
└── qwen3.5.json
# Quickly switch modelsopencode model switch glm-4
opencode model switch minimax-m2.5
opencode model switch kimi-k2.5
opencode model switch qwen3.5
# Set default modelopencode model default qwen3.5
V. Best Practices
1. Prompt Engineering
Characteristics of Good Prompts
A good prompt should:
1. Clear Goal - Clearly state what to do
2. Provide Context - Give relevant background information
3. Specify Format - State the expected output format
4. Give Examples - Provide examples of desired results
5. Set Constraints - Define clear limitations
# Bad Prompt
Write a sorting function
# Good Prompt
Please write a quick sort function in Python with the following requirements:
1. Input: List of integers
2. Output: Sorted list
3. Handle edge cases: empty list, single-element list
4. Include complete docstrings
5. Add type annotations
6. Include unit test examples
Please follow the Google Python Style Guide
# Provide sufficient context information# Bad example - Lacks context# Help me optimize this functiondefprocess(data):
result = []
for item in data:
result.append(item *2)
return result
# Good example - Provides context# Optimize the following function, requirements:# Scenario: Processing large amounts of sensor data (millions)# Current performance: Processing 100k items takes 5 seconds# Goal: Process 100k items in <1 second# Constraint: Data order must be preserved# Language: Pythondefprocess(data):
result = []
for item in data:
result.append(item *2)
return result
Security Practices:API Key Management:- Store API keys in environment variables- Rotate keys regularly- Do not commit to code repositoriesCode Review:- AI-generated code requires human review- Pay special attention to security vulnerabilities- Test edge casesSensitive Information:- Do not let AI access sensitive code- Use anonymized data- Configure ignore rules
Team Collaboration:Configuration Sharing:- Create team configuration files- Unify model selection- Unify prompt templatesVersion Control:# .opencoderc{"model": "qwen3.5","temperature": 0.3,"presets": {"python": "python-dev-preset","go": "go-dev-preset"}}Knowledge Base:- Maintain team prompt library- Share best practices- Regular training
Offline Solutions:Local Models:- Use Ollama to run local models- Supports Llama, CodeLlama, etc.- Configure local API endpointConfiguration:opencode.apiEndpoint:"http://localhost:11434"opencode.model:"codellama:13b"
Summary
As an open-source AI coding assistant, OpenCode has the following advantages:
Open Source & Free: No subscription fees
Flexible Models: Supports multiple LLM backends
Domestic Models: Integrated with GLM, Minimax, Kimi, Qwen
Customizable: Highly configurable
Privacy Protection: Local deployment options
Combined with oh-my-opencode and domestic LLMs, an efficient AI-assisted development environment can be built.