免费白嫖A100活动开始啦,InternLM + LlamaIndex RAG 实践

内容来源:

Docs

前置知识:

检索增强生成(Retrieval Augmented Generation,RAG)

LlamaIndex

LlamaIndex 是一个上下文增强的 LLM 框架,旨在通过将其与特定上下文数据集集成,增强大型语言模型(LLMs)的能力。

xtuner

书生集成的微调,测试大模型平台

环境搭建:

创建环境

服务器已经预设好了conda环境,输入书生服务器封装bash代码运行,激活环境,如下:

studio-conda -t llamaindex -o pytorch-2.1.2
conda activate llamaindex 
pip install llama-index==0.10.38 llama-index-llms-huggingface==0.2.0 "transformers[torch]==4.41.1" "huggingface_hub[inference]==0.23.1" huggingface_hub==0.23.1 sentence-transformers==2.7.0 sentencepiece==0.2.0cd ~
mkdir llamaindex_demo
mkdir model
cd ~/llamaindex_demo
touch download_hf.py
vim download_hf.py

下载RAG模型:

键入I,表示输入,在download_hf.py输入:

import os# 设置环境变量
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'# 下载模型
os.system('huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/model/sentence-transformer')

运行下载:

conda activate llamaindex
python download_hf.py

下载nltk资源:

cd /root
git clone https://gitee.com/yzy0612/nltk_data.git  --branch gh-pages
cd nltk_data
mv packages/*  ./
cd tokenizers
unzip punkt.zip
cd ../taggers
unzip averaged_perceptron_tagger.zip

LlamaIndex HuggingFaceLLM

cd ~/model
ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b/ ./
cd ~/llamaindex_demo
touch llamaindex_internlm.py
vim llamaindex_internlm.py
#点击i 复制一下代码
#from llama_index.llms.huggingface import HuggingFaceLLM
#from llama_index.core.llms import ChatMessage
#llm = HuggingFaceLLM(
#    model_name="/root/model/internlm2-chat-1_8b",
#    tokenizer_name="/root/model/internlm2-chat-1_8b",
#    model_kwargs={"trust_remote_code":True},
#    tokenizer_kwargs={"trust_remote_code":True}
#)#rsp = llm.chat(messages=[ChatMessage(content="xtuner是什么?")])
#print(rsp)#输入完成
#点击ESC
#:wq
#保存conda activate llamaindex
cd ~/llamaindex_demo/
python llamaindex_internlm.py

这里在测试使用大模型是否正常输出,并且跟后续加入RAG后效果对比

conda activate llamaindex
pip install llama-index-embeddings-huggingface llama-index-embeddings-instructor
cd ~/llamaindex_demo
mkdir data
cd data
git clone https://github.com/InternLM/xtuner.git
mv xtuner/README_zh-CN.md ./
cd ~/llamaindex_demo
touch llamaindex_RAG.py
#写入下面python代码
conda activate llamaindex
cd ~/llamaindex_demo/
python llamaindex_RAG.py
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settingsfrom llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface import HuggingFaceLLMembed_model = HuggingFaceEmbedding(model_name="/root/model/sentence-transformer"
)Settings.embed_model = embed_modelllm = HuggingFaceLLM(model_name="/root/model/internlm2-chat-1_8b",tokenizer_name="/root/model/internlm2-chat-1_8b",model_kwargs={"trust_remote_code":True},tokenizer_kwargs={"trust_remote_code":True}
)
Settings.llm = llmdocuments = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("xtuner是什么?")print(response)

关卡任务

完成以下任务,并将实现过程记录截图:

  • 通过 llamaindex 运行 InternLM2 1.8B,询问“你是谁”,将运行结果截图。

  • 通过 llamaindex 实现知识库检索,询问两个问题将运行结果截图。

    • 问题1:xtuner是什么?

    • 问题2:xtuner支持那些模型?

完成作业10%RAG是不够的,但是都到这里了 直接向助教申请30%资源:

/root/.conda/envs/llamaindex/bin/python /root/data/data/llamaindex_RAG.py
/root/.conda/envs/llamaindex/lib/python3.10/site-packages/pydantic/_internal/_fields.py:161: UserWarning: Field "model_id" has conflict with protected namespace "model_".You may be able to resolve this warning by setting `model_config['protected_namespaces'] = ()`.warnings.warn(
Loading checkpoint shards: 100%|██████████████████████████████████████████████████████████████████████████████████████| 2/2 [00:39<00:00, 19.95s/it]
Some parameters are on the meta device device because they were offloaded to the cpu.
Traceback (most recent call last):File "/root/data/data/llamaindex_RAG.py", line 23, in <module>response = query_engine.query("xtuner是什么?")File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 230, in wrapperresult = func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/base/base_query_engine.py", line 52, in queryquery_result = self._query(str_or_query_bundle)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 230, in wrapperresult = func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/query_engine/retriever_query_engine.py", line 190, in _queryresponse = self._response_synthesizer.synthesize(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 230, in wrapperresult = func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/response_synthesizers/base.py", line 241, in synthesizeresponse_str = self.get_response(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 230, in wrapperresult = func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/response_synthesizers/compact_and_refine.py", line 43, in get_responsereturn super().get_response(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 230, in wrapperresult = func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/response_synthesizers/refine.py", line 183, in get_responseresponse = self._give_response_single(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/response_synthesizers/refine.py", line 238, in _give_response_singleprogram(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 230, in wrapperresult = func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/response_synthesizers/refine.py", line 84, in __call__answer = self._llm.predict(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 230, in wrapperresult = func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/llms/llm.py", line 438, in predictresponse = self.complete(formatted_prompt, formatted=True)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/instrumentation/dispatcher.py", line 230, in wrapperresult = func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/core/llms/callbacks.py", line 429, in wrapped_llm_predictf_return_val = f(_self, *args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/llama_index/llms/huggingface/base.py", line 358, in completetokens = self._model.generate(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/torch/utils/_contextlib.py", line 115, in decorate_contextreturn func(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/transformers/generation/utils.py", line 1758, in generateresult = self._sample(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/transformers/generation/utils.py", line 2397, in _sampleoutputs = self(File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_implreturn self._call_impl(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_implreturn forward_call(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/accelerate/hooks.py", line 169, in new_forwardoutput = module._old_forward(*args, **kwargs)File "/root/.cache/huggingface/modules/transformers_modules/internlm2-chat-1_8b/modeling_internlm2.py", line 1060, in forwardlogits = self.output(hidden_states)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_implreturn self._call_impl(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1527, in _call_implreturn forward_call(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/accelerate/hooks.py", line 169, in new_forwardoutput = module._old_forward(*args, **kwargs)File "/root/.conda/envs/llamaindex/lib/python3.10/site-packages/torch/nn/modules/linear.py", line 114, in forwardreturn F.linear(input, self.weight, self.bias)
torch.cuda.OutOfMemoryError: CUDA out of memory. Tried to allocate 316.00 MiB. GPU 0 has a total capacty of 7.99 GiB of which 198.00 MiB is free. Process 1736952 has 35.84 GiB memory in use. Process 364582 has 7.80 GiB memory in use. Of the allocated memory 7.25 GiB is allocated by PyTorch, and 63.81 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting max_split_size_mb to avoid fragmentation.  See documentation for Memory Management and PYTORCH_CUDA_ALLOC_CONF

有RAG和没RAG结果对比:

任务截图

将任务写入py

from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.core.llms import ChatMessage
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface import HuggingFaceLLMembed_model = HuggingFaceEmbedding(model_name="/root/model/sentence-transformer"
)Settings.embed_model = embed_modelllm = HuggingFaceLLM(model_name="/root/model/internlm2-chat-1_8b",tokenizer_name="/root/model/internlm2-chat-1_8b",model_kwargs={"trust_remote_code":True},tokenizer_kwargs={"trust_remote_code":True}
)
Settings.llm = llm
rsp = llm.chat(messages=[ChatMessage(content="你是谁?")])
print("你是谁?",rsp)
documents = SimpleDirectoryReader("/root/data/data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("xtuner是什么?")
print("xtuner是什么?",response)
response = query_engine.query("xtuner支持哪些模型")
print("xtuner支持哪些模型",response)

你是谁?

xtuner是什么?(rag结果)

xtuner支持哪些模型 

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

决策树算法简单介绍:原理和方案实施

决策树算法介绍&#xff1a;原理和方案实施 决策树&#xff08;Decision Tree&#xff09;是一种常用的机器学习算法&#xff0c;它既可以用于分类任务&#xff0c;也可以用于回归任务。由于其直观性和解释性&#xff0c;决策树在数据分析和模型构建中得到了广泛的应用。本文将…

顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶顶

欢迎关注博主 Mindtechnist 或加入【智能科技社区】一起学习和分享Linux、C、C、Python、Matlab&#xff0c;机器人运动控制、多机器人协作&#xff0c;智能优化算法&#xff0c;滤波估计、多传感器信息融合&#xff0c;机器学习&#xff0c;人工智能等相关领域的知识和技术。关…

挖K脚本检测指南

免责声明:本文仅做分享... 目录 挖K样本-Win&Linux-危害&定性 Linux-Web 安全漏洞导致挖K事件 Windows-系统口令爆破导致挖K事件 --怎么被挖K了??? 已经取得了权限. 挖K样本-Win&Linux-危害&定性 危害&#xff1a;CPU 拉满&#xff0c;网络阻塞&…

在Linux下使用Docker部署chirpstack

目录 一、前言 二、chirpstack 1、chirpstack是什么 2、chirpstack组件 3、为什么选择Docker部署 三、Linux下部署过程 四、web界面部署过程 一、前言 本篇文章我是在Linux下使用 Docker 进行部署chirpstack&#xff0c;chirpstack采用的是v4 版本&#xff0c;v4 版本 与…

Logstash常用的filter四大插件

以博客<ELK日志分析系统概述及部署>中实验结果为依据 补充&#xff1a;如何用正则表达式匹配一个ipv4地址 &#xff08;[0-9] | [1-9][0-9] | 1[0-9][0-9] | 2[04][0-9] | 25[0-5]&#xff09;\.&#xff08;[0-9] | [1-9][0-9] | 1[0-9][0-9] | 2[04][0-9] | 25[0-5]&a…

基于Java的数码论坛系统设计与实现

你好&#xff0c;我是计算机领域的研究者。如果你对数码论坛系统开发感兴趣或有相关需求&#xff0c;欢迎联系我。 开发语言&#xff1a; Java 数据库&#xff1a; MySQL 技术&#xff1a; Java技术、MySQL数据库、B/S架构、SpringBoot框架 工具&#xff1a; Eclipse、MySQ…

css 文件重复类样式删除

上传文件 进行无关 className 删除 <div style"display: flex;"><input type"file" change"handleFileUpload" /><el-button click"removeStyles" :disabled"!fileContent">Remove Styles and Download&…

antd a-select下拉框样式修改 vue3 亲测有效

记录一下遇到的问题 1.遇到问题&#xff1a; 使用到Vue3 Ant Design of Vue 3.2.20&#xff0c;但因为项目需求样式&#xff0c;各种查找资料都未能解决; 2.解决问题&#xff1a; ①我们审查元素可以看到&#xff0c;下拉框是在body中的; ①在a-select 元素上添加dropdownCla…

运行时异常与一般异常的异同

运行时异常与一般异常的异同 1、运行时异常&#xff08;Runtime Exception&#xff09;1.1 特点 2、 一般异常&#xff08;Checked Exception&#xff09;2.1 特点 3、异同点总结3.1 相同点3.2 不同点 4、总结 &#x1f496;The Begin&#x1f496;点点关注&#xff0c;收藏不迷…

图形学各种二维基础变换,原来线性代数还能这么用,太牛了

缩放变换 均匀缩放 若想将一个图形缩小0.5倍 若x乘上缩放值s等于x撇&#xff0c;y同理&#xff0c;则 x ′ s x y ′ s y \begin{aligned} & x^{\prime}s x \\ & y^{\prime}s y \end{aligned} ​x′sxy′sy​&#xff0c;这样就表示了x缩小了s倍&#xff0c;y也是…

UML中用例之间的可视化表示

用例除了与参与者有关联关系外&#xff0c;用例之间也存在着一定的关系&#xff0c;如泛化关系、包含关系、扩展关系等。 4.2.1 包含关系 包含关系指的是两个用例之间的关系&#xff0c;其中一个用例&#xff08;称为基本用例&#xff0c;Base Use Case&#xff09;的行为包…

温度传感器的常见故障及处理方法

温度传感器作为现代工业、科研及日常生活中不可或缺的重要元件&#xff0c;其稳定性和准确性直接影响到设备的运行效率和安全。然而&#xff0c;由于各种因素的影响&#xff0c;温度传感器在使用过程中常会遇到一些故障。本文将针对这些常见故障进行分析&#xff0c;并提出相应…

如果你想手写Linux系统

哈喽&#xff0c;我是子牙老师。今天咱们聊聊这个话题吧&#xff0c;Linux作为当今科技世界的地基&#xff0c;我们越来越接近真理了&#xff0c;有木有&#xff1f; 这个文章的角度&#xff0c;你可能全网都很难找到第二篇如此系统讲透这个问题的文章 你可能想问&#xff1a…

LabVIEW电滞回线测试系统

铁电材料的性能评估依赖于电滞回线的测量&#xff0c;这直接关系到材料的应用效果和寿命。传统的电滞回线测量方法操作复杂且设备成本高。开发了一种基于LabVIEW的电滞回线测试系统&#xff0c;解决传统方法的不足&#xff0c;降低成本&#xff0c;提高操作便捷性和数据分析的自…

spring boot 3.x版本中集成spring security 6.x版本进行实现动态权限控制解决方案

一、背景 最近在进行项目从jdk8和spring boot 2.7.x版本技术架构向jdk17和spring boot 3.3.x版本的代码迁移&#xff0c;在迁移过程中&#xff0c;发现spring boot 3.3.x版本依赖的spring security版本已经升级6.x版本了&#xff0c;语法上和spring security 5.x版本有很多地方…

Mysql中存储引擎简介、修改、查询、选择

场景 数据库存储引擎 数据库存储引擎是数据库底层软件组件&#xff0c;数据库管理系统&#xff08;DBMS &#xff09;使用数据引擎进行创建、查询、更新和删除数据的操作。 不同的存储引擎提供不同的存储机制、索引技巧、锁定水平等功能&#xff0c;使用不同的存储引擎还可以…

【C++报错已解决】Invalid Use of ‘this’ Pointer

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 文章目录 引言 一、问题描述1.1 报错示例1.2 报错分析1.3 解决思路 二、解决方法2.1 方法一&#xff1a;修正‘this’指针使用2…

React+TS前台项目实战(二十六)-- 高性能可配置Echarts图表组件封装

文章目录 前言CommonChart组件1. 功能分析2. 代码详细注释3. 使用到的全局hook代码4. 使用方式5. 效果展示 总结 前言 Echarts图表在项目中经常用到&#xff0c;然而&#xff0c;重复编写初始化&#xff0c;更新&#xff0c;以及清除实例等动作对于开发人员来说是一种浪费时间…

LVS-DR负载均衡

LVS-DR负载均衡 LVS—DR工作模式 原理 客户端访问调度器的VIP地址&#xff0c;在路由器上应该设置VIP跟调度器的一对一的映射关系&#xff0c;调度器根据调度算法将该请求“调度“到后端真实服务器&#xff0c;真实服务器处理完毕后直接将处理后的应答报文发送给路由器&#xf…

EDI安全:如何在2024年保护您的数据免受安全和隐私威胁

电子数据交换&#xff08;EDI&#xff09;支持使用标准化格式在组织之间自动交换业务文档。这种数字化转型彻底改变了业务通信&#xff0c;消除了对纸质交易的需求并加速了交易。然而&#xff0c;随着越来越依赖 EDI 来传输发票、采购订单和发货通知等敏感数据&#xff0c;EDI …