免費白嫖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.pswp.cn/web/42291.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/42291.shtml
英文地址,請注明出處:http://en.pswp.cn/web/42291.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何選擇可靠的三方支付公司?

選擇可靠的三方支付公司需要考慮以下幾個方面&#xff1a; - 資質和信譽&#xff1a;確保支付公司具有合法的資質和良好的信譽&#xff0c;可以查看其營業執照、支付業務許可證等相關證件。 - 安全性&#xff1a;了解支付公司的安全措施&#xff0c;如加密技術、風險控制體系等…

【康復學習--LeetCode每日一題】2965. 找出缺失和重復的數字

題目&#xff1a; 給你一個下標從 0 開始的二維整數矩陣 grid&#xff0c;大小為 n * n &#xff0c;其中的值在 [1, n2] 范圍內。除了 a 出現 兩次&#xff0c;b 缺失 之外&#xff0c;每個整數都 恰好出現一次 。 任務是找出重復的數字a 和缺失的數字 b 。 返回一個下標從 0…

探索回歸模型的奧秘:從理論到實踐,以PlugLink為例

回歸模型初探 回歸分析&#xff0c;顧名思義&#xff0c;旨在探索兩個或多個變量之間的關系&#xff0c;特別是當一個變量&#xff08;因變量&#xff09;依賴于其他一個或多個變量&#xff08;自變量&#xff09;時&#xff0c;它能夠預測因變量的值。常見的回歸模型包括線性…

spring web flux 記錄用戶日志及異常日志

package cn.finopen.boot.autoconfigure.aop;Configuration EnableAspectJAutoProxy Order public class EndpointLogAopConfiguration {/*** 請求方法白名單*/private static final String[] METHOD_WHITE_LIST {"get", "unreadCount", "find"…

MySQL8之mysql-community-common的作用

在MySQL 8中&#xff0c;mysql-community-common是一個軟件包&#xff0c;它提供了MySQL服務器和客戶端庫所需的一些共同文件。具體來說&#xff0c;mysql-community-common的作用包括但不限于以下幾點&#xff1a; 1. 提供基礎配置和錯誤信息 錯誤信息和字符集包&#xff1a…

決策樹算法簡單介紹:原理和方案實施

決策樹算法介紹&#xff1a;原理和方案實施 決策樹&#xff08;Decision Tree&#xff09;是一種常用的機器學習算法&#xff0c;它既可以用于分類任務&#xff0c;也可以用于回歸任務。由于其直觀性和解釋性&#xff0c;決策樹在數據分析和模型構建中得到了廣泛的應用。本文將…

如何防御DDoS攻擊

如何防御DDoS攻擊 1.硬件層面 使用高性能的防火墻 高性能的防火墻可以有效地過濾DDoS攻擊流量&#xff0c;從而提高網絡的抗攻擊能力。企業可以使用性能強大的防火墻來防范DDoS攻擊。 使用流量清洗設備 流量清洗設備可以實時監測網絡流量&#xff0c;發現并過濾DDoS攻擊流量&am…

頂頂頂頂頂頂頂頂頂頂頂頂頂頂頂頂頂頂頂

歡迎關注博主 Mindtechnist 或加入【智能科技社區】一起學習和分享Linux、C、C、Python、Matlab&#xff0c;機器人運動控制、多機器人協作&#xff0c;智能優化算法&#xff0c;濾波估計、多傳感器信息融合&#xff0c;機器學習&#xff0c;人工智能等相關領域的知識和技術。關…

Spark操作Excel最佳實踐

Spark操作Excel最佳實踐 1、背景描述2、Apache POI與Spark-Excel2.1、Apache POI簡介2.2、Spark-Excel簡介3、Spark讀取Excel3.1、導入依賴3.2、使用Apache POI3.3、使用Spark-Excel1、背景描述 數據開發中通常會涉及到Excel的處理。Java和Apache Spark都不支持讀取Excel文件,…

挖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…

C++基礎語法:嵌套類(內部類)

前言 "打牢基礎,萬事不愁" .C的基礎語法的學習 引入 嵌套類的理解和使用 嵌套類(內部類)的特征 嵌套類是在類里面定義的類.class里嵌套另一個class,又稱內部類(這種說法更形象) 1>內部類除了定義在外部類內部,和其他類沒有太大區別.內部類對于外部類自動"可…

基于Java的數碼論壇系統設計與實現

你好&#xff0c;我是計算機領域的研究者。如果你對數碼論壇系統開發感興趣或有相關需求&#xff0c;歡迎聯系我。 開發語言&#xff1a; Java 數據庫&#xff1a; MySQL 技術&#xff1a; Java技術、MySQL數據庫、B/S架構、SpringBoot框架 工具&#xff1a; Eclipse、MySQ…

HJ41 稱砝碼下

接上文&#xff0c;HJ41 稱砝碼 更新acd代碼&#xff0c;牛客代碼如下 #include <stdio.h> #include <stdlib.h> #include <string.h>int calculateWeight(int *weight, int weightLen, int *num, int numLen) {int array[20001] {0};int hash[300001] {0…

css 文件重復類樣式刪除

上傳文件 進行無關 className 刪除 <div style"display: flex;"><input type"file" change"handleFileUpload" /><el-button click"removeStyles" :disabled"!fileContent">Remove Styles and Download&…

navigation運動規劃學習筆記

DWA 動態窗口算法(Dynamic Window Approaches, DWA) 是基于預測控制理論的一種次優方法,因其在未知環境下能夠安全、有效的避開障礙物, 同時具有計算量小, 反應迅速、可操作性強等特點。 DWA算法屬于局部路徑規劃算法。 DWA算法的核心思想是根據移動機器人當前的位置狀態和速…

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;收藏不迷…

【全網最全最詳細】Tomcat 面試題大全

目錄 一、說一說Tomcat的啟動流程 二、Tomcat中有哪些類加載器? 三、為什么Tomcat可以把線程數設置為200,而不是N+1? 四、Tomcat處理請求的過程怎樣的? 五、說一說Servlet的生命周期 六、過濾器和攔截器的區別? 七、介紹一下Tomcat的IO模型 八、說一說Tomcat的類加…