在llama-cpp-python中使用自己编译的llama.cpp,解决pip install llama-cpp-python报错

Q:在使用"pip install llama-cpp-python"安装llama-cpp-python出现错误,最后几句错误是这样的:

Building wheel for llama-cpp-python (pyproject.toml) ... error

ERROR: Failed building wheel for llama-cpp-python

Failed to build llama-cpp-python

ERROR: Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based projects

从日志看来,是编译llama.cpp的时候报的错。

A:自己手动编译llama.cpp,然后在llama-cpp-python使用自己编译的llama.cpp,至于生成的是CPU版的llama.cpp,还是GPU版的llama.cpp,需要在手动编译llama.cpp时配置。

步骤:

一、下载llama.cpp源码,并编译

bash 复制代码
git clone https://github.com/ggml-org/llama.cpp.git
cd /d llama.cpp

#以cuda版本为例,其他编译参考:https://github.com/ggml-org/llama.cpp/blob/master/docs/build.md
cmake -B build -DGGML_CUDA=ON
cmake --build build --config Release

生成完成后,在build\bin\Release下会生成对应的二进制文件。

二、安装llama-cpp-python

打开powershell

bash 复制代码
#!PowerShell

#设置PowerShell权限(全局有效)
Set-ExecutionPolicy RemoteSigned
#【或】设置PowerShell权限(当前回话有效)
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process

#创建虚拟环境
Set-Location D:\llama_rag  #D:\llama_rag为你想部署rag的目录
python -m venv env_rag  #创建虚拟环境,名字为:env_rag
.\env_rag\Scripts\activate  #激活虚拟环境

#安装llama-cpp-python
$env:CMAKE_ARGS="-DLLAMA_BUILD=OFF"  #设置在安装llama-cpp-python的过程中不自动安装llama.cpp以使用自己编译的llama.cpp
pip install llama-cpp-python  #安装llama-cpp-python

三、关联手动编译的llama.cpp和llama-cpp-python

1.找到llama-cpp-python中的dll加载路径

bash 复制代码
#!PowerShell

#进入python环境
python
#输入代码:from llama_cpp import Llama

会有如下报错:

>>> from llama_cpp import Llama

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

File "D:\llama_rag\env_rag\Lib\site-packages\llama_cpp\init.py", line 1, in <module>

from .llama_cpp import *

File "D:\llama_rag\env_rag\Lib\site-packages\llama_cpp\llama_cpp.py", line 43, in <module>

_lib = load_shared_library(_lib_base_name, _base_path)

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

File "D:\llama_rag\env_rag\Lib\site-packages\llama_cpp\_ctypes_extensions.py", line 50, in load_shared_library

os.add_dll_directory(str(base_path))

File "<frozen os>", line 1119, in add_dll_directory

FileNotFoundError: [WinError 2] 系统找不到指定的文件。: 'D:\\llama_rag\\env_rag\\Lib\\site-packages\\llama_cpp\\lib'

注意到提示加载dll时找不到指定文件,路径为:D:\llama_rag\env_rag\Lib\site-packages\llama_cpp\lib

2.到"D:\llama_rag\env_rag\Lib\site-packages\llama_cpp"目录下找到lib文件夹,如果没有lib文件夹则新建一个lib文件夹

3.把步骤一中生成的build\bin\Release下会生成对应的二进制文件拷贝到lib目录下

4.重新执行代码:from llama_cpp import Llama后,没有报错即表示已经应用的自己编译的llama.cpp

>>> from llama_cpp import Llama

>>>

至此,完毕

相关推荐
wj3055853781 天前
课程 9:模型测试记录与 Prompt 策略
linux·人工智能·python·comfyui
星寂樱易李1 天前
iperf3 + Python-- 网络带宽、网速、网络稳定性
开发语言·网络·python
解局易否结局1 天前
FlashAttention 在昇腾NPU上的实现:从内存墙到IO感知
llama
qingfeng154151 天前
企业微信机器人开发:如何实现自动化与智能运营?
人工智能·python·机器人·自动化·企业微信
彦为君1 天前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG1 天前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
用户8356290780511 天前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
枫叶林FYL1 天前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
猫猫的小茶馆2 天前
【Python】函数与模块化编程
linux·开发语言·arm开发·驱动开发·python·stm32