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_CHAT

SYSTEM_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 HuggingFaceEmbedding

embed_model = HuggingFaceEmbedding(model_name="/home/featurize/bge-small-zh-v1.5")
from llama_index.core import Settings

Settings.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方案,知识库里面的内容也可以实现中文问答,这非常有利于我们进行私有化部署方案,从而扩展我们的功能。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/442693.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Linux 之六:系统性能监控和挂载

系统性能 Linux系统中&#xff0c;有许多命令用于监测和分析性能指标。以下是一些常用的Linux性能分析命令&#xff1a; top&#xff1a;实时查看并监控CPU、内存以及各个进程的资源占用情况。htop&#xff08;需要安装&#xff09;&#xff1a;一个增强版的 top 命令&#x…

P3768 简单的数学题(莫比乌斯反演+杜教筛)

题目&#xff1a;P3768 简单的数学题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 思路&#xff1a; 代码&#xff1a; #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include<string> #include<cstring> #include<cmath> #include<cti…

Java基础面试题(day 01)

&#x1f4d1;前言 本文主要是【Java】——Java基础面试题的文章&#xff0c;如果有什么需要改进的地方还请大佬指出⛺️ &#x1f3ac;作者简介&#xff1a;大家好&#xff0c;我是听风与他&#x1f947; ☁️博客首页&#xff1a;CSDN主页听风与他 &#x1f304;每日一句&am…

【论文笔记】Mamba: Linear-Time Sequence Modeling with Selective State Spaces

原文链接&#xff1a;https://arxiv.org/abs/2312.00752 1. 引言 基石模型&#xff08;FM&#xff09;的主干网络通常是序列模型&#xff0c;处理任意的输入序列。但现代FM主要基于Transformer这一序列模型&#xff0c;及其核心的注意力。但是&#xff0c;自注意力仅能在上下…

JVM基本概念、命令、参数、GC日志总结

原文: 赵侠客 一、前言 NPE&#xff08;NullPointerException&#xff09;和OOM&#xff08;OutofMemoryError&#xff09;在JAVA程序员中扮演着重要的角色&#xff0c;它也是很多人始终摆脱不掉的梦魇&#xff0c;与NPE不同的是OOM一旦在生产环境中出现就意味着只靠代码已经无…

Git使用教程:入门到精通

Git使用教程&#xff1a;入门到精通 一、Git安装根据需求选择电脑位数安装&#xff1b;20231023210945建议这里先新建一个文件夹如&#xff1a;D:/Git&#xff1b;专门来存放Git安装包和后续Git代码&#xff0c;方便管理&#xff1b; 二、Git使用前的配置需要先创建自己的Gitee…

四桥臂三相逆变器动态电压恢复器(DVR)MATLAB仿真

微❤关注“电气仔推送”获得资料&#xff08;专享优惠&#xff09; 简介 四桥臂三相逆变器 电路 的一般形式如图 1&#xff0c;为 便于分析 &#xff0c;将其等效成图所示的电路 。以直流母线电压Ud的 1&#xff0f;2处为参考点 &#xff0c;逆变器三相和零线相 输 出可等效成…

Kotlin dist downloading failed

现象&#xff1a; 在使用AndroidStudio编写Flutter项目时总是在工具的右下角提示错误信息 该问题通常在刚刚打开AndroidStudio时报出&#xff0c;但可以正常编译和运行flutter项目即Android项目 分析&#xff1a;Flutter项目组认为这是AndroidStudio工具平台本身的问题非Flut…

【CSP试题回顾】202009-2-风险人群筛查

CSP-202009-2-风险人群筛查 解题思路 主循环&#xff08;对每个查询&#xff09;&#xff1a; 使用一个布尔变量pass来标记风险人群是否至少一次进入了特定区域&#xff0c;以及一个布尔变量onlyOnce来确保停留计数 stayNum 在每次查询中最多只增加一次。内循环&#xff08;对…

站长必备溯源教程-绕过CDN查找背后IP的方法手段

绕过CDN查询背后真实IP方法&#xff1a; 方法一 DNS历史解析记录 查询域名的历史解析记录&#xff0c;可能会找到网站使用CDN前的解析记录&#xff0c;从而获取真实IP 相关查询的网站有&#xff1a;iphistory、DNS查询、微步在线、域名查询、DNS历史查询、Netcraft 方法二 …

Aop注解+Redis解决SpringBoot接口幂等性(源码自取)

目录 一、什么是幂等性&#xff1f; 二、哪些请求天生就是幂等的&#xff1f; 三、为什么需要幂等 1.超时重试 2.异步回调 3.消息队列 四、实现幂等的关键因素 关键因素1 关键因素2 五、引入幂等性后对系统的影响 六、Restful API 接口的幂等性 实战Aop注解redis解…

STM32基本定时功能

1、定时器就是计数器。 2、怎么计数&#xff1f; 3、我们需要有一恒定频率的方波信号&#xff0c;再加上一个寄存器。 4、比如每来一个上升沿信号&#xff0c;寄存器值加1&#xff0c;就可以完成计数。 5、假设方波频率是100Hz&#xff0c;也就是1秒100个脉冲。…

海外媒体宣发套餐如何利用3种方式洞察市场-华媒舍

在当今数字化时代&#xff0c;媒体宣发成为了企业推广产品和品牌的重要手段之一。其中&#xff0c;7FT媒体宣发套餐是一种常用而有效的宣传方式。本文将介绍这种媒体宣发套餐&#xff0c;以及如何利用它来洞察市场。 一、关键概念 在深入讨论7FT媒体宣发套餐之前&#xff0c;让…

Django工具

一、分页器介绍 1.1、介绍 分页,就是当我们在页面中显示一些信息列表,内容过多,一个页面显示不完,需要分成多个页面进行显示时,使用的技术就是分页技术 在django项目中,一般是使用3种分页的技术: 自定义分页功能,所有的分页功能都是自己实现django的插件 django-pagin…

机器学习笔记 大语言模型是如何运作的?一、语料库和N-gram模型

一、语料库 语言模型、ChatGPT和人工智能似乎无处不在。了解大型语言模型(LLM)“背后”发生的事情将是驾驭数字世界的关键。 首先在提示中键入一个单词,然后点击提交。您可以尝试新的提示,并根据需要多次重新生成响应。 这个我们称之为“T&C”的语言模型是在一…

大模型概念解析 | In-context Learning

注1:本文系"概念解析"系列之一,致力于简洁清晰地解释、辨析复杂而专业的概念。本次辨析的概念是:大模型中的In-context Learning 大模型概念解析 | In-context Learning PR-418: What learning algorithm is in-context learning? Investigations with linear mo…

【CSP试题回顾】202012-1-期末预测之安全指数

CSP-202012-1-期末预测之安全指数 解题代码 #include <iostream> #include <algorithm> using namespace std;int n, sum;int main() {cin >> n;for (int i 0; i < n; i){int w, s;cin >> w >> s;sum w * s;}sum max(sum, 0);cout <&…

RocketMQ快速入门_2. rocketmq 的应用场景、与其他mq的差异

0. 引言 之前我们讲解过rabbitMQ&#xff0c;本期我们将进入吞吐量更加强大的rocketMQ的学习。 1. 基础概念 如果你是刚接触MQ的同学&#xff0c;还不清楚消息队列的基础概念的&#xff0c;可以参考我之前这篇文章&#xff1a; https://wu55555.blog.csdn.net/article/deta…

JPEG照片被误删除如何恢复?学会这个方法就够了

JPG/JPEG是一种后缀名为“.jpg”或“.jpeg”的图形格式。它是存储照片图像的常用格式&#xff0c;因此我们可以使用数码相机、手机或其他设备来获取大量的JPG/JPEG文件。有时&#xff0c;我们会遇到由于意外删除、格式化驱动器或其他未知原因导致 JPEG 文件丢失的情况。无论哪种…

外包干了30天,技术明显退步。。

&#x1f345; 视频学习&#xff1a;文末有免费的配套视频可观看 &#x1f345; 点击文末小卡片&#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 这次来聊一个大家可能也比较关心的问题&#xff0c;那就是就业城市选择的问题。而谈到这个问题&a…
最新文章