在itop-rk3588上部署deepseek-1.3b模型

1.切换目录

cd ~/
​
# 更新系统包管理器
sudo apt update

2. 安装基础依赖

# 安装编译工具链
sudo apt install -y git build-essential cmake wget curl
sudo apt install -y gcc g++ make pkg-config
sudo apt install -y libopenblas-dev libomp-dev
sudo apt install -y libcurl4-openssl-dev
sudo apt install -y unzip
​
#查看
git --version
gcc --version
cmake --version
make --version

3.获取LLaMA.cpp源码

wget https://github.com/ggerganov/llama.cpp/archive/refs/heads/master.zip -O llama.cpp.zip
​
#解压并重命名
unzip llama.cpp.zip
mv llama.cpp-master llama.cpp
cd ~/llama.cpp
​
# 安装python依赖,用于模型转换
pip3 install torch transformers numpy sentencepiece
pip3 install mistral_common protobuf gguf
​
# 创建构建目录
mkdir -p build
cd build
​
# 配置CMake(RK3588优化)
cmake .. \
    -DLLAMA_OPENBLAS=ON \
    -DLLAMA_NATIVE=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_C_FLAGS="-O3 -march=armv8-a -mtune=cortex-a76" \
    -DCMAKE_CXX_FLAGS="-O3 -march=armv8-a -mtune=cortex-a76"
​
# 编译(使用4个线程)
make -j4
# 检查编译结果
ls -la bin/
# 测试主程序
./bin/llama-cli --help
​
cd ~/llama.cpp/build
​
# 测试命令行工具(新版本叫llama-cli)
./bin/llama-cli --help
​
# 测试服务器(新版本叫llama-server)
./bin/llama-server --help

4.下载模型

# 创建模型存储目录
mkdir -p ~/models
cd ~/models
​
# 安装modelscope
pip3 install modelscope -i https://mirrors.aliyun.com/pypi/simple/
​
# 使用Python脚本下载
python3 << 'EOF'
from modelscope import snapshot_download
import os
​
# 下载DeepSeek模型
model_dir = snapshot_download(
    'deepseek-ai/deepseek-coder-1.3b-base',
    cache_dir='./cache'
)
print(f"模型下载到: {model_dir}")
​
# 查找gguf文件
for root, dirs, files in os.walk(model_dir):
    for file in files:
        if file.endswith('.gguf'):
            print(f"找到GGUF文件: {os.path.join(root, file)}")
转换为GGUF格式
--------------------------------------------------------------------------------
cd ~/llama.cpp
​
# 尝试转换
python3 convert_hf_to_gguf.py ~/models/cache/deepseek-ai/deepseek-coder-1___3b-base/ \
                              --outfile ~/models/deepseek-coder-1.3b-base-f16.gguf \
                              --outtype f16
​
# 将f16量化为q8_0(推荐,质量好且文件小)
./bin/llama-quantize ~/models/deepseek-coder-1.3b-base-f16.gguf \
                     ~/models/deepseek-coder-1.3b-base-q8_0.gguf \
                     q8_0

5.测试

 cd ~/llama.cpp/build
​
# 基础测试
./bin/llama-cli -m ~/models/deepseek-coder-1.3b-base-q8_0.gguf \
                 -p "# 用C++写一个Hello World程序" \
                 -n 256 \
                 -t 4 \
                 --temp 0.7
​
# 交互模式测试
./bin/llama-cli -m ~/models/deepseek-coder-1.3b-base-q8_0.gguf \
                 -i \
                 -t 4 \
                 -c 2048
完整测试页面
# 创建完整的测试页面
cat > ~/test_llama.html << 'EOF'
<!DOCTYPE html>
<html>
<head>
    <title>DeepSeek Coder Test</title>
    <meta charset="UTF-8">
    <style>
        body { 
            font-family: Arial, sans-serif; 
            max-width: 800px; 
            margin: 0 auto; 
            padding: 20px; 
        }
        textarea { 
            width: 100%; 
            font-family: monospace; 
        }
        button { 
            padding: 10px 20px; 
            font-size: 16px; 
            margin: 10px 0; 
        }
        #result { 
            white-space: pre-wrap; 
            background: #f5f5f5; 
            padding: 15px; 
            border: 1px solid #ccc; 
            border-radius: 5px;
            font-family: monospace;
            min-height: 200px;
        }
        .status { 
            color: #666; 
            font-style: italic; 
        }
        .error { 
            color: red; 
        }
    </style>
</head>
<body>
    <h1>🚀 DeepSeek Coder 1.3B 测试界面</h1>
    
    <h3>📝 输入代码提示:</h3>
    <textarea id="prompt" rows="4" placeholder="输入你的代码提示,例如:def hello_world():">def hello_world():</textarea>
    
    <br>
    <button onclick="generate()">🔥生成代码</button>
    <button onclick="clearResult()">🗑️ 清空结果</button>
    
    <h3>📋 生成结果:</h3>
    <div id="result">点击"生成代码"开始测试...</div>
​
    <h3>💡 使用说明:</h3>
    <ul>
        <li>确保llama-server已在localhost:8080运行</li>
        <li>输入代码提示,如函数定义、注释等</li>
        <li>支持多种编程语言:Python、C++、JavaScript等</li>
        <li>生成过程需要几秒钟,请耐心等待</li>
    </ul>
​
    <script>
    async function generate() {
        const prompt = document.getElementById('prompt').value.trim();
        const result = document.getElementById('result');
        
        if (!prompt) {
            result.innerHTML = '<span class="error">请输入代码提示!</span>';
            return;
        }
        
        result.innerHTML = '<span class="status">🤖 AI正在思考中,请稍候...</span>';
        
        try {
            const startTime = Date.now();
            
            const response = await fetch('http://localhost:8080/completion', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    prompt: prompt,
                    n_predict: 256,
                    temperature: 0.7,
                    top_p: 0.9,
                    top_k: 40,
                    repeat_penalty: 1.1,
                    stop: ["\n\n\n", "```"]
                })
            });
            
            if (!response.ok) {
                throw new Error(`HTTP错误: ${response.status} ${response.statusText}`);
            }
            
            const data = await response.json();
            const endTime = Date.now();
            const duration = ((endTime - startTime) / 1000).toFixed(1);
            
            const fullText = prompt + (data.content || '');
            result.innerHTML = `<strong>✅生成完成 (${duration}秒):</strong>\n\n${fullText}`;
            
        } catch (error) {
            console.error('请求错误:', error);
            result.innerHTML = `<span class="error">❌错误: ${error.message}</span>\n\n` +
                              `<strong>常见问题排查:</strong>\n` +
                              `1. 检查llama-server是否在运行: ps aux | grep llama-server\n` +
                              `2. 检查端口8080是否被占用: netstat -tlnp | grep 8080\n` +
                              `3. 尝试重启服务器\n` +
                              `4. 检查防火墙设置`;
        }
    }
    
    function clearResult() {
        document.getElementById('result').textContent = '点击"生成代码"开始测试...';
    }
    
    // 支持回车键快速生成
    document.getElementById('prompt').addEventListener('keydown', function(e) {
        if (e.ctrlKey && e.key === 'Enter') {
            generate();
        }
    });
    </script>
</body>
</html>
EOF
​
echo "完整测试页面已创建: ~/test_llama.html"

6.启动服务并测试完整流程

启动服务并测试完整流程
# 1. 确认模型文件存在
ls -lh ~/models/*.gguf
​
# 2. 启动HTTP服务(如果还未启动)
cd ~/llama.cpp/build
nohup ./bin/llama-server -m ~/models/deepseek-coder-1.3b-base-q8_0.gguf \
                         -c 2048 \
                         -t 4 \
                         --host 0.0.0.0 \
                         --port 8080 \
                         --cors \
                         --log-disable > ~/llama_server.log 2>&1 &
​
# 3. 等待几秒让服务启动
sleep 5
​
# 4. 测试服务状态
curl -s http://localhost:8080/health || echo "服务未就绪,请稍等..."
​
# 5. 快速API测试
curl -X POST http://localhost:8080/completion \
     -H "Content-Type: application/json" \
     -d '{
       "prompt": "def add(a, b):",
       "n_predict": 50,
       "temperature": 0.7
     }' | jq .
​
echo ""
echo " 现在可以用浏览器打开 ~/test_llama.html 进行测试"
echo "或者通过IP访问: http://$(hostname -I | awk '{print $1}'):8080"

更多推荐