llama-index调用qwen大模型实现RAG
背景
llama-index在实现RAG方案的时候多是用的llama等英文大模型,对于国内的诸多模型案例较少,本次将使用qwen大模型实现llama-index的RAG方案。
环境配置
(1)pip包
llamaindex需要预装很多包,这里先把我成功的案例里面的pip包配置发出来,在requirements.txt里面。
absl-py==1.4.0
accelerate==0.27.2
aiohttp==3.9.3
aiosignal==1.3.1
aliyun-python-sdk-core==2.13.36
aliyun-python-sdk-kms==2.16.1
annotated-types==0.6.0
anyio==3.7.1
apphub @ file:///environment/apps/apphub/dist/apphub-1.0.0.tar.gz#sha256=260f99c0de4c575b19ab913aa134877e9efd81b820b97511fc8379674643c253
argon2-cffi==21.3.0
argon2-cffi-bindings==21.2.0
asgiref==3.7.2
asttokens==2.2.1
astunparse==1.6.3
async-timeout==4.0.3
attrs==23.1.0
Babel==2.12.1
backcall==0.2.0
backoff==2.2.1
bcrypt==4.1.2
beautifulsoup4==4.12.3
bleach==6.0.0
boltons @ file:///croot/boltons_1677628692245/work
brotlipy==0.7.0
bs4==0.0.2
build==1.1.1
cachetools==5.3.1
certifi @ file:///croot/certifi_1690232220950/work/certifi
cffi @ file:///croot/cffi_1670423208954/work
chardet==3.0.4
charset-normalizer @ file:///tmp/build/80754af9/charset-normalizer_1630003229654/work
chroma-hnswlib==0.7.3
chromadb==0.4.24
click==7.1.2
cmake==3.25.0
coloredlogs==15.0.1
comm==0.1.4
conda @ file:///croot/conda_1690494963117/work
conda-content-trust @ file:///tmp/abs_5952f1c8-355c-4855-ad2e-538535021ba5h26t22e5/croots/recipe/conda-content-trust_1658126371814/work
conda-libmamba-solver @ file:///croot/conda-libmamba-solver_1685032319139/work/src
conda-package-handling @ file:///croot/conda-package-handling_1685024767917/work
conda_package_streaming @ file:///croot/conda-package-streaming_1685019673878/work
contourpy==1.2.0
crcmod==1.7
cryptography @ file:///croot/cryptography_1686613057838/work
cycler==0.12.1
dataclasses-json==0.6.4
debugpy==1.6.7
decorator==5.1.1
defusedxml==0.7.1
Deprecated==1.2.14
dirtyjson==1.0.8
distro==1.9.0
ecdsa==0.18.0
exceptiongroup==1.1.2
executing==1.2.0
fastapi==0.104.1
fastjsonschema==2.18.0
featurize==0.0.24
filelock==3.9.0
flatbuffers==23.5.26
fonttools==4.44.0
frozenlist==1.4.1
fsspec==2024.2.0
gast==0.4.0
google-auth==2.22.0
google-auth-oauthlib==1.0.0
google-pasta==0.2.0
googleapis-common-protos==1.62.0
greenlet==3.0.3
grpcio==1.62.0
gunicorn==21.2.0
h11==0.14.0
h5py==3.9.0
httpcore==0.17.3
httptools==0.6.1
httpx==0.24.1
huggingface-hub==0.20.3
humanfriendly==10.0
idna==2.10
imageio==2.32.0
importlib-metadata==6.11.0
importlib_resources==6.1.3
ipykernel==6.25.0
ipython==8.14.0
ipython-genutils==0.2.0
ipywidgets==8.1.2
jedi==0.19.0
Jinja2==3.1.2
jmespath==0.10.0
joblib==1.3.2
json5==0.9.14
jsonpatch @ file:///tmp/build/80754af9/jsonpatch_1615747632069/work
jsonpointer==2.1
jsonschema==4.18.6
jsonschema-specifications==2023.7.1
jupyter-server==1.24.0
jupyter_client==8.3.0
jupyter_core==5.3.1
jupyterlab==3.2.9
jupyterlab-pygments==0.2.2
jupyterlab_server==2.24.0
jupyterlab_widgets==3.0.10
keras==2.13.1
kiwisolver==1.4.5
kubernetes==29.0.0
lazy_loader==0.3
libclang==16.0.6
libmambapy @ file:///croot/mamba-split_1685993156657/work/libmambapy
lit==15.0.7
llama-index==0.10.17
llama-index-agent-openai==0.1.5
llama-index-cli==0.1.8
llama-index-core==0.10.17
llama-index-embeddings-huggingface==0.1.4
llama-index-embeddings-openai==0.1.6
llama-index-indices-managed-llama-cloud==0.1.3
llama-index-legacy==0.9.48
llama-index-llms-huggingface==0.1.3
llama-index-llms-openai==0.1.7
llama-index-multi-modal-llms-openai==0.1.4
llama-index-program-openai==0.1.4
llama-index-question-gen-openai==0.1.3
llama-index-readers-file==0.1.8
llama-index-readers-llama-parse==0.1.3
llama-index-vector-stores-chroma==0.1.5
llama-parse==0.3.8
llamaindex-py-client==0.1.13
Markdown==3.4.4
MarkupSafe==2.1.2
marshmallow==3.21.1
matplotlib==3.8.1
matplotlib-inline==0.1.6
mistune==3.0.1
mmh3==4.1.0
monotonic==1.6
mpmath==1.2.1
multidict==6.0.4
mypy-extensions==1.0.0
nbclassic==0.2.8
nbclient==0.8.0
nbconvert==7.7.3
nbformat==5.9.2
nest-asyncio==1.6.0
networkx==3.0
nltk==3.8.1
notebook==6.4.12
numpy==1.24.1
nvidia-cublas-cu12==12.1.3.1
nvidia-cuda-cupti-cu12==12.1.105
nvidia-cuda-nvrtc-cu12==12.1.105
nvidia-cuda-runtime-cu12==12.1.105
nvidia-cudnn-cu12==8.9.2.26
nvidia-cufft-cu12==11.0.2.54
nvidia-curand-cu12==10.3.2.106
nvidia-cusolver-cu12==11.4.5.107
nvidia-cusparse-cu12==12.1.0.106
nvidia-nccl-cu12==2.19.3
nvidia-nvjitlink-cu12==12.4.99
nvidia-nvtx-cu12==12.1.105
oauthlib==3.2.2
onnxruntime==1.17.1
openai==1.13.3
opencv-python==4.8.1.78
opentelemetry-api==1.23.0
opentelemetry-exporter-otlp-proto-common==1.23.0
opentelemetry-exporter-otlp-proto-grpc==1.23.0
opentelemetry-instrumentation==0.44b0
opentelemetry-instrumentation-asgi==0.44b0
opentelemetry-instrumentation-fastapi==0.44b0
opentelemetry-proto==1.23.0
opentelemetry-sdk==1.23.0
opentelemetry-semantic-conventions==0.44b0
opentelemetry-util-http==0.44b0
opt-einsum==3.3.0
orjson==3.9.15
oss2==2.18.1
overrides==7.7.0
packaging @ file:///croot/packaging_1678965309396/work
pandas==2.1.2
pandocfilters==1.5.0
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
Pillow==9.3.0
platformdirs==3.10.0
pluggy @ file:///tmp/build/80754af9/pluggy_1648024709248/work
posthog==3.5.0
prometheus-client==0.17.1
prompt-toolkit==3.0.39
protobuf==4.23.4
psutil==5.9.5
ptyprocess==0.7.0
pulsar-client==3.4.0
pure-eval==0.2.2
pyasn1==0.5.0
pyasn1-modules==0.3.0
pycosat @ file:///croot/pycosat_1666805502580/work
pycparser @ file:///tmp/build/80754af9/pycparser_1636541352034/work
pycryptodome==3.18.0
pydantic==2.4.2
pydantic_core==2.10.1
Pygments==2.15.1
PyMuPDF==1.23.26
PyMuPDFb==1.23.22
pyOpenSSL @ file:///croot/pyopenssl_1677607685877/work
pyparsing==3.1.1
pypdf==4.1.0
PyPika==0.48.9
pyproject_hooks==1.0.0
PySocks @ file:///home/builder/ci_310/pysocks_1640793678128/work
python-dateutil==2.8.2
python-dotenv==1.0.0
pytz==2023.3.post1
PyYAML==6.0.1
pyzmq==25.1.0
referencing==0.30.0
regex==2023.12.25
requests==2.31.0
requests-oauthlib==1.3.1
rpds-py==0.9.2
rsa==4.9
ruamel.yaml @ file:///croot/ruamel.yaml_1666304550667/work
ruamel.yaml.clib @ file:///croot/ruamel.yaml.clib_1666302247304/work
safetensors==0.4.2
scikit-image==0.22.0
scikit-learn==1.3.2
scipy==1.11.3
seaborn==0.13.0
Send2Trash==1.8.2
six @ file:///tmp/build/80754af9/six_1644875935023/work
sniffio==1.3.0
socksio==1.0.0
soupsieve==2.4.1
SQLAlchemy==2.0.28
sshpubkeys==3.3.1
stack-data==0.6.2
starlette==0.27.0
sympy==1.11.1
tabulate==0.8.7
tenacity==8.2.3
tensorboard==2.13.0
tensorboard-data-server==0.7.1
tensorflow==2.13.0
tensorflow-estimator==2.13.0
tensorflow-io-gcs-filesystem==0.33.0
termcolor==2.3.0
terminado==0.17.1
threadpoolctl==3.2.0
tifffile==2023.9.26
tiktoken==0.6.0
tinycss2==1.2.1
tokenizers==0.15.2
tomli==2.0.1
toolz @ file:///croot/toolz_1667464077321/work
torch==2.2.1
torchaudio==2.0.2+cu118
torchvision==0.15.2+cu118
tornado==6.3.2
tqdm==4.66.2
traitlets==5.9.0
transformers==4.38.2
triton==2.2.0
typer==0.9.0
typing-inspect==0.9.0
typing_extensions==4.8.0
tzdata==2023.3
urllib3==1.25.11
uvicorn==0.23.2
uvloop==0.19.0
watchfiles==0.21.0
wcwidth==0.2.5
webencodings==0.5.1
websocket-client==1.2.1
websockets==12.0
Werkzeug==2.3.6
widgetsnbextension==4.0.10
workspace @ file:///home/featurize/work/workspace/dist/workspace-0.1.0.tar.gz#sha256=b292beb3599f79d3791771eff9dc422cc37c58c1fc8daadeafbf025a2e7ea986
wrapt==1.15.0
yarl==1.9.2
zipp==3.17.0
zstandard @ file:///croot/zstandard_1677013143055/work
(2)python 环境
(3)安装命令
!pip install llama-index
!pip install llama-index-llms-huggingface
!pip install llama-index-embeddings-huggingface
!pip install llama-index ipywidgets
!pip install torch
!git clone https://www.modelscope.cn/AI-ModelScope/bge-small-zh-v1.5.git
!git clone https://www.modelscope.cn/qwen/Qwen1.5-4B-Chat.git
(4)目录结构
代码
(1)加载模型
import torch
from llama_index.llms.huggingface import HuggingFaceLLM
from llama_index.core import PromptTemplate
import os
os.environ['KMP_DUPLICATE_LIB_OK']='True'
# Model names (make sure you have access on HF)
LLAMA2_7B = "/home/featurize/Qwen1.5-4B-Chat"
# LLAMA2_7B_CHAT = "meta-llama/Llama-2-7b-chat-hf"
# LLAMA2_13B = "meta-llama/Llama-2-13b-hf"
LLAMA2_13B_CHAT = "/home/featurize/Qwen1.5-4B-Chat"
# LLAMA2_70B = "meta-llama/Llama-2-70b-hf"
# LLAMA2_70B_CHAT = "meta-llama/Llama-2-70b-chat-hf"selected_model = LLAMA2_13B_CHATSYSTEM_PROMPT = """You are an AI assistant that answers questions in a friendly manner, based on the given source documents. Here are some rules you always follow:
- Generate human readable output, avoid creating output with gibberish text.
- Generate only the requested output, don't include any other language before or after the requested output.
- Never say thank you, that you are happy to help, that you are an AI agent, etc. Just answer directly.
- Generate professional language typically used in business documents in North America.
- Never generate offensive or foul language.
"""query_wrapper_prompt = PromptTemplate("[INST]<<SYS>>\n" + SYSTEM_PROMPT + "<</SYS>>\n\n{query_str}[/INST] "
)llm = HuggingFaceLLM(context_window=4096,max_new_tokens=2048,generate_kwargs={"temperature": 0.0, "do_sample": False},query_wrapper_prompt=query_wrapper_prompt,tokenizer_name=selected_model,model_name=selected_model,device_map="auto"
)
(2)加载词嵌入向量
from llama_index.embeddings.huggingface import HuggingFaceEmbeddingembed_model = HuggingFaceEmbedding(model_name="/home/featurize/bge-small-zh-v1.5")
from llama_index.core import SettingsSettings.llm = llm
Settings.embed_model = embed_model
from llama_index.core import SimpleDirectoryReader# load documents
documents = SimpleDirectoryReader("./data/").load_data()
from llama_index.core import VectorStoreIndex
index = VectorStoreIndex.from_documents(documents)
index
# set Logging to DEBUG for more detailed outputs
query_engine = index.as_query_engine()
response = query_engine.query("小额贷款咋规定的?")
print(response)
知识库
llamaindex实现RAG中很关键的一环就是知识库,知识库主要是各种类型的文档,这里给的文档是一个pdf文件,文件内容如下。
总结
从上面的代码可以看出,我们使用qwen和bge-zh模型可以实现本地下载模型的RAG方案,知识库里面的内容也可以实现中文问答,这非常有利于我们进行私有化部署方案,从而扩展我们的功能。
相关文章:

llama-index调用qwen大模型实现RAG
背景 llama-index在实现RAG方案的时候多是用的llama等英文大模型,对于国内的诸多模型案例较少,本次将使用qwen大模型实现llama-index的RAG方案。 环境配置 (1)pip包 llamaindex需要预装很多包,这里先把我成功的案例…...

基于springboot的医院信息管理系统(程序+代码+文档)
** 🍅点赞收藏关注 → 私信领取本源代码、数据库🍅 本人在Java毕业设计领域有多年的经验,陆续会更新更多优质的Java实战项目,希望你能有所收获,少走一些弯路。🍅关注我不迷路🍅** 一、研究背景…...

【环境配置】Linux MySQL8 忘记密码解决措施
本片博客介绍 Linux 操作系统 Ubuntu 下,MySQL8 忘记密码怎么重新设置,笔者亲测有效,分享给大家。 查看 MySQL 版本 $ mysql --version停止 MySQL 服务器,并查看状态是否变更为 Server shutdown complete # 等价命令sudo syste…...

MySQL-锁:共享锁(读)、排他锁(写)、表锁、行锁、意向锁、间隙锁,锁升级
MySQL-锁:共享锁(读)、排他锁(写)、表锁、行锁、意向锁、间隙锁 共享锁(读锁)、排他锁表锁行锁意向锁间隙锁锁升级 MySQL数据库中的锁是控制并发访问的重要机制,它们确保数据的一致性…...

docker 使用官方镜像搭建 PHP 环境
一、所需环境: 1、PHP:7.4.33-fpm 的版本 2、Nginx:1.25.1 的版本 3、MySQL: 5.7 的版本 4、Redis:7.0 的版本 1.1、拉取官方的镜像 docker pull php:7.4.33-fpm docker pull nginx:1.25.1 docker pull mysql:5.7 do…...

STM32CubeIDE基础学习-STM32CubeIDE软件偏好设置
STM32CubeIDE基础学习-STM32CubeIDE软件偏好设置 文章目录 STM32CubeIDE基础学习-STM32CubeIDE软件偏好设置前言第1章 设置字体颜色第2章 设置字体大小第3章 设置代码区背景颜色总结 前言 编程软件环境最好就设置一个自己喜欢的界面进行显示,这样看起来会比较舒服些…...
【《高性能 MySQL》笔记】性能优化
学习知识最快最好的方式就是问对问题。 本文将通过“问正确的问题”的方式循序渐进地深入总结性能优化相关知识。 性能优化基础 Q1:什么是“性能”? 即响应时间(RT,Response Time),完成某个任务所需要的时间度量。 Q2:什么是“性能优化”? 性能优化为在一定工作负…...

Spring AOP底层原理
目录 代理模式 静态代理 动态代理 1. JDK动态代理 创建⼀个代理对象并使用 2. CGLIB动态代理 SpringAOP底层原理面试 代理模式 Spring AOP是基于动态代理模式来实现的 代理模式:静态代理模式动态代理模式 代理模式, 也叫委托模式。 定义:为其…...
proteus8.9 示波器放大
proteus8.9 示波器放大** 1. 打开放大:Windows徽标键 加号 2. 取消放大:Windows徽标键 ESC...

代码随想录训练营第41天 | 动态规划:01背包理论基础、动态规划:01背包理论基础(滚动数组)、LeetCode 416.分割等和子集
动态规划:01背包理论基础 文章讲解:代码随想录(programmercarl.com) 视频讲解:带你学透0-1背包问题!_哔哩哔哩_bilibili 动态规划:01背包理论基础(滚动数组) 文章讲解:代码随想录(…...

1908_Arm Cortex-M3的实现
1908_Arm Cortex-M3的实现 全部学习汇总: g_arm_cores: ARM内核的学习笔记 (gitee.com) 这是第一次看一份这样的手册,之前的MCU编程基本上就是专注于软件接口方面。而OS等方面的一些功能基本上都是用了现成的解决方案,因此也就没有过多的关注…...
编程笔记 html5cssjs 005 小学数学四则运算练习
编程笔记 html5&css&js 005 小学数学四则运算练习 一、代码二、解释 这段代码定义了一个页面,用于小学数学四则运算的练习。这可能有点难,实际如果需要可以通过更改代码来达到要求。 一、代码 <!DOCTYPE html> <html lang"zh&quo…...

【大厂AI课学习笔记NO.71】AI算力芯片GPU/TPU等
AI算力芯片的发展历程 人工智能(AI)算力芯片的发展历程紧密地跟随着AI技术的发展脚步。从早期的基于传统中央处理器(CPU)的计算,到图形处理器(GPU)的广泛应用,再到专门为AI设计的处…...

浅谈Redis和分布式系统
浅谈Redis Redis用于存储数据,且在内存当中进行存储。 但是在日常编写代码中,定义一个变量也就属于在内存当中存储一个数据。 Redis主要会在分布式系统当中发挥重要作用,如果只是单机程序,直接通过变量存储数据的方式会比使用Re…...

微信小程序onLoad加载定义好的函数
这里小程序开发中容易犯的错误-1 给客户做一个程序。需要在页面加载的时候在onLoad(options){}中加载定义好的函数,代码如下 onLoad(options) {get_week_()},运行时老报错 后来修改为正确的代码 onLoad(options) {this.get_week_()//必须加this},再尝试运行&#x…...

C++进阶:详细讲解继承
现在也是结束了初阶部分的内容,今天开始就进入进阶部分了。一刻也没有为初阶的结束而哀悼,立刻赶来“战场”的是进阶部分里的继承 文章目录 1.继承的概念和定义1.1继承的概念1.2继承的定义1.2.1继承的格式1.2.2再讲访问限定符(详讲protected)1.2.3**继承…...

第十一篇 - 应用于市场营销视频场景中的人工智能和机器学习技术 – Video --- 我为什么要翻译介绍美国人工智能科技巨头IAB公司(1)
IAB平台,使命和功能 IAB成立于1996年,总部位于纽约市。 作为美国的人工智能科技巨头社会媒体和营销专业平台公司,互动广告局(IAB- the Interactive Advertising Bureau)自1996年成立以来,先后为700多家媒体…...

基于决策树实现葡萄酒分类
基于决策树实现葡萄酒分类 将葡萄酒数据集拆分成训练集和测试集,搭建tree_1和tree_2两个决策树模型,tree_1使用信息增益作为特征选择指标,B树使用基尼指数作为特征选择指标,各自对训练集进行训练,然后分别对训练集和测…...

上位机图像处理和嵌入式模块部署(qmacvisual三个特色)
【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing 163.com】 了解了qmacvisual的配置之后,正常来说,我们需要了解下不同插件的功能是什么。不过我们不用着急,可以继续学习下…...

电脑解锁后黑屏有鼠标--亲测!!不需要重装系统!!
问题:上周电脑黑屏,只有鼠标,鼠标还不能右键!! 中招:win10系统最新版火绒安全 ,那你有概率获得开机黑屏套餐一份。 原因是:火绒把我们的explorer删除了导致黑屏,这个文…...

Lombok 的 @Data 注解失效,未生成 getter/setter 方法引发的HTTP 406 错误
HTTP 状态码 406 (Not Acceptable) 和 500 (Internal Server Error) 是两类完全不同的错误,它们的含义、原因和解决方法都有显著区别。以下是详细对比: 1. HTTP 406 (Not Acceptable) 含义: 客户端请求的内容类型与服务器支持的内容类型不匹…...

智能在线客服平台:数字化时代企业连接用户的 AI 中枢
随着互联网技术的飞速发展,消费者期望能够随时随地与企业进行交流。在线客服平台作为连接企业与客户的重要桥梁,不仅优化了客户体验,还提升了企业的服务效率和市场竞争力。本文将探讨在线客服平台的重要性、技术进展、实际应用,并…...
python如何将word的doc另存为docx
将 DOCX 文件另存为 DOCX 格式(Python 实现) 在 Python 中,你可以使用 python-docx 库来操作 Word 文档。不过需要注意的是,.doc 是旧的 Word 格式,而 .docx 是新的基于 XML 的格式。python-docx 只能处理 .docx 格式…...

Python爬虫(一):爬虫伪装
一、网站防爬机制概述 在当今互联网环境中,具有一定规模或盈利性质的网站几乎都实施了各种防爬措施。这些措施主要分为两大类: 身份验证机制:直接将未经授权的爬虫阻挡在外反爬技术体系:通过各种技术手段增加爬虫获取数据的难度…...

ElasticSearch搜索引擎之倒排索引及其底层算法
文章目录 一、搜索引擎1、什么是搜索引擎?2、搜索引擎的分类3、常用的搜索引擎4、搜索引擎的特点二、倒排索引1、简介2、为什么倒排索引不用B+树1.创建时间长,文件大。2.其次,树深,IO次数可怕。3.索引可能会失效。4.精准度差。三. 倒排索引四、算法1、Term Index的算法2、 …...

学习STC51单片机32(芯片为STC89C52RCRC)OLED显示屏2
每日一言 今天的每一份坚持,都是在为未来积攒底气。 案例:OLED显示一个A 这边观察到一个点,怎么雪花了就是都是乱七八糟的占满了屏幕。。 解释 : 如果代码里信号切换太快(比如 SDA 刚变,SCL 立刻变&#…...

华硕a豆14 Air香氛版,美学与科技的馨香融合
在快节奏的现代生活中,我们渴望一个能激发创想、愉悦感官的工作与生活伙伴,它不仅是冰冷的科技工具,更能触动我们内心深处的细腻情感。正是在这样的期许下,华硕a豆14 Air香氛版翩然而至,它以一种前所未有的方式&#x…...

2025年渗透测试面试题总结-腾讯[实习]科恩实验室-安全工程师(题目+回答)
安全领域各种资源,学习文档,以及工具分享、前沿信息分享、POC、EXP分享。不定期分享各种好玩的项目及好用的工具,欢迎关注。 目录 腾讯[实习]科恩实验室-安全工程师 一、网络与协议 1. TCP三次握手 2. SYN扫描原理 3. HTTPS证书机制 二…...
C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)
名人说:路漫漫其修远兮,吾将上下而求索。—— 屈原《离骚》 创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊) 专栏介绍:《编程项目实战》 目录 一、为什么要开发一个日历程序?1. 深入理解时间算法2. 练习面向对象设计3. 学习数据结构应用二、核心算法深度解析…...

Windows安装Miniconda
一、下载 https://www.anaconda.com/download/success 二、安装 三、配置镜像源 Anaconda/Miniconda pip 配置清华镜像源_anaconda配置清华源-CSDN博客 四、常用操作命令 Anaconda/Miniconda 基本操作命令_miniconda创建环境命令-CSDN博客...