分类 大模型 下的文章

系统要求

树莓派安装的为ubuntu系统

  • 系统版本:Linux wudg 6.14.0-1017-raspi #17-Ubuntu SMP PREEMPT_DYNAMIC Tue Oct 14 16:43:21 UTC 2025 aarch64 aarch64 aarch64 GNU/Linux
  • 硬件为树莓派4B 4c8g

模型选择和运行

由于配置较低,只能运行DeepSeek-R1-Distill-Qwen-1.5B-Q3_K_M.gguf 量化版本 Q3 ,测试速度 3~4token/s,速度不是很满意,但是能跑出结果,后续尝试使用 Q1 量化再试试,毕竟树莓派 cpu 计算能力较弱。

下载 gguf 的 deepseek 大模型文件

# 下载可能需要梯子,如果需要可以联系留言作者会私发给你
wget https://huggingface.co/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B-GGUF/resolve/main/DeepSeek-R1-Distill-Qwen-1.5B-Q3_K_M.gguf -O DeepSeek-R1-Distill-Qwen-1.5B-Q3_K_M.gguf

编译llama.cpp 并运行

  • 下载 llama.cpp 源码,github 上没有编译好的适合树莓派 arm 架构的二进制,需要自行编译。llama.cpp 下载地址
  • 编译 llama.cpp ,可以根据文档自行编译,编译过程中可能会缺少一些类库文件,编译前提前安装好 cmake 等编译工具

    sudo apt-get install build-essential cmake libpthread-stubs0-dev libcurl4-openssl-dev
    
  • 编译时间预计在 10 分钟左右,编译成功后 会在 build/bin 下有对应的二进制文件可以用来执行 model 模型文件
  • 编译成功后直接运行

     # --host=0.0.0.0 局域网可访问,--port=8090 对外端口8090
     ./llama-server -m /home/admin/llm/model/DeepSeek-R1-Distill-Qwen-1.5B-Q3_K_M.gguf --host=0.0.0.0 --port=8090

python 要求和扩展安装

python版本 3.13.3,使用 venv 虚拟环境

安装 paddlepaddle & paddleocr 类库

pip install paddlepaddle==3.2.0 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/
pip install "paddleocr[all]" -i https://pypi.tuna.tsinghua.edu.cn/simple

安装 pdf 转图片类库

pip install pdf2image -i https://pypi.mirrors.ustc.edu.cn/simple/

最终python核心扩展

Package                  Version
------------------------ -----------
llama_cpp_python         0.3.16
numpy                    2.3.4
pdf2image                1.17.0
pillow                   12.0.0
paddleocr                3.3.2
paddlepaddle             3.2.0
paddlex                  3.3.9

执行异常修复

  • 异常 ImportError: libGL.so.1: cannot open shared object file: No such file or directory,可运行如下指令安装对应类库解决

     sudo apt-get install -y libgl1 libglib2.0-0 libsm6 libxrender1 libxext6

最终实现

    from paddleocr import PaddleOCR
    from pdf2image import convert_from_path
    pdfPath = './tst_contact.pdf'
    import time

    start = time.time()
    images = convert_from_path(pdfPath, dpi=80)

    ocr = PaddleOCR(
        text_detection_model_name='PP-OCRv5_mobile_det',
        text_recognition_model_name='PP-OCRv5_mobile_rec',
        use_doc_orientation_classify=False,
        use_doc_unwarping=False,
        enable_mkldnn=True,
        device='cpu',
        cpu_threads=4,
        use_textline_orientation=False) # 文本检测+文本识别

    all_texts = []
    # 保存所有页面
    for i, image in enumerate(images):
        imagePath = f'page_{i+1}.png'
        image.save(imagePath, 'PNG')
        print("正在分析 "+ imagePath)
        result_generator = ocr.predict_iter(imagePath)
        for page_result in result_generator:
            all_texts.append(page_result['rec_texts'])

    from llama_cpp import Llama
    flat_texts = [line for page in all_texts for line in page]
    full_text = "\n".join(flat_texts)
    prompt = f"""
    根据以下文本
    {full_text}
    请帮我提取被许可人姓名(name),姓名拼音(namePy),地址(address),协议开始时间(startTime),协议时间(endTime),服务费(rentAmount),付款方式(rentType),保证金金额(otherMmount),注册费(regAmount),总服务费(totalAmount) 等信息,没有相关内容可以为空。json格式返回,括号为 json 的key 值
    """
    print(prompt)
    model_Path = '/home/llm/model/DeepSeek-R1-Distill-Qwen-1.5B-Q8_0.gguf'
    llm = Llama(
        model_path=model_Path,
        n_ctx=2048,  # 上下文长度
        n_threads=4  # CPU线程数
    )
    output = llm(
        prompt,
        max_tokens=2048,
        echo=True
    )
    print(output['choices'][0]['text'])
    end = time.time()
    print("总执行时间:", end - start, "秒")

最终执行合同消耗时间在 130 秒左右,效果不是 很好,看来树莓派不适合跑大模型, 后续部署在 4c/8g 的阿里云机器上 大模型使用线上 api 方式调用 提升很明显,一个 5 页合同能在 20 秒内返回结果