任务类型 | 任务内容 | 预计耗时 | 完成度 |
---|---|---|---|
闯关任务 | 基于 LlamaIndex 构建自己的 RAG 知识库,寻找一个问题 A 在使用 LlamaIndex 之前InternLM2-Chat-1.8B模型不会回答,借助 LlamaIndex 后 InternLM2-Chat-1.8B 模型具备回答 A 的能力,截图保存 | 45mins | 完成 |
- check this out! github repo llamaindex
vi llamaindex_env.sh
, copy below, andbash llamaindex_env.sh
run it!conda create -n llamaindex python=3.10 -y source activate llamaindex pip install torch==2.1.2+cu118 torchvision==0.16.2+cu118 torchaudio==2.1.2 torchtext \ llama-index llama-index-llms-huggingface \ llama-index-embeddings-huggingface llama-index-embeddings-instructor \ transformers huggingface_hub \ sentence-transformers sentencepiece \ einops protobuf \ -f https://mirror.sjtu.edu.cn/pytorch-wheels/cu118/?mirror_intel_list \ -f https://download.pytorch.org/whl/cu118/torch_stable.html
- download sentence-transformers
conda activate llamaindex \ && export HF_ENDPOINT='https://hf-mirror.com' \ && mkdir -p /root/model && \ huggingface-cli download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/model/sentence-transformer
- download
NLTK
:vi nltk.sh
and copy it, thenbash nltk.sh
git clone https://gitee.com/yzy0612/nltk_data.git --branch gh-pages /root/nltk_data mv /root/nltk_data/packages/* /root/nltk_data/ unzip /root/nltk_data/tokenizers/punkt.zip -d /root/nltk_data/tokenizers unzip /root/nltk_data/taggers/averaged_perceptron_tagger.zip -d /root/nltk_data/taggers
vi llamaindex_internlm.py
, copy it, thenconda activate llamaindex && python llamaindex_internlm.py
from llama_index.llms.huggingface import HuggingFaceLLM from llama_index.core.llms import ChatMessage llm = HuggingFaceLLM( model_name="/root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b", tokenizer_name="/root/share/new_models/Shanghai_AI_Laboratory/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)
- run it
mkdir -p /root/code/llamaindex/data && wget https://raw.githubusercontent.com/InternLM/xtuner/main/README_zh-CN.md -O /root/code/llamaindex/data/README_zh-CN.md
vi llamaindex_RAG.py
, copy it, thenpython llamaindex_RAG.py
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings from llama_index.embeddings.huggingface import HuggingFaceEmbedding from llama_index.llms.huggingface import HuggingFaceLLM #初始化一个HuggingFaceEmbedding对象,用于将文本转换为向量表示 embed_model = HuggingFaceEmbedding( #指定了一个预训练的sentence-transformer模型的路径 model_name="/root/model/sentence-transformer" ) #将创建的嵌入模型赋值给全局设置的embed_model属性, #这样在后续的索引构建过程中就会使用这个模型。 Settings.embed_model = embed_model llm = HuggingFaceLLM( model_name="/root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b", tokenizer_name="/root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b", model_kwargs={"trust_remote_code":True}, tokenizer_kwargs={"trust_remote_code":True} ) #设置全局的llm属性,这样在索引查询时会使用这个模型。 Settings.llm = llm #从指定目录读取所有文档,并加载数据到内存中 documents = SimpleDirectoryReader("/root/code/llamaindex/data").load_data() #创建一个VectorStoreIndex,并使用之前加载的文档来构建索引。 # 此索引将文档转换为向量,并存储这些向量以便于快速检索。 index = VectorStoreIndex.from_documents(documents) # 创建一个查询引擎,这个引擎可以接收查询并返回相关文档的响应。 query_engine = index.as_query_engine() response = query_engine.query("xtuner是什么?") print(response)