配景
llama-
index在实现RAG方案的时间多是用的llama等英文大模子,对于国内的诸多模子案例较少,本次将利用qwen大模子实现llama-
index的RAG方案。
情况设置
(1)pip包
llama
index须要预装许多包,这里先把我乐成的案例内里的pip包设置发出来,在requirements.txt内里。(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)
复制代码 - # set Logging to DEBUG for more detailed outputsquery_engine =
- index.as_query_engine()
复制代码- response = query_engine.query("小额贷款咋规定的?")
- print(response)
复制代码
知识库
llama
index实现RAG中很关键的一环就是知识库,知识库告急是各种范例的文档,这里给的文档是一个pdf文件,文件内容如下。
总结
从上面的代码可以看出,我们利用qwen和bge-zh模子可以实现当地下载模子的RAG方案,知识库内里的内容也可以实现中文问答,这非常有利于我们举行私有化摆设方案,从而扩展我们的功能。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金 |